You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by wi...@apache.org on 2013/09/26 17:05:51 UTC
[3/5] git commit: MARMOTTA-322: unified all update implementations in
the web service too
MARMOTTA-322: unified all update implementations in the web service too
Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/d1a4ab68
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/d1a4ab68
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/d1a4ab68
Branch: refs/heads/develop
Commit: d1a4ab682652b456a8b60f375a01dd68210e43f4
Parents: b3bb9d7
Author: Sergio Fernández <wi...@apache.org>
Authored: Thu Sep 26 16:33:38 2013 +0200
Committer: Sergio Fernández <wi...@apache.org>
Committed: Thu Sep 26 16:33:38 2013 +0200
----------------------------------------------------------------------
.../sparql/webservices/SparqlWebService.java | 141 +++++++++----------
1 file changed, 64 insertions(+), 77 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/d1a4ab68/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
index 06d281a..614868b 100644
--- a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
+++ b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
@@ -332,12 +332,69 @@ public class SparqlWebService {
*/
@GET
@Path(UPDATE)
- public Response updateGet(@QueryParam("update") String update, @QueryParam("query") String query, @QueryParam("output") String resultType,
- @Context HttpServletRequest request) {
- try {
- String q = getUpdateQuery(update, query);
- if (StringUtils.isNotBlank(q)) {
- sparqlService.update(QueryLanguage.SPARQL, q);
+ public Response updateGet(@QueryParam("update") String update, @QueryParam("query") String query, @QueryParam("output") String resultType, @Context HttpServletRequest request) {
+ String q = getUpdateQuery(update, query);
+ return update(q, resultType, request);
+ }
+
+ /**
+ * Execute a SPARQL 1.1 Update request using update via POST directly;
+ * see details at http://www.w3.org/TR/sparql11-protocol/\#update-operation
+ *
+ * @param request the servlet request (to retrieve the SPARQL 1.1 Update query passed in the
+ * body of the POST request)
+ * @HTTP 200 in case the update was carried out successfully
+ * @HTTP 400 in case the update query is missing or invalid
+ * @HTTP 500 in case the update was not successful
+ * @return empty content in case the update was successful, the error message in case an error
+ * occurred
+ */
+ @POST
+ @Path(UPDATE)
+ @Consumes("application/sparql-update")
+ public Response updatePostDirectly(@Context HttpServletRequest request, @QueryParam("output") String resultType) {
+ try {
+ String q = CharStreams.toString(request.getReader());
+ return update(q, resultType, request);
+ } catch (IOException e) {
+ return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
+ }
+ }
+
+ /**
+ * Execute a SPARQL 1.1 Update request using update via URL-encoded POST;
+ * see details at http://www.w3.org/TR/sparql11-protocol/\#update-operation
+ *
+ * @param request the servlet request (to retrieve the SPARQL 1.1 Update query passed in the
+ * body of the POST request)
+ * @HTTP 200 in case the update was carried out successfully
+ * @HTTP 400 in case the update query is missing or invalid
+ * @HTTP 500 in case the update was not successful
+ * @return empty content in case the update was successful, the error message in case an error
+ * occurred
+ */
+ @POST
+ @Path(UPDATE)
+ @Consumes({"application/x-www-url-form-urlencoded", "application/x-www-form-urlencoded"})
+ public Response updatePostUrlEncoded(@Context HttpServletRequest request) {
+ try {
+ Map<String,String> params = parseEncodedQueryParameters(CharStreams.toString(request.getReader()));
+ String q = StringUtils.defaultString(params.get("update"));
+ String resultType = StringUtils.defaultString(params.get("output"));
+ return update(q, resultType, request);
+ } catch (IOException e) {
+ return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
+ }
+ }
+
+ /**
+ * Actual update implementation
+ *
+ */
+ private Response update(String update, String resultType, HttpServletRequest request) {
+ try {
+ if (StringUtils.isNotBlank(update)) {
+ sparqlService.update(QueryLanguage.SPARQL, update);
return Response.ok().build();
} else {
if (resultType == null) {
@@ -363,7 +420,7 @@ public class SparqlWebService {
} catch (URISyntaxException e) {
return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
}
- }
+ }
/**
* Get right update query from both possible parameters, for keeping
@@ -381,76 +438,6 @@ public class SparqlWebService {
return query;
} else
return null;
- }
-
- /**
- * Execute a SPARQL 1.1 Update request using update via POST directly;
- * see details at http://www.w3.org/TR/sparql11-protocol/\#update-operation
- *
- * @param request the servlet request (to retrieve the SPARQL 1.1 Update query passed in the
- * body of the POST request)
- * @HTTP 200 in case the update was carried out successfully
- * @HTTP 400 in case the update query is missing or invalid
- * @HTTP 500 in case the update was not successful
- * @return empty content in case the update was successful, the error message in case an error
- * occurred
- */
- @POST
- @Path(UPDATE)
- @Consumes("application/sparql-update")
- public Response updatePostDirectly(@Context HttpServletRequest request) {
- try {
- String query = CharStreams.toString(request.getReader());
- if (StringUtils.isNotBlank(query)) {
- sparqlService.update(QueryLanguage.SPARQL, query);
- return Response.ok().build();
- } else
- return Response.status(Response.Status.BAD_REQUEST).entity("no SPARQL query given").build();
- } catch (MalformedQueryException e) {
- return Response.status(Response.Status.BAD_REQUEST).entity(WebServiceUtil.jsonErrorResponse(e)).build();
- } catch(UpdateExecutionException e) {
- log.error("update execution threw an exception", e);
- return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
- } catch (MarmottaException e) {
- return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
- } catch (IOException e) {
- return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
- }
- }
-
- /**
- * Execute a SPARQL 1.1 Update request using update via URL-encoded POST;
- * see details at http://www.w3.org/TR/sparql11-protocol/\#update-operation
- *
- * @param request the servlet request (to retrieve the SPARQL 1.1 Update query passed in the
- * body of the POST request)
- * @HTTP 200 in case the update was carried out successfully
- * @HTTP 400 in case the update query is missing or invalid
- * @HTTP 500 in case the update was not successful
- * @return empty content in case the update was successful, the error message in case an error
- * occurred
- */
- @POST
- @Path(UPDATE)
- @Consumes({"application/x-www-url-form-urlencoded", "application/x-www-form-urlencoded"})
- public Response updatePostUrlEncoded(@Context HttpServletRequest request) {
- try {
- Map<String,String> params = parseEncodedQueryParameters(CharStreams.toString(request.getReader()));
- if (params.containsKey("update") && StringUtils.isNotBlank(params.get("update"))) {
- sparqlService.update(QueryLanguage.SPARQL, params.get("update"));
- return Response.ok().build();
- } else
- return Response.status(Response.Status.BAD_REQUEST).entity("no SPARQL query given").build();
- } catch (MalformedQueryException e) {
- return Response.status(Response.Status.BAD_REQUEST).entity(WebServiceUtil.jsonErrorResponse(e)).build();
- } catch(UpdateExecutionException e) {
- log.error("update execution threw an exception", e);
- return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
- } catch (MarmottaException e) {
- return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
- } catch (IOException e) {
- return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
- }
}
/**