diff --git a/appengine/endpoints-frameworks-v2/backend/.gitignore b/appengine/endpoints-frameworks-v2/backend/.gitignore new file mode 100644 index 00000000000..168aa9434f5 --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/.gitignore @@ -0,0 +1 @@ +swagger.json diff --git a/appengine/endpoints-frameworks-v2/backend/README.md b/appengine/endpoints-frameworks-v2/backend/README.md new file mode 100644 index 00000000000..18322cab66e --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/README.md @@ -0,0 +1,77 @@ +# App Engine Standard & Google Cloud Endpoints Frameworks & Java + +This sample demonstrates how to use Google Cloud Endpoints Frameworks using +Java on App Engine Standard. + +## Adding the project ID to the sample API code + +You must add the project ID obtained when you created your project to the +sample's `pom.xml` before you can deploy the code. + +To add the project ID: + +0. Edit the file `pom.xml`. + +0. For ``, replace the value `YOUR_PROJECT_ID` with +your project ID. + +0. Save your changes. + +## Building the sample project + +To build the project: + + mvn clean package + +## Generating the swagger.json file + +To generate the required configuration file `swagger.json`: + +0. Download and unzip the [Endpoints Framework tools +package](http://search.maven.org/remotecontent?filepath=com/google/endpoints/endpoints-framework-tools/2.0.0-beta.7/endpoints-framework-tools-2.0.0-beta.7.zip). + +0. Invoke the Endpoints Tool using this command: + + path/to/endpoints-framework-tools-2.0.0-beta.7/bin/endpoints-framework-tools get-swagger-doc \ + -h .appspot.com \ + -w target/echo-1.0-SNAPSHOT com.example.echo.Echo + + Replace`` with your project ID. + +## Deploying the sample API to App Engine + +To deploy the sample API: + +0. Invoke the `gcloud` command to deploy the API configuration file: + + gcloud beta service-management deploy swagger.json + +0. Deploy the API implementation code by invoking: + + mvn appengine:update + + The first time you upload a sample app, you may be prompted to authorize the + deployment. Follow the prompts: when you are presented with a browser window + containing a code, copy it to the terminal window. + +0. Wait for the upload to finish. + +## Sending a request to the sample API + +After you deploy the API and its configuration file, you can send requests +to the API. + +To send a request to the API, from a command line, invoke the following `cURL` +command: + + curl \ + -H "Content-Type: application/json" \ + -X POST \ + -d '{"message":"echo"}' \ + https://$PROJECT_ID.appspot.com/_ah/api/echo/v1/echo + +You will get a 200 response with the following data: + + { + "message": "echo" + } diff --git a/appengine/endpoints-frameworks-v2/backend/pom.xml b/appengine/endpoints-frameworks-v2/backend/pom.xml new file mode 100644 index 00000000000..3c4796f3294 --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + war + 1.0-SNAPSHOT + + com.example.echo + echo + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + + UTF-8 + + YOUR_PROJECT_ID + + + + + + javax.servlet + servlet-api + 2.5 + provided + + + com.google.endpoints + endpoints-framework + 2.0.0-beta.7 + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.codehaus.mojo + versions-maven-plugin + 2.2 + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.6 + + + + ${basedir}/src/main/webapp/WEB-INF + true + WEB-INF + + + + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + false + + + + + + + + + diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java new file mode 100644 index 00000000000..8a7d050051a --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.example.echo; + +import com.google.api.server.spi.auth.common.User; +import com.google.api.server.spi.config.Api; +import com.google.api.server.spi.config.ApiMethod; +import com.google.api.server.spi.config.ApiNamespace; + +/** The Echo API which Endpoints will be exposing. */ +@Api( + name = "echo", + version = "v1", + namespace = + @ApiNamespace( + ownerDomain = "echo.example.com", + ownerName = "echo.example.com", + packagePath = "" + ) + ) +public class Echo { + /** + * Echoes the received message back. + * + * Note that name is specified and will override the default name of "{class name}.{method + * name}". For example, the default is "echo.echo". + * + * Note that httpMethod is not specified. This will default to a reasonable HTTP method + * depending on the API method name. In this case, the HTTP method will default to POST. + */ + @ApiMethod(name = "echo") + public Message echo(Message message) { + return message; + } + + /** + * Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP + * 401. + * + * Note that name is not specified. This will default to "{class name}.{method name}". For + * example, the default is "echo.getUserEmail". + * + * Note that httpMethod is not required here. Without httpMethod, this will default to GET due + * to the API method name. httpMethod is added here for example purposes. + */ + @ApiMethod(httpMethod = ApiMethod.HttpMethod.GET) + public Email getUserEmail(User user) throws UnauthorizedException { + if (user == null) { + throw new UnauthorizedException(); + } + + Email response = new Email(); + response.setEmail(user.getEmail()); + return response; + } +} diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Email.java b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Email.java new file mode 100644 index 00000000000..e7725a9d9cc --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Email.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.example.echo; + +/** The email bean that will be used in the getUserEmail response. */ +public class Email { + private String email; + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Message.java b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Message.java new file mode 100644 index 00000000000..64c043c8857 --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Message.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.example.echo; + +/** The message bean that will be used in the echo request and response. */ +public class Message { + + private String message; + + public String getMessage() { + return this.message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java new file mode 100644 index 00000000000..9d73e09ca06 --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.example.echo; + +import com.google.api.server.spi.ServiceException; + +/** + * Throw this when the user is unauthorized. + * + * Note that this must inherit from ServiceException. + */ +public class UnauthorizedException extends ServiceException { + public UnauthorizedException() { + super(401, "Unauthorized"); + } +} diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..274894e136b --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,29 @@ + + + + ${endpoints.project.id} + 1 + true + + + 2 + + + + + + diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/logging.properties b/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..a375465b2fc --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,25 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except +# in compliance with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under +# the License. + +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = WARNING diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/web.xml b/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..53beffe8208 --- /dev/null +++ b/appengine/endpoints-frameworks-v2/backend/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,35 @@ + + + + + + EndpointsServlet + com.google.api.server.spi.EndpointsServlet + + services + com.example.echo.Echo + + + + + EndpointsServlet + /_ah/api/* + + + index.html + + diff --git a/pom.xml b/pom.xml index faaab723c76..185fc3497b2 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,7 @@ appengine/datastore/indexes appengine/datastore/indexes-exploding appengine/datastore/indexes-perfect + appengine/endpoints-frameworks-v2/backend appengine/firebase-event-proxy/gae-firebase-event-proxy appengine/guestbook-objectify appengine/helloworld