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 f1feef9

Browse files
author
SJAsus
committed
Added a Base HTTP Handler and a Demo Implementation with Dependency
Injection
1 parent 7939762 commit f1feef9

File tree

11 files changed

+371
-15
lines changed

11 files changed

+371
-15
lines changed
Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
/**
2-
* Copyright 2017. All Rights Reserved.
2+
* Copyright 2017 shalabh.us
33
*
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
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
1216
*/
1317
package us.shalabh.alp.client;
1418

1519
import java.io.IOException;
1620

1721
import com.amazonaws.services.lambda.runtime.events.S3Event;
1822

23+
import us.shalabh.alp.function.DemoHttpRequestLambda;
1924
import us.shalabh.alp.function.DemoS3EventLambda;
25+
import us.shalabh.alp.model.HttpRequest;
26+
import us.shalabh.alp.model.HttpResponse;
2027

2128
/**
2229
* Just invokes the Demo Lambda Handlers
@@ -26,32 +33,58 @@
2633
public class SimpleAWSLambdaSimulator
2734
{
2835
/**
29-
* psvm
36+
* psvm
3037
*
3138
* @param args
3239
*/
3340
public static void main(String args[])
3441
{
3542
try
3643
{
37-
invokeDemoS3EventLambda();
44+
switch (args[0])
45+
{
46+
case "S3_EVENT":
47+
invokeDemoS3EventLambda();
48+
break;
49+
case "HTTP_REQUEST":
50+
invokeDemoHttpRequestLambda();
51+
break;
52+
default:
53+
break;
54+
}
55+
3856
}
3957
catch (Throwable t)
4058
{
4159
// something went wrong
4260
t.printStackTrace();
4361
}
4462
}
45-
63+
4664
/**
47-
* Simulate the lambda being invoked.
65+
* Simulate the lambda being invoked.
4866
*
4967
* @throws IOException
5068
*/
5169
private static void invokeDemoS3EventLambda() throws IOException
52-
{
70+
{
5371
// invoke the lambda
5472
DemoS3EventLambda lambda = new DemoS3EventLambda();
5573
lambda.handleRequest(new S3Event(null), new ClientTestContext());
5674
}
75+
76+
/**
77+
* Simulate the HTTP Request lambda being invoked.
78+
*
79+
* @throws IOException
80+
*/
81+
private static void invokeDemoHttpRequestLambda() throws IOException
82+
{
83+
// invoke the lambda
84+
DemoHttpRequestLambda lambda = new DemoHttpRequestLambda();
85+
HttpResponse response = lambda.handleRequest(new HttpRequest(), new ClientTestContext());
86+
87+
System.out.println(response.getBody());
88+
}
89+
5790
}

‎src/main/java/us/shalabh/alp/config/AppComponent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.amazonaws.services.s3.AmazonS3;
1818

1919
import dagger.Component;
20+
import us.shalabh.alp.service.DemoHttpRequestService;
2021
import us.shalabh.alp.service.DemoS3EventService;
2122

2223
/**
@@ -42,4 +43,11 @@ public interface AppComponent
4243
* @return
4344
*/
4445
DemoS3EventService getDemoS3EventService();
46+
47+
/**
48+
* gets DemoHttpRequestService object
49+
*
50+
* @return
51+
*/
52+
DemoHttpRequestService getDemoHttpRequestService();
4553
}

‎src/main/java/us/shalabh/alp/config/AppModule.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717

1818
import dagger.Module;
1919
import dagger.Provides;
20+
import us.shalabh.alp.dao.DemoHttpRequestDao;
2021
import us.shalabh.alp.dao.DemoS3EventDao;
22+
import us.shalabh.alp.dao.IDemoHttpRequestDao;
2123
import us.shalabh.alp.dao.IDemoS3EventDao;
24+
import us.shalabh.alp.service.DemoHttpRequestService;
2225
import us.shalabh.alp.service.DemoS3EventService;
26+
import us.shalabh.alp.service.IDemoHttpRequestService;
2327
import us.shalabh.alp.service.IDemoS3EventService;
2428

2529
/**
@@ -64,4 +68,28 @@ public IDemoS3EventDao provideDemoS3EventDao(DemoS3EventDao demoS3EventDao)
6468
{
6569
return demoS3EventDao;
6670
}
71+
72+
/**
73+
* Provides an implementation of an <code>IDemoHttpRequestDao</code>
74+
*
75+
* @param demoHttpRequestDao
76+
* @return
77+
*/
78+
@Provides
79+
public IDemoHttpRequestDao provideDemoHttpRequestDao(DemoHttpRequestDao demoHttpRequestDao)
80+
{
81+
return demoHttpRequestDao;
82+
}
83+
84+
/**
85+
* Provides an implementation of an <code>IDemoHttpRequestService</code>
86+
*
87+
* @param demoHttpRequestService
88+
* @return
89+
*/
90+
@Provides
91+
public IDemoHttpRequestService provideDemoHttpRequestService(DemoHttpRequestService demoHttpRequestService)
92+
{
93+
return demoHttpRequestService;
94+
}
6795
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.dao;
14+
15+
import javax.inject.Inject;
16+
import javax.inject.Singleton;
17+
18+
/**
19+
* Gets data from a data source for eg. DynamoDB
20+
*
21+
* @author Shalabh Jaiswal
22+
*/
23+
@Singleton
24+
public class DemoHttpRequestDao implements IDemoHttpRequestDao
25+
{
26+
/**
27+
* default constructor
28+
*/
29+
@Inject
30+
public DemoHttpRequestDao()
31+
{
32+
// do nothing.
33+
}
34+
35+
/* (non-Javadoc)
36+
* @see us.shalabh.alp.dao.IDemoHttpRequestDao#getData()
37+
*/
38+
@Override
39+
public String getData()
40+
{
41+
return "Data for the HTTP Request";
42+
}
43+
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.dao;
14+
15+
/**
16+
* DAO class
17+
*
18+
* @author Shalabh Jaiswal
19+
*/
20+
public interface IDemoHttpRequestDao
21+
{
22+
/**
23+
* Gets data from the data source
24+
*
25+
* @return
26+
*/
27+
public String getData();
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 javax.inject.Inject;
16+
17+
import com.amazonaws.services.lambda.runtime.Context;
18+
19+
import us.shalabh.alp.model.HttpRequest;
20+
import us.shalabh.alp.model.HttpResponse;
21+
import us.shalabh.alp.service.IDemoHttpRequestService;
22+
23+
/**
24+
* Demo Lambda to handle Http Requests via API Gateway.
25+
*
26+
* @author Shalabh Jaiswal
27+
*/
28+
public class DemoHttpRequestLambda extends HttpBaseHandler
29+
{
30+
// service class
31+
@Inject
32+
IDemoHttpRequestService demoHttpRequestService;
33+
34+
/* (non-Javadoc)
35+
* @see us.shalabh.alp.function.HttpBaseHandler#init()
36+
*/
37+
@Override
38+
protected void init()
39+
{
40+
demoHttpRequestService = appComponent.getDemoHttpRequestService();
41+
}
42+
43+
/* (non-Javadoc)
44+
* @see us.shalabh.alp.function.HttpBaseHandler#process(us.shalabh.alp.model.HttpRequest, us.shalabh.alp.model.HttpResponse, com.amazonaws.services.lambda.runtime.Context)
45+
*/
46+
@Override
47+
protected HttpResponse process(HttpRequest request, HttpResponse response, Context context)
48+
{
49+
// json created by hand.
50+
String body = "{\"data\":\"" +demoHttpRequestService.getData() +"\"}";
51+
response.setBody(body);
52+
53+
return response;
54+
}
55+
56+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.amazonaws.services.lambda.runtime.Context;
1616
import com.amazonaws.services.lambda.runtime.RequestHandler;
1717

18+
import us.shalabh.alp.config.AppComponent;
19+
import us.shalabh.alp.config.DaggerAppComponent;
1820
import us.shalabh.alp.exception.AccessDeniedException;
1921
import us.shalabh.alp.model.HttpRequest;
2022
import us.shalabh.alp.model.HttpResponse;
@@ -30,7 +32,9 @@
3032
*/
3133
public abstract class HttpBaseHandler implements RequestHandler<HttpRequest, HttpResponse>
3234
{
33-
35+
// dagger app component
36+
static final AppComponent appComponent = DaggerAppComponent.builder().build();
37+
3438
/**
3539
* initialization code. used for setting up dependencies.
3640
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.service;
14+
15+
import javax.inject.Inject;
16+
import javax.inject.Singleton;
17+
18+
import us.shalabh.alp.dao.IDemoHttpRequestDao;
19+
20+
/**
21+
* Service Class Implementation
22+
*
23+
* @author Shalabh Jaiswal
24+
*/
25+
@Singleton
26+
public class DemoHttpRequestService implements IDemoHttpRequestService
27+
{
28+
private IDemoHttpRequestDao demoHttpRequestDao;
29+
30+
/**
31+
* contructor injection
32+
*/
33+
@Inject
34+
public DemoHttpRequestService(IDemoHttpRequestDao demoHttpRequestDao)
35+
{
36+
this.demoHttpRequestDao = demoHttpRequestDao;
37+
}
38+
39+
/* (non-Javadoc)
40+
* @see us.shalabh.alp.service.IDemoHttpRequestService#getData()
41+
*/
42+
@Override
43+
public String getData()
44+
{
45+
return demoHttpRequestDao.getData();
46+
}
47+
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.service;
14+
15+
/**
16+
* Service class
17+
*
18+
* @author Shalabh Jaiswal
19+
*/
20+
public interface IDemoHttpRequestService
21+
{
22+
/**
23+
* gets data from data source
24+
*
25+
* @return
26+
*/
27+
public String getData();
28+
}

0 commit comments

Comments
(0)

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