Author: alexdma
Date: Tue Nov 22 23:04:16 2011
New Revision: 1205216

URL: http://svn.apache.org/viewvc?rev=1205216&view=rev
Log:
STANBOL-247 :
- CRUD support for session REST resources (minus U methods to replace managed 
ontologies with other versions)
- Scope REST resource now has the same POST methods for adding ontologies as 
for session resources (supports text content, file upload and IRI dereferencing)

Modified:
    
incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/ScopeResource.java
    
incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionByIdResource.java

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=1205216&r1=1205215&r2=1205216&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 Nov 22 23:04:16 2011
@@ -18,9 +18,12 @@ package org.apache.stanbol.ontologymanag
 
 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
 import static javax.ws.rs.core.Response.Status.CONFLICT;
+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.OK;
 
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -41,6 +44,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 
 import org.apache.stanbol.commons.web.base.ContextHelper;
@@ -49,6 +53,7 @@ import org.apache.stanbol.commons.web.ba
 import org.apache.stanbol.ontologymanager.ontonet.api.DuplicateIDException;
 import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
 import org.apache.stanbol.ontologymanager.ontonet.api.io.BlankOntologySource;
+import 
org.apache.stanbol.ontologymanager.ontonet.api.io.OntologyContentInputSource;
 import org.apache.stanbol.ontologymanager.ontonet.api.io.OntologyInputSource;
 import 
org.apache.stanbol.ontologymanager.ontonet.api.io.OntologySetInputSource;
 import org.apache.stanbol.ontologymanager.ontonet.api.io.RootOntologyIRISource;
@@ -69,7 +74,6 @@ import org.slf4j.LoggerFactory;
 @Path("/ontonet/ontology/{scopeid}")
 public class ScopeResource extends BaseStanbolResource {
 
-    @SuppressWarnings("unused")
     private Logger log = LoggerFactory.getLogger(getClass());
 
     /*
@@ -78,15 +82,32 @@ public class ScopeResource extends BaseS
     protected ONManager onm;
 
     /*
-     * Placeholder for the ONManager to be fetched from the servlet context.
+     * Placeholder for the RegistryManager to be fetched from the servlet 
context.
      */
     protected RegistryManager regMgr;
 
-    public ScopeResource(@Context ServletContext servletContext) {
+    protected OntologyScope scope;
+
+    public ScopeResource(@PathParam(value = "scopeid") String scopeId, 
@Context ServletContext servletContext) {
+        super();
+        log.info("<init> with scope {}", scopeId);
+
         this.servletContext = servletContext;
         this.onm = (ONManager) 
ContextHelper.getServiceFromContext(ONManager.class, servletContext);
         this.regMgr = (RegistryManager) 
ContextHelper.getServiceFromContext(RegistryManager.class,
             servletContext);
+
+        if (scopeId == null || scopeId.isEmpty()) {
+            log.error("Missing path parameter scopeid={}", scopeId);
+            throw new WebApplicationException(NOT_FOUND);
+        }
+        scope = onm.getScopeRegistry().getScope(scopeId);
+        
+        // // Skip null checks: the scope might be created with a PUT
+        // if (scope == null) {
+        // log.error("Scope {} not found", scopeId);
+        // throw new WebApplicationException(NOT_FOUND);
+        // }
     }
 
     @DELETE
@@ -96,9 +117,8 @@ public class ScopeResource extends BaseS
                                 @Context ServletContext servletContext) {
 
         ScopeRegistry reg = onm.getScopeRegistry();
-        OntologyScope scope = reg.getScope(scopeid/* 
IRI.create(uriInfo.getAbsolutePath()) */);
-        if (scope == null) return;
         reg.deregisterScope(scope);
+        scope = null;
     }
 
     @GET
@@ -108,13 +128,66 @@ public class ScopeResource extends BaseS
                                    @Context UriInfo uriInfo,
                                    @Context HttpHeaders headers,
                                    @Context ServletContext servletContext) {
-        ScopeRegistry reg = onm.getScopeRegistry();
-        OntologyScope scope = reg.getScope(scopeid/* 
IRI.create(uriInfo.getAbsolutePath()) */);
         if (scope == null) return Response.status(NOT_FOUND).build();
         else return Response.ok(scope.asOWLOntology()).build();
     }
 
+    /**
+     * Tells the session that it should manage the ontology obtained by 
parsing the supplied content.<br>
+     * <br>
+     * Note that the PUT method cannot be used, as it is not possible to 
predict what ID the ontology will
+     * have until it is parsed.
+     * 
+     * @param content
+     *            the ontology content
+     * @return {@link Status#OK} if the addition was successful, {@link 
Status#NOT_FOUND} if there is no such
+     *         session at all, {@link Status#FORBIDDEN} if the session is 
locked or cannot modified for some
+     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some 
other error occurs.
+     */
+    @POST
+    @Consumes(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, 
KRFormat.FUNCTIONAL_OWL,
+                       KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response manageOntology(InputStream content) {
+        if (scope == null) return Response.status(NOT_FOUND).build();
+        try {
+            scope.getCustomSpace().addOntology(new 
OntologyContentInputSource(content));
+        } catch (UnmodifiableOntologyCollectorException e) {
+            throw new WebApplicationException(e, FORBIDDEN);
+        } catch (OWLOntologyCreationException e) {
+            throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
+        }
+        return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
+    }
+
+    /**
+     * Tells the session that it should manage the ontology obtained by 
dereferencing the supplied IRI.<br>
+     * <br>
+     * Note that the PUT method cannot be used, as it is not possible to 
predict what ID the ontology will
+     * have until it is parsed.
+     * 
+     * @param content
+     *            the ontology physical IRI
+     * @return {@link Status#OK} if the addition was successful, {@link 
Status#NOT_FOUND} if there is no such
+     *         session at all, {@link Status#FORBIDDEN} if the session is 
locked or cannot modified for some
+     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some 
other error occurs.
+     */
     @POST
+    @Consumes(value = MediaType.TEXT_PLAIN)
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response manageOntology(String iri) {
+        if (scope == null) return Response.status(NOT_FOUND).build();
+        try {
+            scope.getCustomSpace().addOntology(new 
RootOntologyIRISource(IRI.create(iri)));
+        } catch (UnmodifiableOntologyCollectorException e) {
+            throw new WebApplicationException(e, FORBIDDEN);
+        } catch (OWLOntologyCreationException e) {
+            throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
+        }
+        return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
+    }
+
+    // @POST
     // @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     public Response loadCustomOntology(@PathParam("scopeid") String scopeid,
                                        @FormParam("location") String physIri,

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=1205216&r1=1205215&r2=1205216&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
 Tue Nov 22 23:04:16 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.stanbol.ontologymanager.web.resources;
 
+import static javax.ws.rs.core.Response.Status.CONFLICT;
 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;
@@ -25,8 +26,10 @@ import java.io.InputStream;
 
 import javax.servlet.ServletContext;
 import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 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;
@@ -35,6 +38,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 
 import org.apache.stanbol.commons.web.base.ContextHelper;
@@ -43,13 +47,22 @@ import org.apache.stanbol.commons.web.ba
 import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
 import 
org.apache.stanbol.ontologymanager.ontonet.api.io.OntologyContentInputSource;
 import org.apache.stanbol.ontologymanager.ontonet.api.io.RootOntologyIRISource;
+import 
org.apache.stanbol.ontologymanager.ontonet.api.ontology.IrremovableOntologyException;
+import 
org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyCollectorModificationException;
 import 
org.apache.stanbol.ontologymanager.ontonet.api.ontology.UnmodifiableOntologyCollectorException;
+import 
org.apache.stanbol.ontologymanager.ontonet.api.session.DuplicateSessionIDException;
 import org.apache.stanbol.ontologymanager.ontonet.api.session.Session;
 import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionManager;
 import org.semanticweb.owlapi.model.IRI;
 import org.semanticweb.owlapi.model.OWLOntology;
 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
 
+/**
+ * The REST resource of an OntoNet {@link Session} whose identifier is known.
+ * 
+ * @author alexdma
+ * 
+ */
 @Path("/ontonet/session/{id}")
 public class SessionByIdResource extends BaseStanbolResource {
 
@@ -60,32 +73,126 @@ public class SessionByIdResource extends
 
     protected ServletContext servletContext;
 
+    protected SessionManager sesMgr;
+
     protected Session session;
 
     public SessionByIdResource(@PathParam(value = "id") String sessionId,
                                @Context ServletContext servletContext) {
         this.servletContext = servletContext;
         this.onm = (ONManager) 
ContextHelper.getServiceFromContext(ONManager.class, servletContext);
-        SessionManager mgr = onm.getSessionManager();
-        session = mgr.getSession(sessionId);
+        sesMgr = onm.getSessionManager();
+        session = sesMgr.getSession(sessionId);
     }
 
+    /**
+     * Gets the OWL ontology form of the session.
+     * 
+     * @param sessionId
+     *            the session identifier.
+     * @param uriInfo
+     * @param headers
+     * @return the ontology if the session exists, otherwise {@link 
Status#NOT_FOUND}.
+     */
     @GET
     @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, 
KRFormat.FUNCTIONAL_OWL,
                        KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
-    public Response getSessionAsOntology(@PathParam("id") String sessionId,
-                                         @Context UriInfo uriInfo,
-                                         @Context HttpHeaders headers) {
+    public Response asOntology(@PathParam("id") String sessionId,
+                               @Context UriInfo uriInfo,
+                               @Context HttpHeaders headers) {
         if (session == null) return Response.status(NOT_FOUND).build();
-        // return 
Response.ok(SessionRenderer.getSessionMetadataRDFasOntology(ses)).build();
         return Response.ok(session.asOWLOntology(false)).build();
     }
 
+    /**
+     * Used to create an OntoNet session with a specified identifier.
+     * 
+     * @param sessionId
+     *            the identifier of the session to be created.
+     * @param uriInfo
+     * @param headers
+     * @return {@link Status#OK} if the creation was successful, or {@link 
Status#CONFLICT} if a session with
+     *         that ID already exists.
+     */
+    @PUT
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response createSession(@PathParam("id") String sessionId,
+                                  @Context UriInfo uriInfo,
+                                  @Context HttpHeaders headers) {
+        try {
+            session = sesMgr.createSession(sessionId);
+        } catch (DuplicateSessionIDException e) {
+            throw new WebApplicationException(e, CONFLICT);
+        }
+        return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
+    }
+
+    /**
+     * Destroys the session and unmanages its ontologies (which are also lost 
unless stored).
+     * 
+     * @param sessionId
+     *            the session identifier
+     * @param uriInfo
+     * @param headers
+     * @return {@link Status#OK} if the deletion was successful, {@link 
Status#NOT_FOUND} if there is no such
+     *         session at all.
+     */
+    @DELETE
+    @Consumes(MediaType.WILDCARD)
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response deleteSession(@PathParam("id") String sessionId,
+                                  @Context UriInfo uriInfo,
+                                  @Context HttpHeaders headers) {
+        if (session == null) return Response.status(NOT_FOUND).build();
+        sesMgr.destroySession(sessionId);
+        session = null;
+        return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
+    }
+
+    /**
+     * 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
+    @Path(value = "/{ontologyId:.+}")
+    @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, 
KRFormat.FUNCTIONAL_OWL,
+                       KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
+    public Response getManagedOntology(@PathParam("id") String sessionId,
+                                       @PathParam("ontologyId") String 
ontologyId,
+                                       @Context UriInfo uriInfo,
+                                       @Context HttpHeaders headers) {
+        if (session == null) return Response.status(NOT_FOUND).build();
+        OWLOntology o = session.getOntology(IRI.create(ontologyId));
+        if (o == null) return Response.status(NOT_FOUND).build();
+        return Response.ok(o).build();
+    }
+
+    /**
+     * Tells the session that it should manage the ontology obtained by 
parsing the supplied content.<br>
+     * <br>
+     * Note that the PUT method cannot be used, as it is not possible to 
predict what ID the ontology will
+     * have until it is parsed.
+     * 
+     * @param content
+     *            the ontology content
+     * @return {@link Status#OK} if the addition was successful, {@link 
Status#NOT_FOUND} if there is no such
+     *         session at all, {@link Status#FORBIDDEN} if the session is 
locked or cannot modified for some
+     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some 
other error occurs.
+     */
     @POST
     @Consumes(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, 
KRFormat.FUNCTIONAL_OWL,
                        KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
     @Produces(MediaType.TEXT_PLAIN)
-    public Response addOntology(InputStream content) {
+    public Response manageOntology(InputStream content) {
+        if (session == null) return Response.status(NOT_FOUND).build();
         try {
             session.addOntology(new OntologyContentInputSource(content));
         } catch (UnmodifiableOntologyCollectorException e) {
@@ -96,10 +203,23 @@ public class SessionByIdResource extends
         return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
     }
 
+    /**
+     * Tells the session that it should manage the ontology obtained by 
dereferencing the supplied IRI.<br>
+     * <br>
+     * Note that the PUT method cannot be used, as it is not possible to 
predict what ID the ontology will
+     * have until it is parsed.
+     * 
+     * @param content
+     *            the ontology physical IRI
+     * @return {@link Status#OK} if the addition was successful, {@link 
Status#NOT_FOUND} if there is no such
+     *         session at all, {@link Status#FORBIDDEN} if the session is 
locked or cannot modified for some
+     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some 
other error occurs.
+     */
     @POST
     @Consumes(value = MediaType.TEXT_PLAIN)
     @Produces(MediaType.TEXT_PLAIN)
-    public Response addOntology(String iri) {
+    public Response manageOntology(String iri) {
+        if (session == null) return Response.status(NOT_FOUND).build();
         try {
             session.addOntology(new RootOntologyIRISource(IRI.create(iri)));
         } catch (UnmodifiableOntologyCollectorException e) {
@@ -110,17 +230,43 @@ public class SessionByIdResource extends
         return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
     }
 
-    @GET
+    /**
+     * Tells the session to no longer manage the ontology with the supplied 
<i>logical</i> identifier. The
+     * ontology will be lost if not stored or not managed by another collector.
+     * 
+     * @param sessionId
+     *            the session identifier.
+     * @param ontologyId
+     *            the ontology identifier.
+     * @param uriInfo
+     * @param headers
+     * @return {@link Status#OK} if the removal was successful, {@link 
Status#NOT_FOUND} if there is no such
+     *         session at all, {@link Status#FORBIDDEN} if the session or the 
ontology is locked or cannot
+     *         modified for some other reason, {@link 
Status#INTERNAL_SERVER_ERROR} if some other error
+     *         occurs.
+     */
+    @DELETE
     @Path(value = "/{ontologyId:.+}")
-    @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE, 
KRFormat.FUNCTIONAL_OWL,
-                       KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
-    public Response getSessionOntology(@PathParam("id") String sessionId,
-                                       @PathParam("ontologyId") String 
ontologyId,
-                                       @Context UriInfo uriInfo,
-                                       @Context HttpHeaders headers) {
-        OWLOntology o = session.getOntology(IRI.create(ontologyId));
-        if (o == null) return Response.status(NOT_FOUND).build();
-        return Response.ok(o).build();
+    @Consumes(MediaType.WILDCARD)
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response unmanageOntology(@PathParam("id") String sessionId,
+                                     @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);
+        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);
+        }
+        return Response.status(OK).type(MediaType.TEXT_PLAIN).build();
     }
 
 }


Reply via email to