diff --git a/appengine/endpoints-v1-helloworld/README.md b/appengine/endpoints-v1-helloworld/README.md new file mode 100644 index 00000000000..49e0e7d59e8 --- /dev/null +++ b/appengine/endpoints-v1-helloworld/README.md @@ -0,0 +1,57 @@ +endpoints-skeleton +================== + +# DEPRECATED + +### This sample uses endpoints v1 the preferred strategy for new development is to use Endpoints v2 + +A skeleton application for Google Cloud Endpoints in Java. + +- [App Engine][1] + +- [Java][2] + +- [Google Cloud Endpoints][3] +- [Google App Engine Maven plugin][4] + + +1. Update the value of `application` in `appengine-web.xml` to the app + ID you have registered in the App Engine admin console and would + like to use to host your instance of this sample. + +1. Add your API method to `src/main/java/com/example/helloworld/YourFirstAPI.java`. + +1. Optional step: These sub steps are not required but you need this + if you want to have auth protected methods. + + 1. Update the values in `src/main/java/com/example/helloworld/Constants.java` + to reflect the respective client IDs you have registered in the + [APIs Console][6]. + + 1. You also need to supply the web client ID you have registered + in the [APIs Console][4] to your client of choice (web, Android, + iOS). + +1. Run the application with `mvn appengine:devserver`, and ensure it's + running by visiting your local server's api explorer's address (by + default [localhost:8080/_ah/api/explorer][5].) + +1. Get the client library with + + $ mvn appengine:endpoints_get_client_lib + + It will generate a client library jar file under the + `target/endpoints-client-libs//target` directory of your + project, as well as install the artifact into your local maven + repository. + +1. Deploy your application to Google App Engine with + + $ mvn appengine:update + +[1]: https://developers.google.com/appengine +[2]: http://java.com/en/ +[3]: https://developers.google.com/appengine/docs/java/endpoints/ +[4]: https://developers.google.com/appengine/docs/java/tools/maven +[5]: https://localhost:8080/_ah/api/explorer +[6]: https://console.developers.google.com/ diff --git a/appengine/endpoints-v1-helloworld/pom.xml b/appengine/endpoints-v1-helloworld/pom.xml new file mode 100644 index 00000000000..559928305ee --- /dev/null +++ b/appengine/endpoints-v1-helloworld/pom.xml @@ -0,0 +1,160 @@ + + + + + 4.0.0 + war + 1.0-SNAPSHOT + + com.example.helloworld + helloworld + + + com.google.cloud + appengine-doc-samples + 1.0.0 + .. + + + + 1 + UTF-8 + + 1.9.56 + 1.7 + 1.7 + + + + 3.1.0 + + + + + + com.google.appengine + appengine-api-1.0-sdk + ${appengine.plugin.version} + + + com.google.appengine + appengine-endpoints + ${appengine.plugin.version} + + + javax.servlet + servlet-api + 2.5 + provided + + + javax.inject + javax.inject + 1 + + + + + junit + junit + 4.11 + test + + + org.mockito + mockito-all + 1.9.5 + test + + + com.google.appengine + appengine-testing + ${appengine.plugin.version} + test + + + com.google.appengine + appengine-api-stubs + ${appengine.plugin.version} + test + + + + + + ${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 + + ${project.build.directory}/generated-sources/appengine-endpoints/WEB-INF/web.xml + + + + ${project.build.directory}/generated-sources/appengine-endpoints + + + WEB-INF/*.discovery + WEB-INF/*.api + + + + + + + + com.google.appengine + appengine-maven-plugin + ${appengine.plugin.version} + + false + + + + + + + + + endpoints_get_discovery_doc + + + + + + + + diff --git a/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/Bar.java b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/Bar.java new file mode 100644 index 00000000000..df4dad666ed --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/Bar.java @@ -0,0 +1,37 @@ +/* + * 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. + */ +package com.example.helloworld; + +import com.google.api.server.spi.config.ApiTransformer; + +// [START all] +@ApiTransformer(BarTransformer.class) +public class Bar { + private final int x; + private final int y; + + public Bar(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } +} +// [END all] diff --git a/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/BarTransformer.java b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/BarTransformer.java new file mode 100644 index 00000000000..2d22b4ed51d --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/BarTransformer.java @@ -0,0 +1,29 @@ +/* + * 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. + */ +package com.example.helloworld; + +import com.google.api.server.spi.config.Transformer; + +// [START all] +public class BarTransformer implements Transformer { + public String transformTo(Bar in) { + return in.getX() + "," + in.getY(); + } + + public Bar transformFrom(String in) { + String[] xy = in.split(","); + return new Bar(Integer.parseInt(xy[0]), Integer.parseInt(xy[1])); + } +} +// [END all] diff --git a/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/Constants.java b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/Constants.java new file mode 100644 index 00000000000..ff9f762e240 --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/Constants.java @@ -0,0 +1,31 @@ +/** + * 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. + */ + +package com.example.helloworld; + +// [START all] +/** + * Contains the client IDs and scopes for allowed clients consuming your API. + */ +public class Constants { + public static final String WEB_CLIENT_ID = "replace this with your web client ID"; + public static final String ANDROID_CLIENT_ID = "replace this with your Android client ID"; + public static final String IOS_CLIENT_ID = "replace this with your iOS client ID"; + public static final String ANDROID_AUDIENCE = WEB_CLIENT_ID; + + public static final String EMAIL_SCOPE = "https://www.googleapis.com/auth/userinfo.email"; +} +// [END all] diff --git a/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/MyBean.java b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/MyBean.java new file mode 100644 index 00000000000..92aef481cee --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/MyBean.java @@ -0,0 +1,31 @@ +/** + * 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. + */ +package com.example.helloworld; + +//[START all] + +/** The object model for the data we are sending through endpoints */ +public class MyBean { + + private String myData; + + public String getData() { + return myData; + } + + public void setData(String data) { + myData = data; + } +} +//[END all] diff --git a/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/RequestTimeoutException.java b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/RequestTimeoutException.java new file mode 100644 index 00000000000..a0f6ceb2482 --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/RequestTimeoutException.java @@ -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. + */ +package com.example.helloworld; + +// [START all] +import com.google.api.server.spi.ServiceException; + +// Exceptions must inherit from ServiceException +public class RequestTimeoutException extends ServiceException { + public RequestTimeoutException(String message) { + super(408, message); + } +} +// [END all] diff --git a/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/YourFirstAPI.java b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/YourFirstAPI.java new file mode 100644 index 00000000000..604eab29e60 --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/java/com/example/helloworld/YourFirstAPI.java @@ -0,0 +1,135 @@ +/** + * 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. + */ + +package com.example.helloworld; + +import com.google.api.server.spi.auth.common.User; +import com.google.api.server.spi.config.AnnotationBoolean; +import com.google.api.server.spi.config.Api; +import com.google.api.server.spi.config.ApiMethod; +import com.google.api.server.spi.config.ApiNamespace; +import com.google.api.server.spi.config.ApiResourceProperty; +import com.google.api.server.spi.response.NotFoundException; +import com.google.appengine.api.oauth.OAuthRequestException; + +import java.io.IOException; + +import javax.inject.Named; + +// [START header] +/** An endpoint class we are exposing */ +@Api(name = "myApi", + version = "v1", + namespace = @ApiNamespace(ownerDomain = "helloworld.example.com", + ownerName = "helloworld.example.com", + packagePath = "")) +// [END header] + +public class YourFirstAPI { + + // [START hi] + /** A simple endpoint method that takes a name and says Hi back */ + @ApiMethod(name = "sayHi") + public MyBean sayHi(@Named("name") String name) { + MyBean response = new MyBean(); + response.setData("Hi, " + name); + + return response; + } + //[END hi] + + // [START hi_user] + /** A simple endpoint method that takes a name and says Hi back */ + @ApiMethod( + name = "sayHiUser", + httpMethod = ApiMethod.HttpMethod.GET) + public MyBean sayHiUser(@Named("name") String name, User user) + throws OAuthRequestException, IOException { + MyBean response = new MyBean(); + response.setData("Hi, " + name + "(" + user.getEmail() + ")"); + + return response; + } + //[END hi_user] + + // [START post] + @ApiMethod( + name = "mybean.insert", + path = "mybean", + httpMethod = ApiMethod.HttpMethod.POST + ) + public void insertFoo(MyBean foo) { + } + // [END post] + // [START resources] + class Resp { + private String foobar = "foobar"; + private String bin = "bin"; + + @ApiResourceProperty + private String visible = "nothidden"; + + @ApiResourceProperty(ignored = AnnotationBoolean.TRUE) + public String getBin() { + return bin; + } + + public void setBin(String bin) { + this.bin = bin; + } + + @ApiResourceProperty(name = "baz") + public String getFoobar() { + return foobar; + } + + public void setFoobar(String foobar) { + this.foobar = foobar; + } + } + + public Resp getResp() { + return new Resp(); + } + // [END resources] + + @SuppressWarnings("unused") + // [START lookmeup] + /** A simple endpoint method that takes a name and says Hi back */ + @ApiMethod( + name = "lookmeup", + httpMethod = ApiMethod.HttpMethod.GET) + public MyBean lookMeUp( User user) + throws OAuthRequestException, RequestTimeoutException, NotFoundException, IOException { + MyBean response = new MyBean(); + + // Look me up here... + // response = lookup(user); + // + + if (response != null) { + // [START notfound] + throw new NotFoundException(user.getEmail()); + // [END notfound] + } + if (true /* did we time out */ ) { + // [START timeout] + throw new RequestTimeoutException("lookMeUp() timed out"); // custom timeout exception + // [END timeout] + } + + return response; + } + // [END lookmeup] +} diff --git a/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..480f4b3aa3c --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + your-app-id + 1 + true + + + + + diff --git a/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/logging.properties b/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..a375465b2fc --- /dev/null +++ b/appengine/endpoints-v1-helloworld/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-v1-helloworld/src/main/webapp/WEB-INF/web.xml b/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..4911034a798 --- /dev/null +++ b/appengine/endpoints-v1-helloworld/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + SystemServiceServlet + com.google.api.server.spi.SystemServiceServlet + + services + com.example.helloworld.YourFirstAPI + + + + SystemServiceServlet + /_ah/spi/* + + + index.html + + diff --git a/appengine/pom.xml b/appengine/pom.xml index a8beaad98f0..89040c55c3e 100644 --- a/appengine/pom.xml +++ b/appengine/pom.xml @@ -53,6 +53,9 @@ endpoints-frameworks-v2/backend endpoints-frameworks-v2/migration-example endpoints-frameworks-v2/guice-example + + endpoints-v1-helloworld + firebase-event-proxy/gae-firebase-event-proxy firebase-tictactoe