Mercurial > hg > AnnotationManager
changeset 3:e5f0906c107c
Neue Startseite
Header bei Post werden jetzt richtig gesetzt.
author | dwinter |
---|---|
date | Fri, 25 Nov 2011 08:43:25 +0100 |
parents | 6888ae3287b8 |
children | c32080f364c6 |
files | src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorInfo.java src/de/mpiwg/itgroup/annotationManager/restlet/RestServer.java src/de/mpiwg/itgroup/annotationManager/staticPages/main.html |
diffstat | 5 files changed, 181 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java Thu Nov 24 11:39:12 2011 +0100 +++ b/src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java Fri Nov 25 08:43:25 2011 +0100 @@ -216,7 +216,7 @@ } - public String storeAnnotation(Convert.Annotation annot) throws TripleStoreStoreError { + public Annotation storeAnnotation(Convert.Annotation annot) throws TripleStoreStoreError { //Convert convert = new Convert("<file:///annotations>"); @@ -252,8 +252,8 @@ } - - return getAnnotUrl(); + annot.annotationUri=getAnnotUrl(); + return annot; }
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java Thu Nov 24 11:39:12 2011 +0100 +++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java Fri Nov 25 08:43:25 2011 +0100 @@ -40,14 +40,6 @@ private Logger logger = Logger.getRootLogger(); - /** - * - * json hash: username: name des users xpointer: xpointer auf den Ausschnitt - * (incl. der URL des Dokumentes) text: text der annotation annoturl: url - * auf eine Annotation falls extern - * - * @return - */ @Options @@ -82,7 +74,7 @@ RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file String retString="<html><body><table>"; - String lineFormat="<tr><td>%s</td>" + + String lineFormat="<tr><td><a href=\"%s\">%s</a></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 { @@ -96,11 +88,10 @@ 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); + retString+=String.format(lineFormat, userName,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); - + retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri); } } @@ -124,6 +115,49 @@ return new StringRepresentation(retString,MediaType.TEXT_HTML); } + /** + * Erzeugt aus einer Annotation, das fŸr den Annotator notwendige JSON-Format + * @param annot + * @return + */ + public JSONObject annot2AnnotatorJSON(Convert.Annotation annot){ + JSONObject jo = new JSONObject(); + try { + jo.put("text", annot.text); + jo.put("uri",annot.url); + + JSONObject userObject= new JSONObject(); + userObject.put("id",annot.creator); + + RestServer restServer = (RestServer) getApplication(); + + 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); + + List<String> xpointer = new ArrayList<String>(); + + if (annot.xpointers==null || annot.xpointers.size()==0) + xpointer.add(annot.xpointer); + else { + for(String xpointerString:annot.xpointers){ + xpointer.add(xpointerString); + } + } + jo.put("ranges", transformToRanges(xpointer)); + jo.put("id", annot.annotationUri); + return jo; + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + } @Get("json") public Representation doGetJSON(Representation entity){ @@ -147,35 +181,41 @@ ja = new JSONArray(); for (Convert.Annotation annot:annots){ - JSONObject jo = new JSONObject(); - jo.put("text", annot.text); - jo.put("uri",annot.url); - - JSONObject userObject= new JSONObject(); - userObject.put("id",annot.creator); - - RestServer restServer = (RestServer) getApplication(); - - String userID= annot.creator; - if (userID.startsWith(NS.MPIWG_PERSONS)){ - userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE +// JSONObject jo = new JSONObject(); +// jo.put("text", annot.text); +// jo.put("uri",annot.url); +// +// JSONObject userObject= new JSONObject(); +// userObject.put("id",annot.creator); +// +// RestServer restServer = (RestServer) getApplication(); +// +// 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); +// +// List<String> xpointer = new ArrayList<String>(); +// +// if (annot.xpointers==null || annot.xpointers.size()==0) +// xpointer.add(annot.xpointer); +// else { +// for(String xpointerString:annot.xpointers){ +// xpointer.add(xpointerString); +// } +// } +// jo.put("ranges", transformToRanges(xpointer)); + JSONObject jo = annot2AnnotatorJSON(annot); + if (jo!=null){ + ja.put(annot2AnnotatorJSON(annot)); + } else { + setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error"); + return null; } - String userName=restServer.getUserNameFromLdap(userID); - userObject.put("name",userName); - - jo.put("user",userObject); - - List<String> xpointer = new ArrayList<String>(); - - if (annot.xpointers==null || annot.xpointers.size()==0) - xpointer.add(annot.xpointer); - else { - for(String xpointerString:annot.xpointers){ - xpointer.add(xpointerString); - } - } - jo.put("ranges", transformToRanges(xpointer)); - ja.put(jo); } } catch (TripleStoreHandlerException e) { // TODO Auto-generated catch block @@ -187,12 +227,7 @@ e.printStackTrace(); setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error"); return null; - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error"); - return null; - } + } JSONObject retObject = new JSONObject(); try { @@ -262,24 +297,41 @@ } + + /** + * + * json hash: username: name des users xpointer: xpointer auf den Ausschnitt + * (incl. der URL des Dokumentes) text: text der annotation annoturl: url + * auf eine Annotation falls extern + * + * @return + */ + @Post("json") public Representation doPostJson(Representation entity) { - String retVal = doPost(entity); - JSONObject jo; - try { - jo = new JSONObject("{\"annotUrl\":\"" + retVal + "\"}"); - } catch (JSONException e) { - setStatus(Status.SERVER_ERROR_INTERNAL); + Annotation retVal = doPost(entity); + + +// JSONObject jo; +// try { +// jo = new JSONObject("{\"annotUrl\":\"" + retVal + "\"}"); +// } catch (JSONException e) { +// setStatus(Status.SERVER_ERROR_INTERNAL); +// return null; +// } + + if (retVal==null) return null; - } + JSONObject jo = annot2AnnotatorJSON(retVal); JsonRepresentation retRep = new JsonRepresentation(jo); return retRep; } @Post("html") public Representation doPostHtml(Representation entity) { - String retVal = doPost(entity); + Annotation retValAnnot = doPost(entity); + String retVal=retValAnnot.annotationUri; if (retVal == null) { return null; } @@ -292,7 +344,9 @@ return retRep; } - public String doPost(Representation entity) { + public Convert.Annotation doPost(Representation entity) { + + doOptions(entity); Convert.Annotation annot; // versuche basic authentifizierung und hole den Benutzer von dort. @@ -490,7 +544,7 @@ /** * uses the specification from the annotator project. * - * @see{https://github.com/okfn/annotator/wiki/Annotation-format The user + * @see{https://github.com/okfn/annotator/wiki/Annotation-format} The user * object must * contain an * id and
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorInfo.java Fri Nov 25 08:43:25 2011 +0100 @@ -0,0 +1,54 @@ +package de.mpiwg.itgroup.annotationManager.restlet; + +import java.io.InputStream; + +import org.apache.log4j.Logger; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.representation.InputRepresentation; +import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; +import org.restlet.resource.Get; +import org.restlet.resource.Options; +import org.restlet.resource.Post; +import org.restlet.resource.ServerResource; + + + + + +public class AnnotatorInfo extends ServerResource { + +private Logger logger = Logger.getRootLogger(); + + /** + * Erlaubt cross scripting bei Aufruf aus Javascript + * @param entity + */ + @Options + public void doOptions(Representation entity) { + Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers"); + if (responseHeaders == null) { + responseHeaders = new Form(); + getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); + } + responseHeaders.add("Access-Control-Allow-Origin", "*"); + responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET"); + responseHeaders.add("Access-Control-Allow-Headers", "Content-Type"); + responseHeaders.add("Access-Control-Allow-Credentials", "false"); + responseHeaders.add("Access-Control-Max-Age", "60"); + } + + + + @Get("html") + public Representation getHTML(){ + InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/annotationManager/staticPages/main.html"); + + Representation rep = new InputRepresentation(is,MediaType.TEXT_HTML); + return rep; + + } + +} +
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/RestServer.java Thu Nov 24 11:39:12 2011 +0100 +++ b/src/de/mpiwg/itgroup/annotationManager/restlet/RestServer.java Fri Nov 25 08:43:25 2011 +0100 @@ -34,6 +34,7 @@ import org.restlet.data.ClientInfo; import org.restlet.engine.component.ChildContext; import org.restlet.ext.jaas.JaasVerifier; +import org.restlet.routing.Redirector; import org.restlet.routing.Router; import org.restlet.routing.Template; import org.restlet.routing.TemplateRoute; @@ -125,12 +126,19 @@ this.authenticator = createAuthenticator(); + //String target = "{rh}/{rf}/XX"; + //Redirector redirector = new Redirector(getContext().createChildContext(), target, + // Redirector.MODE_CLIENT_SEE_OTHER); + Router router = new Router(getContext()); + router.attach("/annotations",AddAndSearchAnnotations.class); router.attach("/search",AddAndSearchAnnotations.class); // annotator api askes for different uris for search and adding router.attach("/dummy",Dummy.class); + //router.attach("",redirector); + router.attach("/",AnnotatorInfo.class); authenticator.setNext(router); return authenticator;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/de/mpiwg/itgroup/annotationManager/staticPages/main.html Fri Nov 25 08:43:25 2011 +0100 @@ -0,0 +1,6 @@ +<html> +<body> +<h2>Annotation manager</h2> +<p><a href="annotations">Annotations overview</a></p> +</body> +</html> \ No newline at end of file