1. Joeri Sykora
  2. easy-oauth-dalicore

Commits

Joeri Sykora  committed 2201f07 Draft

remove jersey dependencies and correctly handle callback

  • Participants
  • Parent commits c5513f7
  • Branches jsf

Comments (0)

Files changed (7)

File oauthconsumer/pom.xml

View file
  • Ignore whitespace
 			<version>1.0.0</version>
 		</dependency>
 		<dependency>
-			<groupId>com.sun.jersey</groupId>
-			<artifactId>jersey-server</artifactId>
-			<version>1.11.1</version>
-			<scope>provided</scope>
-		</dependency>
-		<dependency>
-			<groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
-			<artifactId>oauth-signature</artifactId>
-			<version>1.11.1</version>
-		</dependency>
-		<dependency>
 			<groupId>javax</groupId>
 			<artifactId>javaee-web-api</artifactId>
 			<version>6.0</version>

File oauthconsumer/src/main/java/com/mycompany/oauthconsumer/HomeHandler.java

  • Ignore whitespace
-package com.mycompany.oauthconsumer;
-
-import com.lodgon.dali.core.social.DaliCoreExternalNetwork;
-import com.lodgon.dali.core.social.entity.ExternalToken;
-import com.sun.jersey.api.view.Viewable;
-import java.net.URI;
-import java.net.URISyntaxException;
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.Response;
-
-@Path("home")
-public class HomeHandler {
-	private static final String PROVIDER_URL = "http://localhost:8080/oauthprovider";
-	private static final String CONSUMER_KEY = "72d0d4cf36a16b8e";
-	private static final String CONSUMER_SECRET = "2c0625282b3268a8";
-	private static final String CONSUMER_CALLBACK = "http://localhost:8080/oauthconsumer/rest/home/callback";
-
-	@Context
-	HttpServletRequest request;
-
-	@GET
-	public Response home() throws URISyntaxException {
-		ExternalToken accessToken = (ExternalToken) request.getSession().getAttribute("accessToken");
-		if (accessToken == null) {
-			DaliCoreExternalNetwork externalNetwork = new DaliCoreExternalNetwork(PROVIDER_URL,
-							CONSUMER_KEY, CONSUMER_SECRET);
-			return externalNetwork.connect(CONSUMER_CALLBACK);
-		} else {
-			return Response.ok(new Viewable("/home.jsp", accessToken)).build();
-		}
-	}
-
-	@GET
-	@Path("callback")
-	public Response callback(@QueryParam("oauth_token") String requestToken,
-					@QueryParam("oauth_verifier") String verifier) throws URISyntaxException {
-		DaliCoreExternalNetwork externalNetwork = new DaliCoreExternalNetwork(PROVIDER_URL,
-						CONSUMER_KEY, CONSUMER_SECRET);
-		ExternalToken accessToken = externalNetwork.callback(requestToken, verifier);
-		request.getSession().setAttribute("accessToken", accessToken);
-		return Response.seeOther(new URI("/home")).build();
-	}
-}

File oauthconsumer/src/main/java/com/mycompany/oauthconsumer/view/Callback.java

View file
  • Ignore whitespace
 package com.mycompany.oauthconsumer.view;
 
 import com.lodgon.dali.core.social.DaliCoreExternalNetwork;
-import javax.annotation.PostConstruct;
+import java.io.IOException;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.ManagedProperty;
 import javax.faces.bean.RequestScoped;
+import javax.faces.context.FacesContext;
 
 /**
  *
 @ManagedBean
 @RequestScoped
 public class Callback {
-	private static final String PROVIDER_URL = "http://localhost:8080/oauthprovider";
-	private static final String CONSUMER_KEY = "72d0d4cf36a16b8e";
-	private static final String CONSUMER_SECRET = "2c0625282b3268a8";
-
 	@ManagedProperty(value = "#{param.oauth_token}")
 	private String requestToken;
 	@ManagedProperty(value = "#{param.verifier}")
 	private String verifier;
 
-
 	public String getRequestToken() {
 		return requestToken;
 	}
 		this.home = home;
 	}
 
-	public String home() {
-		return "/pages/home";
-	}
+	public void handle() throws IOException {
+		DaliCoreExternalNetwork externalNetwork = new DaliCoreExternalNetwork(
+						Constants.PROVIDER_URL, Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
+		home.setAccessToken(externalNetwork.callback(requestToken, verifier));
 
-	@PostConstruct
-	public void postConstruct() {
-		DaliCoreExternalNetwork externalNetwork = new DaliCoreExternalNetwork(PROVIDER_URL,
-						CONSUMER_KEY, CONSUMER_SECRET);
-		home.setAccessToken(externalNetwork.callback(requestToken, verifier));
+		FacesContext.getCurrentInstance().getExternalContext().redirect("home.faces");
 	}
 }

File oauthconsumer/src/main/java/com/mycompany/oauthconsumer/view/Constants.java

View file
  • Ignore whitespace
+package com.mycompany.oauthconsumer.view;
+
+/**
+ *
+ * @author joeri
+ */
+public class Constants {
+	public static final String PROVIDER_URL = "http://localhost:8080/oauthprovider";
+	public static final String CONSUMER_KEY = "72d0d4cf36a16b8e";
+	public static final String CONSUMER_SECRET = "2c0625282b3268a8";
+}

File oauthconsumer/src/main/java/com/mycompany/oauthconsumer/view/Home.java

View file
  • Ignore whitespace
 import com.lodgon.dali.core.social.DaliCoreExternalNetwork;
 import com.lodgon.dali.core.social.entity.ExternalToken;
 import java.io.IOException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.SessionScoped;
 import javax.faces.context.FacesContext;
-import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
 
 /**
 @ManagedBean
 @SessionScoped
 public class Home {
-	private static final String PROVIDER_URL = "http://localhost:8080/oauthprovider";
-	private static final String CONSUMER_KEY = "72d0d4cf36a16b8e";
-	private static final String CONSUMER_SECRET = "2c0625282b3268a8";
 	private static final String CONSUMER_CALLBACK = "http://localhost:8080/oauthconsumer/pages/callback.faces";
 
 	private ExternalToken accessToken;
 		this.accessToken = accessToken;
 	}
 
-	public String authorize() throws IOException {
-		if (accessToken != null) {
-			return "/pages/home";
-		}
+	public void authorize() throws IOException {
+		DaliCoreExternalNetwork externalNetwork = new DaliCoreExternalNetwork(
+						Constants.PROVIDER_URL, Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
 
-		DaliCoreExternalNetwork externalNetwork = new DaliCoreExternalNetwork(PROVIDER_URL,
-							CONSUMER_KEY, CONSUMER_SECRET);
+		Response response = externalNetwork.connect(CONSUMER_CALLBACK);
+		String location = response.getMetadata().getFirst("Location").toString();
 
-		try {
-			Response response = externalNetwork.connect(CONSUMER_CALLBACK);
-			String location = response.getMetadata().getFirst("Location").toString();
-
-			FacesContext.getCurrentInstance().getExternalContext().redirect(location);
-		} catch (WebApplicationException ex) {
-			Logger.getLogger(Home.class.getName()).log(Level.SEVERE, "Connect failed: " + ex.getResponse().getStatus(), ex);
-		}
-
-		return "/pages";
+		FacesContext.getCurrentInstance().getExternalContext().redirect(location);
 	}
 }

File oauthconsumer/src/main/webapp/pages/callback.xhtml

View file
  • Ignore whitespace
 	</h:head>
 	<h:body>
 		<f:metadata>
-			<f:event type="preRenderView" listener="#{callback.home()}"/>
+			<f:viewParam id="oauthToken" name="oauth_token" value="#{callback.requestToken}"/>
+			<f:viewParam id="oauthVerifier" name="oauth_verifier" value="#{callback.verifier}"/>
+			<f:event type="preRenderView" listener="#{callback.handle()}"/>
 		</f:metadata>
 	</h:body>
 </html>

File oauthconsumer/src/main/webapp/pages/home.xhtml

View file
  • Ignore whitespace
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
 			xmlns:c="http://java.sun.com/jsp/jstl/core"
-			xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html">
 	<h:head>
 		<title>dalicore-oauth service provider</title>
 	</h:head>
 	<h:body>
 		<c:if test="${empty home.accessToken}">
-			<f:metadata>
-				<f:event type="preRenderView" listener="#{home.authorize()}"/>
-			</f:metadata>
+			<h1>Not connected!</h1>
+
+			<h:form>
+				<p>You are currently not connected. <h:commandLink value="Connect now!" action="#{home.authorize}"/></p>
+			</h:form>
 		</c:if>
 		<c:if test="${not empty home.accessToken}">
 			<h1>Connected!</h1>
 
-			<p>You successfully connected with the external network ${home.accessToken.network}.</p>
+			<p>You successfully connected with the external network <b><h:outputText value="#{home.accessToken.network}"/></b>.</p>
 
-			Access Token key: ${home.accessToken.token}<br/>
-			Access Token secret: ${home.accessToken.secret}
+			Access Token key: <h:outputText value="#{home.accessToken.token}"/><br/>
+			Access Token secret: <h:outputText value="#{home.accessToken.secret}"/>
 		</c:if>
 	</h:body>
 </html>