jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rve...@apache.org
Subject svn commit: r1301098 - in /incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http: HttpQuery.java QueryEngineHTTP.java
Date Thu, 15 Mar 2012 17:12:59 GMT
Author: rvesse
Date: Thu Mar 15 17:12:58 2012
New Revision: 1301098

URL: http://svn.apache.org/viewvc?rev=1301098&view=rev
Log:
Some clean up of my previous patch for QueryEngineHTTP - regardless of what formats the user
requests we now attempt to respect the returned Content-Type when parsing the response, if
the returned Content-Type is not supported an exception is thrown which notes what was returned
and that is was not supported

Modified:
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java

Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java?rev=1301098&r1=1301097&r2=1301098&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java
(original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/HttpQuery.java
Thu Mar 15 17:12:58 2012
@@ -127,22 +127,40 @@ public class HttpQuery extends Params
     
     /** Set the content type (Accept header) for the results
      */
-    
     public void setAccept(String contentType)
     {
         contentTypeResult = contentType ;
     }
     
+    /**
+     * Gets the Content Type, if the query has been made this reflects the Content-Type header
returns, if it has not been made this reflects only the Accept header that will be sent (as
set via the {@link #setAccept(String)} method)
+     */
+    public String getContentType()
+    {
+    	return contentTypeResult;
+    }
+    
+    /**
+     * Sets whether the HTTP request will include a Accept-Encoding: gzip header
+     */
     public void setAllowGZip(boolean allow)
     {
     	allowGZip = allow;
     }
     
+    /**
+     * Sets whether the HTTP request will include a Accept-Encoding: deflate header
+     */
     public void setAllowDeflate(boolean allow)
     {
     	allowDeflate = allow;
     }
     
+    /**
+     * Sets basic authentication
+     * @param user Username
+     * @param password Password
+     */
     public void setBasicAuthentication(String user, char[] password)
     {
         this.user = user ;
@@ -412,11 +430,18 @@ public class HttpQuery extends Params
             //httpConnection.setReadTimeout(10) ;
             InputStream in = httpConnection.getInputStream() ;
 
-            String x$ = httpConnection.getContentType() ;
-            String y$ = httpConnection.getContentEncoding() ;
+            //Get the returned content type so we can expose this later via the getContentType()
method
+            //We strip any parameters off the returned content type e.g. ;charset=UTF-8 since
code that
+            //consumes our getContentType() method will expect a bare MIME type
+            contentTypeResult = httpConnection.getContentType() ;
+            if (contentTypeResult.contains(";"))
+            {
+            	contentTypeResult = contentTypeResult.substring(0, contentTypeResult.indexOf(';'));
+            }
+            
             
             //If compression was enabled and we got a compressed response as indicated by
the presence of
-            //a Transfer-Encoding header we need to ensure the input stream is appropriately
wrapped in
+            //a Content-Encoding header we need to ensure the input stream is appropriately
wrapped in
             //the relevant stream type but checking that the JVM hasn't been clever enough
to do
             //this for us already
             String contentEnc = httpConnection.getContentEncoding() ;

Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java?rev=1301098&r1=1301097&r2=1301098&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
(original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
Thu Mar 15 17:12:58 2012
@@ -124,10 +124,10 @@ public class QueryEngineHTTP implements 
 
     @Override
     public void setInitialBinding(QuerySolution binding)
-    { throw new UnsupportedOperationException("Initial bindings not supported for remote
queries") ; }
+    { throw new UnsupportedOperationException("Initial bindings not supported for remote
queries, consider using a ParameterizedSparqlString to prepare a query for remote execution")
; }
     
     public void setInitialBindings(ResultSet table)
-    { throw new UnsupportedOperationException("Initial bindings not supported for remote
queries") ; }
+    { throw new UnsupportedOperationException("Initial bindings not supported for remote
queries, consider using a ParameterizedSparqlString to prepare a query for remote execution")
; }
     
     /**  @param defaultGraphURIs The defaultGraphURIs to set. */
     public void setDefaultGraphURIs(List<String> defaultGraphURIs)
@@ -196,7 +196,6 @@ public class QueryEngineHTTP implements 
     public ResultSet execSelect()
     {
         HttpQuery httpQuery = makeHttpQuery() ;
-        // TODO Allow other content types.
         httpQuery.setAccept(selectContentType) ;
         InputStream in = httpQuery.exec() ;
         
@@ -209,16 +208,28 @@ public class QueryEngineHTTP implements 
         }
         
         retainedConnection = in; // This will be closed on close()
+        
         //TODO: Find a way to auto-detect how to create the ResultSet based on the content
type in use
-        if (selectContentType.equals(WebContent.contentTypeResultsXML))
+        
+        //Don't assume the endpoint actually gives back the content type we asked for
+        String actualContentType = httpQuery.getContentType();
+        
+        //If the server fails to return a Content-Type then we will assume
+        //the server returned the type we asked for
+        if (actualContentType == null || actualContentType.equals(""))
+        {
+        	actualContentType = selectContentType;
+        }
+        
+        if (actualContentType.equals(WebContent.contentTypeResultsXML))
             return ResultSetFactory.fromXML(in);
-        if (selectContentType.equals(WebContent.contentTypeResultsJSON))
-            return  ResultSetFactory.fromJSON(in);
-        if (selectContentType.equals(WebContent.contentTypeTextTSV))
+        if (actualContentType.equals(WebContent.contentTypeResultsJSON))
+            return ResultSetFactory.fromJSON(in); 
+        if (actualContentType.equals(WebContent.contentTypeTextTSV))
             return ResultSetFactory.fromTSV(in);
-        if (selectContentType.equals(WebContent.contentTypeTextCSV))
+        if (actualContentType.equals(WebContent.contentTypeTextCSV))
             return CSVInput.fromCSV(in);
-        throw new QueryException("SELECT Content-Type is not yet supported by this query
engine");
+        throw new QueryException("Endpoint returned Content-Type: " + actualContentType +
" which is not currently supported for SELECT queries");
     }
 
     @Override
@@ -239,9 +250,19 @@ public class QueryEngineHTTP implements 
         httpQuery.setAccept(modelContentType) ;
         InputStream in = httpQuery.exec() ;
         
+        //Don't assume the endpoint actually gives back the content type we asked for
+        String actualContentType = httpQuery.getContentType();
+        
+        //If the server fails to return a Content-Type then we will assume
+        //the server returned the type we asked for
+        if (actualContentType == null || actualContentType.equals(""))
+        {
+        	actualContentType = modelContentType;
+        }
+        
         //Try to select language appropriately here based on the model content type
-        Lang lang = WebContent.contentTypeToLang(modelContentType);
-        if (!lang.isTriples()) throw new QueryException("Content Type returned is not a valid
RDF Graph syntax");
+        Lang lang = WebContent.contentTypeToLang(actualContentType);
+        if (!lang.isTriples()) throw new QueryException("Endpoint returned Content Type:
" + actualContentType + " is not a valid RDF Graph syntax");
         model.read(in, null, lang.getName()) ; 
         
         return model ;
@@ -255,12 +276,22 @@ public class QueryEngineHTTP implements 
         InputStream in = httpQuery.exec() ;
 
         try {
+            //Don't assume the endpoint actually gives back the content type we asked for
+            String actualContentType = httpQuery.getContentType();
+            
+            //If the server fails to return a Content-Type then we will assume
+            //the server returned the type we asked for
+            if (actualContentType == null || actualContentType.equals(""))
+            {
+            	actualContentType = askContentType;
+            }
+        	
             //Parse the result appropriately depending on the selected content type
             if (askContentType.equals(WebContent.contentTypeResultsXML))
                 return XMLInput.booleanFromXML(in) ;
             if (askContentType.equals(WebContent.contentTypeResultsJSON))
                 return JSONInput.booleanFromJSON(in) ;
-            throw new QueryException("ASK Content-Type is not yet supported by this query
engine");
+            throw new QueryException("Endpoint returned Content-Type: " + actualContentType
+ " which is not currently supported for ASK queries");
         } finally {
             // Ensure connection is released
             try { in.close(); }



Mime
View raw message