Java SP Code Sample
Scroll to the bottom for sample files you can download. The sections below also contain a line-numbered copy for quick reference.
oauth/javademo/src/com/google/appengine/demos/oauthsp/OAuth2LeggedServlet.java
1: /* 2: * Copyright 2009 Google Inc. 3: * 4: * Licensed under the Apache License, Version 2.0 (the "License"); 5: * you may not use this file except in compliance with the License. 6: * You may obtain a copy of the License at 7: * 8: * http://www.apache.org/licenses/LICENSE-2.0 9: * 10: * Unless required by applicable law or agreed to in writing, software 11: * distributed under the License is distributed on an "AS IS" BASIS, 12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13: * See the License for the specific language governing permissions and 14: * limitations under the License. 15: */ 16: 17: package com.google.appengine.demos.oauthsp; 18: 19: import java.io.IOException; 20: import java.util.logging.Level; 21: import java.util.logging.Logger; 22: 23: import javax.servlet.http.HttpServlet; 24: import javax.servlet.http.HttpServletRequest; 25: import javax.servlet.http.HttpServletResponse; 26: 27: /** 28: * Simple servlet to demonstrate the 2-legged OAuth API. 29: */ 30: public class OAuth2LeggedServlet extends HttpServlet { 31: 32: private static final Logger logger = 33: Logger.getLogger(OAuth2LeggedServlet.class.getName()); 34: 35: @Override 36: public void doGet(HttpServletRequest req, HttpServletResponse res) 37: throws IOException { 38: OAuthService oauthService = new OAuthService(); 39: 40: try { 41: String message = "Signature is valid. OAuth consumer key: " 42: + oauthService.getOAuthConsumerKey(); 43: printAndLog(res, message, null); 44: } catch (OAuthService.OAuthInvalidRequestException ex) { 45: printAndLog(res, "Invalid OAuth request", ex); 46: } catch (OAuthService.OAuthException ex) { 47: printAndLog(res, "OAuth exception", ex); 48: } 49: } 50: 51: @Override 52: public void doPost(HttpServletRequest req, HttpServletResponse res) 53: throws IOException { 54: doGet(req, res); 55: } 56: 57: private void printAndLog(HttpServletResponse res, String message, 58: Throwable thrown) throws IOException { 59: res.getWriter().println(message); 60: if (thrown != null) { 61: logger.log(Level.INFO, message, thrown); 62: } else { 63: logger.log(Level.INFO, message); 64: } 65: } 66: }
oauth/javademo/src/com/google/appengine/demos/oauthsp/OAuthService.java
1: /* 2: * Copyright 2009 Google Inc. 3: * 4: * Licensed under the Apache License, Version 2.0 (the "License"); 5: * you may not use this file except in compliance with the License. 6: * You may obtain a copy of the License at 7: * 8: * http://www.apache.org/licenses/LICENSE-2.0 9: * 10: * Unless required by applicable law or agreed to in writing, software 11: * distributed under the License is distributed on an "AS IS" BASIS, 12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13: * See the License for the specific language governing permissions and 14: * limitations under the License. 15: */ 16: 17: package com.google.appengine.demos.oauthsp; 18: 19: import com.google.appengine.api.users.UserServiceFailureException; 20: import com.google.apphosting.api.ApiProxy; 21: import com.google.apphosting.api.UserServicePb.CheckOAuthSignatureRequest; 22: import com.google.apphosting.api.UserServicePb.CheckOAuthSignatureResponse; 23: import com.google.apphosting.api.UserServicePb.UserServiceError; 24: 25: /** 26: * The OAuthService provides methods useful for validating OAuth requests. 27: */ 28: public class OAuthService { 29: 30: /** 31: * Thrown if an unknown OAuth error occurs. 32: */ 33: public static class OAuthException extends Exception { 34: public OAuthException(String message) { 35: super(message); 36: } 37: } 38: 39: /** 40: * Thrown when a request is not a valid OAuth request. 41: */ 42: public static class OAuthInvalidRequestException extends Exception { 43: public OAuthInvalidRequestException(String message) { 44: super(message); 45: } 46: } 47: 48: /** 49: * Returns the oauth_consumer_key OAuth parameter from the request. 50: * 51: * @return The oauth_consumer_key OAuth parameter. 52: * 53: * @throws OAuthInvalidRequestException If the request was malformed, 54: * contained a bad signature, or was 55: * otherwise invalid. 56: * @throws OAuthException If an unknown OAuth error occurred. 57: */ 58: public String getOAuthConsumerKey() throws OAuthInvalidRequestException, 59: OAuthException { 60: CheckOAuthSignatureRequest request = new CheckOAuthSignatureRequest(); 61: try { 62: byte[] responseBytes = ApiProxy.makeSyncCall("user", 63: "CheckOAuthSignature", request.toByteArray()); 64: CheckOAuthSignatureResponse response = new CheckOAuthSignatureResponse(); 65: response.mergeFrom(responseBytes); 66: return response.getOauthConsumerKey(); 67: } catch (ApiProxy.ApplicationException ex) { 68: UserServiceError.ErrorCode errorCode = 69: UserServiceError.ErrorCode.valueOf(ex.getApplicationError()); 70: switch (errorCode) { 71: case OAUTH_INVALID_REQUEST: 72: throw new OAuthInvalidRequestException(ex.getErrorDetail()); 73: case OAUTH_ERROR: 74: throw new OAuthException(ex.getErrorDetail()); 75: default: 76: throw new UserServiceFailureException(ex.getErrorDetail()); 77: } 78: } 79: } 80: }
oauth/javademo/war/WEB-INF/appengine-web.xml
1: <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> 2: <application>oauth-sp-demo-java</application> 3: <version>1</version> 4: </appengine-web-app>
oauth/javademo/war/WEB-INF/web.xml
1: <?xml version="1.0" encoding="ISO-8859-1"?> 2: <web-app 3: xmlns="http://java.sun.com/xml/ns/javaee" 4: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5: xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 6: version="2.5"> 7: <display-name>OAuth Service Provider - Java</display-name> 8: 9: <servlet> 10: <servlet-name>oauth2legged</servlet-name> 11: <servlet-class>com.google.appengine.demos.oauthsp.OAuth2LeggedServlet</servlet-class> 12: </servlet> 13: 14: <servlet-mapping> 15: <servlet-name>oauth2legged</servlet-name> 16: <url-pattern>/oauth2legged</url-pattern> 17: </servlet-mapping> 18: 19: </web-app>
oauth/javademo/build.xml
1: <project name="oauth-sp-demo-java" default="compile"> 2: <property name="project.dir" location="." /> 3: <property name="sdk.dir" location="../appengine-java-sdk" /> 4: 5: <path id="project.classpath"> 6: <pathelement path="${project.dir}/war/WEB-INF/classes" /> 7: <fileset dir="${project.dir}/war/WEB-INF/lib"> 8: <include name="**/*.jar" /> 9: </fileset> 10: <fileset dir="${sdk.dir}/lib"> 11: <include name="shared/**/*.jar" /> 12: </fileset> 13: </path> 14: 15: <target name="copyjars"> 16: <copy 17: todir="${project.dir}/war/WEB-INF/lib"> 18: <fileset dir="${sdk.dir}/lib/user"> 19: <include name="*.jar" /> 20: </fileset> 21: </copy> 22: </target> 23: 24: <target name="compile" depends="copyjars"> 25: <javac 26: srcdir="${project.dir}/src" 27: destdir="${project.dir}/war/WEB-INF/classes" 28: classpathref="project.classpath" 29: debug="on" /> 30: </target> 31: 32: </project>