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

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

Introduction

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

Prototype

public abstract InputStream getResponseBodyAsStream() throws IOException;

Source Link

Usage

From source file:org.springbyexample.httpclient.HttpClientOxmTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * /*from  w w w . ja  v a2 s .  co  m*/
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
@SuppressWarnings("unchecked")
protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback callback) {
    try {
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        if (callback != null) {
            Object value = unmarshaller.unmarshal(new StreamSource(httpMethod.getResponseBodyAsStream()));

            callback.doWithResponse((T) value);
        }
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.springbyexample.httpclient.HttpClientTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * /*from  w w w . jav  a2s. co m*/
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback<?> callback) {
    try {
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        if (callback instanceof ResponseByteCallback) {
            ((ResponseByteCallback) callback).doWithResponse(httpMethod.getResponseBody());
        } else if (callback instanceof ResponseStreamCallback) {
            ((ResponseStreamCallback) callback).doWithResponse(httpMethod.getResponseBodyAsStream());
        } else if (callback instanceof ResponseStringCallback) {
            ((ResponseStringCallback) callback).doWithResponse(httpMethod.getResponseBodyAsString());
        }
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

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);/* www  . ja  v  a2  s.c  o  m*/
            }
        }

    } 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.tgta.tagger.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from  ww  w  .j a va 2s . co  m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

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

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody;
        InputStream in = method.getResponseBodyAsStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        //System.out.println(out.toString());   //Prints the string content read from input stream
        reader.close();

        response = out.toString();

        //TODO Going to buffer response body of large or unknown size. 
        //Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        //response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:org.tuckey.web.filters.urlrewrite.RequestProxy.java

/**
 * This method performs the proxying of the request to the target address.
 *
 * @param target     The target address. Has to be a fully qualified address. The request is send as-is to this address.
 * @param hsRequest  The request data which should be send to the
 * @param hsResponse The response data which will contain the data returned by the proxied request to target.
 * @throws java.io.IOException Passed on from the connection logic.
 *///from w  w  w . ja  v  a2  s .  c  o  m
public static void execute(final String target, final HttpServletRequest hsRequest,
        final HttpServletResponse hsResponse) throws IOException {
    if (log.isInfoEnabled()) {
        log.info("execute, target is " + target);
        log.info("response commit state: " + hsResponse.isCommitted());
    }

    if (StringUtils.isBlank(target)) {
        log.error("The target address is not given. Please provide a target address.");
        return;
    }

    log.info("checking url");
    final URL url;
    try {
        url = new URL(target);
    } catch (MalformedURLException e) {
        log.error("The provided target url is not valid.", e);
        return;
    }

    log.info("seting up the host configuration");

    final HostConfiguration config = new HostConfiguration();

    ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy"));
    if (proxyHost != null)
        config.setProxyHost(proxyHost);

    final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
    config.setHost(url.getHost(), port, url.getProtocol());

    if (log.isInfoEnabled())
        log.info("config is " + config.toString());

    final HttpMethod targetRequest = setupProxyRequest(hsRequest, url);
    if (targetRequest == null) {
        log.error("Unsupported request method found: " + hsRequest.getMethod());
        return;
    }

    //perform the reqeust to the target server
    final HttpClient client = new HttpClient(new SimpleHttpConnectionManager());
    if (log.isInfoEnabled()) {
        log.info("client state" + client.getState());
        log.info("client params" + client.getParams().toString());
        log.info("executeMethod / fetching data ...");
    }

    final int result;
    if (targetRequest instanceof EntityEnclosingMethod) {
        final RequestProxyCustomRequestEntity requestEntity = new RequestProxyCustomRequestEntity(
                hsRequest.getInputStream(), hsRequest.getContentLength(), hsRequest.getContentType());
        final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) targetRequest;
        entityEnclosingMethod.setRequestEntity(requestEntity);
        result = client.executeMethod(config, entityEnclosingMethod);

    } else {
        result = client.executeMethod(config, targetRequest);
    }

    //copy the target response headers to our response
    setupResponseHeaders(targetRequest, hsResponse);

    InputStream originalResponseStream = targetRequest.getResponseBodyAsStream();
    //the body might be null, i.e. for responses with cache-headers which leave out the body
    if (originalResponseStream != null) {
        OutputStream responseStream = hsResponse.getOutputStream();
        copyStream(originalResponseStream, responseStream);
    }

    log.info("set up response, result code was " + result);
}

From source file:org.wingsource.plugin.impl.gadget.bean.Gadget.java

private InputStream getContentStream(String href) {
    logger.finest("Fetching content using HttpClient....");
    HttpClient hc = new HttpClient();
    hc.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    HttpMethod method = new GetMethod(href);
    method.setFollowRedirects(true);/*from   www  . j av  a  2 s.c  o  m*/

    InputStream responseStream = null;
    try {
        hc.executeMethod(method);
        responseStream = method.getResponseBodyAsStream();
    } catch (HttpException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return responseStream;
}

From source file:org.xwiki.xwoot.manager.internal.DefaultXWootManager.java

private Map<String, Object> getStatusMap(String type) {
    String uri = String.format("%s/status?type=%s", getXWootAppAddress(), type);
    HttpMethod method = new GetMethod(uri);
    try {//w ww  .j a  va  2 s .  c om
        getLogger().debug("Requesting: " + method.getURI());
        if (client.executeMethod(method) < 400) {
            XStream xstream = new XStream(new DomDriver());
            Map<String, Object> result = (Map<String, Object>) xstream
                    .fromXML(method.getResponseBodyAsStream());
            getLogger().debug("Result: " + result);
            return result;
        }
        getLogger().info("Failed call: " + method.getStatusLine());
    } catch (Exception ex) {
        getLogger().warn("Exception occured while calling [" + uri + "]", ex);
    } finally {
        // Release the connection, since HTTPClient reuses connections for improved performance
        method.releaseConnection();
    }

    return new HashMap<String, Object>();
}

From source file:pl.umk.mat.zawodyweb.compiler.classes.LanguageMAIN.java

/**
 * Sprawdza rozwizanie na input//from   w  ww.j  av  a  2s  .  co  m
 * @param path kod rdowy
 * @param input w formacie:
 *              <pre>c=NUMER_KONKURSU<br/>t=NUMER_ZADANIA<br/>m=MAX_POINTS</pre>
 * @return
 */
@Override
public TestOutput runTest(String path, TestInput input) {
    TestOutput result = new TestOutput(null);

    Integer contest_id = null;
    Integer task_id = null;
    Integer max_points = null;
    try {
        try {
            Matcher matcher = null;

            matcher = Pattern.compile("c=([0-9]+)").matcher(input.getInputText());
            if (matcher.find()) {
                contest_id = Integer.valueOf(matcher.group(1));
            }

            matcher = Pattern.compile("t=([0-9]+)").matcher(input.getInputText());
            if (matcher.find()) {
                task_id = Integer.valueOf(matcher.group(1));
            }

            matcher = Pattern.compile("m=([0-9]+)").matcher(input.getInputText());
            if (matcher.find()) {
                max_points = Integer.valueOf(matcher.group(1));
            }

            if (contest_id == null) {
                throw new IllegalArgumentException("task_id == null");
            }
            if (task_id == null) {
                throw new IllegalArgumentException("task_id == null");
            }
        } catch (PatternSyntaxException ex) {
            throw new IllegalArgumentException(ex);
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(ex);
        } catch (IllegalStateException ex) {
            throw new IllegalArgumentException(ex);
        }
    } catch (IllegalArgumentException e) {
        logger.error("Exception when parsing input", e);
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("MAIN IllegalArgumentException");
        return result;
    }
    logger.debug("Contest id = " + contest_id);
    logger.debug("Task id = " + task_id);
    logger.debug("Max points = " + max_points);

    String loginUrl = "http://main.edu.pl/pl/login";
    String login = properties.getProperty("main_edu_pl.login");
    String password = properties.getProperty("main_edu_pl.password");

    HttpClient client = new HttpClient();

    HttpClientParams params = client.getParams();
    params.setParameter("http.useragent", "Opera/9.64 (Windows NT 6.0; U; pl) Presto/2.1.1");
    //params.setParameter("http.protocol.handle-redirects", true);
    client.setParams(params);
    /* logowanie */
    logger.debug("Logging in");
    PostMethod postMethod = new PostMethod(loginUrl);
    NameValuePair[] dataLogging = { new NameValuePair("auth", "1"), new NameValuePair("login", login),
            new NameValuePair("pass", password) };
    postMethod.setRequestBody(dataLogging);
    try {
        client.executeMethod(postMethod);
        if (Pattern.compile("Logowanie udane").matcher(postMethod.getResponseBodyAsString(1024 * 1024))
                .find() == false) {
            logger.error("Unable to login (" + login + ":" + password + ")");
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setOutputText("Logging in failed");
            postMethod.releaseConnection();
            return result;
        }
    } catch (HttpException e) {
        logger.error("Exception when logging in", e);
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("HttpException");
        postMethod.releaseConnection();
        return result;
    } catch (IOException e) {
        logger.error("Exception when logging in", e);
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("IOException");
        postMethod.releaseConnection();
        return result;
    }
    postMethod.releaseConnection();

    /* wchodzenie na stron z wysyaniem zada i pobieranie pl z hidden */
    logger.debug("Getting submit page");
    ArrayList<Part> values = new ArrayList<Part>();
    try {
        GetMethod getMethod = new GetMethod("http://main.edu.pl/user.phtml?op=submit&m=insert&c=" + contest_id);
        client.executeMethod(getMethod);
        String response = getMethod.getResponseBodyAsString(1024 * 1024);
        getMethod.releaseConnection();

        Matcher tagMatcher = Pattern.compile("<input[^>]*>").matcher(response);
        Pattern namePattern = Pattern.compile("name\\s*=\"([^\"]*)\"");
        Pattern valuePattern = Pattern.compile("value\\s*=\"([^\"]*)\"");
        while (tagMatcher.find()) {
            Matcher matcher = null;
            String name = null;
            String value = null;

            String inputTag = tagMatcher.group();

            matcher = namePattern.matcher(inputTag);
            if (matcher.find()) {
                name = matcher.group(1);
            }

            matcher = valuePattern.matcher(inputTag);
            if (matcher.find()) {
                value = matcher.group(1);
            }

            if (name != null && value != null && name.equals("solution") == false) {
                values.add(new StringPart(name, value));
            }
        }
    } catch (HttpException ex) {
        logger.error("Exception when getting submit page", ex);
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(ex.getMessage());
        result.setOutputText("IOException");
        return result;
    } catch (IOException ex) {
        logger.error("Exception when getting submit page", ex);
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(ex.getMessage());
        result.setOutputText("IOException");
        return result;
    }

    values.add(new StringPart("task", task_id.toString()));

    String filename = properties.getProperty("CODE_FILENAME");
    filename = filename.replaceAll("\\." + properties.getProperty("CODEFILE_EXTENSION") + "$", "");
    filename = filename + "." + properties.getProperty("CODEFILE_EXTENSION");
    FilePart filePart = new FilePart("solution", new ByteArrayPartSource(filename, path.getBytes()));
    values.add(filePart);

    /* wysyanie rozwizania */
    logger.debug("Submiting solution");
    Integer solution_id = null;
    postMethod = new PostMethod("http://main.edu.pl/user.phtml?op=submit&m=db_insert&c=" + contest_id);
    postMethod.setRequestEntity(new MultipartRequestEntity(values.toArray(new Part[0]), client.getParams()));
    try {
        try {
            client.executeMethod(postMethod);
            HttpMethod method = postMethod;

            /* check if redirect */
            Header locationHeader = postMethod.getResponseHeader("location");
            if (locationHeader != null) {
                String redirectLocation = locationHeader.getValue();
                GetMethod getMethod = new GetMethod(
                        new URI(postMethod.getURI(), new URI(redirectLocation, false)).getURI());
                client.executeMethod(getMethod);
                method = getMethod;
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            Matcher matcher = Pattern.compile("<tr id=\"rptr\">.*?</tr>", Pattern.DOTALL)
                    .matcher(sb.toString());
            if (matcher.find()) {
                Matcher idMatcher = Pattern.compile("id=([0-9]+)").matcher(matcher.group());
                if (idMatcher.find()) {
                    solution_id = Integer.parseInt(idMatcher.group(1));
                }
            }
            if (solution_id == null) {
                throw new IllegalArgumentException("solution_id == null");
            }
        } catch (HttpException e) {
            new IllegalArgumentException(e);
        } catch (IOException e) {
            new IllegalArgumentException(e);
        } catch (NumberFormatException e) {
            new IllegalArgumentException(e);
        } catch (IllegalStateException e) {
            new IllegalArgumentException(e);
        }

    } catch (IllegalArgumentException e) {
        logger.error("Exception when submiting solution", e);
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("IllegalArgumentException");
        postMethod.releaseConnection();
        return result;
    }

    postMethod.releaseConnection();

    /* sprawdzanie statusu */
    logger.debug("Checking result for main.id=" + solution_id);
    Pattern resultRowPattern = Pattern.compile("id=" + solution_id + ".*?</tr>", Pattern.DOTALL);
    Pattern resultPattern = Pattern.compile(
            "</td>.*?<td.*?>.*?</td>.*?<td.*?>(.*?)</td>.*?<td.*?>.*?</td>.*?<td.*?>(.*?)</td>",
            Pattern.DOTALL);
    result_loop: while (true) {
        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(e.getMessage());
            result.setOutputText("InterruptedException");
            return result;
        }

        GetMethod getMethod = new GetMethod("http://main.edu.pl/user.phtml?op=zgloszenia&c=" + contest_id);
        try {
            client.executeMethod(getMethod);
            String response = getMethod.getResponseBodyAsString(1024 * 1024);
            getMethod.releaseConnection();

            Matcher matcher = resultRowPattern.matcher(response);
            // "</td>.*?<td.*?>.*?[NAZWA_ZADANIA]</td>.*?<td.*?>(.*?[STATUS])</td>.*?<td.*?>.*?</td>.*?<td.*?>(.*?[PUNKTY])</td>"
            while (matcher.find()) {
                Matcher resultMatcher = resultPattern.matcher(matcher.group());

                if (resultMatcher.find() && resultMatcher.groupCount() == 2) {
                    String resultType = resultMatcher.group(1);
                    if (resultType.equals("?")) {
                        continue;
                    } else if (resultType.matches("B..d kompilacji")) { // CE
                        result.setStatus(ResultsStatusEnum.CE.getCode());
                        result.setPoints(
                                calculatePoints(resultMatcher.group(2), input.getMaxPoints(), max_points));
                    } else if (resultType.matches("Program wyw.aszczony")) { // TLE
                        result.setStatus(ResultsStatusEnum.TLE.getCode());
                        result.setPoints(
                                calculatePoints(resultMatcher.group(2), input.getMaxPoints(), max_points));
                    } else if (resultType.matches("B..d wykonania")) { // RTE
                        result.setStatus(ResultsStatusEnum.RE.getCode());
                        result.setPoints(
                                calculatePoints(resultMatcher.group(2), input.getMaxPoints(), max_points));
                    } else if (resultType.matches("Z.a odpowied.")) { // WA
                        result.setStatus(ResultsStatusEnum.WA.getCode());
                        result.setPoints(
                                calculatePoints(resultMatcher.group(2), input.getMaxPoints(), max_points));
                    } else if (resultType.equals("OK")) { // AC
                        result.setStatus(ResultsStatusEnum.ACC.getCode());
                        result.setPoints(
                                calculatePoints(resultMatcher.group(2), input.getMaxPoints(), max_points));
                    } else {
                        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
                        result.setNotes("Unknown status: \"" + resultType + "\"");
                    }
                    break result_loop;
                }
            }
        } catch (HttpException ex) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(ex.getMessage());
            result.setOutputText("HttpException");
            return result;
        } catch (IOException ex) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(ex.getMessage());
            result.setOutputText("IOException");
            return result;
        }
    }
    return result;
}

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)));
    }/*from  w  ww. j a va 2 s  .  co  m*/
    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:pt.webdetails.cns.notifications.sparkl.kettle.baserver.web.utils.HttpConnectionHelper.java

public static Response callHttp(String url, String user, String password)
        throws IOException, KettleStepException {

    // used for calculating the responseTime
    long startTime = System.currentTimeMillis();

    HttpClient httpclient = SlaveConnectionManager.getInstance().createHttpClient();
    HttpMethod method = new GetMethod(url);
    httpclient.getParams().setAuthenticationPreemptive(true);
    Credentials credentials = new UsernamePasswordCredentials(user, password);
    httpclient.getState().setCredentials(AuthScope.ANY, credentials);
    HostConfiguration hostConfiguration = new HostConfiguration();

    int status;//from w w w  .j  av a  2s .  c o m
    try {
        status = httpclient.executeMethod(hostConfiguration, method);
    } catch (IllegalArgumentException ex) {
        status = -1;
    }

    Response response = new Response();
    if (status != -1) {
        String body = null;
        String encoding = "";
        Header contentTypeHeader = method.getResponseHeader("Content-Type");
        if (contentTypeHeader != null) {
            String contentType = contentTypeHeader.getValue();
            if (contentType != null && contentType.contains("charset")) {
                encoding = contentType.replaceFirst("^.*;\\s*charset\\s*=\\s*", "").replace("\"", "").trim();
            }
        }

        // get the response
        InputStreamReader inputStreamReader = null;
        if (!Const.isEmpty(encoding)) {
            inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream(), encoding);
        } else {
            inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream());
        }
        StringBuilder bodyBuffer = new StringBuilder();
        int c;
        while ((c = inputStreamReader.read()) != -1) {
            bodyBuffer.append((char) c);
        }
        inputStreamReader.close();
        body = bodyBuffer.toString();

        // Get response time
        long responseTime = System.currentTimeMillis() - startTime;

        response.setStatusCode(status);
        response.setResult(body);
        response.setResponseTime(responseTime);
    }
    return response;
}