Skip to content

Commit e0ffe50

Browse files
authored
Merge pull request #850 from GoogleCloudPlatform/endpoints-guice
[Endpoints] Update Endpoints + Guice sample
2 parents 86c64d7 + 52bb096 commit e0ffe50

File tree

7 files changed

+56
-75
lines changed

7 files changed

+56
-75
lines changed

appengine-java8/endpoints-v2-guice/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ To add the project ID:
1717
0. For `<endpoints.project.id>`, replace the value `YOUR_PROJECT_ID` with
1818
your project ID.
1919

20-
0. Edit the file `src/main/java/com/example/echo/Echo.java`.
20+
0. Edit the file `src/main/java/com/example/echo/Echo.java` and
21+
`src/main/java/com/example/echo/EchoEndpointModule.java`.
2122

2223
0. Replace the value `YOUR-PROJECT-ID` with your project ID.
2324

@@ -82,7 +83,8 @@ You will get a 200 response with the following data:
8283
0. For `def projectId = 'YOUR_PROJECT_ID'`, replace the value `YOUR_PROJECT_ID`
8384
with your project ID.
8485

85-
0. Edit the file `src/main/java/com/example/echo/Echo.java
86+
0. Edit the file `src/main/java/com/example/echo/Echo.java` and
87+
`src/main/java/com/example/echo/EchoEndpointModule.java`.
8688

8789
0. Replace the value `YOUR-PROJECT-ID` with your project ID.
8890

appengine-java8/endpoints-v2-guice/src/main/java/com/example/echo/EchoEndpointModule.java

+19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,32 @@
1616

1717
package com.example.echo;
1818

19+
import com.google.api.control.ServiceManagementConfigFilter;
20+
import com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter;
21+
import com.google.api.server.spi.EndpointsServlet;
1922
import com.google.api.server.spi.guice.EndpointsModule;
2023
import com.google.common.collect.ImmutableList;
24+
import com.google.inject.servlet.GuiceFilter;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import javax.inject.Singleton;
2128

2229
// [START endpoints_module]
2330
public class EchoEndpointModule extends EndpointsModule {
2431
@Override
2532
public void configureServlets() {
33+
super.configureServlets();
34+
35+
bind(ServiceManagementConfigFilter.class).in(Singleton.class);
36+
filter("/_ah/api/*").through(ServiceManagementConfigFilter.class);
37+
38+
Map<String, String> apiController = new HashMap<String, String>();
39+
apiController.put("endpoints.projectId", "YOUR-PROJECT-ID");
40+
apiController.put("endpoints.serviceName", "YOUR-PROJECT-ID.appspot.com");
41+
42+
bind(GoogleAppEngineControlFilter.class).in(Singleton.class);
43+
filter("/_ah/api/*").through(GoogleAppEngineControlFilter.class, apiController);
44+
2645
bind(Echo.class).toInstance(new Echo());
2746
configureEndpoints("/_ah/api/*", ImmutableList.of(Echo.class));
2847
}

appengine-java8/endpoints-v2-guice/src/main/webapp/WEB-INF/web.xml

+5-29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
2323
</filter>
2424

25+
<!--
26+
URL Pattern /_ah/api/* instead of /* because a legacy v1 servlet uses
27+
the route /_ah/api/ and using /* will erronously use the legacy v1
28+
servlet instead of routing to your API.
29+
-->
2530
<filter-mapping>
2631
<filter-name>guiceFilter</filter-name>
2732
<url-pattern>/_ah/api/*</url-pattern>
@@ -35,33 +40,4 @@
3540
<welcome-file>index.html</welcome-file>
3641
</welcome-file-list>
3742

38-
<!-- Add a filter that fetches the service config from service management. -->
39-
<filter>
40-
<filter-name>endpoints-api-configuration</filter-name>
41-
<filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
42-
</filter>
43-
44-
<!-- Add a filter that performs Endpoints logging and monitoring. -->
45-
<filter>
46-
<filter-name>endpoints-api-controller</filter-name>
47-
<filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
48-
<init-param>
49-
<param-name>endpoints.projectId</param-name>
50-
<param-value>${endpoints.project.id}</param-value>
51-
</init-param>
52-
<init-param>
53-
<param-name>endpoints.serviceName</param-name>
54-
<param-value>${endpoints.project.id}.appspot.com</param-value>
55-
</init-param>
56-
</filter>
57-
58-
<filter-mapping>
59-
<filter-name>endpoints-api-configuration</filter-name>
60-
<servlet-name>EndpointsServlet</servlet-name>
61-
</filter-mapping>
62-
63-
<filter-mapping>
64-
<filter-name>endpoints-api-controller</filter-name>
65-
<servlet-name>EndpointsServlet</servlet-name>
66-
</filter-mapping>
6743
</web-app>

appengine/endpoints-frameworks-v2/guice-example/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ To add the project ID:
1717
0. For `<endpoints.project.id>`, replace the value `YOUR_PROJECT_ID` with
1818
your project ID.
1919

20-
0. Edit the file `src/main/java/com/example/echo/Echo.java`.
20+
0. Edit the file `src/main/java/com/example/echo/Echo.java` and
21+
`src/main/java/com/example/echo/EchoEndpointModule.java`.
2122

2223
0. Replace the value `YOUR-PROJECT-ID` with your project ID.
2324

@@ -82,7 +83,8 @@ You will get a 200 response with the following data:
8283
0. For `def projectId = 'YOUR_PROJECT_ID'`, replace the value `YOUR_PROJECT_ID`
8384
with your project ID.
8485

85-
0. Edit the file `src/main/java/com/example/echo/Echo.java
86+
0. Edit the file `src/main/java/com/example/echo/Echo.java` and
87+
`src/main/java/com/example/echo/EchoEndpointModule.java`
8688

8789
0. Replace the value `YOUR-PROJECT-ID` with your project ID.
8890

appengine/endpoints-frameworks-v2/guice-example/src/main/java/com/example/echo/EchoEndpointModule.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,34 @@
1616

1717
package com.example.echo;
1818

19+
import com.google.api.control.ServiceManagementConfigFilter;
20+
import com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter;
21+
import com.google.api.server.spi.EndpointsServlet;
1922
import com.google.api.server.spi.guice.EndpointsModule;
2023
import com.google.common.collect.ImmutableList;
24+
import com.google.inject.servlet.GuiceFilter;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import javax.inject.Singleton;
2128

2229
// [START endpoints_module]
2330
public class EchoEndpointModule extends EndpointsModule {
2431
@Override
2532
public void configureServlets() {
33+
super.configureServlets();
34+
35+
bind(ServiceManagementConfigFilter.class).in(Singleton.class);
36+
filter("/_ah/api/*").through(ServiceManagementConfigFilter.class);
37+
38+
Map<String, String> apiController = new HashMap<String, String>();
39+
apiController.put("endpoints.projectId", "YOUR-PROJECT-ID");
40+
apiController.put("endpoints.serviceName", "YOUR-PROJECT-ID.appspot.com");
41+
42+
bind(GoogleAppEngineControlFilter.class).in(Singleton.class);
43+
filter("/_ah/api/*").through(GoogleAppEngineControlFilter.class, apiController);
44+
2645
bind(Echo.class).toInstance(new Echo());
2746
configureEndpoints("/_ah/api/*", ImmutableList.of(Echo.class));
28-
super.configureServlets();
2947
}
3048
}
3149
// [END endpoints_module]

appengine/endpoints-frameworks-v2/guice-example/src/main/main74.iml

-12
This file was deleted.

appengine/endpoints-frameworks-v2/guice-example/src/main/webapp/WEB-INF/web.xml

+5-29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
2323
</filter>
2424

25+
<!--
26+
URL Pattern /_ah/api/* instead of /* because a legacy v1 servlet uses
27+
the route /_ah/api/ and using /* will erronously use the legacy v1
28+
servlet instead of routing to your API.
29+
-->
2530
<filter-mapping>
2631
<filter-name>guiceFilter</filter-name>
2732
<url-pattern>/_ah/api/*</url-pattern>
@@ -35,33 +40,4 @@
3540
<welcome-file>index.html</welcome-file>
3641
</welcome-file-list>
3742

38-
<!-- Add a filter that fetches the service config from service management. -->
39-
<filter>
40-
<filter-name>endpoints-api-configuration</filter-name>
41-
<filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
42-
</filter>
43-
44-
<!-- Add a filter that performs Endpoints logging and monitoring. -->
45-
<filter>
46-
<filter-name>endpoints-api-controller</filter-name>
47-
<filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
48-
<init-param>
49-
<param-name>endpoints.projectId</param-name>
50-
<param-value>${endpoints.project.id}</param-value>
51-
</init-param>
52-
<init-param>
53-
<param-name>endpoints.serviceName</param-name>
54-
<param-value>${endpoints.project.id}.appspot.com</param-value>
55-
</init-param>
56-
</filter>
57-
58-
<filter-mapping>
59-
<filter-name>endpoints-api-configuration</filter-name>
60-
<servlet-name>EndpointsServlet</servlet-name>
61-
</filter-mapping>
62-
63-
<filter-mapping>
64-
<filter-name>endpoints-api-controller</filter-name>
65-
<servlet-name>EndpointsServlet</servlet-name>
66-
</filter-mapping>
6743
</web-app>

0 commit comments

Comments
 (0)