jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aj...@apache.org
Subject [1/2] jena git commit: JENA-1604: transform HTTP requests
Date Sat, 15 Sep 2018 14:16:13 GMT
Repository: jena
Updated Branches:
  refs/heads/master cf08538d9 -> 726f32ca0


JENA-1604: transform HTTP requests


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/18a23ca2
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/18a23ca2
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/18a23ca2

Branch: refs/heads/master
Commit: 18a23ca279b3025d4ec12c2977d634c9b86b00f6
Parents: cf08538
Author: ajs6f <ajs6f@apache.org>
Authored: Fri Sep 14 14:25:25 2018 -0400
Committer: ajs6f <ajs6f@apache.org>
Committed: Fri Sep 14 14:25:25 2018 -0400

----------------------------------------------------------------------
 .../java/org/apache/jena/riot/web/HttpOp.java   | 31 ++++++++++++++------
 .../jena/riot/web/HttpRequestTransformer.java   | 29 ++++++++++++++++++
 .../apache/jena/riot/web/HttpResponseLib.java   |  9 ++----
 3 files changed, 53 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/18a23ca2/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
index 80eaf01..173a91d 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
@@ -120,12 +120,14 @@ public class HttpOp {
     /**
      * User-Agent header to use
      */
-    static private String userAgent = ARQ_USER_AGENT;
+    private static String userAgent = ARQ_USER_AGENT;
 
     /**
      * "Do nothing" response handler.
      */
-    static private HttpResponseHandler nullHandler = HttpResponseLib.nullResponse;
+    private static HttpResponseHandler nullHandler = HttpResponseLib.nullResponse;
+
+	private static HttpRequestTransformer reqTransformer = null;
 
     /** Capture response as a string (UTF-8 assumed) */
     public static class CaptureString implements HttpCaptureResponse<String> {
@@ -204,7 +206,19 @@ public class HttpOp {
     public static void setDefaultHttpClient(HttpClient client) {
         defaultHttpClient = firstNonNull(client, initialDefaultHttpClient);
     }
-    
+
+    /**
+     * Setting an {@link HttpRequestTransformer} allows manipulation or enhancement of HTTP
requests.
+     * 
+     * @param tform HttpRequestTransformer to use, {@code null} for none (the default)
+     * @return the previous HttpRequestTransformer in use
+     */
+    public static HttpRequestTransformer setRequestTransformer(HttpRequestTransformer tform)
{
+    	HttpRequestTransformer tmp = reqTransformer;
+    	reqTransformer = tform;
+    	return tmp;
+    }
+
     /**
      * Create an HttpClient that performs connection pooling. This can be used
      * with {@link #setDefaultHttpClient} or provided in the HttpOp calls.
@@ -1058,15 +1072,15 @@ public class HttpOp {
     }
 
     // ---- Perform the operation!
-    private static void exec(String url, HttpUriRequest request, String acceptHeader, HttpResponseHandler
handler, HttpClient httpClient, HttpContext httpContext) {
-        // whether we should close the client after request execution
-        // only true if we built the client right here
+    private static void exec(String url, HttpUriRequest req, String acceptHeader, HttpResponseHandler
handler, HttpClient httpClient, HttpContext httpContext) {
+        // we should close the client after request execution if we built the client right
here
         httpClient = firstNonNull(httpClient, getDefaultHttpClient());
-        // and also only true if the handler won't close the client for us
+        // or if the handler won't close the client for us
         try {
             if (handler == null)
                 // This cleans up left-behind streams
                 handler = nullHandler;
+            HttpUriRequest request = reqTransformer  == null ? req : reqTransformer.apply(req);
 
             long id = counter.incrementAndGet();
             String baseURI = determineBaseIRI(url);
@@ -1085,8 +1099,7 @@ public class HttpOp {
             int statusCode = statusLine.getStatusCode();
             if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
                 log.debug(format("[%d] %s %s", id, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
-                // Error responses can have bodies so it is important to clear
-                // up.
+                // Error responses can have bodies so it is important to clear up.
 				final String contentPayload = readPayload(response.getEntity());
 				throw new HttpException(statusLine.getStatusCode(), statusLine.getReasonPhrase(), contentPayload);
             }

http://git-wip-us.apache.org/repos/asf/jena/blob/18a23ca2/jena-arq/src/main/java/org/apache/jena/riot/web/HttpRequestTransformer.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpRequestTransformer.java b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpRequestTransformer.java
new file mode 100644
index 0000000..f45cb67
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpRequestTransformer.java
@@ -0,0 +1,29 @@
+/*
+ * 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 org.apache.jena.riot.web;
+
+import java.util.function.Function;
+
+import org.apache.http.client.methods.HttpUriRequest;
+
+/**
+ * Transform an HTTP request (e.g. to add or manipulate headers)
+ *
+ */
+public interface HttpRequestTransformer extends Function<HttpUriRequest, HttpUriRequest>
{}

http://git-wip-us.apache.org/repos/asf/jena/blob/18a23ca2/jena-arq/src/main/java/org/apache/jena/riot/web/HttpResponseLib.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpResponseLib.java b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpResponseLib.java
index a249493..a9991bf 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpResponseLib.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpResponseLib.java
@@ -124,13 +124,8 @@ public class HttpResponseLib
     } ;
     
     /** Consume a response quietly. */
-    public static HttpResponseHandler nullResponse = new HttpResponseHandler() {
-        @Override
-        public void handle(String baseIRI , HttpResponse response ) {
-            EntityUtils.consumeQuietly(response.getEntity()) ;
-        }
-    } ;
-    
+    public static HttpResponseHandler nullResponse = (b, r) -> EntityUtils.consumeQuietly(r.getEntity());
+
     // Old world.
     // See also ResultSetFactory.load(in, fmt) 
     private static ResultsFormat contentTypeToResultsFormat(String contentType) { return
mapContentTypeToResultSet.get(contentType) ; }


Mime
View raw message