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.codehaus.mojo.fitnesse.FitnesseRemoteRunnerMojo.java

void getRemoteResource(String pUrl, OutputStream pOutStream, Fitnesse pServer) throws MojoExecutionException {
    try {/*from  w w w. j a  v a  2  s . c om*/
        HttpClient tClient = new HttpClient();
        getLog().info("Request resources from [" + pUrl + "]");
        if (pServer.getServerId() != null) {
            tClient.getParams().setAuthenticationPreemptive(true);
            Credentials defaultcreds = getCredential(pServer.getServerId());
            AuthScope tAuthScope = new AuthScope(pServer.getHostName(), pServer.getPort(), AuthScope.ANY_REALM);
            tClient.getState().setCredentials(tAuthScope, defaultcreds);
            getLog().info("Use credential for remote connection");
        }
        HttpMethod tMethod = new GetMethod(pUrl);
        int tStatusCode = tClient.executeMethod(tMethod);
        if (tStatusCode != 200) {
            throw new MojoExecutionException(
                    "Bad response code from resource [" + pUrl + "], return code=[" + tStatusCode + "]");
        }

        InputStream tResponseStream = tMethod.getResponseBodyAsStream();
        byte[] tbytes = new byte[512];
        int tReadBytes = tResponseStream.read(tbytes);
        while (tReadBytes >= 0) {
            pOutStream.write(tbytes, 0, tReadBytes);
            tReadBytes = tResponseStream.read(tbytes);
        }
        pOutStream.flush();
        tMethod.releaseConnection();
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to read FitNesse server response.", e);
    } finally {
        try {
            pOutStream.close();
        } catch (IOException e) {
            getLog().error("Unable to close Stream.");
        }
    }
}

From source file:org.codehaus.wadi.web.impl.CommonsHttpProxy.java

protected void doProxy(URI uri, WebInvocation context) throws ProxyingException {
    HttpServletRequest hreq = context.getHreq();
    HttpServletResponse hres = context.getHres();

    long startTime = System.currentTimeMillis();

    String m = hreq.getMethod();//  w ww .jav  a  2  s.c om
    Class clazz = (Class) _methods.get(m);
    if (clazz == null) {
        throw new IrrecoverableException("unsupported http method: " + m);
    }

    HttpMethod hm = null;
    try {
        hm = (HttpMethod) clazz.newInstance();
    } catch (Exception e) {
        throw new IrrecoverableException("could not create HttpMethod instance", e); // should never happen
    }

    String requestURI = getRequestURI(hreq);
    hm.setPath(requestURI);

    String queryString = hreq.getQueryString();
    if (queryString != null) {
        hm.setQueryString(queryString);
        requestURI += queryString;
    }

    hm.setFollowRedirects(false);
    //hm.setURI(new URI(uri));
    hm.setStrictMode(false);

    // check connection header
    String connectionHdr = hreq.getHeader("Connection"); // TODO - what if there are multiple values ?
    if (connectionHdr != null) {
        connectionHdr = connectionHdr.toLowerCase();
        if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close"))
            connectionHdr = null; // TODO  ??
    }

    // copy headers
    boolean xForwardedFor = false;
    boolean hasContent = false;
    int contentLength = 0;
    Enumeration enm = hreq.getHeaderNames();
    while (enm.hasMoreElements()) {
        // TODO could be better than this! - using javax.servlet ?
        String hdr = (String) enm.nextElement();
        String lhdr = hdr.toLowerCase();

        if (_DontProxyHeaders.contains(lhdr))
            continue;
        if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0)
            continue;

        if ("content-length".equals(lhdr)) {
            try {
                contentLength = hreq.getIntHeader(hdr);
                hasContent = contentLength > 0;
            } catch (NumberFormatException e) {
                if (_log.isWarnEnabled())
                    _log.warn("bad Content-Length header value: " + hreq.getHeader(hdr), e);
            }
        }

        if ("content-type".equals(lhdr)) {
            hasContent = true;
        }

        Enumeration vals = hreq.getHeaders(hdr);
        while (vals.hasMoreElements()) {
            String val = (String) vals.nextElement();
            if (val != null) {
                hm.addRequestHeader(hdr, val);
                // if (_log.isInfoEnabled()) _log.info("Request " + hdr + ": " + val);
                xForwardedFor |= "X-Forwarded-For".equalsIgnoreCase(hdr); // why is this not in the outer loop ?
            }
        }
    }

    // cookies...

    // although we copy cookie headers into the request abover - commons-httpclient thinks it knows better and strips them out before sending.
    // we have to explicitly use their interface to add the cookies - painful...

    // DOH! - an org.apache.commons.httpclient.Cookie is NOT a
    // javax.servlet.http.Cookie - and it looks like the two don't
    // map onto each other without data loss...
    HttpState state = new HttpState();
    javax.servlet.http.Cookie[] cookies = hreq.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            javax.servlet.http.Cookie c = cookies[i];
            String domain = c.getDomain();
            if (domain == null) {
                domain = hreq.getServerName(); // TODO - tmp test
                // _log.warn("defaulting cookie domain");
            }
            //     domain=null;
            String cpath = c.getPath();
            if (cpath == null) {
                cpath = hreq.getContextPath(); // fix for Jetty
                // _log.warn("defaulting cookie path");
            }
            //if (_log.isTraceEnabled()) _log.trace("PATH: value="+path+" length="+(path==null?0:path.length()));
            Cookie cookie = new Cookie(domain, c.getName(), c.getValue(), cpath, c.getMaxAge(), c.getSecure()); // TODO - sort out domain
            //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.getDomain()+","+ cookie.getName()+","+ cookie.getValue()+","+ cookie.getPath()+","+ cookie.getExpiryDate()+","+ cookie.getSecure());
            state.addCookie(cookie);
            //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.toString());
        }
    }

    // Proxy headers
    hm.addRequestHeader("Via", "1.1 " + hreq.getLocalName() + ":" + hreq.getLocalPort() + " \"WADI\"");
    if (!xForwardedFor)
        hm.addRequestHeader("X-Forwarded-For", hreq.getRemoteAddr());
    // Max-Forwards...

    // a little bit of cache control
    //      String cache_control = hreq.getHeader("Cache-Control");
    //      if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0))
    //      httpMethod.setUseCaches(false);

    // customize Connection
    //      uc.setDoInput(true);

    int client2ServerTotal = 0;
    if (hasContent) {
        //         uc.setDoOutput(true);

        try {
            if (hm instanceof EntityEnclosingMethod)
                ((EntityEnclosingMethod) hm).setRequestBody(hreq.getInputStream());
            // TODO - do we need to close response stream at end... ?
        } catch (IOException e) {
            throw new IrrecoverableException("could not pss request input across proxy", e);
        }
    }

    try {
        HttpClient client = new HttpClient();
        HostConfiguration hc = new HostConfiguration();
        //String host=location.getAddress().getHostAddress();
        // inefficient - but stops httpclient from rejecting half our cookies...
        String host = uri.getHost();
        hc.setHost(host, uri.getPort());
        client.executeMethod(hc, hm, state);
    } catch (IOException e) // TODO
    {
        _log.warn("problem proxying connection:", e);
    }

    InputStream fromServer = null;

    // handler status codes etc.
    int code = 502;
    //      String message="Bad Gateway: could not read server response code or message";

    code = hm.getStatusCode(); // IOException
    //      message=hm.getStatusText(); // IOException
    hres.setStatus(code);
    //      hres.setStatus(code, message); - deprecated...

    try {
        fromServer = hm.getResponseBodyAsStream(); // IOException
    } catch (IOException e) {
        _log.warn("problem acquiring http client output", e);
    }

    // clear response defaults.
    hres.setHeader("Date", null);
    hres.setHeader("Server", null);

    // set response headers
    // TODO - is it a bug in Jetty that I have to start my loop at 1 ? or that key[0]==null ?
    // Try this inside Tomcat...
    Header[] headers = hm.getResponseHeaders();
    for (int i = 0; i < headers.length; i++) {
        String h = headers[i].toExternalForm();
        int index = h.indexOf(':');
        String key = h.substring(0, index).trim().toLowerCase();
        String val = h.substring(index + 1, h.length()).trim();
        if (val != null && !_DontProxyHeaders.contains(key)) {
            hres.addHeader(key, val);
            // if (_log.isInfoEnabled()) _log.info("Response: "+key+" - "+val);
        }
    }

    hres.addHeader("Via", "1.1 (WADI)");

    // copy server->client
    int server2ClientTotal = 0;
    if (fromServer != null) {
        try {
            OutputStream toClient = hres.getOutputStream();// IOException
            server2ClientTotal += copy(fromServer, toClient, 8192);// IOException
        } catch (IOException e) {
            _log.warn("problem proxying server response back to client", e);
        } finally {
            try {
                fromServer.close();
            } catch (IOException e) {
                // well - we did our best...
                _log.warn("problem closing server response stream", e);
            }
        }
    }

    long endTime = System.currentTimeMillis();
    long elapsed = endTime - startTime;
    if (_log.isDebugEnabled()) {
        _log.debug("in:" + client2ServerTotal + ", out:" + server2ClientTotal + ", status:" + code + ", time:"
                + elapsed + ", uri:" + uri);
    }
}

From source file:org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument.java

public void setResponse(HttpMethod method, int status) throws IOException, DocumentException {
    this.status = status;
    InputStream stream = method.getResponseBodyAsStream();
    SAXReader reader = new SAXReader();
    if (status >= 400) {
        log.error("Got error : " + IOUtils.toString(stream));
    }// w w w. j  av a2s  . co m
    // TODO errorhandling
    Document out = null;
    Header content_type = method.getResponseHeader("Content-Type");
    if (content_type != null && "application/xml".equals(content_type.getValue())) {
        if (log.isDebugEnabled()) {
            ByteArrayOutputStream dump = new ByteArrayOutputStream();
            // TODO CSPACE-2552 add ,"UTF-8" to reader.read()?
            out = reader.read(new TeeInputStream(stream, dump));
            log.debug(dump.toString("UTF-8"));
        } else {
            // TODO CSPACE-2552 add ,"UTF-8" to reader.read()?
            out = reader.read(stream);
        }
    }
    stream.close();
    doc = out;
}

From source file:org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument.java

public void setResponse(HttpMethod method, int status) throws Exception {
    setStatus(status);//  w  w  w.  j  ava  2 s.c om
    InputStream stream = method.getResponseBodyAsStream();
    SAXReader reader = new SAXReader();
    if (isErrorStatus()) {
        log.info("Got error : " + IOUtils.toString(stream));
    }
    // TODO errorhandling
    Document doc = null;
    Header content_type = method.getResponseHeader("Content-Type");
    if (content_type != null && "application/xml".equals(content_type.getValue())) {

        if (log.isDebugEnabled()) {
            ByteArrayOutputStream dump = new ByteArrayOutputStream();
            doc = reader.read(new TeeInputStream(stream, dump));
            log.debug(dump.toString("UTF-8"));
        } else {
            doc = reader.read(stream, "UTF-8");
        }
        //split up document
        Element root = doc.getRootElement();
        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();
            addDocument(element.getName(), DocumentHelper.parseText(element.asXML()));
        }
    }
    stream.close();
}

From source file:org.colombbus.tangara.net.TConnection.java

private String executeMethod(HttpMethod method) throws CommandException {
    int statusCode = 0;
    try {//from www . java2s .  c o  m
        statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_REQUEST_TIMEOUT) {
            LOG.warn("Request timeout");
        }
        if (statusCode != HttpStatus.SC_OK) {
            String msg = "Method failed: " + method.getStatusLine(); //$NON-NLS-1$
            LOG.error(msg);
            throw new IOException(msg);
        }

        InputStream in = method.getResponseBodyAsStream();
        byte[] buffer = IOUtils.toByteArray(in);

        String response = new String(buffer, encodingCharset);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Response content:\n" + response);
        }
        return response;
    } catch (HttpException httpEx) {
        String msg = String.format("An HTTP error occurs during the " + //$NON-NLS-1$
                "execution of the method %s. The returned HTTP code is %d", //$NON-NLS-1$
                method.getPath(), statusCode);
        LOG.error(msg, httpEx);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg,
                httpEx);
    } catch (IOException ioEx) {
        String msg = String.format("An IO communication error occurs during the execution of the method %s.",
                method.getPath());
        LOG.error(msg, ioEx);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg,
                ioEx);
    } catch (Throwable th) {
        String msg = String.format("An unknown error occurs during the execution of the method %s",
                method.getPath());
        LOG.error(msg, th);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg, th);

    }
}

From source file:org.crosswire.common.util.WebResource.java

/**
 * Copy this WebResource to the destination.
 *
 * @param dest/* w  w  w . j  av a2 s . c  om*/
 * @throws LucidException
 */
public void copy(URI dest) throws LucidException {
    InputStream in = null;
    OutputStream out = null;

    HttpMethod method = new GetMethod(uri.getPath());

    try {
        // Execute the method.
        if (client.executeMethod(method) == HttpStatus.SC_OK) {
            in = method.getResponseBodyAsStream();

            // Download the index file
            out = NetUtil.getOutputStream(dest);

            byte[] buf = new byte[4096];
            int count = in.read(buf);
            while (-1 != count) {
                out.write(buf, 0, count);
                count = in.read(buf);
            }
        }
    } catch (IOException e) {
        throw new LucidException(Msg.MISSING_FILE, e);
    } finally {
        // Release the connection.
        method.releaseConnection();
        // Close the streams
        IOUtil.close(in);
        IOUtil.close(out);
    }
}

From source file:org.cryptomator.frontend.webdav.WebDavServerTest.java

@Test
public void testGet() throws HttpException, IOException {
    final HttpClient client = new HttpClient();

    // write test content:
    final byte[] testContent = "hello world".getBytes();
    try (WritableFile w = fs.file("foo.txt").openWritable()) {
        w.write(ByteBuffer.wrap(testContent));
    }/*from w  ww. j av  a  2 s .  com*/

    // check get response body:
    final HttpMethod getMethod = new GetMethod(servletRoot + "/foo.txt");
    final int statusCode = client.executeMethod(getMethod);
    Assert.assertEquals(200, statusCode);
    Assert.assertThat(getMethod.getResponseHeaders(), hasItemInArray(new Header("Accept-Ranges", "bytes")));
    Assert.assertTrue(
            IOUtils.contentEquals(new ByteArrayInputStream(testContent), getMethod.getResponseBodyAsStream()));
    getMethod.releaseConnection();
}

From source file:org.cryptomator.frontend.webdav.WebDavServerTest.java

@Test
public void testMultipleGetWithRangeAsync() throws IOException, URISyntaxException, InterruptedException {
    final String testResourceUrl = servletRoot + "/foo.txt";

    // prepare 8MiB test data:
    final byte[] plaintextData = new byte[2097152 * Integer.BYTES];
    final ByteBuffer plaintextDataByteBuffer = ByteBuffer.wrap(plaintextData);
    for (int i = 0; i < 2097152; i++) {
        plaintextDataByteBuffer.putInt(i);
    }//from w w  w.  j av  a2s .co m
    try (WritableFile w = fs.file("foo.txt").openWritable()) {
        plaintextDataByteBuffer.flip();
        w.write(plaintextDataByteBuffer);
    }

    final MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
    cm.getParams().setDefaultMaxConnectionsPerHost(50);
    final HttpClient client = new HttpClient(cm);

    // multiple async range requests:
    final List<ForkJoinTask<?>> tasks = new ArrayList<>();
    final Random generator = new Random(System.currentTimeMillis());

    final AtomicBoolean success = new AtomicBoolean(true);

    // 10 full interrupted requests:
    for (int i = 0; i < 10; i++) {
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                final int statusCode = client.executeMethod(getMethod);
                if (statusCode != 200) {
                    LOG.error("Invalid status code for interrupted full request");
                    success.set(false);
                }
                getMethod.getResponseBodyAsStream().read();
                getMethod.getResponseBodyAsStream().close();
                getMethod.releaseConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 50 crappy interrupted range requests:
    for (int i = 0; i < 50; i++) {
        final int lower = generator.nextInt(plaintextData.length);
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-");
                final int statusCode = client.executeMethod(getMethod);
                if (statusCode != 206) {
                    LOG.error("Invalid status code for interrupted range request");
                    success.set(false);
                }
                getMethod.getResponseBodyAsStream().read();
                getMethod.getResponseBodyAsStream().close();
                getMethod.releaseConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 50 normal open range requests:
    for (int i = 0; i < 50; i++) {
        final int lower = generator.nextInt(plaintextData.length - 512);
        final int upper = plaintextData.length - 1;
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-");
                final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1);
                final int statusCode = client.executeMethod(getMethod);
                final byte[] responseBody = new byte[upper - lower + 10];
                final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
                getMethod.releaseConnection();
                if (statusCode != 206) {
                    LOG.error("Invalid status code for open range request");
                    success.set(false);
                } else if (upper - lower + 1 != bytesRead) {
                    LOG.error("Invalid response length for open range request");
                    success.set(false);
                } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) {
                    LOG.error("Invalid response body for open range request");
                    success.set(false);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 200 normal closed range requests:
    for (int i = 0; i < 200; i++) {
        final int pos1 = generator.nextInt(plaintextData.length - 512);
        final int pos2 = pos1 + 512;
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final int lower = Math.min(pos1, pos2);
                final int upper = Math.max(pos1, pos2);
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-" + upper);
                final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1);
                final int statusCode = client.executeMethod(getMethod);
                final byte[] responseBody = new byte[upper - lower + 1];
                final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
                getMethod.releaseConnection();
                if (statusCode != 206) {
                    LOG.error("Invalid status code for closed range request");
                    success.set(false);
                } else if (upper - lower + 1 != bytesRead) {
                    LOG.error("Invalid response length for closed range request");
                    success.set(false);
                } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) {
                    LOG.error("Invalid response body for closed range request");
                    success.set(false);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    Collections.shuffle(tasks, generator);

    final ForkJoinPool pool = new ForkJoinPool(4);
    for (ForkJoinTask<?> task : tasks) {
        pool.execute(task);
    }
    for (ForkJoinTask<?> task : tasks) {
        task.join();
    }
    pool.shutdown();
    cm.shutdown();

    Assert.assertTrue(success.get());
}

From source file:org.cryptomator.ui.controllers.WelcomeController.java

private void checkForUpdates() {
    checkForUpdatesStatus.setText(localization.getString("welcome.checkForUpdates.label.currentlyChecking"));
    checkForUpdatesIndicator.setVisible(true);
    asyncTaskService.asyncTaskOf(() -> {
        final HttpClient client = new HttpClient();
        final HttpMethod method = new GetMethod("https://cryptomator.org/downloads/latestVersion.json");
        client.getParams().setParameter(HttpClientParams.USER_AGENT,
                "Cryptomator VersionChecker/" + ApplicationVersion.orElse("SNAPSHOT"));
        client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
        client.getParams().setConnectionManagerTimeout(5000);
        client.executeMethod(method);/*from w  ww  . j  ava  2 s. co  m*/
        final InputStream responseBodyStream = method.getResponseBodyAsStream();
        if (method.getStatusCode() == HttpStatus.SC_OK && responseBodyStream != null) {
            final byte[] responseData = IOUtils.toByteArray(responseBodyStream);
            final ObjectMapper mapper = new ObjectMapper();
            final Map<String, String> map = mapper.readValue(responseData,
                    new TypeReference<HashMap<String, String>>() {
                    });
            if (map != null) {
                this.compareVersions(map);
            }
        }
    }).andFinally(() -> {
        checkForUpdatesStatus.setText("");
        checkForUpdatesIndicator.setVisible(false);
    }).run();
}

From source file:org.dacapo.tomcat.Page.java

/**
 * Fetch a page from an Http connection.
 * @param method The method to invoke/* w  w  w.  jav a 2 s.  c o  m*/
 * @param logFile Where to write the log (if written)
 * @param keep Write the log on success (always writes on failure)
 * @return Whether the fetch failed or succeeded
 * @throws IOException A network or disk I/O error
 */
protected final boolean fetch(Session session, HttpMethod method, File logFile, boolean keep)
        throws IOException {
    final int iGetResultCode = session.httpClient.executeMethod(method);
    final String strGetResponseBody = readStream(method.getResponseBodyAsStream());
    final String strGetResponseBodyLocalized = strGetResponseBody.replace("\n",
            System.getProperty("line.separator"));
    if (keep) {
        writeLog(logFile, strGetResponseBodyLocalized);
    }

    if (iGetResultCode != expectedStatus) {
        System.err.printf("URL %s returned status %d (expected %d)%n", address, iGetResultCode, expectedStatus);
        if (!keep)
            writeLog(logFile, strGetResponseBodyLocalized);
        return false;
    }

    if (expectedDigest == null) {
        return true;
    }

    String digestString = stringDigest(strGetResponseBody);
    boolean digestMatch = digestString.equals(expectedDigest);
    if (!digestMatch) {
        if (!keep)
            writeLog(logFile, strGetResponseBodyLocalized);
        System.err.printf(
                "URL %s%n" + "   expected %s%n" + "   found    %s%n" + "   response code %d, log file %s%n",
                address, expectedDigest, digestString, iGetResultCode, logFile.getName());
    }
    return digestMatch;
}