Browse files

Add support for ProxyCacheManager to be set in the properties

  • Loading branch information...
1 parent faca0e3 commit 6ecd2c5b28b735c993b58602b99df991c1bf2a7a mguymon committed Dec 16, 2012
View
26 src/main/java/org/littleshoot/proxy/HttpServerPipelineFactory.java
@@ -13,6 +13,7 @@
import javax.management.ObjectName;
import javax.net.ssl.SSLEngine;
+import org.apache.commons.lang3.ClassUtils;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelPipeline;
@@ -28,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Optional;
+
/**
* Factory for creating pipelines for incoming requests to our listening
* socket.
@@ -38,10 +41,6 @@
private static final Logger log =
LoggerFactory.getLogger(HttpServerPipelineFactory.class);
- // Please note that caching is not currently supported. This is left
- // as placeholder for a future implementation.
- private static final boolean CACHE_ENABLED = false;
-
private final ProxyAuthorizationManager authenticationManager;
private final ChannelGroup channelGroup;
private final ChainProxyManager chainProxyManager;
@@ -91,10 +90,21 @@ public HttpServerPipelineFactory(
this.chainProxyManager = chainProxyManager;
this.ksm = ksm;
- // Please note that caching is not currently supported. This is left
- // as placeholder for a future implementation.
- if (CACHE_ENABLED) {
- cacheManager = new DefaultProxyCacheManager();
+ Optional<String> managerClassName = Optional.fromNullable( LittleProxyConfig.getProxyCacheManagerClass() );
+ if (managerClassName.isPresent()) {
+ ProxyCacheManager configCacheManager = null;
+ try {
+ Class managerClass = Class.forName( managerClassName.get() );
+ configCacheManager = (ProxyCacheManager)managerClass.newInstance();
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException("Failed to find class: " + managerClassName.get(), e);
+ } catch (InstantiationException e) {
+ throw new RuntimeException("Failed to create instance of ProxyCacheManager: " + managerClassName.get(), e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException("Failed to create instance of ProxyCacheManager: " + managerClassName.get(), e);
+ }
+
+ cacheManager = configCacheManager;
} else {
cacheManager = new ProxyCacheManager() {
View
11 src/main/java/org/littleshoot/proxy/LittleProxyConfig.java
@@ -47,6 +47,9 @@
private static boolean useJmx =
ProxyUtils.extractBooleanDefaultFalse(props, "jmx");
+ private static String proxyCacheManagerClass =
+ props.getProperty("proxy_cache_manager_class");
+
private LittleProxyConfig(){}
/**
@@ -87,4 +90,12 @@ public static void setUseJmx(boolean useJmx) {
public static boolean isUseJmx() {
return useJmx;
}
+
+ public static void setProxyCacheManagerClass(String clazz) {
+ proxyCacheManagerClass = clazz;
+ }
+
+ public static String getProxyCacheManagerClass() {
+ return proxyCacheManagerClass;
+ }
}
View
24 src/test/java/org/littleshoot/proxy/HttpProxyTest.java
@@ -21,7 +21,10 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
@@ -117,7 +120,7 @@ public void testProxyWithApacheHttpClientChunkedRequestsBadAddress() throws Exce
try {
final String response =
- httpPostWithApacheClient(true, "http://kt.baidu.com");
+ httpPostWithApacheClient(true, "http://test.localhost");
assertTrue(response.startsWith("Bad Gateway"));
} finally {
server.stop();
@@ -158,6 +161,24 @@ public void testProxyAuthorizationWithGetRequests() throws Exception{
server.stop();
}
}
+
+ @Test
+ public void testConfiguredProxyCacheManager() throws IOException {
+ LittleProxyConfig.setProxyCacheManagerClass( SimpleProxyCacheManager.class.getCanonicalName() );
+ final HttpProxyServer server = startHttpProxy();
+ // clear the test manager from config, so it does not bleed into other tests
+ LittleProxyConfig.setProxyCacheManagerClass( null );
+
+ try {
+ String baseResponse = httpPostWithApacheClient(false);
+ String proxyResponse = httpPostWithApacheClient(true);
+ assertEquals(baseResponse, proxyResponse);
+ } finally {
+ server.stop();
+ }
+
+ assertEquals( Arrays.asList("http://opsgenie.com/status/ping"), SimpleProxyCacheManager.requests );
+ }
private String httpPostWithApacheClient(final boolean isProxy)
throws IOException {
@@ -487,3 +508,4 @@ private void checkServer(final int port) {
}
+
View
29 src/test/java/org/littleshoot/proxy/SimpleProxyCacheManager.java
@@ -0,0 +1,29 @@
+package org.littleshoot.proxy;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Future;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.handler.codec.http.HttpRequest;
+
+class SimpleProxyCacheManager implements ProxyCacheManager {
+
+ public static final List<String> requests = new ArrayList<String>();
+
+ public boolean returnCacheHit(HttpRequest request, Channel channel) {
+
+ requests.add( request.getUri() );
+
+ return false;
+ }
+
+ public Future<String> cache(HttpRequest originalRequest,
+ org.jboss.netty.handler.codec.http.HttpResponse httpResponse,
+ Object response, ChannelBuffer encoded) {
+
+ return null;
+ }
+
+}

0 comments on commit 6ecd2c5

Please sign in to comment.