Author: rwesten
Date: Fri Oct 14 09:12:26 2011
New Revision: 1183251

URL: http://svn.apache.org/viewvc?rev=1183251&view=rev
Log:
fixes STANBOL-308: In contrast to the description of the issue for PUT (update) 
requests the default parameter for create is "true". Also updated the 
documentation of the RESTful API accordingly.

Modified:
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/resources/org/apache/stanbol/entityhub/jersey/templates/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource/inc_entity.ftl

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java?rev=1183251&r1=1183250&r2=1183251&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
 Fri Oct 14 09:12:26 2011
@@ -50,8 +50,10 @@ import java.util.Set;
 import javax.servlet.ServletContext;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
 import javax.ws.rs.FormParam;
 import javax.ws.rs.GET;
+import javax.ws.rs.HttpMethod;
 import javax.ws.rs.OPTIONS;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
@@ -225,7 +227,8 @@ public class EntityhubRootResource exten
     @POST
     @Path("entity/")
     @Consumes(MediaType.WILDCARD)
-    public Response createEntity(@QueryParam(value = "id") String id, 
+    public Response createEntity(@QueryParam(value = "id") String id,
+                                 @QueryParam(value = "update") boolean 
allowUpdate,
                                Set<Representation> parsed,
                                @Context HttpHeaders headers){
         //Set<Representation> representations = Collections.emptySet();
@@ -233,13 +236,14 @@ public class EntityhubRootResource exten
         log.info("Headers: "+headers.getRequestHeaders());
         log.info("Entity: "+id);
         log.info("Representations : "+parsed);
-        return updateOrCreateEntity(id, parsed, true,headers);
+        return updateOrCreateEntity(id, parsed, HttpMethod.POST, 
true,allowUpdate,headers);
     }
 
     @PUT
     @Path("entity/")
     @Consumes(MediaType.WILDCARD)
     public Response updateEntity(@QueryParam(value = "id") String id, 
+                                 @QueryParam(value = "create") 
@DefaultValue("true") boolean allowCreate,
                                Set<Representation> parsed,
                                @Context HttpHeaders headers){
         //Set<Representation> representations = Collections.emptySet();
@@ -247,7 +251,7 @@ public class EntityhubRootResource exten
         log.info("Headers: "+headers.getRequestHeaders());
         log.info("Entity: "+id);
         log.info("Representations : "+parsed);
-        return updateOrCreateEntity(id, parsed, false,  headers);
+        return updateOrCreateEntity(id, parsed, HttpMethod.PUT, allowCreate, 
true, headers);
     }
     
     @DELETE
@@ -287,12 +291,18 @@ public class EntityhubRootResource exten
      * <code>null</code> all parsed Representations with other
      * ids will be ignored.
      * @param parsed the parsed representation(s)
-     * @param createState create or update request
+     * @param method the {@link HttpMethod} used by the reuqest. Needed to 
create
+     * the correct response.
+     * @param create allow to create new Entities
+     * @param update allow to update existing Entities
      * @param headers the HTTP headers of the request
      * @return the created/updated representation as response
      */
     private Response updateOrCreateEntity(String id,Set<Representation> 
parsed, 
-                                          boolean createState, HttpHeaders 
headers){
+                                          String method,
+                                          boolean create, 
+                                          boolean update,
+                                          HttpHeaders headers){
         MediaType accepted = JerseyUtils.getAcceptableMediaType(headers,
             JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES, 
             MediaType.APPLICATION_JSON_TYPE);
@@ -314,27 +324,34 @@ public class EntityhubRootResource exten
             }
         }
         //First check if all parsed Representation can be created/updated
-        for(Representation representation : parsed){
-            boolean exists;
-            try {
-                exists = entityhub.isRepresentation(representation.getId());
-            } catch (EntityhubException e) {
-                log.error(String.format("Exception while checking the 
existance " +
-                    "of an Entity with id  %s in the Entityhub.",
-                    representation.getId()),e);
-                return Response.status(Status.INTERNAL_SERVER_ERROR)
-                .entity(String.format("Unable to %s Entity %s because of" +
-                        "an Error while checking the current version of that" +
-                        "Entity within the Entityhub (Message: %s)",
-                        createState?"create":"update", 
representation.getId(),e.getMessage()))
+        if(!(create && update)){ //if both create and update are enabled skip 
this
+            for(Representation representation : parsed){
+                boolean exists;
+                try {
+                    exists = 
entityhub.isRepresentation(representation.getId());
+                } catch (EntityhubException e) {
+                    log.error(String.format("Exception while checking the 
existance " +
+                        "of an Entity with id  %s in the Entityhub.",
+                        representation.getId()),e);
+                    return Response.status(Status.INTERNAL_SERVER_ERROR)
+                    .entity(String.format("Unable to process Entity %s because 
of" +
+                            "an Error while checking the current version of 
that" +
+                            "Entity within the Entityhub (Message: %s)",
+                            representation.getId(),e.getMessage()))
+                            .header(HttpHeaders.ACCEPT, accepted).build();
+                }
+                if((exists && !update) || (!exists && !create)){
+                    return 
Response.status(Status.BAD_REQUEST).entity(String.format(
+                        "Unable to %s an Entity %s becuase it %s and %s is 
deactivated. " +
+                        " You might want to set the '%s' parameter to TRUE in 
your Request",
+                        exists ? "update" : "create",
+                        representation.getId(),
+                        exists ? "does already exists " : "does not",
+                        exists ? "updateing existing" : "creating new",
+                        exists ? "does already" : "does not exists",
+                        exists ? "update" : "create"))
                         .header(HttpHeaders.ACCEPT, accepted).build();
-            }
-            if(createState == exists){
-                return 
Response.status(Status.BAD_REQUEST).entity(String.format(
-                    "Unable to %s an Entity that %s exist",
-                    createState?"create":"update",
-                    exists?"does already":"does not"))
-                    .header(HttpHeaders.ACCEPT, accepted).build();
+                }
             }
         }
         //store the Representations
@@ -351,9 +368,8 @@ public class EntityhubRootResource exten
                 Entity entity = entityhub.store(representation);
                 updated.put(entity.getId(), entity);
             }catch (EntityhubException e) {
-                log.error(String.format("Exception while %s representation %s" 
+
-                        "in the Entityhub.",
-                        createState?"create":"update",representation),e);
+                log.error(String.format("Exception while storing Entity %s" +
+                        "in the Entityhub.",representation),e);
             }
         }
         //create the response for the Entity
@@ -367,7 +383,7 @@ public class EntityhubRootResource exten
             return rb.build();
         } else {
             Entity entity = updated.values().iterator().next();
-            if(createState){
+            if(method.equals(HttpMethod.POST)){
                 ResponseBuilder rb = 
Response.created(uriInfo.getAbsolutePathBuilder()
                     .queryParam("id", "{entityId}")
                     .build(entity.getId()));

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/resources/org/apache/stanbol/entityhub/jersey/templates/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource/inc_entity.ftl
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/resources/org/apache/stanbol/entityhub/jersey/templates/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource/inc_entity.ftl?rev=1183251&r1=1183250&r2=1183251&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/resources/org/apache/stanbol/entityhub/jersey/templates/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource/inc_entity.ftl
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/resources/org/apache/stanbol/entityhub/jersey/templates/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource/inc_entity.ftl
 Fri Oct 14 09:12:26 2011
@@ -82,13 +82,15 @@ function getEntityForUri() {
     </tr>
     <tr>
         <th>Request</th>
-        <td>POST /entityhub/entity?[id={uri}]</td>
+        <td>POST /entityhub/entity?[id={uri}]&[update=true/false]</td>
     </tr>
     <tr>
         <th>Parameter</th>
-        <td>id: optional the id of the Entity to add. If an id is parsed it is
+        <td><b>id:</b> optional the id of the Entity to add. If an id is 
parsed it is
         ensured that regardless of the included data only the entity with the
-        parsed id is created. Information for other ids will be ignored.</th>
+        parsed id is created. Information for other ids will be ignored.<br>
+        <b>update:</b> Switch that allows to allow updates to existing entities
+        for POST requests. Default is <code>false</code>
     </tr>
     <tr>
         <th>Produces</th>
@@ -96,6 +98,17 @@ function getEntityForUri() {
     </tr>
 </tbody>
 </table>
+<h5>Examples:</h5>
+<p> The following request would create all Entities defines within {file.rdf} 
in
+the entityhub. If any of such Entities already exists within the Entityhub the
+request would fail with BAD REQUEST</p>
+<pre>curl -i -X POST -H "Content-Type:application/rdf+xml" -T {file.rdf} 
"${it.publicBaseUri}entityhub/entity</pre>
+<p> Here the same request, but now it would be also allowed to update existing
+Entities</p>
+<pre>curl -i -X POST -H "Content-Type:application/rdf+xml" -T {file.rdf} 
"${it.publicBaseUri}entityhub/entity?update=true</pre>
+<p>This request would only import the Entity with the id {id} while ignoring
+all triples with a subject other that {id} contained within {file.rdf}</p>
+<pre>curl -i -X POST -H "Content-Type:application/rdf+xml" -T {file.rdf} 
"${it.publicBaseUri}entityhub/entity?id={id}</pre>
 
 <h4> Update an Entity</h4>
 <table>
@@ -106,13 +119,17 @@ function getEntityForUri() {
     </tr>
     <tr>
         <th>Request</th>
-        <td>POST /entityhub/entity?[id={uri}]</td>
+        <td>PUT /entityhub/entity?[id={uri}]&[create=true/false]</td>
     </tr>
     <tr>
         <th>Parameter</th>
-        <td>id: optional the id of the Entity to update. If an id is parsed it 
is
+        <td><b>id:</b> optional the id of the Entity to update. If an id is 
parsed it is
         ensured that regardless of the parsed data only information of this 
entity
-        are updated</th>
+        are updated.<br>
+        <b>create:</b> Switch that allows to enable/disable the creation of new
+        Entities for update (PUT) requests. The default <code>true</code>.
+        </td>
+        <td>
     </tr>
     <tr>
         <th>Produces</th>
@@ -121,6 +138,20 @@ function getEntityForUri() {
     </tr>
 </tbody>
 </table>
+<h5>Examples:</h5>
+<p> The following request would update/create all Entities defines within 
{file.rdf} in
+the entityhub. Non existent Entities will be created and already existing one 
will be
+updated (replaced with the submitted version).</p>
+<pre>curl -i -X PUT -H "Content-Type:application/rdf+xml" -T {file.rdf} 
"${it.publicBaseUri}entityhub/entity</pre>
+<p> This request would only update Entities. If any of the Entities in 
{file.rdf}
+would not already be present within the Entityhub this request would return a
+BAD REQUEST.</p>
+<pre>curl -i -X PUT -H "Content-Type:application/rdf+xml" -T {file.rdf} 
"${it.publicBaseUri}entityhub/entity?create=false</pre>
+<p>This request would update the Entity with the id {id} while ignoring
+all triples with a subject other that {id} contained within {file}. If an
+entity with {id} is not yet present within the Entityhub, than a BAD REQUEST 
would
+be returned</p>
+<pre>curl -i -X POST -H "Content-Type:application/rdf+xml" -T {file.rdf} 
"${it.publicBaseUri}entityhub/entity?id={id}&create=false</pre>
 
 <h4> Delete an Entity  entityhub/entity</h4>
 <table>


Reply via email to