Repository navigation
Expand file tree
/
Copy pathAbstractActionFrontEnd.java
More file actions
84 lines (70 loc) · 3.2 KB
/
Copy pathAbstractActionFrontEnd.java
File metadata and controls
84 lines (70 loc) · 3.2 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
package demo;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.impl.DSL;
/**
* Executes a database action for the demo UI and redirects the browser to the table view.
*
* <p>Subclasses supply the database operation through {@link #performAction()}.
* {@link WebHandler} reuses these handlers within the shared UI Lambda. A successful
* action returns HTTP 307; exceptions from the action are logged and returned as HTTP 500
* with the exception text. Both responses retain the shared redirect and cache headers.
*/
public abstract class AbstractActionFrontEnd implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
/**
* Creates an action handler with its request logger; database work begins only when invoked.
*/
public AbstractActionFrontEnd() {
}
/** Logger for incoming requests and database-action failures. */
Logger log = LogManager.getLogger();
/** Unqualified demo address table, resolved through the database connection's search path. */
protected final static Table<Record> ADDRESS_TABLE = DSL.table("address");
/** Shared response headers that disable caching and identify the root redirect target. */
final static Map<String, String> headers = new HashMap<>();
static {
headers.put("Cache-Control", "no-cache");
// Redirect back to main page
headers.put("Location", "/");
}
/**
* Performs the subclass's database mutation before the redirect response is returned.
*
* <p>Implementations establish their own database access and transaction boundaries.
* Exceptions are handled by {@link #handleRequest(APIGatewayProxyRequestEvent, Context)}.
*/
protected abstract void performAction();
/**
* Executes the action once and builds the existing redirect or error response.
*
* @param input API Gateway payload-format 1.0 request; logged but not used to select data
* @param context Lambda invocation metadata; unused by this implementation
* @return HTTP 307 with {@code Location: /} on success, or HTTP 500 with the exception
* text on failure; both responses include the shared headers
*/
@Override
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
log.debug(input);
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers)
// Redirect Temp
.withStatusCode(307);
try {
performAction();
return response;
} catch (Exception e) {
log.error("Front End Error", e);
return response
.withBody(e.toString())
.withStatusCode(500);
}
}
}