Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/ScopeResource.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/ScopeResource.java?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/ScopeResource.java (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/ScopeResource.java Tue Jul 10 17:00:53 2012 @@ -30,7 +30,6 @@ import static javax.ws.rs.core.Response. import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; import static javax.ws.rs.core.Response.Status.NOT_FOUND; -import static javax.ws.rs.core.Response.Status.UNSUPPORTED_MEDIA_TYPE; import static org.apache.stanbol.commons.web.base.CorsHelper.addCORSOrigin; import static org.apache.stanbol.commons.web.base.CorsHelper.enableCORS; import static org.apache.stanbol.commons.web.base.format.KRFormat.FUNCTIONAL_OWL; @@ -43,9 +42,11 @@ import static org.apache.stanbol.commons import static org.apache.stanbol.commons.web.base.format.KRFormat.TURTLE; import static org.apache.stanbol.commons.web.base.format.KRFormat.X_TURTLE; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.ArrayList; @@ -77,11 +78,12 @@ import javax.ws.rs.core.UriInfo; import org.apache.clerezza.rdf.core.Graph; import org.apache.clerezza.rdf.core.TripleCollection; -import org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException; import org.apache.stanbol.commons.web.base.ContextHelper; import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource; import org.apache.stanbol.ontologymanager.ontonet.api.ONManager; +import org.apache.stanbol.ontologymanager.ontonet.api.OntologyLoadingException; import org.apache.stanbol.ontologymanager.ontonet.api.collector.DuplicateIDException; +import org.apache.stanbol.ontologymanager.ontonet.api.collector.IrremovableOntologyException; import org.apache.stanbol.ontologymanager.ontonet.api.collector.OntologyCollectorModificationException; import org.apache.stanbol.ontologymanager.ontonet.api.collector.UnmodifiableOntologyCollectorException; import org.apache.stanbol.ontologymanager.ontonet.api.io.GraphContentInputSource; @@ -96,7 +98,7 @@ import org.apache.stanbol.ontologymanage import org.apache.stanbol.ontologymanager.registry.api.model.Library; import org.apache.stanbol.ontologymanager.registry.io.LibrarySource; import org.apache.stanbol.ontologymanager.web.util.OntologyPrettyPrintResource; -import org.coode.owlapi.turtle.TurtleOntologyFormat; +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; @@ -211,6 +213,19 @@ public class ScopeResource extends BaseS return result; } + @GET + @Path("/core") + @Produces(value = {RDF_XML, TURTLE, X_TURTLE, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN}) + public Response getCoreSpace(@DefaultValue("false") @QueryParam("merge") boolean merge, + @Context UriInfo uriInfo, + @Context HttpHeaders headers) { + OntologySpace space = scope.getCoreSpace(); + OWLOntology o = space.export(OWLOntology.class, merge); + ResponseBuilder rb = Response.ok(o); + addCORSOrigin(servletContext, rb, headers); + return rb.build(); + } + public SortedSet<String> getCustomOntologies() { SortedSet<String> result = new TreeSet<String>(); for (IRI iri : scope.getCustomSpace().listManagedOntologies()) @@ -219,6 +234,19 @@ public class ScopeResource extends BaseS } @GET + @Path("/custom") + @Produces(value = {RDF_XML, TURTLE, X_TURTLE, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN}) + public Response getCustomSpace(@DefaultValue("false") @QueryParam("merge") boolean merge, + @Context UriInfo uriInfo, + @Context HttpHeaders headers) { + OntologySpace space = scope.getCustomSpace(); + OWLOntology o = space.export(OWLOntology.class, merge); + ResponseBuilder rb = Response.ok(o); + addCORSOrigin(servletContext, rb, headers); + return rb.build(); + } + + @GET @Produces(TEXT_HTML) public Response getHtmlInfo(@Context HttpHeaders headers) { ResponseBuilder rb; @@ -248,6 +276,22 @@ public class ScopeResource extends BaseS } @OPTIONS + @Path("/core") + public Response handleCorsPreflightCore(@Context HttpHeaders headers) { + ResponseBuilder rb = Response.ok(); + enableCORS(servletContext, rb, headers, GET, OPTIONS); + return rb.build(); + } + + @OPTIONS + @Path("/custom") + public Response handleCorsPreflightCustom(@Context HttpHeaders headers) { + ResponseBuilder rb = Response.ok(); + enableCORS(servletContext, rb, headers, GET, OPTIONS); + return rb.build(); + } + + @OPTIONS @Path("/{ontologyId:.+}") public Response handleCorsPreflightOntology(@Context HttpHeaders headers) { ResponseBuilder rb = Response.ok(); @@ -274,45 +318,23 @@ public class ScopeResource extends BaseS @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) { + log.debug("Absolute URL Path {}", uriInfo.getRequestUri()); + log.debug("Ontology ID {}", ontologyId); ResponseBuilder rb; if (scope == null) rb = Response.status(NOT_FOUND); - else { - // First of all, it could be a simple request for the space root! - - String absur = uriInfo.getRequestUri().toString(); - log.debug("Absolute URL Path {}", absur); - log.debug("Ontology ID {}", ontologyId); - - IRI ontiri = IRI.create(ontologyId); - - // TODO: hack (ma anche no) - if (!ontiri.isAbsolute()) ontiri = IRI.create(absur); - - // First of all, it could be a simple request for the space root! - String temp = scope.getID() + "/" + ontologyId; - OntologySpace space = scope.getCoreSpace(); - if (temp.equals(space.getID())) rb = Response.ok(space.export(Graph.class, merge)); - else { - space = scope.getCustomSpace(); - if (temp.equals(space.getID())) rb = Response.ok(space.export(Graph.class, merge)); - else { - Graph o = null; - IRI ontologyIri = IRI.create(ontologyId); - OntologySpace spc = scope.getCustomSpace(); - if (spc != null && spc.hasOntology(ontologyIri)) { - // o = spc.getOntology(ontologyIri, merge); - o = spc.getOntology(ontologyIri, Graph.class, merge); - } else { - spc = scope.getCoreSpace(); - if (spc != null && spc.hasOntology(ontologyIri)) - // o = spc.getOntology(ontologyIri, merge); - o = spc.getOntology(ontologyIri, Graph.class, merge); - } - if (o == null) return Response.status(NOT_FOUND).build(); - else rb = Response.ok(o); - } + Graph o = null; + IRI ontologyIri = IRI.create(ontologyId); + OntologySpace spc = scope.getCustomSpace(); + if (spc != null && spc.hasOntology(ontologyIri)) { + o = spc.getOntology(ontologyIri, Graph.class, merge); + } else { + spc = scope.getCoreSpace(); + if (spc != null && spc.hasOntology(ontologyIri)) o = spc.getOntology(ontologyIri, + Graph.class, merge); } + if (o == null) rb = Response.status(NOT_FOUND); + else rb = Response.ok(o); } addCORSOrigin(servletContext, rb, headers); @@ -338,53 +360,31 @@ public class ScopeResource extends BaseS @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) { - + log.debug("Absolute URL Path {}", uriInfo.getRequestUri()); + log.debug("Ontology ID {}", ontologyId); ResponseBuilder rb; if (scope == null) rb = Response.status(NOT_FOUND); - else { - // First of all, it could be a simple request for the space root! - - String absur = uriInfo.getRequestUri().toString(); - log.debug("Absolute URL Path {}", absur); - log.debug("Ontology ID {}", ontologyId); - - IRI ontiri = IRI.create(ontologyId); - - // TODO: hack (ma anche no) - if (!ontiri.isAbsolute()) ontiri = IRI.create(absur); - - // First of all, it could be a simple request for the space root! - String temp = scope.getID() + "/" + ontologyId; - OntologySpace space = scope.getCoreSpace(); - if (temp.equals(space.getID())) rb = Response.ok(space.export(OWLOntology.class, merge)); - else { - space = scope.getCustomSpace(); - if (temp.equals(space.getID())) rb = Response.ok(space.export(OWLOntology.class, merge)); - else { - OWLOntology o = null; - IRI ontologyIri = IRI.create(ontologyId); - OntologySpace spc = scope.getCustomSpace(); - if (spc != null && spc.hasOntology(ontologyIri)) { - o = spc.getOntology(ontologyIri, OWLOntology.class, merge); - } else { - spc = scope.getCoreSpace(); - if (spc != null && spc.hasOntology(ontologyIri)) o = spc.getOntology(ontologyIri, - OWLOntology.class, merge); - } - if (o == null) return Response.status(NOT_FOUND).build(); - else rb = Response.ok(o); - } + OWLOntology o = null; + IRI ontologyIri = IRI.create(ontologyId); + OntologySpace spc = scope.getCustomSpace(); + if (spc != null && spc.hasOntology(ontologyIri)) { + o = spc.getOntology(ontologyIri, OWLOntology.class, merge); + } else { + spc = scope.getCoreSpace(); + if (spc != null && spc.hasOntology(ontologyIri)) o = spc.getOntology(ontologyIri, + OWLOntology.class, merge); } + if (o == null) rb = Response.status(NOT_FOUND); + else rb = Response.ok(o); } - addCORSOrigin(servletContext, rb, headers); return rb.build(); } - // @GET - // @Path("/{ontologyId:.+}") - // @Produces(TEXT_HTML) + @GET + @Path("/{ontologyId:.+}") + @Produces(TEXT_HTML) public Response managedOntologyShow(@PathParam("ontologyId") String ontologyId, @Context HttpHeaders headers) { ResponseBuilder rb; @@ -398,9 +398,9 @@ public class ScopeResource extends BaseS if (o == null) rb = Response.status(NOT_FOUND); else try { ByteArrayOutputStream out = new ByteArrayOutputStream(); - o.getOWLOntologyManager().saveOntology(o, new TurtleOntologyFormat(), out); + o.getOWLOntologyManager().saveOntology(o, new ManchesterOWLSyntaxOntologyFormat(), out); rb = Response.ok(new Viewable("ontology", new OntologyPrettyPrintResource(servletContext, - uriInfo, out))); + uriInfo, out, scope))); } catch (OWLOntologyStorageException e) { throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); } @@ -420,26 +420,30 @@ public class ScopeResource extends BaseS */ @DELETE @Path("/{ontologyId:.+}") - public Response managedOntologyUnload(@PathParam("uri") String ontologyid, + public Response managedOntologyUnload(@PathParam("ontologyId") String ontologyId, + @PathParam("scopeid") String scopeId, @Context UriInfo uriInfo, @Context HttpHeaders headers) { - - if (ontologyid != null && !ontologyid.equals("")) { - IRI ontIri = IRI.create(ontologyid); - String scopeId = scope.getID(); + ResponseBuilder rb; + if (ontologyId != null && !ontologyId.equals("")) { + IRI ontIri = IRI.create(ontologyId); OntologySpace cs = scope.getCustomSpace(); if (cs.hasOntology(ontIri)) { try { onm.setScopeActive(scopeId, false); cs.removeOntology(ontIri); - onm.setScopeActive(scopeId, true); + rb = Response.ok(); + } catch (IrremovableOntologyException e) { + throw new WebApplicationException(e, FORBIDDEN); + } catch (UnmodifiableOntologyCollectorException e) { + throw new WebApplicationException(e, FORBIDDEN); } catch (OntologyCollectorModificationException e) { - onm.setScopeActive(scopeId, true); throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); + } finally { + onm.setScopeActive(scopeId, true); } - } - } - ResponseBuilder rb = Response.ok(); + } else rb = Response.notModified(); // ontology not managed + } else rb = Response.status(BAD_REQUEST); // null/blank ontology ID addCORSOrigin(servletContext, rb, headers); return rb.build(); } @@ -511,6 +515,7 @@ public class ScopeResource extends BaseS return rb.build(); } + @SuppressWarnings("unused") @POST @Consumes({MULTIPART_FORM_DATA}) @Produces({TEXT_HTML, TEXT_PLAIN, RDF_XML, TURTLE, X_TURTLE, N3}) @@ -551,16 +556,13 @@ public class ScopeResource extends BaseS OntologyInputSource<?,?> src = null; if (fileOk) { try { - - InputStream content = new FileInputStream(file); + // Use a buffered stream that can be reset for multiple attempts. + InputStream content = new BufferedInputStream(new FileInputStream(file)); src = new GraphContentInputSource(content, format); - } catch (UnsupportedFormatException e) { - log.warn( - "POST method failed for media type {}. This should not happen (should fail earlier)", - headers.getMediaType()); - rb = Response.status(UNSUPPORTED_MEDIA_TYPE); - } catch (Exception e) { - throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); + } catch (OntologyLoadingException e) { + throw new WebApplicationException(e, BAD_REQUEST); + } catch (IOException e) { + throw new WebApplicationException(e, BAD_REQUEST); } } else if (location != null) { try { @@ -604,10 +606,10 @@ public class ScopeResource extends BaseS * @param scopeid * @param coreRegistry * a. If it is a well-formed IRI it supersedes <code>coreOntology</code>. - * @param coreOntology + * @param coreOntologies * @param customRegistry * a. If it is a well-formed IRI it supersedes <code>customOntology</code>. - * @param customOntology + * @param customOntologies * @param activate * if true, the new scope will be activated upon creation. * @param uriInfo @@ -617,77 +619,50 @@ public class ScopeResource extends BaseS @PUT @Consumes(MediaType.WILDCARD) public Response registerScope(@PathParam("scopeid") String scopeid, - @QueryParam("corereg") String coreRegistry, - @QueryParam("coreont") String coreOntology, - @QueryParam("customreg") String customRegistry, - @QueryParam("customont") String customOntology, + @QueryParam("corereg") final List<String> coreRegistries, + @QueryParam("coreont") final List<String> coreOntologies, @DefaultValue("false") @QueryParam("activate") boolean activate, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context ServletContext servletContext) { log.debug("Request URI {}", uriInfo.getRequestUri()); + List<OntologyInputSource<?,?>> srcs = new ArrayList<OntologyInputSource<?,?>>(coreOntologies.size() + + coreRegistries.size()); + // First thing, check registry sources. + if (coreRegistries != null) for (String reg : coreRegistries) + if (reg != null && !reg.isEmpty()) try { + // Library IDs are sanitized differently + srcs.add(new LibrarySource(IRI.create(reg.replace("%23", "#")), regMgr)); + } catch (Exception e1) { + throw new WebApplicationException(e1, BAD_REQUEST); + // Bad or not supplied core registry, try the ontology. + } - OntologyScope scope; - OntologyInputSource<?,?> coreSrc = null, custSrc = null; - - // First thing, check the core source. - if (coreRegistry != null && !coreRegistry.isEmpty()) try { - coreSrc = new LibrarySource(IRI.create(coreRegistry.replace("%23", "#")), regMgr); - } catch (Exception e1) { - throw new WebApplicationException(e1, BAD_REQUEST); - // Bad or not supplied core registry, try the ontology. - } - else if (coreOntology != null && !coreOntology.isEmpty()) try { - coreSrc = new RootOntologyIRISource(IRI.create(coreOntology)); - } catch (Exception e2) { - // If this fails too, throw a bad request. - throw new WebApplicationException(e2, BAD_REQUEST); - } - - // Don't bother if no custom was supplied at all... - if (customRegistry != null && !customRegistry.isEmpty()) - // ...but if it was, be prepared to throw exceptions. - try { - coreSrc = new LibrarySource(IRI.create(customRegistry.replace("%23", "#")), regMgr); - } catch (Exception e1) { - throw new WebApplicationException(e1, BAD_REQUEST); - // Bad or not supplied custom registry, try the ontology. - } - if (customOntology != null && !customOntology.isEmpty()) try { - custSrc = new RootOntologyIRISource(IRI.create(customOntology)); - } catch (Exception e2) { - // If this fails too, throw a bad request. - throw new WebApplicationException(e2, BAD_REQUEST); - } + // Then ontology sources + if (coreOntologies != null) for (String ont : coreOntologies) + if (ont != null && !ont.isEmpty()) try { + srcs.add(new RootOntologyIRISource(IRI.create(ont))); + } catch (Exception e2) { + // If this fails too, throw a bad request. + throw new WebApplicationException(e2, BAD_REQUEST); + } // Now the creation. try { // Expand core sources List<OntologyInputSource<?,?>> expanded = new ArrayList<OntologyInputSource<?,?>>(); - if (coreSrc != null) { - if (coreSrc instanceof SetInputSource) { - for (Object o : ((SetInputSource<?>) coreSrc).getOntologies()) { - OntologyInputSource<?,?> src = null; - if (o instanceof OWLOntology) src = new RootOntologySource((OWLOntology) o); - else if (o instanceof TripleCollection) src = new GraphSource((TripleCollection) o); - if (src != null) expanded.add(src); - } - } else expanded.add(coreSrc); // Must be denoting a single ontology - } - if (custSrc != null) { - if (custSrc instanceof SetInputSource) { - for (Object o : ((SetInputSource<?>) custSrc).getOntologies()) { - OntologyInputSource<?,?> src = null; - if (o instanceof OWLOntology) src = new RootOntologySource((OWLOntology) o); - else if (o instanceof TripleCollection) src = new GraphSource((TripleCollection) o); - if (src != null) expanded.add(src); - } - } else expanded.add(custSrc); // Must be denoting a single ontology - } - // Invoke the appropriate factory method depending on the - // availability of a custom source. - // scope = (custSrc != null) ? f.createOntologyScope(scopeid, coreSrc, custSrc) : f - // .createOntologyScope(scopeid, coreSrc); + for (OntologyInputSource<?,?> coreSrc : srcs) + if (coreSrc != null) { + if (coreSrc instanceof SetInputSource) { + for (Object o : ((SetInputSource<?>) coreSrc).getOntologies()) { + OntologyInputSource<?,?> src = null; + if (o instanceof OWLOntology) src = new RootOntologySource((OWLOntology) o); + else if (o instanceof TripleCollection) src = new GraphSource( + (TripleCollection) o); + if (src != null) expanded.add(src); + } + } else expanded.add(coreSrc); // Must be denoting a single ontology + } scope = onm.createOntologyScope(scopeid, expanded.toArray(new OntologyInputSource[0])); // Setup and register the scope. If no custom space was set, it will // still be open for modification.
Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java Tue Jul 10 17:00:53 2012 @@ -30,7 +30,6 @@ import static javax.ws.rs.core.Response. import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; import static javax.ws.rs.core.Response.Status.NOT_FOUND; -import static javax.ws.rs.core.Response.Status.UNSUPPORTED_MEDIA_TYPE; import static org.apache.stanbol.commons.web.base.CorsHelper.addCORSOrigin; import static org.apache.stanbol.commons.web.base.CorsHelper.enableCORS; import static org.apache.stanbol.commons.web.base.format.KRFormat.FUNCTIONAL_OWL; @@ -43,9 +42,11 @@ import static org.apache.stanbol.commons import static org.apache.stanbol.commons.web.base.format.KRFormat.TURTLE; import static org.apache.stanbol.commons.web.base.format.KRFormat.X_TURTLE; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.HashSet; @@ -75,10 +76,10 @@ import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import org.apache.clerezza.rdf.core.Graph; -import org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException; import org.apache.stanbol.commons.web.base.ContextHelper; import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource; import org.apache.stanbol.ontologymanager.ontonet.api.ONManager; +import org.apache.stanbol.ontologymanager.ontonet.api.OntologyLoadingException; import org.apache.stanbol.ontologymanager.ontonet.api.collector.IrremovableOntologyException; import org.apache.stanbol.ontologymanager.ontonet.api.collector.OntologyCollectorModificationException; import org.apache.stanbol.ontologymanager.ontonet.api.collector.UnmodifiableOntologyCollectorException; @@ -91,7 +92,7 @@ import org.apache.stanbol.ontologymanage import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionLimitException; import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionManager; import org.apache.stanbol.ontologymanager.web.util.OntologyPrettyPrintResource; -import org.coode.owlapi.turtle.TurtleOntologyFormat; +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; @@ -300,8 +301,7 @@ public class SessionResource extends Bas @Context HttpHeaders headers) { if (session == null) return Response.status(NOT_FOUND).build(); Graph o = session.getOntology(IRI.create(ontologyId), Graph.class, merge); - if (o == null) return Response.status(NOT_FOUND).build(); - ResponseBuilder rb = Response.ok(o); + ResponseBuilder rb = (o != null) ? Response.ok(o) : Response.status(NOT_FOUND); addCORSOrigin(servletContext, rb, headers); return rb.build(); } @@ -330,12 +330,10 @@ public class SessionResource extends Bas if (session == null) rb = Response.status(NOT_FOUND); else if (merge) { Graph g = session.getOntology(IRI.create(ontologyId), Graph.class, merge); - if (g == null) rb = Response.status(NOT_FOUND); - else rb = Response.ok(g); + rb = (g != null) ? Response.ok(g) : Response.status(NOT_FOUND); } else { OWLOntology o = session.getOntology(IRI.create(ontologyId), OWLOntology.class, merge); - if (o == null) rb = Response.status(NOT_FOUND); - else rb = Response.ok(o); + rb = (o != null) ? Response.ok(o) : Response.status(NOT_FOUND); } addCORSOrigin(servletContext, rb, headers); return rb.build(); @@ -363,8 +361,7 @@ public class SessionResource extends Bas @Context HttpHeaders headers) { if (session == null) return Response.status(NOT_FOUND).build(); OWLOntology o = session.getOntology(IRI.create(ontologyId), OWLOntology.class, merge); - if (o == null) return Response.status(NOT_FOUND).build(); - ResponseBuilder rb = Response.ok(o); + ResponseBuilder rb = (o != null) ? Response.ok(o) : Response.status(NOT_FOUND); addCORSOrigin(servletContext, rb, headers); return rb.build(); } @@ -382,9 +379,9 @@ public class SessionResource extends Bas if (o == null) rb = Response.status(NOT_FOUND); else try { ByteArrayOutputStream out = new ByteArrayOutputStream(); - o.getOWLOntologyManager().saveOntology(o, new TurtleOntologyFormat(), out); + o.getOWLOntologyManager().saveOntology(o, new ManchesterOWLSyntaxOntologyFormat(), out); rb = Response.ok(new Viewable("ontology", new OntologyPrettyPrintResource(servletContext, - uriInfo, out))); + uriInfo, out, session))); } catch (OWLOntologyStorageException e) { throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); } @@ -415,20 +412,23 @@ public class SessionResource extends Bas @PathParam("ontologyId") String ontologyId, @Context UriInfo uriInfo, @Context HttpHeaders headers) { - if (session == null) return Response.status(NOT_FOUND).build(); - IRI iri = IRI.create(ontologyId); - OWLOntology o = session.getOntology(iri, OWLOntology.class); - if (o == null) return Response.notModified().build(); - try { - session.removeOntology(iri); - } catch (IrremovableOntologyException e) { - throw new WebApplicationException(e, FORBIDDEN); - } catch (UnmodifiableOntologyCollectorException e) { - throw new WebApplicationException(e, FORBIDDEN); - } catch (OntologyCollectorModificationException e) { - throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); + ResponseBuilder rb; + if (session == null) rb = Response.status(NOT_FOUND); + else { + IRI iri = IRI.create(ontologyId); + OWLOntology o = session.getOntology(iri, OWLOntology.class); + if (o == null) rb = Response.notModified(); + else try { + session.removeOntology(iri); + rb = Response.ok(); + } catch (IrremovableOntologyException e) { + throw new WebApplicationException(e, FORBIDDEN); + } catch (UnmodifiableOntologyCollectorException e) { + throw new WebApplicationException(e, FORBIDDEN); + } catch (OntologyCollectorModificationException e) { + throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); + } } - ResponseBuilder rb = Response.ok(); addCORSOrigin(servletContext, rb, headers); return rb.build(); } @@ -493,6 +493,7 @@ public class SessionResource extends Bas return rb.build(); } + @SuppressWarnings("unused") @POST @Consumes({MULTIPART_FORM_DATA}) @Produces({TEXT_HTML, TEXT_PLAIN, RDF_XML, TURTLE, X_TURTLE, N3}) @@ -531,15 +532,13 @@ public class SessionResource extends Bas OntologyInputSource<?,?> src = null; if (fileOk) { // File first try { - InputStream content = new FileInputStream(file); + // Use a buffered stream that can be reset for multiple attempts. + InputStream content = new BufferedInputStream(new FileInputStream(file)); src = new GraphContentInputSource(content, format); - } catch (UnsupportedFormatException e) { - log.warn( - "POST method failed for media type {}. This should not happen (should fail earlier)", - headers.getMediaType()); - rb = Response.status(UNSUPPORTED_MEDIA_TYPE); - } catch (Exception e) { - throw new WebApplicationException(e, INTERNAL_SERVER_ERROR); + } catch (OntologyLoadingException e) { + throw new WebApplicationException(e, BAD_REQUEST); + } catch (IOException e) { + throw new WebApplicationException(e, BAD_REQUEST); } } else if (location != null) { try { @@ -573,5 +572,4 @@ public class SessionResource extends Bas addCORSOrigin(servletContext, rb, headers); return rb.build(); } - } Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/util/OntologyPrettyPrintResource.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/util/OntologyPrettyPrintResource.java?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/util/OntologyPrettyPrintResource.java (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/util/OntologyPrettyPrintResource.java Tue Jul 10 17:00:53 2012 @@ -20,6 +20,7 @@ import javax.servlet.ServletContext; import javax.ws.rs.core.UriInfo; import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource; +import org.apache.stanbol.ontologymanager.ontonet.api.NamedResource; /** * This is a dummy resource to be used for human-readable HTML output. @@ -29,6 +30,8 @@ import org.apache.stanbol.commons.web.ba */ public class OntologyPrettyPrintResource extends BaseStanbolResource { + private NamedResource owner = null; + private Object result; public OntologyPrettyPrintResource(ServletContext context, UriInfo uriInfo, Object result) { @@ -37,6 +40,18 @@ public class OntologyPrettyPrintResource this.uriInfo = uriInfo; } + public OntologyPrettyPrintResource(ServletContext context, + UriInfo uriInfo, + Object result, + NamedResource owner) { + this(context, uriInfo, result); + this.owner = owner; + } + + public NamedResource getOwner() { + return this.owner; + } + public Object getResult() { return this.result; } Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/OntologyNetworkResource/index.ftl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/OntologyNetworkResource/index.ftl?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/OntologyNetworkResource/index.ftl (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/OntologyNetworkResource/index.ftl Tue Jul 10 17:00:53 2012 @@ -19,13 +19,25 @@ <#escape x as x?html> <@common.page title="Apache Stanbol OntoNet scope manager" hasrestapi=true> - + + <a href="${it.publicBaseUri}ontonet/ontology">Scope Manager</a> + <div class="panel" id="webview"> <#assign scopes = it.scopes> <p>This is the start page of the ontology scope manager.</p> + + <fieldset> + <legend>New Scope</legend> + <p> + <b>ID:</b> <input type="text" name="sid" id="sid" size="40" value=""/> + <input type="submit" value="Create" onClick="javascript:createScope(document.getElementById('sid').value)"/> + </p> + </fieldset> - <div id="scopes" class="storeContents"> - <table id="allScopes"> + <h3>Registered Scopes</h3> + <div class="storeContents" id="allScopes"> + <div> + <table> <div> <tr> <th></th> @@ -39,8 +51,8 @@ <td> <#-- <img src="${it.staticRootUrl}/contenthub/images/edit_icon_16.png" title="(not available yet) Edit this item" /> - <img src="${it.staticRootUrl}/contenthub/images/delete_icon_16.png" title="(not available yet) Delete this item" /> --> + <img src="${it.staticRootUrl}/contenthub/images/delete_icon_16.png" title="Delete this item" onClick="javascript:deleteScope('${scope.ID}')"/> </td> <td><a href="${it.publicBaseUri}ontonet/ontology/${scope.ID}" title="${scope.ID}">${scope.ID}</a></td> <td>${scope.locked?string("locked", "modifiable")}</td> @@ -49,8 +61,9 @@ </tr> </#list> </div> - </table> <!-- allScopes --> - </div> + </table> + </div> + </div> <!-- allScopes --> </div> <!-- web view --> @@ -62,6 +75,82 @@ </div> + <!-- Scripts --> + <script language="JavaScript"> + + // On document load + $(function(){ + + // Set button disabled + $("input[type=submit]").attr("disabled", "disabled"); + + // Append a change event listener to you inputs + $('input').change(function() { + // Validate your form here: + var validated = true; + if(isBlank($('#sid').val())) validated = false; + + //I f form is validated enable form + if(validated) $("input[type=submit]").removeAttr("disabled"); + else $("input[type=submit]").attr("disabled", "disabled"); + }); + + // Trigger change function once to check if the form is validated on page load + $('input:first').trigger('change'); + }); + + function deleteScope(sid) { + var lurl = "${it.publicBaseUri}ontonet/ontology/" + sid; + $.ajax({ + url: lurl, + type: "DELETE", + async: true, + cache: false, + success: function(data, textStatus, xhr) { + $("#allScopes").load("${it.publicBaseUri}ontonet/ontology #allScopes>div"); + }, + error: function() { + alert(result.status + ' ' + result.statusText); + } + }); + } + + function createScope(sid) { + var lurl = "${it.publicBaseUri}ontonet/ontology/" + sid; + $.ajax({ + url: lurl, + type: "PUT", + async: true, + cache: false, + success: function(data, textStatus, xhr) { + console.log(xhr.status); + console.log(xhr.getResponseHeader("Location")); + }, + error: function(xhr, textStatus, errorThrown) { + switch(xhr.status) { + case 409: + alert('Conflict: ID \"' + sid + '\" already taken.'); + break; + default: + alert(xhr.status + ' ' + xhr.statusText); + } + }, + complete: function(xhr, textStatus) { + switch(xhr.status) { + case 201: + var loc = xhr.getResponseHeader("Location"); + window.location.href = loc; + break; + } + } + }); + } + + function isBlank(str) { + return (!str || /^\s*$/.test(str)); + } + + </script> </@common.page> </#escape> \ No newline at end of file Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/index.ftl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/index.ftl?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/index.ftl (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/index.ftl Tue Jul 10 17:00:53 2012 @@ -18,7 +18,9 @@ <#escape x as x?html> <@common.page title="${it.scope.ID} : Apache Stanbol OntoNet scope" hasrestapi=false> - + + <a href="${it.publicBaseUri}ontonet/ontology">Scope Manager</a> > Scope <tt>${it.scope.ID}</tt> + <div class="panel" id="webview"> <h3>Load ontologies</h3> Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/ontology.ftl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/ontology.ftl?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/ontology.ftl (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/ScopeResource/ontology.ftl Tue Jul 10 17:00:53 2012 @@ -16,11 +16,13 @@ --> <#import "/imports/common.ftl" as common> <#escape x as x?html> -<@common.page title="Ontology Manager : Ontology Detail" hasrestapi=false> - <div class="panel"> -<pre> -${it.result} -</pre> - </div> + <@common.page title="Ontology Manager : Ontology Detail" hasrestapi=false> + + <a href="${it.publicBaseUri}ontonet/ontology">Scope Manager</a> > <a href="${it.publicBaseUri}ontonet/ontology/${it.owner.ID}">Scope <tt>${it.owner.ID}</tt></a> > Ontology + + <div class="panel"> + <pre>${it.result}</pre> + </div> + </@common.page> </#escape> \ No newline at end of file Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionManagerResource/index.ftl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionManagerResource/index.ftl?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionManagerResource/index.ftl (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionManagerResource/index.ftl Tue Jul 10 17:00:53 2012 @@ -19,13 +19,25 @@ <#escape x as x?html> <@common.page title="Apache Stanbol OntoNet session manager" hasrestapi=true> - + + <a href="${it.publicBaseUri}ontonet/session">Session Manager</a> + <div class="panel" id="webview"> <#assign sessions = it.sessions> <p>This is the start page of the Session Manager.</p> - <div class="storeContents"> - <table id="allSessions"> + <fieldset> + <legend>New Session</legend> + <p> + <b>ID:</b> <input type="text" name="sid" id="sid" size="40" value=""/> + <input type="submit" value="Create" onClick="javascript:createSession(document.getElementById('sid').value)"/> + </p> + </fieldset> + + <h3>Registered Sessions</h3> + <div class="storeContents" id="allSessions"> + <div> + <table> <div> <tr> <th></th> @@ -38,9 +50,9 @@ <tr> <td> <#-- - <img src="${it.staticRootUrl}/contenthub/images/edit_icon_16.png" title="(not available yet) Edit this item" /> - <img src="${it.staticRootUrl}/contenthub/images/delete_icon_16.png" title="(not available yet) Delete this item" /> + <img src="${it.staticRootUrl}/ontonet/images/edit_icon_16.png" title="(not available yet) Edit this item" /> --> + <img src="${it.staticRootUrl}/ontonet/images/delete_icon_16.png" title="Delete this item" onClick="javascript:deleteSession('${session.ID}')"/> </td> <td><a href="${it.publicBaseUri}ontonet/session/${session.ID}" title="${session.ID}">${session.ID}</a></td> <td>${session.locked?string("locked", "modifiable")}</td> @@ -49,8 +61,10 @@ </tr> </#list> </div> - </table> <!-- allSessions --> - </div> + </table> + </div> + </div> <!-- allSessions --> + </div> <!-- webview --> <div class="panel" id="restapi" style="display: none;"> @@ -58,5 +72,61 @@ <#include "/imports/inc_sessionmgr.ftl"> </div> <!-- restapi --> + <script language="JavaScript"> + + function createSession(sid) { + var lurl = "${it.publicBaseUri}ontonet/session" + (isBlank(sid)?"":("/"+sid)); + var method = isBlank(sid)?"POST":"PUT"; + $.ajax({ + url: lurl, + type: method, + async: true, + cache: false, + success: function(data, textStatus, xhr) { + // window.location.href = data.redirectToUrl; + console.log(xhr.status); + console.log(xhr.getResponseHeader("Location")); + }, + error: function(xhr, textStatus, errorThrown) { + switch(xhr.status) { + case 409: + alert('Conflict: ID \"' + sid + '\" already taken.'); + break; + default: + alert(xhr.status + ' ' + xhr.statusText); + } + }, + complete: function(xhr, textStatus) { + if (xhr.status == 201) { + var loc = xhr.getResponseHeader("Location"); + window.location.href = loc; + } + } + }); + } + + function deleteSession(sid) { + var lurl = "${it.publicBaseUri}ontonet/session/" + sid; + $.ajax({ + url: lurl, + type: "DELETE", + async: true, + cache: false, + success: function(data, textStatus, xhr) { + $("#allSessions").load("${it.publicBaseUri}ontonet/session #allSessions>div"); + }, + error: function() { + alert(result.status + ' ' + result.statusText); + } + }); + } + + function isBlank(str) { + return (!str || /^\s*$/.test(str)); + } + + + </script> + </@common.page> </#escape> \ No newline at end of file Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/index.ftl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/index.ftl?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/index.ftl (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/index.ftl Tue Jul 10 17:00:53 2012 @@ -18,7 +18,9 @@ <#escape x as x?html> <@common.page title="${it.session.ID} : an Apache Stanbol OntoNet session" hasrestapi=false> - + + <a href="${it.publicBaseUri}ontonet/session">Session Manager</a> > Session <tt>${it.session.ID}</tt> + <div class="panel" id="webview"> <h3>Load an ontology</h3> Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/ontology.ftl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/ontology.ftl?rev=1359786&r1=1359785&r2=1359786&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/ontology.ftl (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/resources/org/apache/stanbol/ontologymanager/web/templates/org/apache/stanbol/ontologymanager/web/resources/SessionResource/ontology.ftl Tue Jul 10 17:00:53 2012 @@ -16,11 +16,13 @@ --> <#import "/imports/common.ftl" as common> <#escape x as x?html> -<@common.page title="Ontology Manager : Ontology Detail" hasrestapi=false> - <div class="panel"> -<pre> -${it.result} -</pre> - </div> + <@common.page title="Ontology Manager : Ontology Detail" hasrestapi=false> + + <a href="${it.publicBaseUri}ontonet/session">Session Manager</a> > <a href="${it.publicBaseUri}ontonet/session/${it.owner.ID}">Session <tt>${it.owner.ID}</tt></a> > Ontology + + <div class="panel"> + <pre>${it.result}</pre> + </div> + </@common.page> </#escape> \ No newline at end of file
