incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stefane...@apache.org
Subject svn commit: r1565164 - in /sling/trunk/bundles/extensions/discovery/impl/src/main: java/org/apache/sling/discovery/impl/ java/org/apache/sling/discovery/impl/topology/connector/ resources/OSGI-INF/metatype/
Date Thu, 06 Feb 2014 11:14:52 GMT
Author: stefanegli
Date: Thu Feb  6 11:14:52 2014
New Revision: 1565164

URL: http://svn.apache.org/r1565164
Log:
SLING-3367 : gzip-ing request (if configured) and response (if client supplies Accept-Encoding:
gzip)

Modified:
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/TopologyWebConsolePlugin.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClientInformation.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorServlet.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java
Thu Feb  6 11:14:52 2014
@@ -103,6 +103,12 @@ public class Config {
     private static final String AUTO_STOP_LOCAL_LOOP_ENABLED = "autoStopLocalLoopEnabled";
 
     /**
+     * If set to true, request body will be gzipped - only works if counter-part accepts
gzip-requests!
+     */
+    @Property(boolValue=false)
+    private static final String GZIP_CONNECTOR_REQUESTS_ENABLED = "gzipConnectorRequestsEnabled";
+
+    /**
      * If set to true, hmac is enabled and the white list is disabled.
      */
     @Property(boolValue=false)
@@ -152,6 +158,11 @@ public class Config {
      * true when encryption is enabled.
      */
     private boolean encryptionEnabled;
+    
+    /**
+     * true when topology connector requests should be gzipped
+     */
+    private boolean gzipConnectorRequestsEnabled;
 
     @Activate
     protected void activate(final Map<String, Object> properties) {
@@ -235,6 +246,7 @@ public class Config {
                 this.leaderElectionRepositoryDescriptor);
 
         autoStopLocalLoopEnabled = PropertiesUtil.toBoolean(properties.get(AUTO_STOP_LOCAL_LOOP_ENABLED),
false);
+        gzipConnectorRequestsEnabled = PropertiesUtil.toBoolean(properties.get(GZIP_CONNECTOR_REQUESTS_ENABLED),
false);
         
         hmacEnabled = PropertiesUtil.toBoolean(properties.get(HMAC_ENABLED), true);
         encryptionEnabled = PropertiesUtil.toBoolean(properties.get(ENCRYPTION_ENABLED),
false);
@@ -361,6 +373,14 @@ public class Config {
     }
     
     /**
+     * @return true if requests on the topology connector should be gzipped
+     * (which only works if the server accepts that.. ie discovery.impl 1.0.4+)
+     */
+    public boolean isGzipConnectorRequestsEnabled() {
+        return gzipConnectorRequestsEnabled;
+    }
+    
+    /**
      * @return true if the auto-stopping of local-loop topology connectors is enabled.
      */
     public boolean isAutoStopLocalLoopEnabled() {

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/TopologyWebConsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/TopologyWebConsolePlugin.java?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/TopologyWebConsolePlugin.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/TopologyWebConsolePlugin.java
Thu Feb  6 11:14:52 2014
@@ -370,6 +370,8 @@ public class TopologyWebConsolePlugin ex
         pw.println("<th class=\"header ui-widget-header\">Connector url</th>");
         pw.println("<th class=\"header ui-widget-header\">Connected to slingId</th>");
         pw.println("<th class=\"header ui-widget-header\">Connector status</th>");
+        pw.println("<th class=\"header ui-widget-header\">Request encoding</th>");
+        pw.println("<th class=\"header ui-widget-header\">Response encoding</th>");
         // pw.println("<th class=\"header ui-widget-header\">Fallback connector urls</th>");
         pw.println("</tr>");
         pw.println("</thead>");
@@ -426,6 +428,8 @@ public class TopologyWebConsolePlugin ex
                 pw.println("<td><b>not connected</b></td>");
                 pw.println("<td"+tooltip+"><b>not ok (HTTP Status-Code: "+statusCode+",
"+statusDetails+")</b></td>");
             }
+            pw.println("<td>"+topologyConnectorClient.getLastRequestEncoding()+"</td>");
+            pw.println("<td>"+topologyConnectorClient.getLastResponseEncoding()+"</td>");
             // //TODO fallback urls are not yet implemented!
             // String fallbackConnectorUrls;
             // List<String> urls = topologyConnectorClient

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
Thu Feb  6 11:14:52 2014
@@ -18,19 +18,23 @@
  */
 package org.apache.sling.discovery.impl.topology.connector;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.URL;
 import java.util.Iterator;
 import java.util.UUID;
+import java.util.zip.GZIPOutputStream;
 
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
+import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.URIException;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
 import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
@@ -89,6 +93,12 @@ public class TopologyConnectorClient imp
 
     private TopologyRequestValidator requestValidator;
 
+    /** value of Content-Encoding of the last request **/
+    private String lastRequestEncoding;
+
+    /** value of Content-Encoding of the last repsonse **/
+    private String lastResponseEncoding;
+
     TopologyConnectorClient(final ClusterViewService clusterViewService,
             final AnnouncementRegistry announcementRegistry, final Config config,
             final URL connectorUrl, final String serverInfo) {
@@ -163,12 +173,30 @@ public class TopologyConnectorClient imp
                 }
             });
             final String p = requestValidator.encodeMessage(topologyAnnouncement.asJSON());
-
-        	if (logger.isDebugEnabled()) {
-        		logger.debug("ping: topologyAnnouncement json is: " + p);
-        	}
-        	requestValidator.trustMessage(method, p);
-            method.setRequestEntity(new StringRequestEntity(p, "application/json", "UTF-8"));
+            
+            if (logger.isDebugEnabled()) {
+                logger.debug("ping: topologyAnnouncement json is: " + p);
+            }
+            requestValidator.trustMessage(method, p);
+            if (config.isGzipConnectorRequestsEnabled()) {
+                // tell the server that the content is gzipped:
+                method.addRequestHeader("Content-Encoding", "gzip");
+                // and gzip the body:
+                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                final GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
+                gzipOut.write(p.getBytes("UTF-8"));
+                gzipOut.close();
+                final byte[] gzippedEncodedJson = baos.toByteArray();
+                method.setRequestEntity(new ByteArrayRequestEntity(gzippedEncodedJson, "application/json"));
+                lastRequestEncoding = "gzip";
+            } else {
+                // otherwise plaintext:
+                method.setRequestEntity(new StringRequestEntity(p, "application/json", "UTF-8"));
+                lastRequestEncoding = "plaintext";
+            }
+            // independent of request-gzipping, we do accept the response to be gzipped,
+            // so indicate this to the server:
+            method.addRequestHeader("Accept-Encoding", "gzip");
             DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0,
false);
             httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);
             httpClient.executeMethod(method);
@@ -177,7 +205,15 @@ public class TopologyConnectorClient imp
 	                    + method.getStatusText());
         	}
             lastStatusCode = method.getStatusCode();
+            lastResponseEncoding = null;
             if (method.getStatusCode()==HttpServletResponse.SC_OK) {
+                final Header contentEncoding = method.getResponseHeader("Content-Encoding");
+                if (contentEncoding!=null && contentEncoding.getValue()!=null &&
+                        contentEncoding.getValue().contains("gzip")) {
+                    lastResponseEncoding = "gzip";
+                } else {
+                    lastResponseEncoding = "plaintext";
+                }
                 String responseBody = requestValidator.decodeMessage(method); // limiting
to 16MB, should be way enough
             	if (logger.isDebugEnabled()) {
             		logger.debug("ping: response body=" + responseBody);
@@ -300,7 +336,15 @@ public class TopologyConnectorClient imp
     public boolean isAutoStopped() {
     	return autoStopped;
     }
+    
+    public String getLastRequestEncoding() {
+        return lastRequestEncoding==null ? "" : lastRequestEncoding;
+    }
 
+    public String getLastResponseEncoding() {
+        return lastResponseEncoding==null ? "" : lastResponseEncoding;
+    }
+    
     public String getRemoteSlingId() {
         if (lastInheritedAnnouncement == null) {
             return null;

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClientInformation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClientInformation.java?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClientInformation.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClientInformation.java
Thu Feb  6 11:14:52 2014
@@ -49,4 +49,10 @@ public interface TopologyConnectorClient
     /** the unique id of this connector **/
     String getId();
 
+    /** the Content-Encoding of the last request **/
+    String getLastRequestEncoding();
+
+    /** the Content-Encoding of the last response **/
+    String getLastResponseEncoding();
+
 }

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorServlet.java?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorServlet.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorServlet.java
Thu Feb  6 11:14:52 2014
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.discovery.impl.topology.connector;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -26,11 +27,13 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.zip.GZIPOutputStream;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.sling.SlingServlet;
@@ -262,9 +265,22 @@ public class TopologyConnectorServlet ex
             }
             final String p = requestValidator.encodeMessage(replyAnnouncement.asJSON());
             requestValidator.trustMessage(response, request, p);
-            final PrintWriter pw = response.getWriter();
-            pw.print(p);
-            pw.flush();
+            // gzip the response if the client accepts this
+            final String acceptEncodingHeader = request.getHeader("Accept-Encoding");
+            if (acceptEncodingHeader!=null && acceptEncodingHeader.contains("gzip"))
{
+                // tell the client that the content is gzipped:
+                response.setHeader("Content-Encoding", "gzip");
+                
+                // then gzip the body
+                final GZIPOutputStream gzipOut = new GZIPOutputStream(response.getOutputStream());
+                gzipOut.write(p.getBytes("UTF-8"));
+                gzipOut.close();
+            } else {
+                // otherwise plaintext
+                final PrintWriter pw = response.getWriter();
+                pw.print(p);
+                pw.flush();
+            }
         } catch (JSONException e) {
             logger.error("doPost: Got a JSONException: " + e, e);
             response.sendError(500);

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
Thu Feb  6 11:14:52 2014
@@ -34,6 +34,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.zip.GZIPInputStream;
 
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
@@ -535,7 +536,17 @@ public class TopologyRequestValidator {
      * @throws IOException
      */
     private String getRequestBody(HttpServletRequest request) throws IOException {
-        return IOUtils.toString(request.getReader());
+        final String contentEncoding = request.getHeader("Content-Encoding");
+        if (contentEncoding!=null && contentEncoding.contains("gzip")) {
+            // then treat the request body as gzip:
+            final GZIPInputStream gzipIn = new GZIPInputStream(request.getInputStream());
+            final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
+            gzipIn.close();
+            return gunzippedEncodedJson;
+        } else {
+            // otherwise assume plain-text:
+            return IOUtils.toString(request.getReader());
+        }
     }
 
     /**
@@ -544,10 +555,21 @@ public class TopologyRequestValidator {
      * @throws IOException
      */
     private String getResponseBody(HttpMethod method) throws IOException {
-        if (method instanceof HttpMethodBase) {
-            return ((HttpMethodBase) method).getResponseBodyAsString(16 * 1024 * 1024);
+        final Header contentEncoding = method.getResponseHeader("Content-Encoding");
+        if (contentEncoding!=null && contentEncoding.getValue()!=null &&
+                contentEncoding.getValue().contains("gzip")) {
+            // then the server sent gzip - treat it so:
+            final GZIPInputStream gzipIn = new GZIPInputStream(method.getResponseBodyAsStream());
+            final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
+            gzipIn.close();
+            return gunzippedEncodedJson;
+        } else {
+            // otherwise the server sent plaintext:
+            if (method instanceof HttpMethodBase) {
+                return ((HttpMethodBase) method).getResponseBodyAsString(16 * 1024 * 1024);
+            }
+            return method.getResponseBodyAsString();
         }
-        return method.getResponseBodyAsString();
     }
 
     /**

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1565164&r1=1565163&r2=1565164&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties
Thu Feb  6 11:14:52 2014
@@ -63,6 +63,12 @@ autoStopLocalLoopEnabled.description = I
  topology connector, the corresponding topology connector will be automatically stopped.
\
  This is useful to prevent unnecessary loops with eg pre-configured topology connectors.
 
+gzipConnectorRequestsEnabled.name = Enable GZip-ing requests
+gzipConnectorRequestsEnabled.description = If true, the payloads of topology connector requests
\
+ will be gzipped. This is advisable on certain connector structures, eg in a tree structure,
where \
+ a topology connector announces a large sub-topology. Note that this only works with \
+ the server running discovery.impl 1.0.4 and onwards.
+
 hmacEnabled.name = Enable Hmac message signatures
 hmacEnabled.description = If true, and the Shared Key is set to the same value on all members
of the \
  topology, the messages will be validated using a HMAC of a digest of the body of the message.
\



Mime
View raw message