changeset 2:6888ae3287b8

changed to namespace handling for users
author dwinter
date Thu, 24 Nov 2011 11:39:12 +0100
parents f2f41d0dedf5
children e5f0906c107c
files .project WebContent/WEB-INF/web.xml.template src/de/mpiwg/itgroup/annotationManager/Constants/NS.java src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java src/de/mpiwg/itgroup/annotationManager/restlet/RestServer.java
diffstat 7 files changed, 190 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/.project	Wed Nov 23 15:26:33 2011 +0100
+++ b/.project	Thu Nov 24 11:39:12 2011 +0100
@@ -36,4 +36,11 @@
 		<nature>org.eclipse.jdt.core.javanature</nature>
 		<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
 	</natures>
+	<linkedResources>
+		<link>
+			<name>docs/annotations.owl</name>
+			<type>1</type>
+			<locationURI>PARENT-1-PROJECT_LOC/NamedIdentityManager/ontologies/annotations.owl</locationURI>
+		</link>
+	</linkedResources>
 </projectDescription>
--- a/WebContent/WEB-INF/web.xml.template	Wed Nov 23 15:26:33 2011 +0100
+++ b/WebContent/WEB-INF/web.xml.template	Thu Nov 24 11:39:12 2011 +0100
@@ -11,11 +11,16 @@
 <param-value>de.mpiwg.itgroup.annotationManager.restlet.RestServer</param-value>
 </context-param>
 
+<context-param>
+<param-name>de.mpiwg.itgroup.annotationManager.jaas.configFilePath</param-name>
+<param-value>file:///etc/jaasAuth.conf</param-value>
+</context-param>
 
 <context-param>
 <param-name>de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser</param-name>
 <param-value>USERNAME</param-value>
 </context-param>
+
 <context-param>
 <param-name>de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword</param-name>
 <param-value>PASSWORD</param-value>
--- a/src/de/mpiwg/itgroup/annotationManager/Constants/NS.java	Wed Nov 23 15:26:33 2011 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/Constants/NS.java	Thu Nov 24 11:39:12 2011 +0100
@@ -1,6 +1,7 @@
 package de.mpiwg.itgroup.annotationManager.Constants;
 
 public class NS {
+	public static final String MPIWG_PERSONS = "http://www.mpiwg-berlin.mpg.de/en/staff/members/";
 	public static String ANNOTATION_TYPE="http://www.w3.org/2000/10/annotationType#";
 	public static String ANNOTATION_NS="http://www.w3.org/2000/10/annotation-ns#";
 	public static String RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
--- a/src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java	Wed Nov 23 15:26:33 2011 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java	Thu Nov 24 11:39:12 2011 +0100
@@ -48,7 +48,27 @@
 		public String type=null;
 		public String url;
 		public List<String> xpointers=null; // list of xpointers on the page url, can be empty or null if there is only one.
+		public String annotationUri=null; // uri der annotation im triplestore
 		
+		
+		/**
+		 * @param xpointer Beschreibt die Annotation
+		 * @param creator Username des Creators oder URI der Creators
+		 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
+		 * @param text der Annotation
+		 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
+		 * @param url Url einer Annotation
+		 * @param annotationUri Uri der Annotation im Triplestore
+		 */
+		public Annotation(String xpointer, String creator, String time, String text, String type, String url,String annotationUri){
+			this.xpointer=xpointer;
+			this.creator=creator;
+			this.time=time;
+			this.text=text;
+			this.type=type;
+			this.url=url;
+			this.annotationUri=annotationUri;
+		}
 		/**
 		 * @param xpointer Beschreibt die Annotation
 		 * @param creator Username des Creators
@@ -120,7 +140,13 @@
 		retQuad.add(new Quadruple(annotation, NS.RDF+"type", NS.ANNOTATION_TYPE+annot.type, ctx));
 		
 		//add author
-		retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx));
+		
+		
+		if (annot.creator.startsWith("http")){
+		retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx));
+		} else {
+			retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx));
+		}
 		
 		// creation time
 		retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"created", annot.time, ctx));
@@ -133,7 +159,7 @@
 		}
 		
 		// now add the xpointers 
-		retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"docuviewerText", xpointerSplitted[0], ctx));
+		retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"annotatesDocuviewerText", xpointerSplitted[0], ctx));
 		retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"textSelection", annot.xpointer, ctx));
 		
 		String annotationtext =createRessourceURL("annotText:");
--- a/src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java	Wed Nov 23 15:26:33 2011 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java	Thu Nov 24 11:39:12 2011 +0100
@@ -10,6 +10,8 @@
 import org.restlet.Context;
 import org.restlet.engine.component.ChildContext;
 
+import sun.security.action.GetBooleanAction;
+
 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
@@ -31,6 +33,15 @@
 
 
 
+	/** Sucht im Triplestore nach Annotationen
+	 * @param uri Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource oder NULL oder leer
+	 * @param user Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt wurden.
+	 * @param limit
+	 * @param offset
+	 * @return
+	 * @throws TripleStoreHandlerException
+	 * @throws TripleStoreSearchError
+	 */
 	public List<Annotation> search(String uri, String user, String limit,
 			String offset) throws TripleStoreHandlerException, TripleStoreSearchError {
 
@@ -46,15 +57,19 @@
 		//whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
 
 		if(uri!=null && !uri.equals("")){
-			whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> <%s>.",uri);}
+			whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.",uri);}
 		else {
-			whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?uri.");
+			whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.");
 		}	
 		whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
 		whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
 
 		if(user!=null && !user.equals("")){
-			whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".",user);
+			if (user.startsWith("http")){
+				whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.",user);	
+			} else {
+				whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".",user);
+			}
 		} else {
 			whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
 		}
@@ -91,7 +106,7 @@
 				Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), 
 						annotUser, result.getBinding("created").getValue().stringValue(), 
 						result.getBinding("text").getValue().stringValue(), null,
-						annotUri);
+						annotUri,result.getBinding("s").getValue().stringValue());
 				retAnnots.add(annot);
 			}
 		} catch (Exception e) {
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java	Wed Nov 23 15:26:33 2011 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java	Thu Nov 24 11:39:12 2011 +0100
@@ -28,6 +28,7 @@
 import org.restlet.resource.ServerResource;
 import org.restlet.security.User;
 
+import de.mpiwg.itgroup.annotationManager.Constants.NS;
 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
@@ -65,8 +66,67 @@
 		responseHeaders.add("Access-Control-Max-Age", "60");
 	}
 	
+	@Get("html")
+	public Representation doGetHTML(Representation entity){
+		
+		doOptions(entity);
+		Form form = getRequest().getResourceRef().getQueryAsForm();
+		String uri = form.getFirstValue("uri");
+		String user = form.getFirstValue("user");
+
+		String limit=form.getFirstValue("limit");
+		String offset=form.getFirstValue("offset");
+
+		
+//		
+		RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
+
+		String retString="<html><body><table>";
+		String lineFormat="<tr><td>%s</td>" +
+				"<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
+		try {
+			
+			List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+
+			for (Convert.Annotation annot:annots){
+				
+				
+				RestServer restServer = (RestServer) getApplication();
+				String userName=restServer.getUserNameFromLdap(annot.creator);
+				List<String> xpointer = new ArrayList<String>();
+
+				if (annot.xpointers==null || annot.xpointers.size()==0)
+					retString+=String.format(lineFormat, userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri);
+				else {
+					for(String xpointerString:annot.xpointers){
+						retString+=String.format(lineFormat, userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri);
+								
+					}
+				}
+			
+			}
+		} catch (TripleStoreHandlerException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
+			return null;
+		} catch (TripleStoreSearchError e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
+			return null;
+		} 
+
+		retString+="</table></body></html>";
+		
+		logger.debug("sending:");
+		logger.debug(retString);
+		return new StringRepresentation(retString,MediaType.TEXT_HTML);
+	}
+
+	
 	@Get("json")
-	public Representation doGetHTML(Representation entity){
+	public Representation doGetJSON(Representation entity){
 		
 		doOptions(entity);
 		Form form = getRequest().getResourceRef().getQueryAsForm();
@@ -95,7 +155,12 @@
 				userObject.put("id",annot.creator);
 				
 				RestServer restServer = (RestServer) getApplication();
-				String userName=restServer.getUserNameFromLdap(annot.creator);
+				
+				String userID= annot.creator;
+				if (userID.startsWith(NS.MPIWG_PERSONS)){
+					userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
+				}
+				String userName=restServer.getUserNameFromLdap(userID);
 				userObject.put("name",userName);
 				
 				jo.put("user",userObject);
@@ -302,6 +367,19 @@
 		}
 	}
 
+	
+	/**
+	 * 
+	 * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
+	 * 
+	 * username,password is optional, if not given BasicAuthentification is used.
+	 * 
+	 * 
+	 * 
+	 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
+	 * 
+	 * @return
+	 */
 	protected Convert.Annotation handleForm(Representation entity) {
 		Convert.Annotation annot;
 		Form form = new Form(entity);
@@ -338,6 +416,10 @@
 			username = authUser.getIdentifier();
 		}
 
+		//username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
+		if (!username.startsWith("http"))
+			username=NS.MPIWG_PERSONS+username;
+		
 		annot = new Convert.Annotation(xpointer, username, null, text,
 				type, url);
 		return annot;
@@ -370,6 +452,8 @@
 	 *            be used.
 	 * @param authUser
 	 *            user object
+	 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
+	
 	 * @return
 	 * @throws JSONException
 	 */
@@ -396,6 +480,10 @@
 		if (username == null)
 			username = authUser.getIdentifier();
 
+		//username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
+		if (!username.startsWith("http"))
+			username=NS.MPIWG_PERSONS+username;
+		
 		return new Convert.Annotation(xpointer, username, null, text, type, url);
 	}
 
@@ -410,7 +498,7 @@
 	 *                                                               basic
 	 *                                                               authentification
 	 *                                                               is used.
-	 * 
+	 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
 	 * @param jo
 	 * @param authUser
 	 * @return
@@ -476,6 +564,11 @@
 		} else {
 			xpointer = url;
 		}
+		
+		//username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
+		if (!username.startsWith("http"))
+			username=NS.MPIWG_PERSONS+username;
+		
 		return new Convert.Annotation(xpointer, username, null, text, null);
 	}
 
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/RestServer.java	Wed Nov 23 15:26:33 2011 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/RestServer.java	Thu Nov 24 11:39:12 2011 +0100
@@ -32,6 +32,7 @@
 
 import org.restlet.data.ChallengeScheme;
 import org.restlet.data.ClientInfo;
+import org.restlet.engine.component.ChildContext;
 import org.restlet.ext.jaas.JaasVerifier;
 import org.restlet.routing.Router;
 import org.restlet.routing.Template;
@@ -62,10 +63,6 @@
         ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC;
         String realm = "Annotation Service";
 
-        // MapVerifier isn't very secure; see docs for alternatives
-        //MapVerifier verifier = new MapVerifier();
-        //verifier.getLocalSecrets().put("user", "password".toCharArray());
-
         JaasVerifier verifier = new JaasVerifier("BasicJaasAuthenticationApplication");
         
         
@@ -90,11 +87,21 @@
         return auth;
     }
 
+	/**
+	 * Konfiguration fŸr den Authentificator in Jaas. Pfad zum JAAS-Konfigurationsfile liegt im Context-Parameter 
+	 * "de.mpiwg.itgroup.annotationManager.jaas.configFilePath".
+	 * @return
+	 */
 	protected Configuration createConfiguration() {
 		Configuration jaasConfig;
 		URI confUri;
+	
+		Context context = getContext();
+		String configFilePath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.jaas.configFilePath");
+		
+		
 		try {
-			confUri = new URI("file:///etc/jaasAuth.conf"); //TODO shoould be configurable
+			confUri = new URI(configFilePath);
 		} catch (URISyntaxException e) {
 			e.printStackTrace();
 			confUri = null;
@@ -131,7 +138,13 @@
 		
 	}
 	
-	 public boolean authenticate(Request request, Response response) {
+	 /**
+	  * Authentifiziere den Benutzer aus dem Request (BasicAuthenfication)
+	 * @param request
+	 * @param response
+	 * @return
+	 */
+	public boolean authenticate(Request request, Response response) {
 	        if (!request.getClientInfo().isAuthenticated()) {
 	            authenticator.challenge(response, false);
 	            return false;
@@ -142,9 +155,19 @@
 	        	authenticator.challenge(response, false);
 	            return false;
 	        }
+	        
+	        
 	        return true;
 	    }
 
+	/**
+	 * Authentifiziere den Benutzer 
+	 * 
+	 * @param username
+	 * @param password
+	 * @param request
+	 * @return
+	 */
 	public boolean authenticate(String username, String password,Request request) {
 		LoginContext lc;
 		
@@ -169,6 +192,11 @@
 		return true;
 	}
 
+	/**
+	 * Hole den vollen Benutzernamen aus dem LDAP
+	 * @param creator
+	 * @return
+	 */
 	public String getUserNameFromLdap(String creator) {
 		String retString=creator; // falls nichts gefunden wird einfach den creator zurueckgeben
 		Hashtable<String,String> env = new Hashtable<String,String>();