Author: [email protected]
Date: Mon Apr  4 09:38:08 2011
New Revision: 930

Log:
[sandbox] httpproxy: align to trunk version

Added:
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/pom.xml
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/osgi/
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/osgi/Activator.java
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/HttpForwardProxyServlet.java
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/HttpProxyServletContext.java
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/test/
   sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/test/java/
Modified:
   sandbox/bdekruijff/amdatu-httpproxy/clientservice/pom.xml
   sandbox/bdekruijff/amdatu-httpproxy/pom.xml
   sandbox/bdekruijff/amdatu-httpproxy/proxyservice/pom.xml
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/CachingHttpClientfactory.java
   
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/FSHttpCacheStorage.java

Modified: sandbox/bdekruijff/amdatu-httpproxy/clientservice/pom.xml
==============================================================================
--- sandbox/bdekruijff/amdatu-httpproxy/clientservice/pom.xml   (original)
+++ sandbox/bdekruijff/amdatu-httpproxy/clientservice/pom.xml   Mon Apr  4 
09:38:08 2011
@@ -5,7 +5,7 @@
   <parent>
     <groupId>org.amdatu.httpproxy</groupId>
     <artifactId>httpproxy</artifactId>
-    <version>0.0.6-SNAPSHOT</version>
+    <version>0.2.0-SNAPSHOT</version>
   </parent>
   <artifactId>clientservice</artifactId>
   <packaging>bundle</packaging>

Modified: sandbox/bdekruijff/amdatu-httpproxy/pom.xml
==============================================================================
--- sandbox/bdekruijff/amdatu-httpproxy/pom.xml (original)
+++ sandbox/bdekruijff/amdatu-httpproxy/pom.xml Mon Apr  4 09:38:08 2011
@@ -5,7 +5,7 @@
   <parent>
     <groupId>org.amdatu</groupId>
     <artifactId>amdatu</artifactId>
-    <version>0.0.6-SNAPSHOT</version>
+    <version>0.2.0-SNAPSHOT</version>
   </parent>
   <groupId>org.amdatu.httpproxy</groupId>
   <artifactId>httpproxy</artifactId>
@@ -16,6 +16,13 @@
   <dependencyManagement>
     <dependencies>
       <dependency>
+        <groupId>org.amdatu.web</groupId>
+        <artifactId>httpcontext</artifactId>
+        <version>0.1.0-SNAPSHOT</version>
+        <scope>provided</scope>
+        <type>bundle</type>
+      </dependency>
+      <dependency>
         <groupId>org.apache.httpcomponents</groupId>
         <artifactId>httpcore</artifactId>
         <version>4.1</version>
@@ -38,6 +45,7 @@
 
   <modules>
     <module>proxyservice</module>
+    <module>proxyservlet</module>
     <module>clientservice</module>
   </modules>
 </project>

Modified: sandbox/bdekruijff/amdatu-httpproxy/proxyservice/pom.xml
==============================================================================
--- sandbox/bdekruijff/amdatu-httpproxy/proxyservice/pom.xml    (original)
+++ sandbox/bdekruijff/amdatu-httpproxy/proxyservice/pom.xml    Mon Apr  4 
09:38:08 2011
@@ -5,7 +5,7 @@
   <parent>
     <groupId>org.amdatu.httpproxy</groupId>
     <artifactId>httpproxy</artifactId>
-    <version>0.0.6-SNAPSHOT</version>
+    <version>0.2.0-SNAPSHOT</version>
   </parent>
   <artifactId>proxyservice</artifactId>
   <packaging>bundle</packaging>

Modified: 
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/CachingHttpClientfactory.java
==============================================================================
--- 
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/CachingHttpClientfactory.java
        (original)
+++ 
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/CachingHttpClientfactory.java
        Mon Apr  4 09:38:08 2011
@@ -30,6 +30,7 @@
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.ProtocolVersion;
 import org.apache.http.client.cache.HttpCacheStorage;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.scheme.PlainSocketFactory;
@@ -55,37 +56,77 @@
                 new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
         ClientConnectionManager manager = new 
ThreadSafeClientConnManager(registry, 1000, TimeUnit.MILLISECONDS);
         DefaultHttpClient hc = new DefaultHttpClient(manager);
-        hc.addRequestInterceptor(new GzipHttpRequestInterceptor());
-        hc.addResponseInterceptor(new GzipHttpResponseInterceptor());
+        hc.addRequestInterceptor(new GzipEncodingRequestInterceptor());
+        hc.addResponseInterceptor(new GzipEncodingResponseInterceptor());
+        hc.addResponseInterceptor(new RFC2616ViaHeaderResponseInterceptor());
 
         // Configure with a simple filebased FS storage and a max object size.
         CacheConfig cc = new CacheConfig();
         cc.setMaxObjectSizeBytes(1024 * 1000);
         HttpCacheStorage hcs = new FSHttpCacheStorage(cacheDirectory);
-        return new CachingHttpClient(hc, hcs, cc);
 
+        CachingHttpClient chc = new CachingHttpClient(hc, hcs, cc);
+        return chc;
     }
 
-    static class GzipHttpRequestInterceptor implements HttpRequestInterceptor {
+    /**
+     * Add Via header to inform downstream servers about this proxy (RFC2616)
+     * 
+     * FIXME currently CachingHttpClient overwrites the header set by the 
backend
+     * client and it has no support for interceptors itself.
+     */
+    static class RFC2616ViaHeaderResponseInterceptor implements 
HttpResponseInterceptor {
+        public void process(final HttpResponse response, final HttpContext 
context) throws HttpException,
+            IOException {
+
+            // FIXME hardcoded servername/port and version
+            final ProtocolVersion pv = response.getProtocolVersion();
+            final String viaHeaderElement = String.format("%s/%d.%d 
localhost:3737 (Amdatu-HttpProxy/%s (servlet))",
+                    pv.getProtocol(), pv.getMajor(), pv.getMinor(), 
"0.1.0-SNAPSHOT");
+
+            if (!response.containsHeader("Via")) {
+                response.addHeader("Via", viaHeaderElement);
+            }
+            else {
+                Header header = response.getFirstHeader("Via");
+                response.removeHeader(header);
+                response.setHeader("Via", header.getValue() + ", " + 
viaHeaderElement);
+            }
+        }
+    }
+
+    /**
+     * If the client is not accepting any kind of encoding this interceptor 
will transparently
+     * add an accept for gzip encoding.
+     */
+    static class GzipEncodingRequestInterceptor implements 
HttpRequestInterceptor {
         public void process(final HttpRequest request, final HttpContext 
context) throws HttpException, IOException {
             if (!request.containsHeader("Accept-Encoding")) {
                 request.addHeader("Accept-Encoding", "gzip");
+                request.addHeader("X-Amdatu-encoding", "gzip");
             }
         }
     }
 
-    static class GzipHttpResponseInterceptor implements 
HttpResponseInterceptor {
+    /**
+     * If the accept encoding for gzip was added by the request interceptor 
and the response is gzip
+     * encoded this interceptor will transparently decode it.
+     */
+    static class GzipEncodingResponseInterceptor implements 
HttpResponseInterceptor {
         public void process(final HttpResponse response, final HttpContext 
context) throws HttpException,
             IOException {
-            HttpEntity entity = response.getEntity();
-            if (entity != null) {
-                Header ceheader = entity.getContentEncoding();
-                if (ceheader != null) {
-                    HeaderElement[] codecs = ceheader.getElements();
-                    for (int i = 0; i < codecs.length; i++) {
-                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
-                            response.setEntity(new 
GzipDecompressingEntity(response.getEntity()));
-                            return;
+            Header encHeader = response.getFirstHeader("X-Amdatu-encoding");
+            if (encHeader != null && "gzip".equals(encHeader.getValue())) {
+                HttpEntity entity = response.getEntity();
+                if (entity != null) {
+                    Header ceheader = entity.getContentEncoding();
+                    if (ceheader != null) {
+                        HeaderElement[] codecs = ceheader.getElements();
+                        for (int i = 0; i < codecs.length; i++) {
+                            if (codecs[i].getName().equalsIgnoreCase("gzip")) {
+                                response.setEntity(new 
GzipDecompressingEntity(response.getEntity()));
+                                return;
+                            }
                         }
                     }
                 }
@@ -93,6 +134,9 @@
         }
     }
 
+    /**
+     * Wrapper that decorates a gzipped entity with decompression.
+     */
     static class GzipDecompressingEntity extends HttpEntityWrapper {
 
         public GzipDecompressingEntity(final HttpEntity entity) {

Modified: 
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/FSHttpCacheStorage.java
==============================================================================
--- 
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/FSHttpCacheStorage.java
      (original)
+++ 
sandbox/bdekruijff/amdatu-httpproxy/proxyservice/src/main/java/org/amdatu/httpproxy/proxyservice/cache/FSHttpCacheStorage.java
      Mon Apr  4 09:38:08 2011
@@ -25,9 +25,6 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
 
 import org.apache.http.client.cache.HttpCacheEntry;
 import org.apache.http.client.cache.HttpCacheEntrySerializer;
@@ -68,6 +65,7 @@
     }
 
     public void putEntry(String uri, HttpCacheEntry entry) throws IOException {
+        
         File storageFile = getStorageFile(uri);
         if (!storageFile.exists()) {
             if (!storageFile.getParentFile().exists()) {
@@ -115,6 +113,9 @@
         File hostDirectory = new File(m_dataDirectory, uri.getHost());
 
         String path = uri.getPath();
+        if (path == null || "".equals(path) || "/".equals(path))
+            path = "/_index.html";
+
         String[] pathParts = path.split("/");
         File pathDirectory = hostDirectory;
         for (int i = 0; i < (pathParts.length - 1); i++) {

Added: sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/pom.xml
==============================================================================
--- (empty file)
+++ sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/pom.xml    Mon Apr  4 
09:38:08 2011
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu.httpproxy</groupId>
+    <artifactId>httpproxy</artifactId>
+    <version>0.2.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>proxyservlet</artifactId>
+  <packaging>bundle</packaging>
+  <name>Amdatu HttpProxy servlet</name>
+  <description>This bundle provides a HTTP proxy servlet</description>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.amdatu.web</groupId>
+      <artifactId>httpcontext</artifactId>
+      <version>0.2.0-SNAPSHOT</version>
+      <type>bundle</type>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient</artifactId>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <configuration>
+          <instructions>
+            
<Bundle-Activator>org.amdatu.httpproxy.proxyservlet.osgi.Activator</Bundle-Activator>
+            
<Bundle-SymbolicName>org.amdatu.httpproxy.proxyservlet</Bundle-SymbolicName>
+            <Embed-Dependency>*;scope=compile</Embed-Dependency>
+            <Embed-Transitive>true</Embed-Transitive>
+            <Bundle-ClassPath>.</Bundle-ClassPath>
+            <Import-Package>
+              *;resolution:=optional
+            </Import-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Added: 
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/osgi/Activator.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/osgi/Activator.java
        Mon Apr  4 09:38:08 2011
@@ -0,0 +1,69 @@
+/*
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.httpproxy.proxyservlet.osgi;
+
+import java.util.Hashtable;
+
+import javax.servlet.Servlet;
+
+import org.amdatu.httpproxy.proxyservlet.service.HttpForwardProxyServlet;
+import org.amdatu.httpproxy.proxyservlet.service.HttpProxyServletContext;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.http.client.HttpClient;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.log.LogService;
+
+/**
+ */
+public final class Activator extends DependencyActivatorBase {
+
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
+
+        Hashtable contextProps = new Hashtable();
+        contextProps.put("contextId", "org.amdatu.httpproxy");
+
+        manager.add(
+            createComponent()
+                .setImplementation(HttpProxyServletContext.class)
+                .setInterface(HttpContext.class.getName(), contextProps)
+                .add(createServiceDependency()
+                    .setService(LogService.class).setRequired(true)));
+
+        Hashtable servletProps = new Hashtable();
+        servletProps.put("alias", "/");
+        servletProps.put("contextId", "org.amdatu.httpproxy");
+
+        manager.add(
+            createComponent()
+                .setImplementation(HttpForwardProxyServlet.class)
+                .setInterface(Servlet.class.getName(), servletProps)
+                .add(createServiceDependency()
+                    .setService(HttpContext.class, 
"(contextId=org.amdatu.httpproxy)").setRequired(true))
+                .add(createServiceDependency()
+                    .setService(HttpClient.class).setRequired(true))
+                .add(createServiceDependency()
+                    .setService(LogService.class).setRequired(true)));
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/HttpForwardProxyServlet.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/HttpForwardProxyServlet.java
       Mon Apr  4 09:38:08 2011
@@ -0,0 +1,124 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.httpproxy.proxyservlet.service;
+
+import java.io.IOException;
+import java.net.URI;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.osgi.service.log.LogService;
+
+public class HttpForwardProxyServlet extends HttpServlet {
+
+    private static final long serialVersionUID = 4615260022937291009L;
+
+    // injected
+    private volatile HttpClient m_httpClient;
+    private volatile LogService m_logService;
+
+    private ServletConfig m_servletConfig;
+
+    public HttpForwardProxyServlet() {
+    }
+
+    public void init(ServletConfig servletConfig) throws ServletException {
+        m_logService.log(LogService.LOG_ERROR, "init called on proxyservlet");
+        m_servletConfig = servletConfig;
+    }
+
+    public void destroy() {
+        m_logService.log(LogService.LOG_ERROR, "destroy called on 
proxyservlet");
+    }
+
+    public ServletConfig getServletConfig() {
+        return m_servletConfig;
+    }
+
+    public String getServletInfo() {
+        return HttpForwardProxyServlet.class.getSimpleName();
+    }
+
+    public void service(ServletRequest servletRequest, ServletResponse 
servletResponse) throws ServletException,
+        IOException {
+
+        HttpServletRequest httpServletRequest = (HttpServletRequest) 
servletRequest;
+        HttpServletResponse httpServletResponse = (HttpServletResponse) 
servletResponse;
+
+        StringBuilder builder = new StringBuilder();
+        builder.append(httpServletRequest.getScheme());
+        builder.append("://");
+        builder.append(httpServletRequest.getServerName());
+        if (httpServletRequest.getServerPort() != -1 && 
httpServletRequest.getServerPort() != 80) {
+            builder.append(":");
+            builder.append(httpServletRequest.getServerPort());
+        }
+        builder.append(httpServletRequest.getPathInfo());
+        if (httpServletRequest.getQueryString() != null && 
!httpServletRequest.equals("")) {
+            builder.append("?");
+            builder.append(httpServletRequest.getQueryString());
+        }
+
+        URI uri = URI.create(builder.toString());
+
+        if (httpServletRequest.getServerName().equals("localhost") && 
httpServletRequest.getParameter("url") != null) {
+            uri = URI.create(httpServletRequest.getParameter("url"));
+        }
+
+        HttpResponse response = null;
+        if (httpServletRequest.getMethod().equals(HttpGet.METHOD_NAME)) {
+            HttpGet get = new HttpGet(uri);
+            response = m_httpClient.execute(get);
+        }
+        else {
+            if (httpServletRequest.getMethod().equals(HttpPost.METHOD_NAME)) {
+                HttpPost post = new HttpPost(uri);
+                response = m_httpClient.execute(post);
+            }
+        }
+
+        Header[] headers = response.getAllHeaders();
+        for (Header header : headers) {
+            if (header.getName().equals("Content-Length") && 
header.getValue().equals("0")) {
+                continue;
+            }
+            httpServletResponse.setHeader(header.getName(), header.getValue());
+        }
+
+        HttpEntity entity = response.getEntity();
+        if (entity != null)
+            try {
+                entity.writeTo(servletResponse.getOutputStream());
+            }
+            catch (IOException e) {
+                e.printStackTrace();
+            }
+    }
+
+}

Added: 
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/HttpProxyServletContext.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-httpproxy/proxyservlet/src/main/java/org/amdatu/httpproxy/proxyservlet/service/HttpProxyServletContext.java
       Mon Apr  4 09:38:08 2011
@@ -0,0 +1,28 @@
+package org.amdatu.httpproxy.proxyservlet.service;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.log.LogService;
+
+public class HttpProxyServletContext implements HttpContext {
+
+    // injected
+    private volatile LogService m_logService;
+
+    public boolean handleSecurity(HttpServletRequest request, 
HttpServletResponse response) throws IOException {
+        return true;
+    }
+
+    public URL getResource(String name) {
+        return null;
+    }
+
+    public String getMimeType(String name) {
+        return null;
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to