Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionByIdResource.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionByIdResource.java?rev=1206502&r1=1206501&r2=1206502&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionByIdResource.java (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionByIdResource.java Sat Nov 26 17:18:41 2011 @@ -27,12 +27,14 @@ import java.io.InputStream; import javax.servlet.ServletContext; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; @@ -102,10 +104,11 @@ public class SessionByIdResource extends @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON}) public Response asOntology(@PathParam("id") String sessionId, + @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) { if (session == null) return Response.status(NOT_FOUND).build(); - return Response.ok(session.asOWLOntology(false)).build(); + return Response.ok(session.asOWLOntology(merge)).build(); } /** @@ -171,10 +174,11 @@ public class SessionByIdResource extends KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON}) public Response getManagedOntology(@PathParam("id") String sessionId, @PathParam("ontologyId") String ontologyId, + @DefaultValue("false") @QueryParam("merge") boolean merge, @Context UriInfo uriInfo, @Context HttpHeaders headers) { if (session == null) return Response.status(NOT_FOUND).build(); - OWLOntology o = session.getOntology(IRI.create(ontologyId)); + OWLOntology o = session.getOntology(IRI.create(ontologyId), merge); if (o == null) return Response.status(NOT_FOUND).build(); return Response.ok(o).build(); } @@ -193,7 +197,7 @@ public class SessionByIdResource extends */ @POST @Consumes(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, KRFormat.FUNCTIONAL_OWL, - KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON}) + KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON, KRFormat.N3, KRFormat.N_TRIPLE}) @Produces(MediaType.TEXT_PLAIN) public Response manageOntology(InputStream content) { long before = System.currentTimeMillis();
Modified: incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredGraphsResource.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredGraphsResource.java?rev=1206502&r1=1206501&r2=1206502&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredGraphsResource.java (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredGraphsResource.java Sat Nov 26 17:18:41 2011 @@ -66,6 +66,7 @@ import com.hp.hpl.jena.ontology.OntModel import com.sun.jersey.api.view.ImplicitProduces; import com.sun.jersey.multipart.FormDataParam; +@Deprecated @Path("/ontonet/graphs") @ImplicitProduces(MediaType.TEXT_HTML + ";qs=2") public class StoredGraphsResource extends BaseStanbolResource { Copied: incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredOntologyResource.java (from r1206244, incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/OntologyIndexResource.java) URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredOntologyResource.java?p2=incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredOntologyResource.java&p1=incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/OntologyIndexResource.java&r1=1206244&r2=1206502&rev=1206502&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/OntologyIndexResource.java (original) +++ incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/StoredOntologyResource.java Sat Nov 26 17:18:41 2011 @@ -16,117 +16,107 @@ */ package org.apache.stanbol.ontologymanager.web.resources; -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + import javax.servlet.ServletContext; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.core.UriInfo; -import org.apache.clerezza.rdf.core.serializedform.Serializer; import org.apache.stanbol.commons.web.base.ContextHelper; +import org.apache.stanbol.commons.web.base.format.KRFormat; import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource; -import org.apache.stanbol.ontologymanager.ontonet.api.ONManager; -import org.apache.stanbol.ontologymanager.ontonet.api.OfflineConfiguration; -import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyIndex; -import org.apache.stanbol.owl.OWLOntologyManagerFactory; -import org.coode.owlapi.turtle.TurtleOntologyFormat; -import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; -import org.semanticweb.owlapi.io.StringDocumentTarget; +import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyProvider; +import org.semanticweb.owlapi.model.AddImport; import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLImportsDeclaration; import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; +import org.semanticweb.owlapi.model.OWLOntologyChange; +import org.semanticweb.owlapi.model.RemoveImport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@Path("/ontonet/ontology/get") -public class OntologyIndexResource extends BaseStanbolResource { +@Path("/ontonet/{ontologyId:.+}") +public class StoredOntologyResource extends BaseStanbolResource { @SuppressWarnings("unused") private Logger log = LoggerFactory.getLogger(getClass()); /* - * Placeholder for the ONManager to be fetched from the servlet context. + * Placeholder for the OntologyProvider to be fetched from the servlet context. */ - protected ONManager onm; - - protected Serializer serializer; + protected OntologyProvider<?> ontologyProvider; - public OntologyIndexResource(@Context ServletContext servletContext) { + public StoredOntologyResource(@PathParam(value = "ontologyId") String ontologyId, + @Context ServletContext servletContext) { this.servletContext = servletContext; - this.onm = (ONManager) ContextHelper.getServiceFromContext(ONManager.class, servletContext); - this.serializer = (Serializer) ContextHelper.getServiceFromContext(Serializer.class, servletContext); + this.ontologyProvider = (OntologyProvider<?>) ContextHelper.getServiceFromContext( + OntologyProvider.class, servletContext); } + /** + * Gets the ontology with the given identifier in its version managed by the session. + * + * @param sessionId + * the session identifier. + * @param ontologyId + * the ontology identifier. + * @param uriInfo + * @param headers + * @return the requested managed ontology, or {@link Status#NOT_FOUND} if either the sessionn does not + * exist, or the if the ontology either does not exist or is not managed. + */ @GET - @Produces("application/rdf+xml") - public Response getOntology(@QueryParam("iri") String ontologyIri) { - - OWLOntologyManager tmpmgr; - OfflineConfiguration offline = (OfflineConfiguration) ContextHelper.getServiceFromContext( - OfflineConfiguration.class, servletContext); - if (offline == null) throw new IllegalStateException("OfflineConfiguration missing in ServletContext"); - else tmpmgr = OWLOntologyManagerFactory.createOWLOntologyManager(offline.getOntologySourceLocations() - .toArray(new IRI[0])); - - IRI iri = null; - try { - iri = IRI.create(ontologyIri); - } catch (Exception ex) { - throw new WebApplicationException(NOT_FOUND); - } - OntologyIndex index = onm.getOntologyIndex(); - if (!index.isOntologyLoaded(iri)) - // No such ontology registered, so return 404. - return Response.status(NOT_FOUND).build(); - - OWLOntology ont = index.getOntology(iri); - - StringDocumentTarget tgt = new StringDocumentTarget(); + @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, KRFormat.FUNCTIONAL_OWL, + KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON}) + public Response getManagedOntology(@PathParam("ontologyId") String ontologyId, + @DefaultValue("false") @QueryParam("merge") boolean merged, + @Context UriInfo uriInfo, + @Context HttpHeaders headers) { + if (ontologyId == null) return Response.status(Status.BAD_REQUEST).build(); try { - tmpmgr.saveOntology(ont, new RDFXMLOntologyFormat(), tgt); - } catch (OWLOntologyStorageException e) { - throw new WebApplicationException(INTERNAL_SERVER_ERROR); - } - return Response.ok(tgt.toString()).build(); - } - - @GET - @Produces("text/turtle") - public Response getOntologyT(@QueryParam("iri") String ontologyIri) { + IRI iri = IRI.create(ontologyId); + OWLOntology o = (OWLOntology) ontologyProvider.getStoredOntology(iri, OWLOntology.class, merged); + if (o == null) return Response.status(NOT_FOUND).build(); + + // Rewrite imports + String uri = uriInfo.getRequestUri().toString(); + URI base = URI.create(uri.substring(0, uri.lastIndexOf(ontologyId) - 1)); + + // Rewrite import statements + List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); + OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory(); + /* + * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of + * going upOne() add "session" or "ontology" if needed). + */ + for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) { + changes.add(new RemoveImport(o, oldImp)); + String s = oldImp.getIRI().toString(); + s = s.substring(s.indexOf("::") + 2, s.length()); + IRI target = IRI.create(base + "/" + s); + changes.add(new AddImport(o, df.getOWLImportsDeclaration(target))); + } + o.getOWLOntologyManager().applyChanges(changes); - OWLOntologyManager tmpmgr; - OfflineConfiguration offline = (OfflineConfiguration) ContextHelper.getServiceFromContext( - OfflineConfiguration.class, servletContext); - if (offline == null) throw new IllegalStateException("OfflineConfiguration missing in ServletContext"); - else tmpmgr = OWLOntologyManagerFactory.createOWLOntologyManager(offline.getOntologySourceLocations() - .toArray(new IRI[0])); - - IRI iri = null; - try { - iri = IRI.create(ontologyIri); + return Response.ok(o).build(); } catch (Exception ex) { - throw new WebApplicationException(NOT_FOUND); + return Response.status(Status.BAD_REQUEST).build(); } - OntologyIndex index = onm.getOntologyIndex(); - if (!index.isOntologyLoaded(iri)) - // No such ontology registered, so return 404. - return Response.status(NOT_FOUND).build(); - OWLOntology ont = index.getOntology(iri); - StringDocumentTarget tgt = new StringDocumentTarget(); - try { - tmpmgr.saveOntology(ont, new TurtleOntologyFormat(), tgt); - } catch (OWLOntologyStorageException e) { - throw new WebApplicationException(INTERNAL_SERVER_ERROR); - } - return Response.ok(tgt.toString()).build(); } } Modified: incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/util/URIUtils.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/util/URIUtils.java?rev=1206502&r1=1206501&r2=1206502&view=diff ============================================================================== --- incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/util/URIUtils.java (original) +++ incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/util/URIUtils.java Sat Nov 26 17:18:41 2011 @@ -32,16 +32,30 @@ public class URIUtils { public static IRI upOne(IRI iri) { return upOne(iri.toURI()); } - + public static IRI createIRI(UriRef uri) { return IRI.create(uri.getUnicodeString()); } - + public static UriRef createUriRef(IRI iri) { return new UriRef(iri.toString()); } /** + * Strips terminating hashes. + * + * @param iri + * @return + */ + public static IRI sanitizeID(IRI iri) { + if (iri == null) throw new IllegalArgumentException("Cannot sanitize null IRI."); + String s = iri.toString(); + while (s.endsWith("#")) + s = s.substring(0, s.length() - 1); + return IRI.create(s); + } + + /** * Removes either the fragment, or query, or last path component from a URI, whatever it finds first. * * @param uri Modified: incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/web/KRFormat.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/web/KRFormat.java?rev=1206502&r1=1206501&r2=1206502&view=diff ============================================================================== --- incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/web/KRFormat.java (original) +++ incubator/stanbol/trunk/owl/src/main/java/org/apache/stanbol/owl/web/KRFormat.java Sat Nov 26 17:18:41 2011 @@ -1,19 +1,19 @@ /* -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You under the Apache License, Version 2.0 -* (the "License"); you may not use this file except in compliance with -* the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.stanbol.owl.web; import javax.ws.rs.core.MediaType; @@ -27,28 +27,36 @@ import javax.ws.rs.core.MediaType; */ public class KRFormat extends MediaType { - public static final String RDF_XML = "application/rdf+xml"; + public static final String FUNCTIONAL_OWL = "application/functional+owl"; - public static final MediaType RDF_XML_TYPE = new MediaType("application", "rdf+xml"); + public static final MediaType FUNCTIONAL_OWL_TYPE = new MediaType("application", "functional+xml"); + + public static final String MANCHESTER_OWL = "application/manchester+owl"; + + public static final MediaType MANCHESTER_OWL_TYPE = new MediaType("application", "manchester+xml"); public static final String OWL_XML = "application/owl+xml"; public static final MediaType OWL_XML_TYPE = new MediaType("application", "owl+xml"); - public static final String MANCHESTER_OWL = "application/manchester+owl"; + public static final String RDF_JSON = "application/rdf+json"; - public static final MediaType MANCHESTER_OWL_TYPE = new MediaType("application", "manchester+xml"); + public static final MediaType RDF_JSON_TYPE = new MediaType("application", "rdf+json"); - public static final String FUNCTIONAL_OWL = "application/functional+owl"; + public static final String RDF_N3 = "text/rdf+n3"; - public static final MediaType FUNCTIONAL_OWL_TYPE = new MediaType("application", "functional+xml"); + public static final MediaType RDF_N3_TYPE = new MediaType("text", "rdf+n3"); - public static final String TURTLE = "application/turtle"; + public static final String RDF_NT = "text/rdf+nt"; - public static final MediaType TURTLE_TYPE = new MediaType("application", "turtle"); + public static final MediaType RDF_NT_TYPE = new MediaType("text", "rdf+nt"); - public static final String RDF_JSON = "application/rdf+json"; + public static final String RDF_XML = "application/rdf+xml"; - public static final MediaType RDF_JSON_TYPE = new MediaType("application", "rdf+json"); + public static final MediaType RDF_XML_TYPE = new MediaType("application", "rdf+xml"); + + public static final String TURTLE = "application/turtle"; + + public static final MediaType TURTLE_TYPE = new MediaType("application", "turtle"); }
