Example usage for org.apache.commons.httpclient.methods PostMethod removeParameter

List of usage examples for org.apache.commons.httpclient.methods PostMethod removeParameter

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod removeParameter.

Prototype

public boolean removeParameter(String paramString) throws IllegalArgumentException 

Source Link

Usage

From source file:org.infoscoop.request.PostCredentialAuthenticator.java

public void doAuthentication(HttpClient client, ProxyRequest request, HttpMethod method, String uid, String pwd)
        throws ProxyAuthenticationException {
    String uidParamNameHeader = request.getRequestHeader(UID_PARAM_NAME);
    String passwdParamNameHeader = request.getRequestHeader(PASSWD_PARAM_NAME);
    String uidParamName = (uidParamNameHeader != null) ? uidParamNameHeader : this.uidParamName;
    String passwdParamName = (passwdParamNameHeader != null) ? passwdParamNameHeader : this.pwdParamName;
    try {//  w w  w  . j a v a 2s. c  o  m
        request.addIgnoreHeader("Content-Type");

        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        NameValuePair[] params = new NameValuePair[2];

        PostMethod pMethod = (PostMethod) method;
        String queryString = pMethod.getQueryString();

        // Delete the same parameter's name that has already exist.
        pMethod.removeParameter(uidParamName);
        pMethod.removeParameter(passwdParamName);
        queryString = RequestUtil.removeQueryStringParam(queryString, uidParamName);
        queryString = RequestUtil.removeQueryStringParam(queryString, passwdParamName);

        pMethod.setQueryString(queryString);

        params[0] = new NameValuePair(uidParamName, uid);
        params[1] = new NameValuePair(passwdParamName, pwd);
        pMethod.setRequestBody(params);

        pMethod.addRequestHeader("content-length",
                String.valueOf(pMethod.getRequestEntity().getContentLength()));
    } catch (Exception e) {
        throw new ProxyAuthenticationException(e);
    }
}

From source file:org.pentaho.platform.web.servlet.ProxyServlet.java

protected void doProxy(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException {
    if (proxyURL == null) { // Got nothing from web.xml
        return;//from   w ww.  ja  v a2 s.  co m
    }

    String servletPath = request.getServletPath();
    // System .out.println( ">>>>>>>> REQ: " + request.getRequestURL().toString() ); //$NON-NLS-1$//$NON-NLS-2$
    PentahoSystem.systemEntryPoint();
    try {
        String theUrl = proxyURL + servletPath;
        PostMethod method = new PostMethod(theUrl);

        // Copy the parameters from the request to the proxy
        // System .out.print( ">>>>>>>> PARAMS: " ); //$NON-NLS-1$
        Map paramMap = request.getParameterMap();
        Map.Entry entry;
        String[] array;
        for (Iterator it = paramMap.entrySet().iterator(); it.hasNext();) {
            entry = (Map.Entry) it.next();
            array = (String[]) entry.getValue();
            for (String element : array) {
                method.addParameter((String) entry.getKey(), element);
                // System.out.print( (String)entry.getKey() + "=" + array[i]
                // + "&" ); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        // System.out.println( "" ); //$NON-NLS-1$

        // Just in case someone is trying to spoof the proxy
        method.removeParameter("_TRUST_USER_"); //$NON-NLS-1$

        // Get the user from the session
        IPentahoSession userSession = getPentahoSession(request);
        String name = userSession.getName();

        // Add the trusted user from the session
        if ((name != null) && (name.length() > 0)) {
            method.addParameter("_TRUST_USER_", name); //$NON-NLS-1$
            // System.out.println( ">>>>>>>> USR: " + name ); //$NON-NLS-1$
        } else if ((errorURL != null) && (errorURL.trim().length() > 0)) {
            response.sendRedirect(errorURL);
            // System.out.println( ">>>>>>>> REDIR: " + errorURL );
            // //$NON-NLS-1$
            return;
        }

        // System.out.println( ">>>>>>>> PROXY: " + theUrl ); //$NON-NLS-1$
        debug(Messages.getInstance().getString("ProxyServlet.DEBUG_0001_OUTPUT_URL", theUrl)); //$NON-NLS-1$

        // Now do the request
        HttpClient client = new HttpClient();

        try {
            // Execute the method.
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                error(Messages.getInstance().getErrorString("ProxyServlet.ERROR_0003_REMOTE_HTTP_CALL_FAILED", //$NON-NLS-1$
                        method.getStatusLine().toString()));
                return;
            }
            setHeader("Content-Type", method, response); //$NON-NLS-1$
            setHeader("Content-Length", method, response); //$NON-NLS-1$

            InputStream inStr = method.getResponseBodyAsStream();
            ServletOutputStream outStr = response.getOutputStream();

            int inCnt = 0;
            byte[] buf = new byte[2048];
            while (-1 != (inCnt = inStr.read(buf))) {
                outStr.write(buf, 0, inCnt);
            }
        } catch (HttpException e) {
            error(Messages.getInstance().getErrorString("ProxyServlet.ERROR_0004_PROTOCOL_FAILURE"), e); //$NON-NLS-1$
            e.printStackTrace();
        } catch (IOException e) {
            error(Messages.getInstance().getErrorString("ProxyServlet.ERROR_0005_TRANSPORT_FAILURE"), e); //$NON-NLS-1$
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        }
    } finally {
        PentahoSystem.systemExitPoint();
    }
}