Example usage for org.apache.commons.httpclient HttpMethod setURI

List of usage examples for org.apache.commons.httpclient HttpMethod setURI

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod setURI.

Prototype

public abstract void setURI(URI paramURI) throws URIException;

Source Link

Usage

From source file:gov.nih.nci.cabio.portal.portlet.RESTProxyServletController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    try {//  ww  w .  j  a va2 s . c o m
        String relativeURL = request.getParameter("relativeURL");
        if (relativeURL == null) {
            log.error("Null relativeURL parameter");
            return null;
        }

        String qs = request.getQueryString();
        if (qs == null) {
            qs = "";
        }

        qs = qs.replaceFirst("relativeURL=(.*?)&", "");
        qs = URLDecoder.decode(qs, "UTF-8");

        String url = caBIORestURL + relativeURL + "?" + qs;
        log.info("Proxying URL: " + url);

        URI uri = new URI(url, false);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod();
        method.setURI(uri);
        client.executeMethod(method);

        response.setContentType(method.getResponseHeader("Content-Type").getValue());

        CopyUtils.copy(method.getResponseBodyAsStream(), response.getOutputStream());
    } catch (Exception e) {
        throw new ServletException("Unable to connect to caBIO", e);
    }

    return null;
}

From source file:gov.nih.nci.cabio.RESTAPITest.java

/**
 * Queries the REST API with the given query string and parses the resulting
 * XML into a DOM document.//  www  . j a  va  2 s. co m
 */
private Document getXML(String query) throws Exception {
    URI uri = new URI(GET_XML_URL + "?" + query, false);
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod();
    method.setURI(uri);
    client.executeMethod(method);
    InputSource inputSource = new InputSource(method.getResponseBodyAsStream());
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.parse(inputSource);
}

From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java

private HttpMethod createNewHttpMethod(HttpMethod oldMethod) throws URIException {
    HttpMethod httpMethod;
    if (oldMethod instanceof GetMethod) {
        httpMethod = new GetMethod();
    } else {/*from   w ww.  j av  a2  s .  c  om*/
        httpMethod = new PostMethod();
        ((PostMethod) httpMethod).setRequestEntity(((PostMethod) oldMethod).getRequestEntity());
        httpMethod.setParams(oldMethod.getParams());
    }
    httpMethod.setURI(oldMethod.getURI());
    httpMethod.setFollowRedirects(oldMethod.getFollowRedirects());
    return httpMethod;
}

From source file:com.sa.npopa.samples.hbase.rest.client.Client.java

/**
 * Execute a transaction method given a complete URI.
 * @param method the transaction method/*from  w w  w.  j a va 2s  .co  m*/
 * @param headers HTTP header values to send
 * @param uri a properly urlencoded URI
 * @return the HTTP response code
 * @throws IOException
 */
public int executeURI(HttpMethod method, Header[] headers, String uri) throws IOException {
    method.setURI(new URI(uri, true));
    for (Map.Entry<String, String> e : extraHeaders.entrySet()) {
        method.addRequestHeader(e.getKey(), e.getValue());
    }
    if (headers != null) {
        for (Header header : headers) {
            method.addRequestHeader(header);
        }
    }
    long startTime = System.currentTimeMillis();
    int code = httpClient.executeMethod(method);
    long endTime = System.currentTimeMillis();
    if (LOG.isDebugEnabled()) {
        LOG.debug(method.getName() + " " + uri + " " + code + " " + method.getStatusText() + " in "
                + (endTime - startTime) + " ms");
    }
    return code;
}

From source file:fr.unix_experience.owncloud_sms.engine.OCHttpClient.java

private int followRedirections(HttpMethod httpMethod) throws IOException {
    int redirectionsCount = 0;
    int status = httpMethod.getStatusCode();
    while (redirectionsCount < 3 && (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {
        Header location = httpMethod.getResponseHeader("Location");
        if (location == null) {
            location = httpMethod.getResponseHeader("location");
        }/*from  w w w. ja  v  a2  s  .  c  om*/
        if (location == null) {
            Log.e(TAG, "No valid location header found when redirecting.");
            return 500;
        }

        try {
            httpMethod.setURI(new URI(location.getValue()));
        } catch (URIException e) {
            Log.e(TAG, "Invalid URI in 302 FOUND response");
            return 500;
        }

        status = executeMethod(httpMethod);
        redirectionsCount++;
    }

    if (redirectionsCount >= 3 && status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
        Log.e(TAG,
                "Too many redirection done. Aborting, please ensure your server is " + "correctly configured");
        return 400;
    }

    return status;
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private void signWithOAuth(HttpMethod method) throws IOException {
    try {/* ww  w.  j  ava2 s  . c  o  m*/
        HostConfiguration hostConfiguration = client.getHostConfiguration();

        URI uri = method.getURI();
        URI newUri = new URI((isSSLEnabled) ? "https" : "http", uri.getUserinfo(), hostConfiguration.getHost(),
                hostConfiguration.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());

        method.setURI(newUri);

        //URI checkUri = method.getURI();
        //String checkUriString = checkUri.toString();
        CommonsHttp3OAuthConsumer oAuthConsumer = new CommonsHttp3OAuthConsumer(consumerKey, consumerSecret);
        oAuthConsumer.setTokenWithSecret(consumerKey, consumerSecret);
        oAuthConsumer.sign(method);
    } catch (Exception e) {
        throw new IOException("Failed to OAuth sign HTTPRequest", e);
    }
}

From source file:ir.keloud.android.lib.common.KeloudClient.java

private int patchRedirection(int status, HttpMethod method) throws HttpException, IOException {
    int redirectionsCount = 0;
    while (redirectionsCount < MAX_REDIRECTIONS_COUNT && (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {

        Header location = method.getResponseHeader("Location");
        if (location == null) {
            location = method.getResponseHeader("location");
        }/*  ww  w  .  ja v a 2 s  . c  o  m*/
        if (location != null) {
            Log_OC.d(TAG + " #" + mInstanceNumber, "Location to redirect: " + location.getValue());

            // Release the connection to avoid reach the max number of connections per host
            // due to it will be set a different url
            exhaustResponse(method.getResponseBodyAsStream());
            method.releaseConnection();

            method.setURI(new URI(location.getValue(), true));
            Header destination = method.getRequestHeader("Destination");
            if (destination == null) {
                destination = method.getRequestHeader("destination");
            }
            if (destination != null) {
                String locationStr = location.getValue();
                int suffixIndex = locationStr
                        .lastIndexOf((mCredentials instanceof KeloudBearerCredentials) ? AccountUtils.ODAV_PATH
                                : AccountUtils.WEBDAV_PATH_4_0);
                String redirectionBase = locationStr.substring(0, suffixIndex);

                String destinationStr = destination.getValue();
                String destinationPath = destinationStr.substring(mBaseUri.toString().length());
                String redirectedDestination = redirectionBase + destinationPath;

                destination.setValue(redirectedDestination);
                method.setRequestHeader(destination);
            }
            status = super.executeMethod(method);
            redirectionsCount++;

        } else {
            Log_OC.d(TAG + " #" + mInstanceNumber, "No location to redirect!");
            status = HttpStatus.SC_NOT_FOUND;
        }
    }
    return status;
}

From source file:com.owncloud.android.lib.common.OwnCloudClient.java

private int patchRedirection(int status, HttpMethod method) throws HttpException, IOException {
    int redirectionsCount = 0;
    while (redirectionsCount < MAX_REDIRECTIONS_COUNT && (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {

        Header location = method.getResponseHeader("Location");
        if (location == null) {
            location = method.getResponseHeader("location");
        }/*from www  . j a v a  2 s. co m*/
        if (location != null) {
            Log_OC.d(TAG + " #" + mInstanceNumber, "Location to redirect: " + location.getValue());

            // Release the connection to avoid reach the max number of connections per host
            // due to it will be set a different url
            exhaustResponse(method.getResponseBodyAsStream());
            method.releaseConnection();

            method.setURI(new URI(location.getValue(), true));
            Header destination = method.getRequestHeader("Destination");
            if (destination == null) {
                destination = method.getRequestHeader("destination");
            }
            if (destination != null) {
                String locationStr = location.getValue();
                int suffixIndex = locationStr.lastIndexOf(
                        (mCredentials instanceof OwnCloudBearerCredentials) ? AccountUtils.ODAV_PATH
                                : AccountUtils.WEBDAV_PATH_4_0);
                String redirectionBase = locationStr.substring(0, suffixIndex);

                String destinationStr = destination.getValue();
                String destinationPath = destinationStr.substring(mBaseUri.toString().length());
                String redirectedDestination = redirectionBase + destinationPath;

                destination.setValue(redirectedDestination);
                method.setRequestHeader(destination);
            }
            status = super.executeMethod(method);
            redirectionsCount++;

        } else {
            Log_OC.d(TAG + " #" + mInstanceNumber, "No location to redirect!");
            status = HttpStatus.SC_NOT_FOUND;
        }
    }
    return status;
}

From source file:com.owncloud.android.oc_framework.network.webdav.WebdavClient.java

@Override
public int executeMethod(HttpMethod method) throws IOException, HttpException {
    boolean customRedirectionNeeded = false;
    try {/*www. j a v a2s .com*/
        method.setFollowRedirects(mFollowRedirects);
    } catch (Exception e) {
        //if (mFollowRedirects) Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() + " method, custom redirection will be used if needed");
        customRedirectionNeeded = mFollowRedirects;
    }
    if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) {
        method.setRequestHeader("Cookie", mSsoSessionCookie);
    }
    int status = super.executeMethod(method);
    int redirectionsCount = 0;
    while (customRedirectionNeeded && redirectionsCount < MAX_REDIRECTIONS_COUNT
            && (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY
                    || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {

        Header location = method.getResponseHeader("Location");
        if (location != null) {
            Log.d(TAG, "Location to redirect: " + location.getValue());
            method.setURI(new URI(location.getValue(), true));
            status = super.executeMethod(method);
            redirectionsCount++;

        } else {
            Log.d(TAG, "No location to redirect!");
            status = HttpStatus.SC_NOT_FOUND;
        }
    }

    return status;
}

From source file:eu.alefzero.webdav.WebdavClient.java

@Override
public int executeMethod(HttpMethod method) throws IOException, HttpException {
    boolean customRedirectionNeeded = false;
    try {/*from  www .j  a  va2s  .  co  m*/
        method.setFollowRedirects(mFollowRedirects);
    } catch (Exception e) {
        if (mFollowRedirects)
            Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName()
                    + " method, custom redirection will be used");
        customRedirectionNeeded = mFollowRedirects;
    }
    if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) {
        method.setRequestHeader("Cookie", mSsoSessionCookie);
    }
    int status = super.executeMethod(method);
    int redirectionsCount = 0;
    while (customRedirectionNeeded && redirectionsCount < MAX_REDIRECTIONS_COUNT
            && (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY
                    || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {

        Header location = method.getResponseHeader("Location");
        if (location != null) {
            Log_OC.d(TAG, "Location to redirect: " + location.getValue());
            method.setURI(new URI(location.getValue(), true));
            status = super.executeMethod(method);
            redirectionsCount++;

        } else {
            Log_OC.d(TAG, "No location to redirect!");
            status = HttpStatus.SC_NOT_FOUND;
        }
    }

    return status;
}