Quantcast

Ajax-Proxy Plugin - Alter ProxyServlet

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Ajax-Proxy Plugin - Alter ProxyServlet

Jake Kretz
I am using the Ajax-Proxy Plugin and want to add some additional features to the ProxyServlet class to support PUT.

I am able to get these changes to work if I modify the plug-in ProxyServlet.java class directly.

I would like to use BootStrap.groovy to make these modifications instead, but no matter what I do, the plug-in class is always used.

Here is my BootStrap.groovy code.  Any thoughts or ideas would be appreciated.

import net.edwardstx.ProxyServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.apache.commons.httpclient.methods.PutMethod
import org.apache.commons.httpclient.methods.StringRequestEntity
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.NameValuePair

class BootStrap {

    def init = { servletContext ->
        monkeyPatchProxyServlet()
    }
    def destroy = {
    }

    def monkeyPatchProxyServlet() {
        ProxyServlet.metaClass.doPut = {HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse ->
            // Create a standard PUT request
            PutMethod putMethodProxyRequest = new PutMethod(this.getProxyURL(httpServletRequest));
            // Forward the request headers
            setProxyRequestHeaders(httpServletRequest, putMethodProxyRequest);

            this.handleStandardPut(putMethodProxyRequest, httpServletRequest);
            // Execute the proxy request
            this.executeProxyRequest(putMethodProxyRequest, httpServletRequest, httpServletResponse);
        }

        ProxyServlet.metaClass.handleStandardPut = {PutMethod putMethodProxyRequest, HttpServletRequest httpServletRequest ->

            BufferedReader reader = httpServletRequest.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }

            putMethodProxyRequest.setRequestEntity(new StringRequestEntity(sb.toString(), httpServletRequest.getContentType(), httpServletRequest.getCharacterEncoding()));
        }

        ProxyServlet.metaClass.handleStandardPost = {PostMethod postMethodProxyRequest, HttpServletRequest httpServletRequest ->
            // Get the client POST data as a Map
            Map<String, String[]> mapPostParameters = (Map<String, String[]>) httpServletRequest.getParameterMap();
            // Create a List to hold the NameValuePairs to be passed to the PostMethod
            List<NameValuePair> listNameValuePairs = new ArrayList<NameValuePair>();

            // Iterate the parameter names
            for (String stringParameterName : mapPostParameters.keySet()) {
                // Iterate the values for each parameter name
                String[] stringArrayParameterValues = mapPostParameters.get(stringParameterName);
                for (String stringParameterValue : stringArrayParameterValues) {
                    // Create a NameValuePair and store in list
                    NameValuePair nameValuePair = new NameValuePair(stringParameterName, stringParameterValue);
                    listNameValuePairs.add(nameValuePair);
                }
            }

            BufferedReader reader = httpServletRequest.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }

            // Set the proxy request POST data
            postMethodProxyRequest.setRequestBody(listNameValuePairs.toArray(new NameValuePair[listNameValuePairs.size()]));
            postMethodProxyRequest.setRequestEntity(new StringRequestEntity(sb.toString(), httpServletRequest.getContentType(), httpServletRequest.getCharacterEncoding()));
        }
    }
}

Thanks

Loading...