jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1158648 - in /incubator/jena/Jena2/ARQ/trunk: .settings/ src-dev/dev/ src-dev/riot/ src-dev/riot/web/
Date Wed, 17 Aug 2011 12:01:16 GMT
Author: andy
Date: Wed Aug 17 12:01:16 2011
New Revision: 1158648

URL: http://svn.apache.org/viewvc?rev=1158648&view=rev
Log:
Migrate httpclient-based HTTP support code to RIOT.  WIP.

Added:
    incubator/jena/Jena2/ARQ/trunk/src-dev/riot/
    incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/
    incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpNames.java   (with props)
    incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpOp.java   (with props)
    incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpResponseLib.java   (with props)
    incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java   (with props)
Modified:
    incubator/jena/Jena2/ARQ/trunk/.settings/org.eclipse.jdt.ui.prefs
    incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java

Modified: incubator/jena/Jena2/ARQ/trunk/.settings/org.eclipse.jdt.ui.prefs
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/.settings/org.eclipse.jdt.ui.prefs?rev=1158648&r1=1158647&r2=1158648&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/.settings/org.eclipse.jdt.ui.prefs (original)
+++ incubator/jena/Jena2/ARQ/trunk/.settings/org.eclipse.jdt.ui.prefs Wed Aug 17 12:01:16
2011
@@ -1,3 +1,5 @@
-#Thu Jan 25 17:30:04 GMT 2007
-eclipse.preferences.version=1
-internal.default.compliance=default
+#Wed Aug 17 12:57:42 BST 2011
+eclipse.preferences.version=1
+internal.default.compliance=default
+org.eclipse.jdt.ui.javadoc=false
+org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates><template
autoinsert\="false" context\="gettercomment_context" deleted\="false" description\="Comment
for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment"
name\="gettercomment"/><template autoinsert\="false" context\="settercomment_context"
deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment"
name\="settercomment"/><template autoinsert\="false" context\="constructorcomment_context"
deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment"
name\="constructorcomment"/><template autoinsert\="true" context\="filecomment_context"
deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment"
name
 \="filecomment">/**\n * \n */</template><template autoinsert\="false" context\="typecomment_context"
deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment"
name\="typecomment">/** \n */</template><template autoinsert\="true" context\="fieldcomment_context"
deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment"
name\="fieldcomment">/**\n * \n */</template><template autoinsert\="false" context\="methodcomment_context"
deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment"
name\="methodcomment"/><template autoinsert\="true" context\="overridecomment_context"
deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment"
name\="overridecomment"/><template autoinsert\="true" cont
 ext\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods"
enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\n
* ${tags}\n * ${see_to_target}\n */</template><template autoinsert\="false" context\="newtype_context"
deleted\="false" description\="New classes and interfaces" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype"
name\="newtype">/**\n * Licensed to the Apache Software Foundation (ASF) under one\n *
or more contributor license agreements.  See the NOTICE file\n * distributed with this work
for additional information\n * regarding copyright ownership.  The ASF licenses this file\n
* to you under the Apache License, Version 2.0 (the\n * "License"); you may not use this file
except in compliance\n * with the License.  You may obtain a copy of the License at\n *\n
*     http\://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law
or agreed to i
 n writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License
for the specific language governing permissions and\n * limitations under the License.\n */\n\n${package_declaration}\n\n${type_declaration}\n</template><template
autoinsert\="true" context\="classbody_context" deleted\="false" description\="Code in new
class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody">\n</template><template
autoinsert\="true" context\="interfacebody_context" deleted\="false" description\="Code in
new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody"
name\="interfacebody">\n</template><template autoinsert\="true" context\="enumbody_context"
deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody"
name
 \="enumbody">\n</template><template autoinsert\="true" context\="annotationbody_context"
deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody"
name\="annotationbody">\n</template><template autoinsert\="false" context\="catchblock_context"
deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock"
name\="catchblock">${exception_var}.printStackTrace();</template><template autoinsert\="false"
context\="methodbody_context" deleted\="false" description\="Code in created method stubs"
enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">${body_statement}</template><template
autoinsert\="false" context\="constructorbody_context" deleted\="false" description\="Code
in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody"
name\="constructorbody"
 >${body_statement}</template><template autoinsert\="true" context\="getterbody_context"
deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody"
name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context"
deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody"
name\="setterbody">${field} \= ${param};</template></templates>

Modified: incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java?rev=1158648&r1=1158647&r2=1158648&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java Wed Aug 17 12:01:16 2011
@@ -9,28 +9,41 @@
 package dev;
 
 import java.io.FileInputStream ;
+import java.io.IOException ;
 import java.io.InputStream ;
+import java.io.OutputStream ;
+import java.net.URLEncoder ;
+import java.util.ArrayList ;
+import java.util.HashMap ;
 import java.util.Iterator ;
+import java.util.List ;
+import java.util.Map ;
 import java.util.NoSuchElementException ;
 import java.util.concurrent.ArrayBlockingQueue ;
 import java.util.concurrent.BlockingQueue ;
 import java.util.concurrent.ExecutorService ;
 import java.util.concurrent.Executors ;
 
+import org.apache.http.HttpResponse ;
 import org.openjena.atlas.io.IndentedWriter ;
+import org.openjena.atlas.iterator.Iter ;
 import org.openjena.atlas.json.JSON ;
 import org.openjena.atlas.json.JsonValue ;
 import org.openjena.atlas.lib.Lib ;
 import org.openjena.atlas.lib.Sink ;
 import org.openjena.atlas.lib.StrUtils ;
 import org.openjena.atlas.logging.Log ;
+import org.openjena.atlas.web.MediaType ;
 import org.openjena.riot.ErrorHandlerFactory ;
 import org.openjena.riot.RiotReader ;
+import org.openjena.riot.WebContent ;
 import org.openjena.riot.checker.CheckerIRI ;
 import org.openjena.riot.pipeline.normalize.CanonicalizeLiteral ;
 import org.openjena.riot.tokens.Token ;
 import org.openjena.riot.tokens.Tokenizer ;
 import org.openjena.riot.tokens.TokenizerFactory ;
+import riot.web.HttpOp ;
+import riot.web.HttpResponseLib ;
 
 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
 import com.hp.hpl.jena.datatypes.xsd.XSDDuration ;
@@ -47,6 +60,7 @@ import com.hp.hpl.jena.query.QueryExecut
 import com.hp.hpl.jena.query.QueryFactory ;
 import com.hp.hpl.jena.query.QuerySolutionMap ;
 import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFactory ;
 import com.hp.hpl.jena.query.ResultSetFormatter ;
 import com.hp.hpl.jena.rdf.model.AnonId ;
 import com.hp.hpl.jena.rdf.model.Model ;
@@ -73,6 +87,7 @@ import com.hp.hpl.jena.sparql.function.F
 import com.hp.hpl.jena.sparql.function.FunctionEnvBase ;
 import com.hp.hpl.jena.sparql.function.FunctionRegistry ;
 import com.hp.hpl.jena.sparql.graph.NodeTransform ;
+import com.hp.hpl.jena.sparql.resultset.ResultsFormat ;
 import com.hp.hpl.jena.sparql.sse.SSE ;
 import com.hp.hpl.jena.sparql.util.ExprUtils ;
 import com.hp.hpl.jena.sparql.util.FmtUtils ;
@@ -117,8 +132,84 @@ public class RunARQ
         System.exit(code) ;
     }
 
-    // count(filter)
-    
+    public static void main2(String ... args) throws Exception
+    {
+        final String queryString1 =  "SELECT * { ?s ?p ?o } LIMIT 1" ;
+        final String queryString2 =  "SELECT * { ?s ?p ?o } LIMIT 10" ;
+        
+        // GET graph
+        Map<String, HttpOp.HttpResponseHandler> handlers = new HashMap<String, HttpOp.HttpResponseHandler>()
;
+        // Chnage to one handler for all graph types.
+        handlers.put(WebContent.contentTypeTurtle, HttpResponseLib.graphReaderTurtle) ;
+        handlers.put(WebContent.contentTypeRDFXML, HttpResponseLib.graphReaderRDFXML) ;
+        handlers.put("*", HttpResponseLib.httpDumpResponse) ;
+
+        // RS support
+//        public static ResultsFormat contentTypeToResultSet(String contentType) { return
mapContentTypeToResultSet.get(contentType) ; }
+        final Map<String, ResultsFormat> mapContentTypeToResultSet = new HashMap<String,
ResultsFormat>() ;
+        {
+            mapContentTypeToResultSet.put(WebContent.contentTypeResultsXML, ResultsFormat.FMT_RS_XML)
;
+            mapContentTypeToResultSet.put(WebContent.contentTypeResultsJSON, ResultsFormat.FMT_RS_JSON)
;
+            mapContentTypeToResultSet.put(WebContent.contentTypeTextTSV, ResultsFormat.FMT_RS_TSV)
;
+        }
+
+        HttpOp.HttpCaptureResponse<ResultSet> captureRS = new HttpOp.HttpCaptureResponse<ResultSet>(){
+            ResultSet rs = null ;
+            //@Override
+            public void handle(String contentType, String baseIRI, HttpResponse response)
throws IOException
+            {
+                MediaType mt = new MediaType(contentType) ;
+                ResultsFormat fmt = mapContentTypeToResultSet.get(contentType) ; // contentTypeToResultSet(contentType)
;
+                InputStream in = response.getEntity().getContent() ;
+                rs = ResultSetFactory.load(in, fmt) ;
+                // Force reading
+                rs = ResultSetFactory.copyResults(rs) ;
+            }
+
+            //@Override
+            public ResultSet get()
+            {
+                return rs ;
+            }} ;
+        
+        handlers.put(WebContent.contentTypeResultsXML, captureRS) ;             
+        handlers.put(WebContent.contentTypeResultsJSON, captureRS) ;            
+        handlers.put(WebContent.contentTypeTextTSV, captureRS) ;     
+        
+        List<String> acceptables = new ArrayList<String>() ;
+        acceptables.add("text/turtle;q=0.8") ;
+        acceptables.add("application/rdf+xml;q=0.1") ;
+        String acceptHeader = Iter.asString(acceptables, " , ") ;
+        
+        HttpOp.execHttpGet("http://localhost:3030/ds/sparql?query="+URLEncoder.encode(queryString1,"UTF-8"),
+                           acceptHeader,
+                           handlers) ;
+        
+        ResultSetFormatter.out(captureRS.get()) ;
+
+        
+        HttpOp.ContentProducer cp = new HttpOp.ContentProducer() {
+            //@Override
+            public void writeTo(OutputStream outstream) throws IOException
+            {
+                outstream.write(StrUtils.asUTF8bytes(queryString2)) ;
+                //outstream.flush() ;
+            }} ;
+         
+        List<String> acceptResultSets = new ArrayList<String>() ;
+        acceptResultSets.add(WebContent.contentTypeResultsJSON+";q=0.9") ;
+        acceptResultSets.add(WebContent.contentTypeResultsXML+";q=0.5") ;
+        acceptResultSets.add(WebContent.contentTypeTextTSV+";q=0.8") ;
+        String acceptResultSet = Iter.asString(acceptResultSets, " , ") ;
+            
+        HttpOp.execHttpPost("http://localhost:3030/ds/sparql",
+                            WebContent.contentTypeSPARQLQuery, cp,
+                            acceptResultSet, handlers) ;
+        ResultSetFormatter.out(captureRS.get()) ;
+        exit(0) ;
+
+    }
+        
     @SuppressWarnings("deprecation")
     public static void main(String[] argv) throws Exception
     {

Added: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpNames.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpNames.java?rev=1158648&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpNames.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpNames.java Wed Aug 17 12:01:16 2011
@@ -0,0 +1,84 @@
+/**
+ * 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 riot.web;
+
+public class HttpNames
+{
+    public static final String hAccept              = "Accept" ;
+    public static final String hAllow               = "Allow" ;
+    public static final String hAcceptEncoding      = "Accept-Encoding" ;
+    public static final String hAcceptCharset       = "Accept-Charset" ;
+    public static final String hAcceptRanges        = "Accept-Ranges" ;
+    
+    public static final String hContentEncoding     = "Content-Encoding" ;
+    public static final String hContentLengh        = "Content-Length" ;
+    public static final String hContentLocation     = "Content-Location" ;
+    public static final String hContentRange        = "Content-Range" ;
+    public static final String hContentType         = "Content-Type" ;
+    public static final String hServer              = "Server" ;
+    public static final String hLocation            = "Location" ; 
+    public static final String charset              = "charset" ;
+    
+    // CORS: 
+    //   http://www.w3.org/TR/cors/  http://esw.w3.org/CORS_Enabled
+    public static final String hAccessControlAllowOrigin = "Access-Control-Allow-Origin"
;
+    
+    // Fuseki parameter names 
+    public static final String paramGraph           = "graph" ;
+    public static final String paramGraphDefault    = "default" ;
+
+    public static final String paramQuery           = "query" ;
+    public static final String paramQueryRef        = "query-ref" ;
+    public static final String paramDefaultGraphURI = "default-graph-uri" ;
+    public static final String paramNamedGraphURI   = "named-graph-uri" ;
+    
+    public static final String paramStyleSheet      = "stylesheet" ;
+    public static final String paramAccept          = "accept" ;
+    public static final String paramOutput1         = "output" ;        // See Yahoo! developer:
http://developer.yahoo.net/common/json.html 
+    public static final String paramOutput2         = "format" ;        // Alternative name

+    public static final String paramCallback        = "callback" ;
+    public static final String paramForceAccept     = "force-accept" ;  // Force the accept
header at the last moment
+    
+    public static final String paramUpdate          = "update" ;
+    public static final String paramRequest         = "request" ;       // Alternative name.

+    
+    
+    public static final String METHOD_DELETE        = "DELETE";
+    public static final String METHOD_HEAD          = "HEAD";
+    public static final String METHOD_GET           = "GET";
+    public static final String METHOD_OPTIONS       = "OPTIONS";
+    public static final String METHOD_PATCH         = "PATCH" ;
+    public static final String METHOD_POST          = "POST";
+    public static final String METHOD_PUT           = "PUT";
+    public static final String METHOD_TRACE         = "TRACE";
+
+    public static final String HEADER_IFMODSINCE    = "If-Modified-Since";
+    public static final String HEADER_LASTMOD       = "Last-Modified";
+    
+    public static final String ServiceQuery     = "/query" ;
+    public static final String ServiceQueryAlt  = "/sparql" ;
+    public static final String ServiceUpdate    = "/update" ;
+    public static final String ServiceData      = "/data" ;
+    public static final String ServiceUpload    = "/upload" ;
+    
+    // Posisble values of fields.
+    // TODO Pull in from results writer.
+    public static final String valueDefault    = "default" ;
+    
+}

Propchange: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpNames.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpOp.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpOp.java?rev=1158648&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpOp.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpOp.java Wed Aug 17 12:01:16 2011
@@ -0,0 +1,341 @@
+/**
+ * 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 riot.web;
+
+import static java.lang.String.format ;
+
+import java.io.IOException ;
+import java.io.InputStream ;
+import java.io.UnsupportedEncodingException ;
+import java.net.URLEncoder ;
+import java.util.Map ;
+import java.util.concurrent.atomic.AtomicLong ;
+
+import org.apache.http.HttpEntity ;
+import org.apache.http.HttpResponse ;
+import org.apache.http.StatusLine ;
+import org.apache.http.client.HttpClient ;
+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.openjena.atlas.web.HttpException ;
+import org.openjena.atlas.web.MediaType ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+import com.hp.hpl.jena.sparql.ARQInternalErrorException ;
+
+/** Simplified HTTP operationls; simplification means only supporting certain needed uses
of HTTP.    
+ * <p>
+ * For HTTP GET, the application supplies a URL, the accept header string, and a 
+ * list of handlers to deal with different content type responses. 
+ * 
+ * @See HttpNames for HTTP related constants
+ * @See WebContent for content type name constants
+ */
+public class HttpOp
+{
+    // ==>> Atlas.web
+    // From Fuseki: httpNames.
+    //  
+    
+    static private Logger log = LoggerFactory.getLogger(HttpOp.class) ;
+    
+    static private AtomicLong counter = new AtomicLong(0) ;
+
+    /** General act-on-HTTP-response */
+    public static interface HttpResponseHandler { void handle(String contentType, String
baseIRI, HttpResponse response) throws IOException ; }
+    
+    /** Act-on-HTTP-response and produce some object */
+    public static interface HttpCaptureResponse<T> extends HttpOp.HttpResponseHandler
{ public T get() ; }
+    
+    /** Interface for sending content over HTTP POST */
+    public static interface ContentProducer extends org.apache.http.entity.ContentProducer
{}
+    
+    /** GET with unencoded query string.
+     *  See {@link #execHttpGet(String, String, Map)} for additional details.
+     *  <p>The query string wil 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 {
+            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") ;
+        }
+    }
+    
+    /** GET
+     *  <p>The acceptHeader string is any legal value for HTTP Accept: field.
+     *  <p>The handlers are the set of content types (without charset),
+     *  used to dispatch the response body for handling.
+     *  <p>A Map entry of ("*",....) is used "no handler found".
+     *  <p>HTTP responses 400 and 500 become exceptions.   
+     */
+    public static void execHttpGet(String url, String acceptHeader, Map<String, HttpResponseHandler>
handlers)
+    {
+        try {
+            long id = counter.incrementAndGet() ;
+            String requestURI = determineRequestURI(url) ;
+            String baseIRI = determineBaseIRI(requestURI) ;
+
+            HttpGet httpget = new HttpGet(requestURI);
+            if ( log.isDebugEnabled() )
+                log.debug(format("[%d] %s %s",id ,httpget.getMethod(),httpget.getURI().toString()))
;
+            // Accept
+            if ( acceptHeader != null )
+                httpget.addHeader(HttpNames.hAccept, acceptHeader) ;
+            
+            // Execute
+            HttpClient httpclient = new DefaultHttpClient();
+            HttpResponse response = httpclient.execute(httpget) ;
+            // Handle response
+            httpResponse(id, response, baseIRI, handlers) ;
+            httpclient.getConnectionManager().shutdown(); 
+        } catch (IOException ex)
+        {
+            ex.printStackTrace(System.err) ;
+        }
+    }
+    
+    /** POST a string without response body.
+     * <p>Execute an HTTP POST, with the string as content.
+     * <p>No response content expected or processed. 
+     */
+    //TODO Use MediaType
+    public static void execHttpPost(String url, String contentType, String content)
+    {
+        execHttpPost(url, contentType, content, null, null) ;
+    }
+    
+    /** POST without response body.
+     * Content read from the the input stream.
+     * <p>Execute an HTTP POST, with the string as content.
+     * <p>No response content expected or processed. 
+     */
+   //TODO Use MediaType
+    public static void execHttpPost(String url, String contentType, 
+                                    InputStream input, int length)
+    {
+        execHttpPost(url, contentType, input, length, null, null ) ;
+    }
+    
+
+    /** POST a string, expect a response body.*/
+    public static void execHttpPost(String url, 
+                                    String contentType, String content,
+                                    String acceptType, Map<String, HttpResponseHandler>
handlers)
+    {
+        StringEntity e = null ;
+        try
+        {
+            e = new StringEntity(content, "UTF-8") ;
+            execHttpPost(url, e, acceptType, handlers) ;
+        } catch (UnsupportedEncodingException e1)
+        {
+            throw new ARQInternalErrorException("Platform does not support required UTF-8")
;
+        } finally { closeEntity(e) ; }
+    }
+    
+    /** POST with response body.
+     * The input stream is assumed to be UTF-8.
+     */
+    public static void execHttpPost(String url, String contentType, 
+                                    InputStream input, int length,
+                                    String acceptType, Map<String, HttpResponseHandler>
handlers)
+    {
+        
+        InputStreamEntity e = new InputStreamEntity(input, length) ;
+        e.setContentType(contentType) ;
+        e.setContentEncoding("UTF-8") ;
+        execHttpPost(url, e, acceptType, handlers) ;
+    }
+    
+    /** POST with response body */
+    public static void execHttpPost(String url, 
+                                    String contentType, ContentProducer provider,
+                                    String acceptType, Map<String, HttpResponseHandler>
handlers)
+    {
+        EntityTemplate entity = new EntityTemplate(provider) ;
+        entity.setContentType(contentType) ;
+        execHttpPost(url, entity, acceptType, handlers) ;
+    }
+                             
+    /** POST with response body.
+     * <p>The content for the POST body comes from the HttpEntity.
+     * <p>The response is handled bythe handler map, as per {@link #execHttpGet(String,
String, Map)}
+     */
+    public static void execHttpPost(String url, 
+                                    HttpEntity provider,
+                                    String acceptType, Map<String, HttpResponseHandler>
handlers)
+    {
+        try {
+            long id = counter.incrementAndGet() ;
+            String requestURI = determineBaseIRI(url) ;
+            String baseIRI = determineBaseIRI(requestURI) ;
+            
+            HttpPost httppost = new HttpPost(requestURI);
+            if ( log.isDebugEnabled() )
+                log.debug(format("[%d] %s %s",id ,httppost.getMethod(),httppost.getURI().toString()))
;
+            
+            if ( provider.getContentType() == null )
+                log.debug(format("[%d] No content type")) ;
+
+            // Execute
+            HttpClient httpclient = new DefaultHttpClient();
+            httppost.setEntity(provider) ;
+            HttpResponse response = httpclient.execute(httppost) ;
+            httpResponse(id, response, baseIRI, handlers) ;
+            
+            httpclient.getConnectionManager().shutdown(); 
+        } catch (IOException ex)
+        {
+            ex.printStackTrace(System.err) ;
+        }
+        finally { closeEntity(provider) ; }
+    }
+
+    private static void closeEntity(HttpEntity entity)
+    {
+        if ( entity == null )
+            return ;
+        try { entity.getContent().close() ; } catch (Exception e) {}
+    }
+
+    private static String determineRequestURI(String url)
+    {
+        String requestURI = url ;
+        if ( requestURI.contains("#") )
+        {
+            // No frag ids.
+            int i = requestURI.indexOf('#') ;
+            requestURI = requestURI.substring(0,i) ;
+        }
+        return requestURI ;
+    }
+
+    private static String determineBaseIRI(String requestURI)
+    {
+        // Technically wrong, but including the query string is "unhelpful"
+        String baseIRI = requestURI ;
+        if ( requestURI.contains("?") )
+        {
+            // No frag ids.
+            int i = requestURI.indexOf('?') ;
+            baseIRI = requestURI.substring(0,i) ;
+        }
+        return baseIRI ;
+    }
+
+    private static void httpResponse(long id, HttpResponse response, String baseIRI, Map<String,
HttpResponseHandler> handlers) throws IllegalStateException, IOException
+    {
+        if ( response == null )
+            return ;
+        try {
+            StatusLine statusLine = response.getStatusLine() ;
+            if ( statusLine.getStatusCode() >= 400 )
+            {
+                log.debug(format("[%d] %s %s",id, statusLine.getStatusCode(), statusLine.getReasonPhrase()))
;
+                throw new HttpException(statusLine.getStatusCode()+" "+statusLine.getReasonPhrase())
;
+            }
+
+            String contentType = response.getFirstHeader(HttpNames.hContentType).getValue()
;
+            MediaType mt = new MediaType(contentType) ;
+            String ct = mt.getContentType() ;
+            if ( log.isDebugEnabled() )
+                log.debug(format("[%d] %d %s :: %s",id, statusLine.getStatusCode(), statusLine.getReasonPhrase()
, mt)) ;
+
+            HttpResponseHandler handler = handlers.get(ct) ;
+            if ( handler == null )
+                // backstop
+                handler = handlers.get("*") ;
+            if ( handler != null )
+                handler.handle(ct, baseIRI, response) ;
+            else
+                log.warn(format("[%d] No handler found for %s", id, mt));
+        } finally { closeEntity(response.getEntity()) ; }
+    }
+    
+//    // Jena 2.6.4 bug.
+//    
+//    public static String readWholeFileAsUTF8(InputStream in) throws IOException
+//    {
+//        Reader r = new BufferedReader(FileUtils.asUTF8(in),1024) ;
+//        return readWholeFileAsUTF8(r) ;
+//    }
+//    
+//    
+//    // Private worker as we are trying to force UTF-8. 
+//    private static String readWholeFileAsUTF8(Reader r) throws IOException
+//    {
+//        StringWriter sw = new StringWriter(1024);
+//        char buff[] = new char[1024];
+//        int l ;
+//        while ( (l = r.read(buff)) != -1 ) {
+//            if (l <= 0)
+//                break;
+//            sw.write(buff, 0, l);
+//        }
+//        r.close();
+//        sw.close();
+//        return sw.toString();  
+//    }
+//    
+//    public static void main2(String...argv) throws Exception
+//    {
+//        String queryString =  "SELECT * { ?s ?p ?o } LIMIT 1" ;
+//        // HttpClient 4.1.2
+//        URI uri = URIUtils.createURI("http",
+//                                     "sparql.org",
+//                                     -1, 
+//                                     "books",
+//                                     "query="+URLEncoder.encode(queryString,"UTF-8"),
+//                                     null
+//                                     ) ;
+//        HttpGet httpget = new HttpGet(uri);
+//        httpget.addHeader("Accept", "application/sparql-results+json") ;
+//        
+//        System.out.println(httpget.getURI());
+//        
+//        DefaultHttpClient httpclient = new DefaultHttpClient();
+//
+//        HttpContext localContext = new BasicHttpContext();
+//        
+//        HttpResponse response = httpclient.execute(httpget, localContext) ;
+//        System.out.println(response.getFirstHeader("Content-type")) ;
+//        
+//        System.out.println(response.getStatusLine()) ;
+//        HttpEntity entity = response.getEntity();
+//        InputStream instream = entity.getContent() ;
+//        try {
+//            //entity = new BufferedHttpEntity(entity) ;
+//            String x = readWholeFileAsUTF8(instream) ;
+//            System.out.print(x) ;  
+//        } finally {
+//            instream.close();
+//        }
+//    }
+}
+

Propchange: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpOp.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpResponseLib.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpResponseLib.java?rev=1158648&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpResponseLib.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpResponseLib.java Wed Aug 17 12:01:16
2011
@@ -0,0 +1,120 @@
+/**
+ * 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 riot.web;
+
+import java.io.IOException ;
+import java.io.InputStream ;
+
+import org.apache.http.HttpEntity ;
+import org.apache.http.HttpResponse ;
+import org.openjena.atlas.io.IO ;
+import org.openjena.atlas.lib.Sink ;
+import org.openjena.atlas.web.MediaType ;
+import org.openjena.riot.RiotReader ;
+import org.openjena.riot.lang.LangRIOT ;
+import org.openjena.riot.lang.SinkTriplesToGraph ;
+import riot.web.HttpOp.* ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.util.graph.GraphFactory ;
+
+/** A collection of handlers fpr response handling.
+ * @See {@link HttpOp#execHttpGet(String, String, String, java.util.Map)}
+ * @See {@link HttpOp#execHttpPost(String, String, ContentProducer, String, java.util.Map)}
+ */
+public class HttpResponseLib
+{
+
+    private static abstract class AbstractGraphReader implements HttpCaptureResponse<Graph>
+    {
+        private Graph graph = null ;
+        //@Override
+        final public void handle(String contentType, String baseIRI, HttpResponse response)
+        {
+            try {
+                Graph g = GraphFactory.createDefaultGraph() ;
+                HttpEntity entity = response.getEntity() ;
+                MediaType mt = new MediaType(response.getFirstHeader(HttpNames.hContentType).getValue())
;
+                mt.getCharset() ;
+                Sink<Triple> sink = new SinkTriplesToGraph(g) ; 
+                InputStream in = entity.getContent() ;
+                LangRIOT parser = createParser(in, baseIRI, sink) ;
+                parser.parse() ;
+                in.close() ;
+                this.graph = g ; 
+            } catch (IOException ex) { IO.exception(ex) ; }
+        }
+    
+        //@Override
+        public Graph get() { return graph ; }
+        
+        abstract protected LangRIOT createParser(InputStream in, String baseIRI, Sink<Triple>
sink) ;
+    }
+
+    public static HttpResponseHandler httpDumpResponse = new HttpResponseHandler()
+    {
+        //@Override
+        public void handle(String contentType, String baseIRI, HttpResponse response)
+        {
+            try {
+                HttpEntity entity = response.getEntity() ;
+                InputStream in = entity.getContent() ;
+                int l ;
+                byte buffer[] = new byte[1024] ;
+                while( (l=in.read(buffer)) != -1 )
+                {
+                    System.out.print(new String(buffer, 0, l, "UTF-8")) ;
+                }
+                    
+    
+                in.close() ;
+            } catch (IOException ex)
+            {
+                ex.printStackTrace(System.err) ;
+            }
+        }
+    } ;
+    public static HttpCaptureResponse<Graph> graphReaderTurtle = new AbstractGraphReader()
+    {
+        @Override
+        protected LangRIOT createParser(InputStream in, String baseIRI, Sink<Triple>
sink)
+        {
+            return RiotReader.createParserTurtle(in, baseIRI, sink) ;
+        }
+    } ;
+    public static HttpCaptureResponse<Graph> graphReaderNTriples = new AbstractGraphReader()
+    {
+        @Override
+        protected LangRIOT createParser(InputStream in, String baseIRI, Sink<Triple>
sink)
+        {
+            return RiotReader.createParserNTriples(in, sink) ;
+        }
+    } ;
+    public static HttpCaptureResponse<Graph> graphReaderRDFXML = new AbstractGraphReader()
+    {
+        @Override
+        protected LangRIOT createParser(InputStream in, String baseIRI, Sink<Triple>
sink)
+        {
+            return RiotReader.createParserRDFXML(in, baseIRI, sink) ;
+        }
+    } ;
+
+}
+

Propchange: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/HttpResponseLib.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java?rev=1158648&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java Wed Aug 17 12:01:16 2011
@@ -0,0 +1,139 @@
+/**
+ * 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 riot.web;
+
+import java.io.IOException ;
+import java.io.InputStream ;
+import java.io.OutputStream ;
+import java.net.URLEncoder ;
+import java.util.ArrayList ;
+import java.util.HashMap ;
+import java.util.List ;
+import java.util.Map ;
+
+import org.apache.http.HttpResponse ;
+import org.openjena.atlas.iterator.Iter ;
+import org.openjena.atlas.lib.StrUtils ;
+import org.openjena.atlas.logging.Log ;
+import org.openjena.atlas.web.MediaType ;
+import org.openjena.riot.WebContent ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFactory ;
+import com.hp.hpl.jena.query.ResultSetFormatter ;
+import com.hp.hpl.jena.sparql.resultset.ResultsFormat ;
+
+public class RunHTTP
+{
+    static String divider = "----------------------------------------" ;
+    static String nextDivider = null ;
+    static void divider()
+    {
+        if ( nextDivider != null )
+            System.out.println(nextDivider) ;
+        nextDivider = divider ;
+    }
+    
+    static { Log.setLog4j() ; }
+
+    public static void exit(int code)
+    {
+        System.out.flush() ;
+        System.out.println("DONE") ;
+        System.exit(code) ;
+    }
+
+    public static void main(String ... args) throws Exception
+    {
+        final String queryString1 =  "SELECT * { ?s ?p ?o } LIMIT 1" ;
+        final String queryString2 =  "SELECT * { ?s ?p ?o } LIMIT 10" ;
+        
+        // GET graph
+        Map<String, HttpOp.HttpResponseHandler> handlers = new HashMap<String, HttpOp.HttpResponseHandler>()
;
+        // Chnage to one handler for all graph types.
+        handlers.put(WebContent.contentTypeTurtle, HttpResponseLib.graphReaderTurtle) ;
+        handlers.put(WebContent.contentTypeRDFXML, HttpResponseLib.graphReaderRDFXML) ;
+        handlers.put("*", HttpResponseLib.httpDumpResponse) ;
+
+        // RS support
+//        public static ResultsFormat contentTypeToResultSet(String contentType) { return
mapContentTypeToResultSet.get(contentType) ; }
+        final Map<String, ResultsFormat> mapContentTypeToResultSet = new HashMap<String,
ResultsFormat>() ;
+        {
+            mapContentTypeToResultSet.put(WebContent.contentTypeResultsXML, ResultsFormat.FMT_RS_XML)
;
+            mapContentTypeToResultSet.put(WebContent.contentTypeResultsJSON, ResultsFormat.FMT_RS_JSON)
;
+            mapContentTypeToResultSet.put(WebContent.contentTypeTextTSV, ResultsFormat.FMT_RS_TSV)
;
+        }
+
+        HttpOp.HttpCaptureResponse<ResultSet> captureRS = new HttpOp.HttpCaptureResponse<ResultSet>(){
+            ResultSet rs = null ;
+            //@Override
+            public void handle(String contentType, String baseIRI, HttpResponse response)
throws IOException
+            {
+                MediaType mt = new MediaType(contentType) ;
+                ResultsFormat fmt = mapContentTypeToResultSet.get(contentType) ; // contentTypeToResultSet(contentType)
;
+                InputStream in = response.getEntity().getContent() ;
+                rs = ResultSetFactory.load(in, fmt) ;
+                // Force reading
+                rs = ResultSetFactory.copyResults(rs) ;
+            }
+
+            //@Override
+            public ResultSet get()
+            {
+                return rs ;
+            }} ;
+        
+        handlers.put(WebContent.contentTypeResultsXML, captureRS) ;             
+        handlers.put(WebContent.contentTypeResultsJSON, captureRS) ;            
+        handlers.put(WebContent.contentTypeTextTSV, captureRS) ;     
+        
+        List<String> acceptables = new ArrayList<String>() ;
+        acceptables.add("text/turtle;q=0.8") ;
+        acceptables.add("application/rdf+xml;q=0.1") ;
+        String acceptHeader = Iter.asString(acceptables, " , ") ;
+        
+        HttpOp.execHttpGet("http://localhost:3030/ds/sparql?query="+URLEncoder.encode(queryString1,"UTF-8"),
+                           acceptHeader,
+                           handlers) ;
+        
+        ResultSetFormatter.out(captureRS.get()) ;
+
+        
+        HttpOp.ContentProducer cp = new HttpOp.ContentProducer() {
+            //@Override
+            public void writeTo(OutputStream outstream) throws IOException
+            {
+                outstream.write(StrUtils.asUTF8bytes(queryString2)) ;
+                //outstream.flush() ;
+            }} ;
+         
+        List<String> acceptResultSets = new ArrayList<String>() ;
+        acceptResultSets.add(WebContent.contentTypeResultsJSON+";q=0.9") ;
+        acceptResultSets.add(WebContent.contentTypeResultsXML+";q=0.5") ;
+        acceptResultSets.add(WebContent.contentTypeTextTSV+";q=0.8") ;
+        String acceptResultSet = Iter.asString(acceptResultSets, " , ") ;
+            
+        HttpOp.execHttpPost("http://localhost:3030/ds/sparql",
+                            WebContent.contentTypeSPARQLQuery, cp,
+                            acceptResultSet, handlers) ;
+        ResultSetFormatter.out(captureRS.get()) ;
+        exit(0) ;
+
+    }
+}

Propchange: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: incubator/jena/Jena2/ARQ/trunk/src-dev/riot/web/RunHTTP.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message