You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2010/02/17 13:53:27 UTC

svn commit: r910952 - in /incubator/wookie/trunk: src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java src-tests/org/apache/wookie/tests/functional/ProxyTest.java src/org/apache/wookie/proxy/ProxyClient.java

Author: scottbw
Date: Wed Feb 17 12:53:26 2010
New Revision: 910952

URL: http://svn.apache.org/viewvc?rev=910952&view=rev
Log:
Modified proxy client to pass through POST parameters as well as POST body, and added more test cases. See WOOKIE-118 for more details.

Modified:
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java
    incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java?rev=910952&r1=910951&r2=910952&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java Wed Feb 17 12:53:26 2010
@@ -44,6 +44,44 @@
 	}
 	
 	@Test
+	public void setPreferenceUsingPostParameters() throws Exception{
+	    try {
+	    	String url = TEST_PROPERTIES_SERVICE_URL_VALID;
+	    	//String url = TEST_PROPERTIES_SERVICE_URL_VALID;
+	        HttpClient client = new HttpClient();
+	        PostMethod post = new PostMethod(url);
+	        post.addParameter("api_key", API_KEY_VALID);
+	        post.addParameter("widgetid", WIDGET_ID_VALID);
+	        post.addParameter("userid", "test");
+	        post.addParameter("is_public", "false");
+	        post.addParameter("shareddatakey","propstest");
+	        post.addParameter("propertyname", "testpost");
+	        post.addParameter("propertyvalue","pass");
+	        client.executeMethod(post);
+	        int code = post.getStatusCode();
+	        assertEquals(201,code);
+	        post.releaseConnection();
+	    }
+	    catch (Exception e) {
+	    	fail("POST failed");
+	    }	 
+	    try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=testpost");
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(200, code);
+	        String resp = get.getResponseBodyAsString();
+	        assertEquals("pass",resp);
+	        get.releaseConnection();
+	    }
+	    catch (Exception e) {
+	    	fail("POST  failed to set info correctly");
+	    }
+	}
+	
+	@Test
 	public void setPreference(){
 		// Set some shared data
 	    try {

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java?rev=910952&r1=910951&r2=910952&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java Wed Feb 17 12:53:26 2010
@@ -19,7 +19,6 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
@@ -146,6 +145,50 @@
 		req.releaseConnection();
 		assertEquals(200,code);
 	}
+	
+	/**
+	 * This tests accessing a site passing through POST parameters
+	 * @throws Exception
+	 */
+	@Test
+	public void postWithPassingParameters() throws Exception{
+	    try {
+	    	String url = PROXY_URL;
+	    	//String url = TEST_PROPERTIES_SERVICE_URL_VALID;
+	        HttpClient client = new HttpClient();
+	        PostMethod post = new PostMethod(url);
+	        post.addParameter("instanceid_key", instance_id_key);
+	        post.addParameter("url", TEST_PROPERTIES_SERVICE_URL_VALID);
+	        post.addParameter("api_key", API_KEY_VALID);
+	        post.addParameter("widgetid", WIDGET_ID_VALID);
+	        post.addParameter("userid", "test");
+	        post.addParameter("is_public", "false");
+	        post.addParameter("shareddatakey","proxytest");
+	        post.addParameter("propertyname", "pass");
+	        post.addParameter("propertyvalue","pass");
+	        client.executeMethod(post);
+	        int code = post.getStatusCode();
+	        assertEquals(200,code);
+	        post.releaseConnection();
+	    }
+	    catch (Exception e) {
+	    	fail("POST via proxy failed");
+	    }	 
+	    try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=proxytest&propertyname=pass");
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(200, code);
+	        String resp = get.getResponseBodyAsString();
+	        assertEquals("pass",resp);
+	        get.releaseConnection();
+	    }
+	    catch (Exception e) {
+	    	fail("POST via proxy failed to set info correctly");
+	    }
+	}
 
 	@Test
 	public void postWithOnlyParameters() throws Exception{

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=910952&r1=910951&r2=910952&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/proxy/ProxyClient.java Wed Feb 17 12:53:26 2010
@@ -18,10 +18,11 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
-import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -29,13 +30,12 @@
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.HttpMethodBase;
 import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.NameValuePair;
 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.auth.AuthenticationException;
-import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
@@ -48,13 +48,15 @@
 
 	static Logger fLogger = Logger.getLogger(ProxyClient.class.getName());
 
-	private String fContentType;	
+	private String fContentType = "text/plain";	
 	private String fProxyUsername = null;
 	private String fProxyPassword = null;
 	private String fBase64Auth = null;
+	private NameValuePair[] parameters = null;
 
 	private boolean fUseProxyAuthentication = false;
 
+	@SuppressWarnings("unchecked")
 	public ProxyClient(HttpServletRequest request){
 		String proxyUserName = request.getParameter("username");
 		String proxyPassword = request.getParameter("password");
@@ -63,6 +65,7 @@
 			this.setProxyAuthConfig(proxyUserName, proxyPassword);
 		if(base64Auth != null)
 			this.setBase64AuthConfig(base64Auth);
+		filterParameters(request.getParameterMap());
 	}
 
 	private void setProxyAuthConfig(String username, String password){
@@ -82,27 +85,40 @@
 
 	public String get(String url, Configuration properties) throws Exception {
 		fLogger.debug("GET from " + url); //$NON-NLS-1$
-		return fetchData(new GetMethod(url), properties);
+		GetMethod method = new GetMethod(url);
+		method.setDoAuthentication(true);
+		return executeMethod(method, properties);
 	}
 
 	public String post(String uri, String xmlData, Configuration properties) throws Exception {
-		PostMethod post = new PostMethod(uri);
 		fLogger.debug("POST to " + uri); //$NON-NLS-1$
-		return sendXmlData(xmlData, post, properties);
-	}
-
-	private String sendXmlData(String xmlData, EntityEnclosingMethod method, Configuration properties) throws Exception {
-		// Tell the method to automatically handle authentication.
+		PostMethod method = new PostMethod(uri);
 		method.setDoAuthentication(true);
-		try {
-			method.setRequestEntity(new StringRequestEntity(xmlData, "text/xml", "UTF8"));//$NON-NLS-1$  //$NON-NLS-2$
-		}
-		catch (UnsupportedEncodingException e) {
-			throw new Exception(e);
-		}
+		method.setRequestEntity(new StringRequestEntity(xmlData, "text/xml", "UTF8"));//$NON-NLS-1$  //$NON-NLS-2$
+		method.addParameters(this.parameters);
 		return executeMethod(method, properties);
 	}
 
+	/**
+	 * Processes the parameters passed through to the request,
+	 * removing the parameters used by the proxy itself
+	 * @return
+	 */
+	private void filterParameters(Map<Object,Object> umap){
+		Map<Object, Object> map = new HashMap<Object, Object>(umap);
+		map.remove("instanceid_key");
+		map.remove("url");
+		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
+		for (Object key:map.keySet().toArray()){
+			for (String value:(String[])map.get(key)){
+				NameValuePair param = new NameValuePair();
+				param.setName((String)key);
+				param.setValue(value);
+				params.add(param);				
+			}
+		}
+		parameters = params.toArray(new NameValuePair[params.size()]);
+	}
 
 	private String executeMethod(HttpMethod method, Configuration properties) throws Exception, AuthenticationException {
 		// Execute the method.
@@ -138,7 +154,7 @@
 
 			if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
 				Header hType = method.getResponseHeader("Content-type");					
-				fContentType = hType.getValue();
+				if (hType != null) fContentType = hType.getValue();
 				// for now we are only expecting Strings					
 				//return method.getResponseBodyAsString();
 				return readFully(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
@@ -158,18 +174,6 @@
 		}
 	}
 
-	private String fetchData(HttpMethodBase method, Configuration properties) throws Exception {			
-		// Provide custom retry handler is necessary
-		//TODO: the line below was causing an error under jboss (not tomcat)
-		//method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
-
-		// Tell the method to automatically handle authentication.
-		method.setDoAuthentication(true);
-
-		return executeMethod(method, properties);
-
-	}
-
 	/**
 	 * This is supposed to be the correct way to read the response instead of using getResponseBody() - which gives a runtime warning;
 	 * 
@@ -178,7 +182,6 @@
 	 * @return
 	 * @throws IOException
 	 */
-	@SuppressWarnings("unused")
 	private String readFully(Reader input) throws IOException {
 		BufferedReader bufferedReader = input instanceof BufferedReader ? (BufferedReader) input
 				: new BufferedReader(input);