Author: hmt
Date: Fri Apr 27 21:04:42 2012
New Revision: 1331597
URL: http://svn.apache.org/viewvc?rev=1331597&view=rev
Log:
Revising proxify servlet\nHandling put, delete request
Modified:
incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java
incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyServlet.java
Modified: incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java?rev=1331597&r1=1331596&r2=1331597&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java Fri Apr
27 21:04:42 2012
@@ -31,9 +31,12 @@ import org.apache.commons.httpclient.Nam
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
@@ -80,6 +83,36 @@ public class ProxyClient {
// fBase64Auth = base64Auth;
//}
+ public ResponseObject execRequest(String url, HttpServletRequest
request, Configuration properties) throws Exception {
+ if (fLogger.isDebugEnabled()) {
+ fLogger.debug(request.getMethod() + " from " + url);
+ }
+ HttpMethod method = null;
+ String sMethod = request.getMethod();
+ if ("GET".equals(sMethod)) {
+ method = new GetMethod(url);
+ } else if ("POST".equals(sMethod)) {
+ method = new PostMethod(url);
+ } else if ("PUT".equals(sMethod)) {
+ method = new PutMethod(url);
+ } else if ("DELETE".equals(sMethod)) {
+ method = new DeleteMethod(url);
+ }
+
+ if (method == null) {
+ return null;
+ } else if (method instanceof EntityEnclosingMethod) {
+ if (this.parameters.length > 0 && method instanceof
PostMethod) {
+ ((PostMethod)
method).addParameters(this.parameters);
+ } else {
+ ((EntityEnclosingMethod)
method).setRequestEntity(new
InputStreamRequestEntity(request.getInputStream()));
+ }
+ }
+
+ method.setDoAuthentication(true);
+ return executeMethod(method, request, properties);
+ }
+
/**
* Process a proxied GET request
* @param url the URL to GET
@@ -88,6 +121,7 @@ public class ProxyClient {
* @return a ResponseObject from the remote site
* @throws Exception
*/
+ @Deprecated
public ResponseObject get(String url, HttpServletRequest request,
Configuration properties) throws Exception {
fLogger.debug("GET from " + url); //$NON-NLS-1$
GetMethod method = new GetMethod(url);
@@ -104,6 +138,7 @@ public class ProxyClient {
* @return a ResponseObject from the remote site
* @throws Exception
*/
+ @Deprecated
public ResponseObject post(String url , HttpServletRequest request,
Configuration properties) throws Exception {
fLogger.debug("POST to " + url); //$NON-NLS-1$
PostMethod method = new PostMethod(url);
Modified: incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyServlet.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyServlet.java?rev=1331597&r1=1331596&r2=1331597&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyServlet.java
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyServlet.java Fri
Apr 27 21:04:42 2012
@@ -49,13 +49,20 @@ public class ProxyServlet extends HttpSe
static Logger fLogger = Logger.getLogger(ProxyServlet.class.getName());
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
- dealWithRequest(request, response, "post");
+ dealWithRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException {
- dealWithRequest(request, response, "get");
+ dealWithRequest(request, response);
}
+ protected void doPut(HttpServletRequest request, HttpServletResponse
response) throws ServletException {
+ dealWithRequest(request, response);
+ }
+
+ protected void doDelete(HttpServletRequest request, HttpServletResponse
response) throws ServletException {
+ dealWithRequest(request, response);
+ }
/**
* Check the validity of a proxy request, and execute it if it checks
out
* @param request
@@ -63,7 +70,7 @@ public class ProxyServlet extends HttpSe
* @param httpMethod
* @throws ServletException
*/
- private void dealWithRequest(HttpServletRequest request,
HttpServletResponse response, String httpMethod) throws ServletException{
+ private void dealWithRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException{
try {
Configuration properties = (Configuration)
request.getSession().getServletContext().getAttribute("properties");
@@ -116,11 +123,7 @@ public class ProxyServlet extends HttpSe
//
// Execute the request and populate the ResponseObject
//
- if(httpMethod.equals("get")){
- responseObject =
proxyclient.get(bean.getNewUrl().toExternalForm(), request, properties);
- } else {
- responseObject =
proxyclient.post(bean.getNewUrl().toExternalForm(), request, properties);
- }
+ responseObject =
proxyclient.execRequest(bean.getNewUrl().toExternalForm(), request, properties);
//
// Set Status