You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2010/07/28 18:44:59 UTC

svn commit: r980120 - /incubator/clerezza/issues/CLEREZZA-243/org.apache.clerezza.platform.accountcontrolpanel/org.apache.clerezza.platform.accountcontrolpanel.core/src/main/java/org/apache/clerezza/platform/accountcontrolpanel/ProfilePanel.java

Author: reto
Date: Wed Jul 28 16:44:59 2010
New Revision: 980120

URL: http://svn.apache.org/viewvc?rev=980120&view=rev
Log:
CLEREZZA-243: adding key information to profile

Modified:
    incubator/clerezza/issues/CLEREZZA-243/org.apache.clerezza.platform.accountcontrolpanel/org.apache.clerezza.platform.accountcontrolpanel.core/src/main/java/org/apache/clerezza/platform/accountcontrolpanel/ProfilePanel.java

Modified: incubator/clerezza/issues/CLEREZZA-243/org.apache.clerezza.platform.accountcontrolpanel/org.apache.clerezza.platform.accountcontrolpanel.core/src/main/java/org/apache/clerezza/platform/accountcontrolpanel/ProfilePanel.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-243/org.apache.clerezza.platform.accountcontrolpanel/org.apache.clerezza.platform.accountcontrolpanel.core/src/main/java/org/apache/clerezza/platform/accountcontrolpanel/ProfilePanel.java?rev=980120&r1=980119&r2=980120&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-243/org.apache.clerezza.platform.accountcontrolpanel/org.apache.clerezza.platform.accountcontrolpanel.core/src/main/java/org/apache/clerezza/platform/accountcontrolpanel/ProfilePanel.java (original)
+++ incubator/clerezza/issues/CLEREZZA-243/org.apache.clerezza.platform.accountcontrolpanel/org.apache.clerezza.platform.accountcontrolpanel.core/src/main/java/org/apache/clerezza/platform/accountcontrolpanel/ProfilePanel.java Wed Jul 28 16:44:59 2010
@@ -18,10 +18,12 @@
  */
 package org.apache.clerezza.platform.accountcontrolpanel;
 
-
+import java.math.BigInteger;
 import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.interfaces.RSAPublicKey;
+import java.util.logging.Level;
 import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
@@ -31,7 +33,9 @@ import javax.ws.rs.core.UriInfo;
 
 import net.bblfish.dev.foafssl.keygen.CertSerialisation;
 import net.bblfish.dev.foafssl.keygen.Certificate;
+import net.bblfish.dev.foafssl.keygen.RSAPubKey;
 import net.bblfish.dev.foafssl.keygen.impl.StaticKeygenService;
+import org.apache.clerezza.foafssl.ontologies.RSA;
 import org.apache.clerezza.jaxrs.utils.RedirectUtil;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
@@ -66,6 +70,7 @@ import org.apache.clerezza.web.fileserve
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import scala.math.BigInt;
 
 /**
  *
@@ -78,27 +83,21 @@ import org.slf4j.LoggerFactory;
 @Property(name = "javax.ws.rs", boolValue = true)
 @Path("/user/{id}/profile")
 public class ProfilePanel extends FileServer {
-	
+
 	private static final Logger logger = LoggerFactory.getLogger(ProfilePanel.class);
-	
 	@Reference
 	private UserManager userManager;
-
-    @Reference
-    private net.bblfish.dev.foafssl.keygen.KeygenService keygenSrvc;
-	
+	@Reference
+	private net.bblfish.dev.foafssl.keygen.KeygenService keygenSrvc;
 	@Reference
 	private TcManager tcManager;
-	
 	@Reference
 	private RenderletManager renderletManager;
-	
 	@Reference
 	private WebIdGraphsService webIdGraphsService;
-	
 	@Reference
 	private PlatformConfig platformConfig;
-	
+
 	protected void activate(ComponentContext componentContext) {
 		URL templateURL = getClass().getResource("profile-panel.ssp");
 		renderletManager.registerRenderlet(ScalaServerPagesRenderlet.class.getName(),
@@ -106,68 +105,70 @@ public class ProfilePanel extends FileSe
 				"naked", MediaType.APPLICATION_XHTML_XML_TYPE, true);
 		configure(componentContext.getBundleContext(), "profile-staticweb");
 	}
-	
+
 	@GET
 	public GraphNode getPersonalProfilePage(@Context UriInfo uriInfo, @PathParam(value = "id") String userName) {
 		TrailingSlash.enforceNotPresent(uriInfo);
-		GraphNode resultNode = getPersonalProfile(userName);
+		GraphNode resultNode = getPersonalProfile(userName, new UriRef(uriInfo.getAbsolutePath().toString()));
 		resultNode.addProperty(RDF.type, PLATFORM.HeadedPage);
 		resultNode.addProperty(RDF.type, CONTROLPANEL.ProfilePage);
 		return resultNode;
 	}
-	
-	private	GraphNode getPersonalProfile(final String userName) {
+
+	private GraphNode getPersonalProfile(final String userName, final UriRef profile) {
 		return AccessController.doPrivileged(new PrivilegedAction<GraphNode>() {
+
 			@Override
 			public GraphNode run() {
 				GraphNode userInSystemGraph = userManager.getUserInSystemGraph(userName);
-				NonLiteral userNodeInSystemGraph = (NonLiteral)userInSystemGraph.getNode();
+				NonLiteral userNodeInSystemGraph = (NonLiteral) userInSystemGraph.getNode();
 				if (userNodeInSystemGraph instanceof BNode) {
 					//no personal profile without web-id
 					SimpleMGraph simpleMGraph = new SimpleMGraph();
 					GraphNode profileNode = new GraphNode(new BNode(), simpleMGraph);
-					profileNode.addProperty(CONTROLPANEL.isLocalProfile, 
+					profileNode.addProperty(CONTROLPANEL.isLocalProfile,
 							LiteralFactory.getInstance().createTypedLiteral(true));
 					UriRef suggestedPPDUri = getSuggestedPPDUri(userName);
-					profileNode.addProperty(CONTROLPANEL.suggestedPPDUri, 
+					profileNode.addProperty(CONTROLPANEL.suggestedPPDUri,
 							LiteralFactory.getInstance().createTypedLiteral(suggestedPPDUri));
 					NonLiteral agent = new BNode();
 					profileNode.addProperty(FOAF.primaryTopic, agent);
-					simpleMGraph.add(new TripleImpl(agent, PLATFORM.userName, 
+					simpleMGraph.add(new TripleImpl(agent, PLATFORM.userName,
 							LiteralFactory.getInstance().createTypedLiteral(userName)));
 					return profileNode;
 				} else {
-					return getProfileInUserGraph((UriRef)userNodeInSystemGraph);
+					return getProfileInUserGraph((UriRef) userNodeInSystemGraph, profile);
 				}
 			}
 		});
 	}
 
 	private UriRef getSuggestedPPDUri(String userName) {
-		return new UriRef(platformConfig.getDefaultBaseUri().getUnicodeString()+
-							"user/"+userName+"/profile");
+		return new UriRef(platformConfig.getDefaultBaseUri().getUnicodeString()
+				+ "user/" + userName + "/profile");
 	}
-	
-	private GraphNode getProfileInUserGraph(UriRef webId) {
+
+	private GraphNode getProfileInUserGraph(UriRef webId, UriRef profile) {
 		WebIdGraphsService.WebIdGraphs webIdGraphs = webIdGraphsService.getWebIdGraphs(webId);
 		MGraph userGraph = webIdGraphs.publicUserGraph();
 		logger.debug("got publicUserGraph of size {}.", userGraph.size());
 		GraphNode userGraphNode = new GraphNode(webId, userGraph);
-		GraphNode resultNode = new GraphNode(new BNode(), 
+		GraphNode resultNode = new GraphNode(profile,
 				new UnionMGraph(new SimpleMGraph(), userGraphNode.getGraph()));
-		resultNode.addProperty(CONTROLPANEL.isLocalProfile, 
+		resultNode.addProperty(CONTROLPANEL.isLocalProfile,
 				LiteralFactory.getInstance().createTypedLiteral(webIdGraphs.isLocal()));
 		resultNode.addProperty(FOAF.primaryTopic, userGraphNode.getNode());
 		return resultNode;
 	}
-	
+
 	@POST
 	@Path("set-existing-webid")
-	public Response setExistingWebId(@Context final UriInfo uriInfo, 
+	public Response setExistingWebId(@Context final UriInfo uriInfo,
 			@FormParam("webid") final UriRef webId, @PathParam(value = "id") final String userName) {
 		//TODO check that its not local
 		//TODO check its not an existing user
 		return AccessController.doPrivileged(new PrivilegedAction<Response>() {
+
 			@Override
 			public Response run() {
 				GraphNode userInSystemGraph = userManager.getUserInSystemGraph(userName);
@@ -183,11 +184,12 @@ public class ProfilePanel extends FileSe
 			@PathParam(value = "id") final String userName) {
 		//TODO check its not an existing user
 		final UriRef ppd = getSuggestedPPDUri(userName);
-		final UriRef webId = new UriRef(ppd.getUnicodeString()+"#me");
+		final UriRef webId = new UriRef(ppd.getUnicodeString() + "#me");
 		final WebIdGraphsService.WebIdGraphs webIdGraphs = webIdGraphsService.getWebIdGraphs(webId);
 		webIdGraphs.localGraph().add(new TripleImpl(ppd, FOAF.primaryTopic, webId));
 		webIdGraphs.localGraph().add(new TripleImpl(ppd, RDF.type, FOAF.PersonalProfileDocument));
 		return AccessController.doPrivileged(new PrivilegedAction<Response>() {
+
 			@Override
 			public Response run() {
 				GraphNode userInSystemGraph = userManager.getUserInSystemGraph(userName);
@@ -197,41 +199,51 @@ public class ProfilePanel extends FileSe
 		});
 	}
 
-    @POST
-    @Path("keygen")
-    public Response createCert(@FormParam("webId") String webId,
-                          @FormParam("cn") String commonName,
-                          @FormParam("spkac") String spkac,
-                          @FormParam("hours") String hours,
-                          @FormParam("days") String days) {
-        logger.info("in keygen code. webId="+webId);
-        logger.info("cn="+commonName);
-        logger.info("hours="+hours);
-        logger.info("days="+days);
-        logger.info("spkac="+spkac);
-        Certificate cert = null;
-        if (spkac != null && spkac.length() > 0) {
-           cert = keygenSrvc.createFromSpkac(spkac);
-           if (cert == null) logger.warn("unable to create keygen from spkac request");
-        }
-        if (cert == null) {
-            return null;
-        }
-        cert.setSubjectCommonName(commonName);
-        cert.addDurationInHours(hours);
-        cert.addDurationInDays(days);
-        cert.setSubjectWebID(webId);
-
-        try {
-            CertSerialisation ser = cert.getSerialisation();
-            Response.ResponseBuilder resBuild = Response.ok(ser.getContent(),MediaType.valueOf(ser.getMimeType()));
-            return resBuild.build();
-        } catch (Exception e) {
-            logger.warn("problem creating cert for webid="+webId,e);
-            return null;
-        }
-    }
+	@POST
+	@Path("keygen")
+	public Response createCert(@FormParam("webId") UriRef webId,
+			@FormParam("cn") String commonName,
+			@FormParam("spkac") String spkac,
+			@FormParam("hours") String hours,
+			@FormParam("days") String days) {
+		logger.info("in keygen code. webId={}", webId);
+		logger.info("cn={}", commonName);
+		logger.info("hours={}", hours);
+		logger.info("days={}", days);
+		logger.info("spkac={}", spkac);
+		Certificate cert = null;
+		if (spkac != null && spkac.length() > 0) {
+			cert = keygenSrvc.createFromSpkac(spkac);
+			if (cert == null) {
+				logger.warn("unable to create keygen from spkac request");
+			}
+		}
+		if (cert == null) {
+			throw new RuntimeException("The server was unable to craete a certificate");
+		}
+		cert.setSubjectCommonName(commonName);
+		cert.addDurationInHours(hours);
+		cert.addDurationInDays(days);
+		cert.setSubjectWebID(webId.getUnicodeString());
+
+		CertSerialisation ser;
+		try {
+			ser = cert.getSerialisation();
+		} catch (Exception ex) {
+			throw new RuntimeException(ex);
+		}
+		RSAPublicKey pubKey = (RSAPublicKey)cert.getSubjectPublicKey().getPublicKey();
+		BigInteger publicExponent = pubKey.getPublicExponent();
+		BigInteger modulus = pubKey.getModulus();
+		final WebIdGraphsService.WebIdGraphs webIdGraphs = webIdGraphsService.getWebIdGraphs(webId);
+		final GraphNode agent = new GraphNode(webId, webIdGraphs.localGraph());
+		agent.addPropertyValue(RSA.modulus, modulus);
+		agent.addPropertyValue(RSA.public_exponent, publicExponent);
 
+		Response.ResponseBuilder resBuild = Response.ok(ser.getContent(), MediaType.valueOf(ser.getMimeType()));
+		return resBuild.build();
+
+	}
 
 	@POST
 	@Path("modify")
@@ -246,9 +258,8 @@ public class ProfilePanel extends FileSe
 		agent.addPropertyValue(FOAF.name, name);
 		agent.deleteProperties(DC.description);
 		agent.addPropertyValue(DC.description, description);
-		logger.info("desc:"+description);
+		logger.info("desc:" + description);
 		logger.info("local graph (uri: {}) is now of size {}", webIdGraphs.localGraphUri(), webIdGraphs.localGraph().size());
 		return RedirectUtil.createSeeOtherResponse("../profile", uriInfo);
 	}
-	
 }