Skip to content

Commit 33f7386

Browse files
committed
Added proxy API and a few convenience classes
1 parent ba89ce6 commit 33f7386

7 files changed

Lines changed: 889 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Eclipse Public License version 1.0, available at
5+
* http://www.eclipse.org/legal/epl-v10.html
6+
*/
7+
package org.ocpsoft.rewrite.param;
8+
9+
import org.ocpsoft.rewrite.context.EvaluationContext;
10+
11+
/**
12+
* Utility for extracting values from the {@link ParameterValueStore}.
13+
*
14+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
15+
*
16+
*/
17+
public class Parameters
18+
{
19+
/**
20+
* Retrieve a single parameter value from the {@link ParameterValueStore}.
21+
*/
22+
public static String retrieve(EvaluationContext context, String string)
23+
{
24+
return ((ParameterValueStore) context.get(ParameterValueStore.class)).retrieve(((ParameterStore) context
25+
.get(ParameterStore.class)).get(string));
26+
}
27+
}

‎config-proxy/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>rewrite-parent</artifactId>
7+
<groupId>org.ocpsoft.rewrite</groupId>
8+
<version>2.0.9.Final-SNAPSHOT</version>
9+
<relativePath>../</relativePath>
10+
</parent>
11+
12+
<artifactId>rewrite-config-proxy</artifactId>
13+
14+
<properties>
15+
<version.jodatime>1.6.2</version.jodatime>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.ocpsoft.rewrite</groupId>
21+
<artifactId>rewrite-api</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.ocpsoft.rewrite</groupId>
25+
<artifactId>rewrite-config-servlet</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.apache.httpcomponents</groupId>
29+
<artifactId>httpclient</artifactId>
30+
<version>4.3.1</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.jboss.spec.javax.servlet</groupId>
34+
<artifactId>jboss-servlet-api_2.5_spec</artifactId>
35+
<scope>provided</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.ocpsoft.rewrite</groupId>
40+
<artifactId>rewrite-test-harness</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Eclipse Public License version 1.0, available at
5+
* http://www.eclipse.org/legal/epl-v10.html
6+
*/
7+
package org.ocpsoft.rewrite.servlet.config.proxy;
8+
9+
import java.util.LinkedHashMap;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
import org.ocpsoft.rewrite.context.EvaluationContext;
14+
import org.ocpsoft.rewrite.exception.RewriteException;
15+
import org.ocpsoft.rewrite.param.ParameterStore;
16+
import org.ocpsoft.rewrite.param.Parameterized;
17+
import org.ocpsoft.rewrite.param.ParameterizedPatternBuilder;
18+
import org.ocpsoft.rewrite.param.RegexParameterizedPatternBuilder;
19+
import org.ocpsoft.rewrite.servlet.config.HttpOperation;
20+
import org.ocpsoft.rewrite.servlet.config.Lifecycle;
21+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
22+
23+
/**
24+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
25+
*
26+
*/
27+
public final class Proxy extends HttpOperation implements Parameterized
28+
{
29+
private final ParameterizedPatternBuilder destination;
30+
31+
private Proxy(String destination)
32+
{
33+
this.destination = new RegexParameterizedPatternBuilder(destination);
34+
}
35+
36+
public static Proxy to(String destination)
37+
{
38+
return new Proxy(destination);
39+
}
40+
41+
@Override
42+
public void performHttp(HttpServletRewrite event, EvaluationContext context)
43+
{
44+
Map<String, String> params = new LinkedHashMap<String, String>();
45+
params.put("targetUri", destination.build(event, context));
46+
ProxyServlet proxyServlet = new ProxyServlet();
47+
ProxyServletConfig proxyConfig = new ProxyServletConfig(event.getServletContext(), params);
48+
try
49+
{
50+
proxyServlet.init(proxyConfig);
51+
proxyServlet.service(event.getRequest(), event.getResponse());
52+
Lifecycle.abort().perform(event, context);
53+
}
54+
catch (Exception e)
55+
{
56+
throw new RewriteException("Could not proxy event [" + event + "] to destination [" + destination + "]", e);
57+
}
58+
}
59+
60+
@Override
61+
public Set<String> getRequiredParameterNames()
62+
{
63+
return destination.getRequiredParameterNames();
64+
}
65+
66+
@Override
67+
public void setParameterStore(ParameterStore store)
68+
{
69+
destination.setParameterStore(store);
70+
}
71+
72+
}

0 commit comments

Comments
 (0)