Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3573b56

Browse files
author
SJAsus
committed
Multiple changes in this commit:
1. Restricted access on BaseHandler 2. Created Base Http Handler
1 parent c9591c3 commit 3573b56

File tree

3 files changed

+182
-3
lines changed

3 files changed

+182
-3
lines changed

‎src/main/java/us/shalabh/alp/function/BaseHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public abstract class BaseHandler<I, O> implements RequestHandler<I, O>
4545
/**
4646
* Initialization code. primarily used to initialize dependencies
4747
*/
48-
public void init()
48+
protected void init()
4949
{
5050
// do nothing
5151
}
@@ -58,12 +58,12 @@ public void init()
5858
* @param context
5959
* @return
6060
*/
61-
public abstract O process(I input, Context context);
61+
protected abstract O process(I input, Context context);
6262

6363
/**
6464
* release resources if any.
6565
*/
66-
public void destroy()
66+
protected void destroy()
6767
{
6868
// do nothing
6969
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright 2017. All Rights Reserved.
3+
*
4+
* NOTICE: All information contained herein is, and remains
5+
* the property of shalabh.us. The intellectual and technical
6+
* concepts contained herein are proprietary to shalabh.us
7+
* and may be covered by U.S. and Foreign Patents,
8+
* patents in process, and are protected by trade secret or copyright law.
9+
* Dissemination of this information or reproduction of this material
10+
* is strictly forbidden unless prior written permission is obtained
11+
* from shalabh.us
12+
*/
13+
package us.shalabh.alp.function;
14+
15+
import com.amazonaws.services.lambda.runtime.Context;
16+
import com.amazonaws.services.lambda.runtime.RequestHandler;
17+
18+
import us.shalabh.alp.exception.AccessDeniedException;
19+
import us.shalabh.alp.model.HttpRequest;
20+
import us.shalabh.alp.model.HttpResponse;
21+
import us.shalabh.alp.utils.http.HttpUtils;
22+
23+
/**
24+
* <p>
25+
* Base HTTP Handler. Creates a canned response and Handles Errors out of the
26+
* box.
27+
* </p>
28+
*
29+
* @author Shalabh Jaiswal
30+
*/
31+
public abstract class HttpBaseHandler implements RequestHandler<HttpRequest, HttpResponse>
32+
{
33+
34+
/**
35+
* initialization code. used for setting up dependencies.
36+
*/
37+
protected void init()
38+
{
39+
// do nothing by default
40+
}
41+
42+
/**
43+
* process the HTTP Request
44+
*
45+
* @param request
46+
* @param response
47+
* @param context
48+
* @return
49+
*/
50+
protected abstract HttpResponse process(HttpRequest request, HttpResponse response, Context context);
51+
52+
/**
53+
* destroy method to release/close resources if any.
54+
*/
55+
protected void destroy()
56+
{
57+
// do nothing by default
58+
}
59+
60+
/*
61+
* (non-Javadoc)
62+
*
63+
* @see
64+
* com.amazonaws.services.lambda.runtime.RequestHandler#handleRequest(java.
65+
* lang.Object, com.amazonaws.services.lambda.runtime.Context)
66+
*/
67+
@Override
68+
public HttpResponse handleRequest(HttpRequest request, Context context)
69+
{
70+
HttpResponse response = new HttpResponse();
71+
72+
try
73+
{
74+
// 1. initialize
75+
init();
76+
77+
// 2. create a canned Response object with CORS enabled and process
78+
HttpUtils.setCORSHeaders(response);
79+
response = process(request, response, context);
80+
81+
// 3. release/close resources
82+
destroy();
83+
}
84+
catch (AccessDeniedException ade)
85+
{
86+
// Access Denied - 401
87+
HttpUtils.setAccessDeniedResponse(response);
88+
89+
// can be logged using log4j too
90+
ade.printStackTrace();
91+
}
92+
catch (Throwable t)
93+
{
94+
// internal server error - 500
95+
HttpUtils.setInternalServerErrorResponse(response);
96+
97+
// can be logged using log4j as well
98+
t.printStackTrace();
99+
}
100+
101+
return response;
102+
}
103+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2017. All Rights Reserved.
3+
*
4+
* NOTICE: All information contained herein is, and remains
5+
* the property of shalabh.us. The intellectual and technical
6+
* concepts contained herein are proprietary to shalabh.us
7+
* and may be covered by U.S. and Foreign Patents,
8+
* patents in process, and are protected by trade secret or copyright law.
9+
* Dissemination of this information or reproduction of this material
10+
* is strictly forbidden unless prior written permission is obtained
11+
* from shalabh.us
12+
*/
13+
package us.shalabh.alp.utils.http;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
import us.shalabh.alp.model.HttpResponse;
19+
20+
/**
21+
* Http Utilities
22+
*
23+
* @author Shalabh Jaiswal
24+
*/
25+
public class HttpUtils
26+
{
27+
// HTTP Status Codes
28+
private static final int HTTP_ACCESS_DENIED = 401;
29+
private static final int HTTP_INTERNAL_SERVER_ERROR = 500;
30+
31+
/**
32+
* Sets CORS headers
33+
*
34+
* @param headers
35+
* @return
36+
*/
37+
public static void setCORSHeaders(HttpResponse response)
38+
{
39+
Map<String, String> headers = response.getHeaders();
40+
41+
if (headers == null)
42+
{
43+
headers = new HashMap<String, String>();
44+
}
45+
46+
// TODO narrow the access, and pick up the url from a properties file.
47+
headers.put("Access-Control-Allow-Origin", "*");
48+
response.setHeaders(headers);
49+
}
50+
51+
/**
52+
* sets an error response with 401 code
53+
*
54+
* @param response
55+
*/
56+
public static void setAccessDeniedResponse(HttpResponse response)
57+
{
58+
response.setStatusCode(HTTP_ACCESS_DENIED);
59+
setCORSHeaders(response);
60+
61+
response.setBody("Access Denied");
62+
}
63+
64+
/**
65+
* sets an error response with 500 code
66+
*
67+
* @param response
68+
*/
69+
public static void setInternalServerErrorResponse(HttpResponse response)
70+
{
71+
response.setStatusCode(HTTP_INTERNAL_SERVER_ERROR);
72+
setCORSHeaders(response);
73+
74+
response.setBody("Internal Server Error");
75+
}
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /