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:org.parosproxy.paros.network.HttpMethodHelper.java

public HttpMethod createRequestMethodNew(HttpRequestHeader header, HttpBody body) throws URIException {
    HttpMethod httpMethod = null;

    String method = header.getMethod();
    URI uri = header.getURI();// w  ww. j a  va 2  s. co  m
    String version = header.getVersion();

    httpMethod = new GenericMethod(method);

    httpMethod.setURI(uri);
    HttpMethodParams httpParams = httpMethod.getParams();
    // default to use HTTP 1.0
    httpParams.setVersion(HttpVersion.HTTP_1_0);
    if (version.equalsIgnoreCase(HttpHeader.HTTP11)) {
        httpParams.setVersion(HttpVersion.HTTP_1_1);
    }

    // set various headers
    int pos = 0;
    // ZAP: FindBugs fix - always initialise pattern
    Pattern pattern = patternCRLF;
    String delimiter = CRLF;

    String msg = header.getHeadersAsString();
    if ((pos = msg.indexOf(CRLF)) < 0) {
        if ((pos = msg.indexOf(LF)) < 0) {
            delimiter = LF;
            pattern = patternLF;
        }
    } else {
        delimiter = CRLF;
        pattern = patternCRLF;
    }

    String[] split = pattern.split(msg);
    String token = null;
    String name = null;
    String value = null;
    //String host = null;

    for (int i = 0; i < split.length; i++) {
        token = split[i];
        if (token.equals("")) {
            continue;
        }

        if ((pos = token.indexOf(":")) < 0) {
            return null;
        }
        name = token.substring(0, pos).trim();
        value = token.substring(pos + 1).trim();
        httpMethod.addRequestHeader(name, value);

    }

    // set body if post method or put method
    if (body != null && body.length() > 0) {
        EntityEnclosingMethod generic = (EntityEnclosingMethod) httpMethod;
        //         generic.setRequestEntity(new StringRequestEntity(body.toString()));
        generic.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));

    }

    httpMethod.setFollowRedirects(false);
    return httpMethod;

}

From source file:org.parosproxy.paros.network.HttpMethodHelper.java

public HttpMethod createRequestMethod(HttpRequestHeader header, HttpBody body) throws URIException {
    HttpMethod httpMethod = null;

    String method = header.getMethod();
    URI uri = header.getURI();/*from w  w  w  . j  av a2  s  . c  o  m*/
    String version = header.getVersion();

    if (method == null || method.trim().length() < 3) {
        throw new URIException("Invalid HTTP method: " + method);
    }

    if (method.equalsIgnoreCase(GET)) {
        //httpMethod = new GetMethod();
        // ZAP: avoid discarding HTTP status code 101 that is used for WebSocket upgrade 
        httpMethod = new ZapGetMethod();
    } else if (method.equalsIgnoreCase(POST)) {
        httpMethod = new ZapPostMethod();
    } else if (method.equalsIgnoreCase(DELETE)) {
        httpMethod = new ZapDeleteMethod();
    } else if (method.equalsIgnoreCase(PUT)) {
        httpMethod = new ZapPutMethod();
    } else if (method.equalsIgnoreCase(HEAD)) {
        httpMethod = new ZapHeadMethod();
    } else if (method.equalsIgnoreCase(OPTIONS)) {
        httpMethod = new ZapOptionsMethod();
    } else if (method.equalsIgnoreCase(TRACE)) {
        httpMethod = new ZapTraceMethod(uri.toString());
    } else {
        httpMethod = new GenericMethod(method);
    }

    try {
        httpMethod.setURI(uri);
    } catch (Exception e1) {
        logger.error(e1.getMessage(), e1);
    }

    HttpMethodParams httpParams = httpMethod.getParams();
    // default to use HTTP 1.0
    httpParams.setVersion(HttpVersion.HTTP_1_0);
    if (version.equalsIgnoreCase(HttpHeader.HTTP11)) {
        httpParams.setVersion(HttpVersion.HTTP_1_1);
    }

    // set various headers
    int pos = 0;
    // ZAP: changed to always use CRLF, like the HttpHeader
    Pattern pattern = patternCRLF;
    String delimiter = header.getLineDelimiter();

    // ZAP: Shouldn't happen as the HttpHeader always uses CRLF
    if (delimiter.equals(LF)) {
        delimiter = LF;
        pattern = patternLF;
    }

    String msg = header.getHeadersAsString();

    String[] split = pattern.split(msg);
    String token = null;
    String name = null;
    String value = null;

    for (int i = 0; i < split.length; i++) {
        token = split[i];
        if (token.equals("")) {
            continue;
        }

        if ((pos = token.indexOf(":")) < 0) {
            return null;
        }
        name = token.substring(0, pos).trim();
        value = token.substring(pos + 1).trim();
        httpMethod.addRequestHeader(name, value);

    }

    // set body if post method or put method
    if (body != null && body.length() > 0 && (httpMethod instanceof EntityEnclosingMethod)) {
        EntityEnclosingMethod post = (EntityEnclosingMethod) httpMethod;
        //         post.setRequestEntity(new StringRequestEntity(body.toString()));
        post.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));

    }

    httpMethod.setFollowRedirects(false);
    return httpMethod;

}

From source file:org.pentaho.versionchecker.VersionChecker.java

/**
 * Sets the URL (and parameters) for the request in the HttpMethod. The data provider information is sed to set the
 * parameters/*www .ja v a2  s  .c o  m*/
 * 
 * @param method
 *          the method which will have the URL set
 * @throws URIException
 *           Indicates an error creating the URI
 */
protected void setURL(HttpMethod method, String guid) throws URIException {
    String urlBase = null;
    final Map parameters = new HashMap();

    // If we have a data provider, get the parameters from there
    if (dataProvider != null) {
        // Get the URL
        urlBase = dataProvider.getBaseURL();

        // Get the extra parameters
        final Map params = dataProvider.getExtraInformation();
        if (params != null && params.size() > 0) {
            parameters.putAll(params);
        }

        // Add the specific parameters
        final String productID = dataProvider.getApplicationID();
        final String version = dataProvider.getApplicationVersion();
        final int depth = dataProvider.getDepth();
        final String vi = computeVI(productID);

        parameters.put("depth", "" + depth); //$NON-NLS-1$ //$NON-NLS-2$
        parameters.put("prodID", productID); //$NON-NLS-1$
        parameters.put("version", version); //$NON-NLS-1$
        parameters.put("guid", guid); //$NON-NLS-1$
        parameters.put("vi", vi); //$NON-NLS-1$
    }

    // Use the default URL if none is specified
    if (urlBase == null) {
        urlBase = getDefaultURL();
    }

    // Set the url in the method
    String urlCreated = createURL(urlBase, parameters);
    URI uri = new URI(urlCreated, true);
    method.setURI(uri);
}

From source file:org.structr.web.Importer.java

private void copyURLToFile(final String uri, final java.io.File fileOnDisk) throws IOException {

    final HttpClient client = getHttpClient();
    final HttpMethod get = new GetMethod();
    get.setURI(new URI(uri, false));

    get.addRequestHeader("User-Agent", "curl/7.35.0");

    logger.log(Level.INFO, "Downloading from {0}", uri);

    final int statusCode = client.executeMethod(get);

    if (statusCode == 200) {

        try (final InputStream is = get.getResponseBodyAsStream()) {

            try (final OutputStream os = new FileOutputStream(fileOnDisk)) {

                IOUtils.copy(is, os);//from   ww w  .  jav  a 2s.  c om
            }
        }

    } else {

        System.out.println("response body: " + new String(get.getResponseBody(), "utf-8"));
        logger.log(Level.WARNING, "Unable to create file {0}: status code was {1}",
                new Object[] { uri, statusCode });
    }
}

From source file:org.zaproxy.zap.extension.ascanrulesAlpha.CloudMetadataScanner.java

private static HttpMethod createRequestMethod(HttpRequestHeader header, HttpBody body, HttpMethodParams params)
        throws URIException {
    HttpMethod httpMethod = new ZapGetMethod();
    httpMethod.setURI(header.getURI());
    httpMethod.setParams(params);//from   ww w  .  j a  v a  2  s. c  o m
    params.setVersion(HttpVersion.HTTP_1_1);

    String msg = header.getHeadersAsString();

    String[] split = Pattern.compile("\\r\\n", Pattern.MULTILINE).split(msg);
    String token = null;
    String name = null;
    String value = null;

    int pos = 0;
    for (int i = 0; i < split.length; i++) {
        token = split[i];
        if (token.equals("")) {
            continue;
        }

        if ((pos = token.indexOf(":")) < 0) {
            return null;
        }
        name = token.substring(0, pos).trim();
        value = token.substring(pos + 1).trim();
        httpMethod.addRequestHeader(name, value);
    }
    if (body != null && body.length() > 0 && (httpMethod instanceof EntityEnclosingMethod)) {
        EntityEnclosingMethod post = (EntityEnclosingMethod) httpMethod;
        post.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
    }
    httpMethod.setFollowRedirects(false);
    return httpMethod;
}

From source file:poisondog.net.ApacheCommonsHttpPost.java

public HttpResponse execute(ApacheCommonsHttpParameter parameter) throws IOException {
    HttpMethod method = parameter.getMethod();
    method.setURI(new URI(parameter.getUrl(), true));
    List<Part> parts = new ArrayList<Part>();
    for (String key : parameter.textKeys()) {
        parts.add(new StringPart(key, parameter.getText(key)));
    }/* w  ww  .  j  a  va 2  s  .com*/
    for (String key : parameter.fileKeys()) {
        parts.add(new FilePart(key, parameter.getFile(key)));
    }

    HttpClient client = new HttpClient();
    int status = client.executeMethod(method);

    HttpResponse res = new HttpResponse(status);
    res.setInputStream(method.getResponseBodyAsStream());
    return res;
}

From source file:smartrics.rest.client.RestClientImpl.java

private void setUri(HttpMethod m, String hostAddr, RestRequest request) {
    String host = hostAddr == null ? client.getHostConfiguration().getHost() : hostAddr;
    if (host == null)
        throw new IllegalStateException("hostAddress is null: please config httpClient host configuration or "
                + "pass a valid host address or config a baseUrl on this client");
    String uriString = host + request.getResource();
    try {/*from   ww w  .  j a v a  2 s . c om*/
        m.setURI(createUri(uriString, false));
    } catch (URIException e) {
        throw new IllegalStateException("Problem when building URI: " + uriString, e);
    } catch (NullPointerException e) {
        throw new IllegalStateException("Building URI with null string", e);
    }
}