You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by su...@apache.org on 2012/12/13 15:47:28 UTC

svn commit: r1421318 - in /stanbol/trunk/contenthub/web/src/main: java/org/apache/stanbol/contenthub/web/resources/ resources/templates/imports/

Author: suat
Date: Thu Dec 13 14:47:27 2012
New Revision: 1421318

URL: http://svn.apache.org/viewvc?rev=1421318&view=rev
Log:
STANBOL-840:
-Enabled CORS for all responses returning error codes e.g NOT_FOUND or BAD_REQUEST
-Added two additional method to the SemanticIndexManagerResource so that the parameters of the services provided by this resources are compatible. For instance while deleting an LDPath program, it is possible to specify the name of the program either as a QueryParam or a PathParam.
-Added a format parameter to the /metadata endpoint so that the metadata could be retrieved in the desired format

Modified:
    stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/SemanticIndexManagerResource.java
    stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java
    stanbol/trunk/contenthub/web/src/main/resources/templates/imports/ldpathrestapi.ftl
    stanbol/trunk/contenthub/web/src/main/resources/templates/imports/storerestapi.ftl

Modified: stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/SemanticIndexManagerResource.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/SemanticIndexManagerResource.java?rev=1421318&r1=1421317&r2=1421318&view=diff
==============================================================================
--- stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/SemanticIndexManagerResource.java (original)
+++ stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/SemanticIndexManagerResource.java Thu Dec 13 14:47:27 2012
@@ -17,7 +17,9 @@
 package org.apache.stanbol.contenthub.web.resources;
 
 import static javax.ws.rs.HttpMethod.DELETE;
+import static javax.ws.rs.HttpMethod.GET;
 import static javax.ws.rs.HttpMethod.OPTIONS;
+import static javax.ws.rs.HttpMethod.POST;
 import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
 import static org.apache.stanbol.commons.web.base.CorsHelper.addCORSOrigin;
 import static org.apache.stanbol.commons.web.base.CorsHelper.enableCORS;
@@ -89,7 +91,7 @@ public class SemanticIndexManagerResourc
     @Path("/program")
     public Response handleCorsPreflightProgram(@Context HttpHeaders headers) {
         ResponseBuilder res = Response.ok();
-        enableCORS(servletContext, res, headers);
+        enableCORS(servletContext, res, headers, GET, DELETE, OPTIONS, POST);
         return res.build();
     }
 
@@ -97,7 +99,7 @@ public class SemanticIndexManagerResourc
     @Path("/program/{name}")
     public Response handleCorsPreflightDeleteProgram(@Context HttpHeaders headers) {
         ResponseBuilder res = Response.ok();
-        enableCORS(servletContext, res, headers, DELETE, OPTIONS);
+        enableCORS(servletContext, res, headers, GET, DELETE, OPTIONS);
         return res.build();
     }
 
@@ -109,6 +111,14 @@ public class SemanticIndexManagerResourc
         return res.build();
     }
 
+    @OPTIONS
+    @Path("/exists/{name}")
+    public Response handleCorsPreflightExistsPath(@Context HttpHeaders headers) {
+        ResponseBuilder res = Response.ok();
+        enableCORS(servletContext, res, headers);
+        return res.build();
+    }
+
     /**
      * HTTP GET method which returns all LDPath programs residing in Contenthub. LDPath programs are uniquely
      * identified by their names. Returning JSON string presents each LDPath program in string format aligned
@@ -156,10 +166,12 @@ public class SemanticIndexManagerResourc
         programName = RestUtil.nullify(programName);
         program = RestUtil.nullify(program);
         if (programName == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'name' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'name' parameter",
+                headers);
         }
         if (program == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'program' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'program' parameter",
+                headers);
         }
         programManager.submitProgram(programName, program);
         ResponseBuilder rb = Response.created(new URI(uriInfo.getBaseUri() + "contenthub/" + programName
@@ -182,19 +194,48 @@ public class SemanticIndexManagerResourc
     public Response getProgramByName(@QueryParam("name") String programName, @Context HttpHeaders headers) {
         programName = RestUtil.nullify(programName);
         if (programName == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'name' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'name' parameter",
+                headers);
         }
         String ldPathProgram = programManager.getProgramByName(programName);
         if (ldPathProgram == null) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         } else {
-            ResponseBuilder rb = Response.ok(ldPathProgram);
-            addCORSOrigin(servletContext, rb, headers);
-            return rb.build();
+            return RestUtil.createResponse(servletContext, Status.OK, ldPathProgram, headers);
         }
     }
 
     /**
+     * HTTP GET method to retrieve an LDPath program, given its name.
+     * 
+     * @param programName
+     *            The name of the LDPath program to be retrieved.
+     * @param headers
+     *            HTTP headers
+     * @return LDPath program in {@link String} format or HTTP BAD_REQUEST(400) or HTTP NOT FOUND(404)
+     */
+    @GET
+    @Path("/program/{name}")
+    public Response getProgramByNamePath(@PathParam("name") String programName, @Context HttpHeaders headers) {
+        return getProgramByName(programName, headers);
+    }
+
+    /**
+     * HTTP DELETE method to delete an LDPath program.
+     * 
+     * @param programName
+     *            The name of the LDPath program.
+     * @param headers
+     *            HTTP headers
+     * @return HTTP OK(200), HTTP BAD_REQUEST(400) or HTTP NOT FOUND(403)
+     */
+    @DELETE
+    @Path("/program")
+    public Response deleteProgram(@QueryParam(value = "name") String programName, @Context HttpHeaders headers) {
+        return deleteProgramPath(programName, headers);
+    }
+
+    /**
      * HTTP DELETE method to delete an LDPath program.
      * 
      * @param programName
@@ -205,18 +246,18 @@ public class SemanticIndexManagerResourc
      */
     @DELETE
     @Path("/program/{name}")
-    public Response deleteProgram(@PathParam(value = "name") String programName, @Context HttpHeaders headers) {
+    public Response deleteProgramPath(@PathParam(value = "name") String programName,
+                                      @Context HttpHeaders headers) {
         programName = RestUtil.nullify(programName);
         if (programName == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'name' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'name' parameter",
+                headers);
         }
         if (!programManager.isManagedProgram(programName)) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
         programManager.deleteProgram(programName);
-        ResponseBuilder rb = Response.ok();
-        addCORSOrigin(servletContext, rb, headers);
-        return rb.build();
+        return RestUtil.createResponse(servletContext, Status.OK, null, headers);
     }
 
     /**
@@ -233,19 +274,31 @@ public class SemanticIndexManagerResourc
     public Response isManagedProgram(@QueryParam("name") String programName, @Context HttpHeaders headers) {
         programName = RestUtil.nullify(programName);
         if (programName == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'name' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'name' parameter",
+                headers);
         }
         if (programManager.isManagedProgram(programName)) {
-            ResponseBuilder rb = Response.ok();
-            addCORSOrigin(servletContext, rb, headers);
-            return rb.build();
+            return RestUtil.createResponse(servletContext, Status.OK, null, headers);
         } else {
-            ResponseBuilder rb = Response.status(Status.NOT_FOUND);
-            addCORSOrigin(servletContext, rb, headers);
-            return rb.build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
     }
 
+    /**
+     * HTTP GET method to check whether an LDPath program exists in Contenthub or not.
+     * 
+     * @param programName
+     *            The name of the LDPath program.
+     * @param headers
+     *            HTTP headers
+     * @return HTTP OK(200), HTTP BAD REQUEST(400) or HTTP NOT FOUND(404)
+     */
+    @GET
+    @Path("/exists/{name}")
+    public Response isManagedProgramPath(@PathParam("name") String programName, @Context HttpHeaders headers) {
+        return isManagedProgram(programName, headers);
+    }
+
     // Helper methods for HTML view
     public List<LDProgram> getLdPrograms() {
         return programManager.retrieveAllPrograms().asList();

Modified: stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java?rev=1421318&r1=1421317&r2=1421318&view=diff
==============================================================================
--- stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java (original)
+++ stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java Thu Dec 13 14:47:27 2012
@@ -247,11 +247,12 @@ public class StoreResource extends BaseS
     public Response getContent(@PathParam(value = "uri") String uri, @Context HttpHeaders headers) throws StoreException {
         uri = RestUtil.nullify(uri);
         if (uri == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'uri' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'uri' parameter",
+                headers);
         }
         ContentItem ci = solrStore.get(uri, indexName);
         if (ci == null) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
 
         // handle smart redirection to browser view
@@ -269,7 +270,7 @@ public class StoreResource extends BaseS
         for (MediaType mt : headers.getAcceptableMediaTypes()) {
             if (RDF_MEDIA_TYPES.contains(mt.toString())) {
                 URI metadataUri = uriInfo.getBaseUriBuilder().path("/contenthub").path(indexName)
-                        .path("store/metadata").path(uri).build();
+                        .path("store/metadata").path(uri).queryParam("format", mt.toString()).build();
                 ResponseBuilder rb = Response.temporaryRedirect(metadataUri);
                 addCORSOrigin(servletContext, rb, headers);
                 return rb.build();
@@ -307,14 +308,16 @@ public class StoreResource extends BaseS
         uri = RestUtil.nullify(uri);
         format = RestUtil.nullify(format);
         if (type == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'type' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'type' parameter",
+                headers);
         }
         if (uri == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'uri' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'uri' parameter",
+                headers);
         }
         ContentItem ci = solrStore.get(uri, indexName);
         if (ci == null) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
         if (type.equals("metadata")) {
             String fileName = URLEncoder.encode(uri, "utf-8") + "-metadata";
@@ -352,19 +355,21 @@ public class StoreResource extends BaseS
      */
     @GET
     @Path("/metadata/{uri:.+}")
-    public Response getContentItemMetaData(@PathParam(value = "uri") String uri, @Context HttpHeaders headers) throws IOException,
-                                                                                                              StoreException {
+    public Response getContentItemMetaData(@PathParam(value = "uri") String uri,
+                                           @QueryParam(value = "format") @DefaultValue(SupportedFormat.RDF_XML) String format,
+                                           @Context HttpHeaders headers) throws IOException, StoreException {
         uri = RestUtil.nullify(uri);
         if (uri == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'uri' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'uri' parameter",
+                headers);
         }
         ContentItem ci = solrStore.get(uri, indexName);
         if (ci == null) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        serializer.serialize(out, ci.getMetadata(), SupportedFormat.RDF_XML);
+        serializer.serialize(out, ci.getMetadata(), format);
         ResponseBuilder rb = Response.ok(out.toString(), "text/plain");
         addCORSOrigin(servletContext, rb, headers);
         return rb.build();
@@ -385,11 +390,12 @@ public class StoreResource extends BaseS
                                                                                                      StoreException {
         uri = RestUtil.nullify(uri);
         if (uri == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'uri' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'uri' parameter",
+                headers);
         }
         ContentItem ci = solrStore.get(uri, indexName);
         if (ci == null) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
         ResponseBuilder rb = Response.ok(ci.getStream(), ci.getMimeType());
         addCORSOrigin(servletContext, rb, headers);
@@ -634,10 +640,6 @@ public class StoreResource extends BaseS
     @DELETE
     public Response deleteContentItemByForm(@QueryParam(value = "uri") String uri,
                                             @Context HttpHeaders headers) throws StoreException {
-        uri = RestUtil.nullify(uri);
-        if (uri == null) {
-            throw new IllegalArgumentException("Missing 'uri' parameter");
-        }
         return deleteContentItem(uri, headers);
     }
 
@@ -654,16 +656,15 @@ public class StoreResource extends BaseS
     public Response deleteContentItem(@PathParam(value = "uri") String uri, @Context HttpHeaders headers) throws StoreException {
         uri = RestUtil.nullify(uri);
         if (uri == null) {
-            return Response.status(Status.BAD_REQUEST).entity("Missing 'uri' parameter").build();
+            return RestUtil.createResponse(servletContext, Status.BAD_REQUEST, "Missing 'uri' parameter",
+                headers);
         }
         ContentItem ci = solrStore.get(uri, indexName);
         if (ci == null) {
-            return Response.status(Status.NOT_FOUND).build();
+            return RestUtil.createResponse(servletContext, Status.NOT_FOUND, null, headers);
         }
         solrStore.deleteById(uri, indexName);
-        ResponseBuilder rb = Response.ok();
-        addCORSOrigin(servletContext, rb, headers);
-        return rb.build();
+        return RestUtil.createResponse(servletContext, Status.OK, null, headers);
     }
 
     /*

Modified: stanbol/trunk/contenthub/web/src/main/resources/templates/imports/ldpathrestapi.ftl
URL: http://svn.apache.org/viewvc/stanbol/trunk/contenthub/web/src/main/resources/templates/imports/ldpathrestapi.ftl?rev=1421318&r1=1421317&r2=1421318&view=diff
==============================================================================
--- stanbol/trunk/contenthub/web/src/main/resources/templates/imports/ldpathrestapi.ftl (original)
+++ stanbol/trunk/contenthub/web/src/main/resources/templates/imports/ldpathrestapi.ftl Thu Dec 13 14:47:27 2012
@@ -22,7 +22,7 @@
 <tbody>
   <tr>
     <th>Description</th>
-    <td>HTTP GET method which returns all LDPath programs residing in Contenthub. LDPath programs are uniquely dentified by their names. Returning JSON string presents each LDPath program in string format aligned with its name.</td>
+    <td>HTTP GET method which returns all LDPath programs residing in Contenthub. LDPath programs are uniquely dentified by their names. Returning JSON string presents each LDPath program in string format aligned with its name. However, to get the proper response <b>Accept</b> header should be set as <b>application/json</b></td>
   </tr>
   <tr>
     <th>Request</th>
@@ -30,7 +30,7 @@
   </tr>
   <tr>
     <th>Produces</th>
-    <td>JSON string of <code>name:program</code> pairs.</td>
+    <td>HTTP 200 (OK) together with the JSON representation of <code>name:program</code> pairs of LDPath programs.</td>
   </tr>
 </tbody>
 </table>
@@ -45,7 +45,7 @@
 <tbody>
   <tr>
     <th>Description</th>
-    <td>HTTP POST method which saves an LDPath program into the persistent store of Contenthub.</td>
+    <td>HTTP POST method which saves an LDPath program into the persistent store of Contenthub. Currently this service only consumes requests having <b>application/x-www-form-urlencoded</b> as the value of <b>Content-Type</b> header.</td>
   </tr>
   <tr>
     <th>Request</th>
@@ -60,11 +60,12 @@
   </tr>
   <tr>
     <th>Produces</th>
-    <td>HTTP OK(200) or BAD REQUEST(400)</td>
-  </tr>
-  <tr>
-    <th>Throws</th>
-    <td>LDPathException</td>
+    <td>
+      <ul>
+        <li>HTTP 201 (CREATED) together with the full URI representing the new index in the Contenthub.</li>
+        <li>HTTP 400 (BAD REQUEST) if one of the required parameters is missing in the request.</li>
+       </ul>
+    </td>
   </tr>
 </tbody>
 </table>
@@ -93,7 +94,7 @@
   </tr>
   <tr>
     <th>Request</th>
-    <td>GET /contenthub/ldpath/program</td>
+    <td>GET /contenthub/ldpath/program/{name}, contenthub/ldpath/program</td>
   </tr>
   <tr>
     <th>Parameter</th>
@@ -101,11 +102,17 @@
   </tr>
   <tr>
     <th>Produces</th>
-    <td>LDPath program in String format or HTTP NOT FOUND(404)</td>
+    <td>
+      <ul>
+        <li>HTTP 200 (OK) together with the actual LDPath program.</li>
+	    <li>HTTP 400 (BAD REQUEST) if there is a missing parameter in the request.</li>
+        <li>HTTP 404 (NOT FOUND) if there is no LDPath program corresponding to the specified name.</li>
+    </td>
   </tr>
 </tbody>
 </table>
 <h4>Example</h4>
+<pre>curl -i "http://localhost:8080/contenthub/ldpath/program/universities"</pre>
 <pre>curl -i "http://localhost:8080/contenthub/ldpath/program?name=universities"</pre>
 
 <hr>
@@ -120,7 +127,7 @@
   </tr>
   <tr>
     <th>Request</th>
-    <td>DELETE /contenthub/ldpath/program/{name}</td>
+    <td>DELETE /contenthub/ldpath/program/{name}, /contenthub/ldpath/program</td>
   </tr>
   <tr>
     <th>Parameter</th>
@@ -128,12 +135,19 @@
   </tr>
   <tr>
     <th>Produces</th>
-    <td>HTTP OK(200)</td>
+    <td>
+      <ul>
+        <li>HTTP 200 (OK) if the existing LDPath program is deleted.</li>
+	    <li>HTTP 400 (BAD REQUEST) if there is a missing parameter in the request.</li>
+        <li>HTTP 404 (NOT FOUND) if there is no LDPath program corresponding to the specified name.</li>
+      </ul>
+    </td>
   </tr>
 </tbody>
 </table>
 <h4>Example</h4>
 <pre>curl -i -X DELETE http://localhost:8080/contenthub/ldpath/program/universities</pre>
+<pre>curl -i -X DELETE http://localhost:8080/contenthub/ldpath/program?name=universities</pre>
 
 <hr>
 
@@ -147,7 +161,7 @@
   </tr>
   <tr>
     <th>Request</th>
-    <td>GET /contenthub/ldpath/exists</td>
+    <td>GET /contenthub/ldpath/exists/{name}, /contenthub/ldpath/exists</td>
   </tr>
   <tr>
     <th>Parameter</th>
@@ -155,11 +169,18 @@
   </tr>
   <tr>
     <th>Produces</th>
-    <td>HTTP OK(200) or HTTP NOT FOUND(404)</td>
+    <td>
+      <ul>
+        <li>HTTP 200 (OK) if an LDPath program exists for the given name.</li>
+	    <li>HTTP 400 (BAD REQUEST) if there is a missing parameter in the request.</li>
+        <li>HTTP 404 (NOT FOUND) if there is no LDPath program corresponding to the specified name.</li>
+      </ul>
+    </td>
   </tr>
 </tbody>
 </table>
 <h4>Example</h4>
+<pre>curl -i http://localhost:8080/contenthub/ldpath/exists/universities</pre>
 <pre>curl -i http://localhost:8080/contenthub/ldpath/exists?name=universities</pre>
 
 <hr>

Modified: stanbol/trunk/contenthub/web/src/main/resources/templates/imports/storerestapi.ftl
URL: http://svn.apache.org/viewvc/stanbol/trunk/contenthub/web/src/main/resources/templates/imports/storerestapi.ftl?rev=1421318&r1=1421317&r2=1421318&view=diff
==============================================================================
--- stanbol/trunk/contenthub/web/src/main/resources/templates/imports/storerestapi.ftl (original)
+++ stanbol/trunk/contenthub/web/src/main/resources/templates/imports/storerestapi.ftl Thu Dec 13 14:47:27 2012
@@ -103,8 +103,7 @@ The following one is an example represen
 <div class="preParent">
   <img onClick="javascript:getLine(this.parentNode);" class="copyImg" src="${it.staticRootUrl}/contenthub/images/copy_icon_16.png" title="Get command in a single line" />
 <pre>
-<div id="curl3" class="curlLine">curl -i -F "content=I live in Berlin.;type=text/plain" "http://localhost:8080/contenthub/contenthub/store?uri=urn:my-content-item3&chain=dbpedia-proper-noun"<hr/></div>curl -i \
-        -F "content=I live in Berlin.;type=text/plain" \
+<div id="curl3" class="curlLine">curl -i -F "content=I live in Berlin.;type=text/plain" "http://localhost:8080/contenthub/myindex/store?uri=urn:my-content-item3&chain=dbpedia-proper-noun"<hr/></div>curl -i -F "content=I live in Berlin.;type=text/plain" \
      "http://localhost:8080/contenthub/myindex/store?uri=urn:my-content-item3&chain=dbpedia-proper-noun"
 </pre>
 </div>