-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
515 lines (443 loc) · 19.7 KB
/
App.java
File metadata and controls
515 lines (443 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
* Java Text Intelligence Starter - Backend Server
*
* Simple REST API server providing text intelligence analysis
* powered by the Deepgram Java SDK.
*
* Key Features:
* - Contract-compliant API endpoint: POST /api/text-intelligence
* - Accepts text or URL in JSON body
* - Supports multiple intelligence features: summarization, topics, sentiment, intents
* - CORS-enabled for frontend communication
* - JWT session auth with rate limiting (production only)
* - Uses Deepgram Java SDK for text analysis
*/
package com.deepgram.starter;
// ============================================================================
// SECTION 1: IMPORTS
// ============================================================================
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import io.github.cdimascio.dotenv.Dotenv;
import io.javalin.Javalin;
import io.javalin.http.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.deepgram.DeepgramClient;
import com.deepgram.core.DeepgramHttpException;
import com.deepgram.resources.read.v1.text.requests.TextAnalyzeRequest;
import com.deepgram.resources.read.v1.text.types.TextAnalyzeRequestSummarize;
import com.deepgram.types.ReadV1Request;
import com.deepgram.types.ReadV1RequestText;
import com.deepgram.types.ReadV1RequestUrl;
import com.deepgram.types.ReadV1Response;
import java.io.File;
import java.net.URI;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
// ============================================================================
// SECTION 2: MAIN APPLICATION
// ============================================================================
public class App {
private static final Logger log = LoggerFactory.getLogger(App.class);
/**
* Shared Jackson ObjectMapper for JSON serialization.
* The Jdk8Module is registered to support serialization of Java 8
* Optional types used throughout the Deepgram SDK response objects.
*/
private static final ObjectMapper jsonMapper = new ObjectMapper()
.registerModule(new Jdk8Module());
private static final TomlMapper tomlMapper = new TomlMapper();
// ========================================================================
// SECTION 3: CONFIGURATION
// ========================================================================
private static int port;
private static String host;
private static String apiKey;
private static DeepgramClient dgClient;
private static Algorithm jwtAlgorithm;
/** JWT expiry time (1 hour) */
private static final long JWT_EXPIRY_SECONDS = 3600;
// ========================================================================
// SECTION 4: STARTUP
// ========================================================================
/**
* Application entry point. Loads configuration, validates the API key,
* initializes the Deepgram SDK client, and starts the Javalin HTTP server.
*
* @param args Command-line arguments (unused)
*/
public static void main(String[] args) {
// Load .env file (ignore if not present)
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load();
// Load configuration
port = Integer.parseInt(getEnv(dotenv, "PORT", "8081"));
host = getEnv(dotenv, "HOST", "0.0.0.0");
// Initialize session secret
initSessionSecret(dotenv);
// Load Deepgram API key and initialize SDK client
apiKey = loadApiKey(dotenv);
dgClient = DeepgramClient.builder()
.apiKey(apiKey)
.build();
// Create and configure Javalin app
Javalin app = Javalin.create(config -> {
config.http.defaultContentType = "application/json";
});
// ====================================================================
// CORS — wildcard is safe (same-origin via Vite proxy / Caddy in prod)
// ====================================================================
app.before(ctx -> {
ctx.header("Access-Control-Allow-Origin", "*");
ctx.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
ctx.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
});
app.options("/*", ctx -> ctx.status(204));
// ====================================================================
// SESSION ROUTES — Auth endpoints (unprotected)
// ====================================================================
app.get("/api/session", App::handleSession);
// ====================================================================
// METADATA ROUTE — Returns deepgram.toml [meta] section
// ====================================================================
app.get("/api/metadata", App::handleMetadata);
// ====================================================================
// HEALTH CHECK
// ====================================================================
app.get("/health", ctx -> {
Map<String, String> body = new LinkedHashMap<>();
body.put("status", "ok");
body.put("service", "text-intelligence");
ctx.json(body);
});
// ====================================================================
// API ROUTES — Protected endpoints
// ====================================================================
app.before("/api/text-intelligence", App::requireSession);
app.post("/api/text-intelligence", App::handleTextIntelligence);
// ====================================================================
// SERVER START
// ====================================================================
app.start(host, port);
System.out.println();
System.out.println("=".repeat(70));
System.out.printf(" Backend API running at http://localhost:%d%n", port);
System.out.println(" GET /api/session");
System.out.println(" POST /api/text-intelligence (auth required)");
System.out.println(" GET /api/metadata");
System.out.println(" GET /health");
System.out.println("=".repeat(70));
System.out.println();
}
// ========================================================================
// SECTION 5: SESSION AUTH — JWT tokens for production security
// ========================================================================
/**
* Initialize the JWT signing algorithm from SESSION_SECRET or generate one.
*/
private static void initSessionSecret(Dotenv dotenv) {
String secret = getEnv(dotenv, "SESSION_SECRET", null);
if (secret != null && !secret.isEmpty()) {
jwtAlgorithm = Algorithm.HMAC256(secret);
} else {
// Generate a random 32-byte secret for local development
byte[] randomBytes = new byte[32];
new SecureRandom().nextBytes(randomBytes);
String generated = Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
jwtAlgorithm = Algorithm.HMAC256(generated);
}
}
/**
* Middleware that validates JWT from Authorization header.
* Returns 401 with JSON error if token is missing or invalid.
*/
private static void requireSession(Context ctx) {
// Skip preflight requests
if ("OPTIONS".equalsIgnoreCase(ctx.method().name())) {
return;
}
String authHeader = ctx.header("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
ctx.status(401);
ctx.json(errorBody("AuthenticationError", "MISSING_TOKEN",
"Authorization header with Bearer token is required"));
ctx.skipRemainingHandlers();
return;
}
String token = authHeader.substring(7);
try {
JWT.require(jwtAlgorithm).build().verify(token);
} catch (TokenExpiredException e) {
ctx.status(401);
ctx.json(errorBody("AuthenticationError", "INVALID_TOKEN",
"Session expired, please refresh the page"));
ctx.skipRemainingHandlers();
} catch (JWTVerificationException e) {
ctx.status(401);
ctx.json(errorBody("AuthenticationError", "INVALID_TOKEN",
"Invalid session token"));
ctx.skipRemainingHandlers();
}
}
// ========================================================================
// SECTION 6: API KEY LOADING
// ========================================================================
/**
* Load the Deepgram API key from environment, exit if missing.
*/
private static String loadApiKey(Dotenv dotenv) {
String key = getEnv(dotenv, "DEEPGRAM_API_KEY", null);
if (key == null || key.isEmpty()) {
System.err.println();
System.err.println("ERROR: Deepgram API key not found!");
System.err.println();
System.err.println("Please set your API key in .env file:");
System.err.println(" DEEPGRAM_API_KEY=your_api_key_here");
System.err.println();
System.err.println("Get your API key at: https://console.deepgram.com");
System.err.println();
System.exit(1);
}
return key;
}
// ========================================================================
// SECTION 7: ROUTE HANDLERS
// ========================================================================
/**
* GET /api/session - Issues a signed JWT session token.
*/
private static void handleSession(Context ctx) {
Instant now = Instant.now();
String token = JWT.create()
.withIssuedAt(now)
.withExpiresAt(now.plusSeconds(JWT_EXPIRY_SECONDS))
.sign(jwtAlgorithm);
Map<String, String> body = new LinkedHashMap<>();
body.put("token", token);
ctx.json(body);
}
/**
* GET /api/metadata - Returns metadata from deepgram.toml.
*/
private static void handleMetadata(Context ctx) {
try {
File tomlFile = new File("deepgram.toml");
JsonNode root = tomlMapper.readTree(tomlFile);
JsonNode meta = root.get("meta");
if (meta == null) {
ctx.status(500);
Map<String, String> err = new LinkedHashMap<>();
err.put("error", "INTERNAL_SERVER_ERROR");
err.put("message", "Missing [meta] section in deepgram.toml");
ctx.json(err);
return;
}
ctx.json(jsonMapper.treeToValue(meta, Map.class));
} catch (Exception e) {
log.error("Error reading deepgram.toml", e);
ctx.status(500);
Map<String, String> err = new LinkedHashMap<>();
err.put("error", "INTERNAL_SERVER_ERROR");
err.put("message", "Failed to read metadata from deepgram.toml");
ctx.json(err);
}
}
/**
* POST /api/text-intelligence
*
* Contract-compliant text intelligence endpoint per starter-contracts specification.
* Uses the Deepgram Java SDK for text analysis.
*
* Accepts:
* - Query parameters: summarize, topics, sentiment, intents, language (all optional)
* - Body: JSON with either text or url field (required, not both)
*
* Returns:
* - Success (200): JSON with results object containing requested intelligence features
* - Error (4XX): JSON error response matching contract format
*/
private static void handleTextIntelligence(Context ctx) {
String url = null;
try {
// Parse JSON body
JsonNode reqBody;
try {
reqBody = jsonMapper.readTree(ctx.body());
} catch (Exception e) {
ctx.status(400);
ctx.json(validationError("INVALID_TEXT", "Invalid JSON body"));
return;
}
String text = reqBody.has("text") && !reqBody.get("text").isNull()
? reqBody.get("text").asText() : null;
url = reqBody.has("url") && !reqBody.get("url").isNull()
? reqBody.get("url").asText() : null;
// Validate: exactly one of text or url
if ((text == null || text.isEmpty()) && (url == null || url.isEmpty())) {
ctx.status(400);
ctx.json(validationError("INVALID_TEXT",
"Request must contain either 'text' or 'url' field"));
return;
}
if (text != null && !text.isEmpty() && url != null && !url.isEmpty()) {
ctx.status(400);
ctx.json(validationError("INVALID_TEXT",
"Request must contain either 'text' or 'url', not both"));
return;
}
// If URL provided, validate format
if (url != null && !url.isEmpty()) {
try {
new URI(url).toURL();
} catch (Exception e) {
ctx.status(400);
ctx.json(validationError("INVALID_URL", "Invalid URL format"));
return;
}
}
// Check for empty text content
if (text != null && text.trim().isEmpty()) {
ctx.status(400);
ctx.json(validationError("EMPTY_TEXT", "Text content cannot be empty"));
return;
}
// Extract query parameters for intelligence features
String language = ctx.queryParam("language");
if (language == null || language.isEmpty()) {
language = "en";
}
String summarize = ctx.queryParam("summarize");
String topics = ctx.queryParam("topics");
String sentiment = ctx.queryParam("sentiment");
String intents = ctx.queryParam("intents");
// Handle summarize v1 rejection
if ("v1".equals(summarize)) {
ctx.status(400);
ctx.json(validationError("INVALID_TEXT",
"Summarization v1 is no longer supported. Please use v2 or true."));
return;
}
// Build the request body — either text or URL
ReadV1Request requestBody;
if (url != null && !url.isEmpty()) {
requestBody = ReadV1Request.of(
ReadV1RequestUrl.builder().url(url).build());
} else {
requestBody = ReadV1Request.of(
ReadV1RequestText.builder().text(text).build());
}
// Build the TextAnalyzeRequest with query parameters via SDK
TextAnalyzeRequest.Builder builder = (TextAnalyzeRequest.Builder)
TextAnalyzeRequest.builder().body(requestBody);
builder.language(language);
if ("true".equalsIgnoreCase(summarize) || "v2".equalsIgnoreCase(summarize)) {
builder.summarize(TextAnalyzeRequestSummarize.V2);
}
if ("true".equalsIgnoreCase(topics)) {
builder.topics(true);
}
if ("true".equalsIgnoreCase(sentiment)) {
builder.sentiment(true);
}
if ("true".equalsIgnoreCase(intents)) {
builder.intents(true);
}
TextAnalyzeRequest request = builder.build();
// Call the Deepgram API via SDK
ReadV1Response response = dgClient.read().v1().text().analyze(request);
// Return results — serialize the SDK response to match contract format
Map<String, Object> result = new LinkedHashMap<>();
result.put("results", jsonMapper.convertValue(response.getResults(), Map.class));
ctx.json(result);
} catch (DeepgramHttpException e) {
// Handle Deepgram API errors with their original status code
log.error("Deepgram API Error (status {}): {}", e.statusCode(), e.getMessage());
String errCode = (url != null && !url.isEmpty()) ? "INVALID_URL" : "INVALID_TEXT";
String errMsg = (url != null && !url.isEmpty()) ? "Failed to process URL" : "Failed to process text";
ctx.status(400);
ctx.json(processingError(errCode, errMsg));
} catch (Exception e) {
log.error("Text Intelligence Error", e);
// Determine appropriate error code
String errorCode = (url != null && !url.isEmpty()) ? "INVALID_URL" : "INVALID_TEXT";
int statusCode = 500;
String msg = e.getMessage() != null ? e.getMessage().toLowerCase() : "";
if (msg.contains("text")) {
errorCode = "INVALID_TEXT";
statusCode = 400;
} else if (msg.contains("too long")) {
errorCode = "TEXT_TOO_LONG";
statusCode = 400;
} else if (url != null && !url.isEmpty()) {
errorCode = "INVALID_URL";
statusCode = 400;
}
ctx.status(statusCode);
ctx.json(processingError(errorCode, e.getMessage() != null ? e.getMessage() : "Text processing failed"));
}
}
// ========================================================================
// SECTION 8: HELPER FUNCTIONS
// ========================================================================
/**
* Get environment variable from dotenv or system env, with optional default.
*/
private static String getEnv(Dotenv dotenv, String key, String defaultValue) {
String value = dotenv.get(key);
if (value != null && !value.isEmpty()) {
return value;
}
value = System.getenv(key);
if (value != null && !value.isEmpty()) {
return value;
}
return defaultValue;
}
/**
* Build a structured error response body (auth errors).
*/
private static Map<String, Object> errorBody(String type, String code, String message) {
Map<String, Object> detail = new LinkedHashMap<>();
detail.put("type", type);
detail.put("code", code);
detail.put("message", message);
Map<String, Object> body = new LinkedHashMap<>();
body.put("error", detail);
return body;
}
/**
* Build a validation error response body.
*/
private static Map<String, Object> validationError(String code, String message) {
Map<String, Object> detail = new LinkedHashMap<>();
detail.put("type", "validation_error");
detail.put("code", code);
detail.put("message", message);
detail.put("details", new LinkedHashMap<>());
Map<String, Object> body = new LinkedHashMap<>();
body.put("error", detail);
return body;
}
/**
* Build a processing error response body.
*/
private static Map<String, Object> processingError(String code, String message) {
Map<String, Object> detail = new LinkedHashMap<>();
detail.put("type", "processing_error");
detail.put("code", code);
detail.put("message", message);
detail.put("details", new LinkedHashMap<>());
Map<String, Object> body = new LinkedHashMap<>();
body.put("error", detail);
return body;
}
}