You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2012/08/25 00:09:24 UTC
svn commit: r1377136 - in /jena/trunk/jena-arq/src/main/java:
com/hp/hpl/jena/sparql/modify/UpdateProcessRemoteForm.java
com/hp/hpl/jena/update/UpdateExecutionFactory.java
org/openjena/riot/web/HttpOp.java org/openjena/riot/web/HttpResponseLib.java
Author: andy
Date: Fri Aug 24 22:09:23 2012
New Revision: 1377136
URL: http://svn.apache.org/viewvc?rev=1377136&view=rev
Log:
Add SPARQL Update by HTML form (to complement using application/sparql-update)
Added:
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateProcessRemoteForm.java
Modified:
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/update/UpdateExecutionFactory.java
jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpOp.java
jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpResponseLib.java
Added: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateProcessRemoteForm.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateProcessRemoteForm.java?rev=1377136&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateProcessRemoteForm.java (added)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/modify/UpdateProcessRemoteForm.java Fri Aug 24 22:09:23 2012
@@ -0,0 +1,77 @@
+/*
+ * 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 com.hp.hpl.jena.sparql.modify;
+
+import java.util.ArrayList ;
+import java.util.HashMap ;
+import java.util.List ;
+import java.util.Map ;
+
+import org.openjena.atlas.lib.Pair ;
+import org.openjena.riot.web.HttpOp ;
+import org.openjena.riot.web.HttpResponseHandler ;
+import org.openjena.riot.web.HttpResponseLib ;
+
+import com.hp.hpl.jena.query.QuerySolution ;
+import com.hp.hpl.jena.sparql.ARQException ;
+import com.hp.hpl.jena.update.GraphStore ;
+import com.hp.hpl.jena.update.UpdateProcessor ;
+import com.hp.hpl.jena.update.UpdateRequest ;
+
+/**
+ * UpdateProcess that send the request to a SPARQL endpoint by using an HTML form and POST
+ * Use of application/sparql-update via @link{UpdateProcessRemote} is preferred.
+ */
+public class UpdateProcessRemoteForm implements UpdateProcessor
+{
+ private final UpdateRequest request ;
+ private final String endpoint ;
+
+ public UpdateProcessRemoteForm(UpdateRequest request , String endpoint )
+ {
+ this.request = request ;
+ this.endpoint = endpoint ;
+ }
+
+ @Override
+ public void setInitialBinding(QuerySolution binding)
+ {
+ throw new ARQException("Initial bindings for a remote update execution request not supported") ;
+ }
+
+ @Override
+ public GraphStore getGraphStore()
+ {
+ return null ;
+ }
+
+ @Override
+ public void execute()
+ {
+ String reqStr = request.toString() ;
+ List<Pair<String, String>> params = new ArrayList<Pair<String, String>>() ;
+ params.add(Pair.create("request", reqStr)) ;
+ Map<String, HttpResponseHandler> handlers = new HashMap<String, HttpResponseHandler>() ;
+ //handlers.put("text/html", HttpResponseLib.nullResponse) ;
+ handlers.put("*", HttpResponseLib.nullResponse) ;
+ HttpOp.execHttpPostForm(endpoint, params, handlers) ;
+ }
+
+}
+
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/update/UpdateExecutionFactory.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/update/UpdateExecutionFactory.java?rev=1377136&r1=1377135&r2=1377136&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/update/UpdateExecutionFactory.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/update/UpdateExecutionFactory.java Fri Aug 24 22:09:23 2012
@@ -22,10 +22,7 @@ import com.hp.hpl.jena.query.ARQ ;
import com.hp.hpl.jena.query.QuerySolution ;
import com.hp.hpl.jena.sparql.engine.binding.Binding ;
import com.hp.hpl.jena.sparql.engine.binding.BindingUtils ;
-import com.hp.hpl.jena.sparql.modify.UpdateEngineFactory ;
-import com.hp.hpl.jena.sparql.modify.UpdateEngineRegistry ;
-import com.hp.hpl.jena.sparql.modify.UpdateProcessRemote ;
-import com.hp.hpl.jena.sparql.modify.UpdateProcessorBase ;
+import com.hp.hpl.jena.sparql.modify.* ;
import com.hp.hpl.jena.sparql.util.Context ;
/** Create UpdateProcessors (one-time executions of a SPARQL Update request) */
@@ -129,4 +126,22 @@ public class UpdateExecutionFactory
{
return new UpdateProcessRemote(updateRequest, remoteEndpoint) ;
}
+
+ /** Create an UpdateProcessor that send the update request to a remote SPARQL Update service using an HTML form
+ * @param update
+ * @param remoteEndpoint
+ */
+ public static UpdateProcessor createRemoteForm(Update update, String remoteEndpoint)
+ {
+ return new UpdateProcessRemoteForm(new UpdateRequest(update), remoteEndpoint) ;
+ }
+
+ /** Create an UpdateProcessor that send the update request to a remote SPARQL Update service using an HTML form
+ * @param updateRequest
+ * @param remoteEndpoint
+ */
+ public static UpdateProcessor createRemoteForm(UpdateRequest updateRequest, String remoteEndpoint)
+ {
+ return new UpdateProcessRemoteForm(updateRequest, remoteEndpoint) ;
+ }
}
Modified: jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpOp.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpOp.java?rev=1377136&r1=1377135&r2=1377136&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpOp.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpOp.java Fri Aug 24 22:09:23 2012
@@ -23,19 +23,27 @@ import static java.lang.String.format ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.UnsupportedEncodingException ;
+import java.util.ArrayList ;
+import java.util.HashMap ;
+import java.util.List ;
import java.util.Map ;
import java.util.concurrent.atomic.AtomicLong ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpResponse ;
+import org.apache.http.NameValuePair ;
import org.apache.http.StatusLine ;
import org.apache.http.client.HttpClient ;
+import org.apache.http.client.entity.UrlEncodedFormEntity ;
import org.apache.http.client.methods.HttpGet ;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.entity.EntityTemplate ;
import org.apache.http.entity.InputStreamEntity ;
import org.apache.http.entity.StringEntity ;
import org.apache.http.impl.client.DefaultHttpClient ;
+import org.apache.http.message.BasicNameValuePair ;
+import org.openjena.atlas.io.IO ;
+import org.openjena.atlas.lib.Pair ;
import org.openjena.atlas.web.HttpException ;
import org.openjena.atlas.web.MediaType ;
import org.openjena.riot.WebContent ;
@@ -59,32 +67,16 @@ import com.hp.hpl.jena.sparql.ARQInterna
*/
public class HttpOp
{
-
// See also:
// Fluent API in HttpClient from v4.2
-
-
static private Logger log = LoggerFactory.getLogger(HttpOp.class) ;
static private AtomicLong counter = new AtomicLong(0) ;
-
-// /** GET with unencoded query string.
-// * See {@link #execHttpGet(String, String, Map)} for additional details.
-// * <p>The query string will be encoded as needed and appended to the URL, inserting a "?".
-// */
-// public static void execHttpGet(String url, String queryString, String acceptHeader, Map<String, HttpResponseHandler> handlers)
-// {
-// try {
-// System.err.println("BROKEN - encodes the queyr string structure") ;
-// String requestURL = url+"?"+URLEncoder.encode(queryString, "UTF-8") ;
-// execHttpGet(requestURL, acceptHeader, handlers) ;
-// } catch (UnsupportedEncodingException ex)
-// {
-// // UTF-8 required of all Java platforms.
-// throw new ARQInternalErrorException("No UTF-8 charset") ;
-// }
-// }
+ static Map<String, HttpResponseHandler> noActionHandlers = new HashMap<String, HttpResponseHandler>() ;
+ static {
+ noActionHandlers.put("*", HttpResponseLib.nullResponse) ;
+ }
/** GET
* <p>The acceptHeader string is any legal value for HTTP Accept: field.
* <p>The handlers are the set of content types (without charset),
@@ -112,10 +104,7 @@ public class HttpOp
// Handle response
httpResponse(id, response, baseIRI, handlers) ;
httpclient.getConnectionManager().shutdown();
- } catch (IOException ex)
- {
- ex.printStackTrace(System.err) ;
- }
+ } catch (IOException ex) { IO.exception(ex) ; }
}
/** POST a string without response body.
@@ -153,9 +142,8 @@ public class HttpOp
e.setContentType(contentType) ;
execHttpPost(url, e, acceptType, handlers) ;
} catch (UnsupportedEncodingException e1)
- {
- throw new ARQInternalErrorException("Platform does not support required UTF-8") ;
- } finally { closeEntity(e) ; }
+ { throw new ARQInternalErrorException("Platform does not support required UTF-8") ; }
+ finally { closeEntity(e) ; }
}
/** POST with response body.
@@ -209,12 +197,42 @@ public class HttpOp
httpResponse(id, response, baseIRI, handlers) ;
httpclient.getConnectionManager().shutdown();
- } catch (IOException ex)
- {
- ex.printStackTrace(System.err) ;
- }
+ } catch (IOException ex) { IO.exception(ex) ; }
finally { closeEntity(provider) ; }
}
+
+ /** Execute an HTTP POST form operation */
+ public static void execHttpPostForm(String url, List<Pair<String, String>> params, Map<String, HttpResponseHandler> handlers)
+ {
+ try {
+ long id = counter.incrementAndGet() ;
+ String requestURI = url ;// determineBaseIRI(url) ;
+ String baseIRI = determineBaseIRI(requestURI) ;
+ //A rat walked over the keyboard: >n'e q5tas5aaas v
+ HttpPost httppost = new HttpPost(requestURI);
+ httppost.setEntity(convertFormParams(params));
+ if ( log.isDebugEnabled() )
+ log.debug(format("[%d] %s %s",id ,httppost.getMethod(),httppost.getURI().toString())) ;
+
+ HttpClient httpclient = new DefaultHttpClient();
+ HttpResponse response = httpclient.execute(httppost) ;
+ httpResponse(id, response, baseIRI, handlers) ;
+ httpclient.getConnectionManager().shutdown();
+ } catch (IOException ex) { IO.exception(ex) ; }
+ }
+
+ private static HttpEntity convertFormParams(List<Pair<String, String>> params)
+ {
+ try {
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>() ;
+ for (Pair<String, String> p : params)
+ nvps.add(new BasicNameValuePair(p.getLeft(), p.getRight())) ;
+ HttpEntity e = new UrlEncodedFormEntity(nvps, "UTF-8") ;
+ return e ;
+ } catch (UnsupportedEncodingException e)
+ { throw new ARQInternalErrorException("Platform does not support required UTF-8") ; }
+ }
+
private static void closeEntity(HttpEntity entity)
{
@@ -252,6 +270,8 @@ public class HttpOp
{
if ( response == null )
return ;
+ if ( handlers == null )
+ handlers = noActionHandlers ;
try {
StatusLine statusLine = response.getStatusLine() ;
if ( statusLine.getStatusCode() >= 400 )
@@ -268,6 +288,7 @@ public class HttpOp
if ( contentType != null )
{
mt = MediaType.create(contentType) ;
+ ct = mt.getContentType() ;
if ( log.isDebugEnabled() )
log.debug(format("[%d] %d %s :: %s",id, statusLine.getStatusCode(), statusLine.getReasonPhrase() , mt)) ;
}
Modified: jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpResponseLib.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpResponseLib.java?rev=1377136&r1=1377135&r2=1377136&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpResponseLib.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/openjena/riot/web/HttpResponseLib.java Fri Aug 24 22:09:23 2012
@@ -96,6 +96,13 @@ public class HttpResponseLib
}
}
} ;
+
+ public static HttpResponseHandler nullResponse = new HttpResponseHandler() {
+ @Override
+ public void handle(String contentType, String baseIRI, HttpResponse response)
+ {}
+ } ;
+
public static HttpCaptureResponse<Graph> graphReaderTurtle = new AbstractGraphReader()
{
@Override