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:fedora.client.test.PerformanceTests.java

private void runGetDatastreamRest(String pid) throws Exception {
    HttpMethod httpMethod = getHttpMethod(pid);
    HttpClient client = getHttpClient();
    client.executeMethod(httpMethod);/*from  w w  w  .ja  va2 s  .c  o m*/
    InputStream in = httpMethod.getResponseBodyAsStream();
    int input = in.read();
    while (input > 0) {
        input = in.read();
    }
}

From source file:com.zimbra.cs.fb.ExchangeFreeBusyProvider.java

@Override
public boolean handleMailboxChange(String accountId) {
    String email = getEmailAddress(accountId);
    ServerInfo serverInfo = getServerInfo(email);
    if (email == null || !serverInfo.enabled) {
        return true; // no retry
    }//from w w  w.  j a v  a  2 s  .c  o  m
    FreeBusy fb;
    try {
        fb = getFreeBusy(accountId, FreeBusyQuery.CALENDAR_FOLDER_ALL);
    } catch (ServiceException se) {
        ZimbraLog.fb.warn("can't get freebusy for account " + accountId, se);
        // retry the request if it's receivers fault.
        return !se.isReceiversFault();
    }
    if (email == null || fb == null) {
        ZimbraLog.fb.warn("account not found / incorrect / wrong host: " + accountId);
        return true; // no retry
    }
    if (serverInfo == null || serverInfo.org == null || serverInfo.cn == null) {
        ZimbraLog.fb.warn("no exchange server info for user " + email);
        return true; // no retry
    }
    ExchangeMessage msg = new ExchangeMessage(serverInfo.org, serverInfo.cn, email);
    String url = serverInfo.url + msg.getUrl();

    HttpMethod method = null;

    try {
        ZimbraLog.fb.debug("POST " + url);
        method = msg.createMethod(url, fb);
        method.setRequestHeader(HEADER_TRANSLATE, "f");
        int status = sendRequest(method, serverInfo);
        if (status != MULTI_STATUS) {
            InputStream resp = method.getResponseBodyAsStream();
            String respStr = (resp == null ? "" : new String(ByteUtil.readInput(resp, 1024, 1024), "UTF-8"));
            ZimbraLog.fb.error("cannot modify resource at %s : http error %d, buf (%s)", url, status, respStr);
            return false; // retry
        }
    } catch (IOException ioe) {
        ZimbraLog.fb.error("error communicating with " + serverInfo.url, ioe);
        return false; // retry
    } finally {
        method.releaseConnection();
    }
    return true;
}

From source file:com.liferay.util.Http.java

public static byte[] URLtoByteArray(String location, Cookie[] cookies, boolean post) throws IOException {

    byte[] byteArray = null;

    HttpMethod method = null;

    try {//from  w  w w. ja  va  2  s. co  m
        HttpClient client = new HttpClient(new SimpleHttpConnectionManager());

        if (location == null) {
            return byteArray;
        } else if (!location.startsWith(HTTP_WITH_SLASH) && !location.startsWith(HTTPS_WITH_SLASH)) {

            location = HTTP_WITH_SLASH + location;
        }

        HostConfiguration hostConfig = new HostConfiguration();

        hostConfig.setHost(new URI(location));

        if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) {
            hostConfig.setProxy(PROXY_HOST, PROXY_PORT);
        }

        client.setHostConfiguration(hostConfig);
        client.setConnectionTimeout(5000);
        client.setTimeout(5000);

        if (cookies != null && cookies.length > 0) {
            HttpState state = new HttpState();

            state.addCookies(cookies);
            state.setCookiePolicy(CookiePolicy.COMPATIBILITY);

            client.setState(state);
        }

        if (post) {
            method = new PostMethod(location);
        } else {
            method = new GetMethod(location);
        }

        method.setFollowRedirects(true);

        client.executeMethod(method);

        Header locationHeader = method.getResponseHeader("location");
        if (locationHeader != null) {
            return URLtoByteArray(locationHeader.getValue(), cookies, post);
        }

        InputStream is = method.getResponseBodyAsStream();

        if (is != null) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            byte[] bytes = new byte[512];

            for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) {

                buffer.write(bytes, 0, i);
            }

            byteArray = buffer.toByteArray();

            is.close();
            buffer.close();
        }

        return byteArray;
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Logger.error(Http.class, e.getMessage(), e);
        }
    }
}

From source file:com.mythesis.userbehaviouranalysis.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*ww  w .jav  a2s.  c o  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 = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        StringWriter writer = new StringWriter();
        IOUtils.copy(responseBodyAsStream, writer, "utf-8");
        response = writer.toString();

    } 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:com.fluidops.iwb.HTMLProvider.HTMLProvider.java

/**
 * Due to some complications with the new CKAN RDF integration, this method
 * will add triples generated from the JSON representation that are missing
 * in the RDF. Hopefully just a temporary solution - maybe the RDF will be
 * updated.//from   w  ww.j a va  2  s . c  o m
 * 
 * @throws RepositoryException
 */
private List<Statement> jsonFallBack(String host, HttpClient client, URI subject) throws RepositoryException {
    logger.debug("Executing JSON fallback for: " + host);
    HttpMethod method = new GetMethod(host);
    method.setFollowRedirects(true);

    List<Statement> res = new LinkedList<Statement>();
    try {
        int status = client.executeMethod(method);

        if (status == HttpStatus.OK_200) {
            InputStream response = method.getResponseBodyAsStream();
            String content = GenUtil.readUrl(response);
            JSONObject ob = (JSONObject) getJson(content);

            // Resources (Distributions)
            JSONArray resources = ob.getJSONArray("resources");

            for (int i = 0; i < resources.length(); i++) {
                JSONObject resource = (JSONObject) resources.get(i);

                // generate a unique timestamp
                long timestamp = System.currentTimeMillis() + i;

                // HINT:
                // the method ProviderUtils.objectAsUri() is a safe
                // replacement for
                // the ValueFactory.createUri(). It may, however, return
                // null. At
                // this position we're null safe, as subject is a valid URI
                // and
                // the timestamp does not break the URI
                URI distributionUri = ProviderUtils.objectAsUri(subject.toString() + "/" + timestamp);

                // HINT:
                // again, ProviderUtils.createStatement() is used to
                // generate statements
                // when all the three components are known
                res.add(ProviderUtils.createStatement(subject, Vocabulary.DCAT.HAS_DISTRIBUTION,
                        distributionUri));
                res.add(ProviderUtils.createStatement(distributionUri, RDF.TYPE, Vocabulary.DCAT.DISTRIBUTION));

                String accessURL = resource.getString("url");
                String format = resource.getString("format");

                // HINT:
                // the method ProviderUtils.createUriStatement() can be used
                // to create
                // statements with a URI in object position whenever
                // subject+predicate
                // (or only the predicate) are known
                if (!StringUtil.isNullOrEmpty(accessURL))
                    res.add(ProviderUtils.createUriStatement(distributionUri, Vocabulary.DCAT.ACCESSURL,
                            accessURL));

                // HINT:
                // the method ProviderUtils.createLiteralStatement() can be
                // used to create
                // statements with literal in object position whenever
                // subject+predicate
                // (or only the predicate) are known
                if (!StringUtil.isNullOrEmpty(format))
                    res.add(ProviderUtils.createLiteralStatement(distributionUri, Vocabulary.DCTERMS.FORMAT,
                            format));
            }

            // tags
            JSONArray tags = ob.getJSONArray("tags");
            for (int i = 0; i < tags.length(); i++) {
                String tag = tags.getString(i);

                if (!StringUtil.isNullOrEmpty(tag))
                    res.add(ProviderUtils.createLiteralStatement(subject, Vocabulary.DCAT.KEYWORD, tag));

                // HINT:
                // below, we use ProviderUtils.objectToURIInNamespace to
                // create a
                // URI in some target namespace; this method works for any
                // object based on
                // its toString() representation; it is null safe, assuming
                // the object's
                // string representation is neither empty nor null
                if (!(tag.startsWith("lod") || tag.contains("-") || tag.startsWith("rdf")))
                    res.add(ProviderUtils.createStatement(subject, Vocabulary.DCAT.THEME,
                            ProviderUtils.objectToURIInNamespace(Vocabulary.DCAT.NAMESPACE, tag)));
            }

            response.close();
        } else {
            logger.warn("Bad response from server, JSON fallback failed (status " + status + ", Url: " + host
                    + ")");
        }
    } catch (Exception e) {
        logger.warn(e.getMessage());
        res.clear(); // do not return partial result
        // ignore warning (affects only a single dataset)
    } finally {
        method.releaseConnection();
    }

    return res;
}

From source file:analysePortalU.AnalysePortalUData.java

public void analyse()
        throws HttpException, IOException, ParserConfigurationException, SAXException, TransformerException {

    Map<String, Map<String, String>> resultMap = new HashMap<String, Map<String, String>>();

    Scanner in = new Scanner(getClass().getClassLoader().getResourceAsStream("keywords.txt"));
    while (in.hasNextLine()) {
        String keyword = URLEncoder.encode(in.nextLine().trim(), "UTF-8");

        int currentPage = 1;
        boolean moreResults = true;

        while (moreResults) {

            String url = "http://www.portalu.de/opensearch/query?q=" + keyword.replace(' ', '+')
                    + "+datatype:metadata+ranking:score&h=" + PAGE_SIZE + "&detail=1&ingrid=1&p=" + currentPage;

            HttpClientParams httpClientParams = new HttpClientParams();
            HttpConnectionManager httpConnectionManager = new SimpleHttpConnectionManager();
            httpClientParams.setSoTimeout(60 * 1000);
            httpConnectionManager.getParams().setConnectionTimeout(60 * 1000);
            httpConnectionManager.getParams().setSoTimeout(60 * 1000);

            HttpClient client = new HttpClient(httpClientParams, httpConnectionManager);
            HttpMethod method = new GetMethod(url);

            // set a request header
            // this can change in the result of the response since it might
            // be
            // interpreted differently
            // method.addRequestHeader("Accept-Language", language);
            // //"de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");

            System.out.println("Query: " + url);
            int status = client.executeMethod(method);
            if (status == 200) {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(method.getResponseBodyAsStream());
                XPathUtils xpath = new XPathUtils(new ConfigurableNamespaceContext());
                NodeList results = xpath.getNodeList(doc, "/rss/channel/item");
                int numberOfResults = results.getLength();

                for (int i = 0; i < results.getLength(); i++) {
                    Node node = results.item(i);
                    String fileIdentifier = xpath.getString(node, ".//*/fileIdentifier/CharacterString");
                    if (!resultMap.containsKey(fileIdentifier)) {
                        resultMap.put(fileIdentifier, new HashMap<String, String>());
                    }/*from w  w w  .ja va  2s  . co  m*/
                    Map<String, String> currentMap = resultMap.get(fileIdentifier);
                    currentMap.put("uuid", fileIdentifier);
                    currentMap.put("partner", xpath.getString(node, "partner"));
                    currentMap.put("provider", xpath.getString(node, "provider"));
                    currentMap.put("udk-class", xpath.getString(node, "udk-class"));
                    currentMap.put("source", xpath.getString(node, "source"));
                    currentMap.put("url", new URL(xpath.getString(node, "link")).toString());
                    currentMap.put("title", xpath.getString(node, ".//*/title/CharacterString"));
                    currentMap.put("description", xpath.getString(node, ".//*/abstract/CharacterString"));
                    Node addressNode = xpath.getNode(node, ".//*/contact/idfResponsibleParty");
                    String addressString = "";
                    String tmp = xpath.getString(addressNode, "indiviualName/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += tmp + "\n";

                    tmp = xpath.getString(addressNode, "organisationName/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += tmp + "\n";

                    tmp = xpath.getString(addressNode,
                            "contactInfo/CI_Contact/address/CI_Address/deliveryPoint/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += tmp + "\n";

                    tmp = xpath.getString(addressNode,
                            "contactInfo/CI_Contact/address/CI_Address/postalCode/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += tmp + " ";
                    tmp = xpath.getString(addressNode,
                            "ontactInfo/CI_Contact/address/CI_Address/city/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += tmp + "\n";

                    tmp = xpath.getString(addressNode,
                            "contactInfo/CI_Contact/address/CI_Address/country/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += tmp + "\n";

                    tmp = xpath.getString(addressNode,
                            "contactInfo/CI_Contact/address/CI_Address/electronicMailAddress/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += "Email: " + tmp + "\n";

                    tmp = xpath.getString(addressNode,
                            "contactInfo/CI_Contact/phone/CI_Telephone/voice/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += "Tel: " + tmp + "\n";

                    tmp = xpath.getString(addressNode,
                            "contactInfo/CI_Contact/phone/CI_Telephone/facsimile/CharacterString");
                    if (tmp != null && tmp.length() > 0)
                        addressString += "Fax: " + tmp + "\n";

                    currentMap.put("pointOfContact", addressString);
                }
                if (numberOfResults > 0 && numberOfResults >= PAGE_SIZE) {
                    currentPage++;
                } else {
                    moreResults = false;
                }
            } else {
                moreResults = false;
            }
        }

    }

    StringWriter sw = new StringWriter();
    ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(sw);
    boolean fieldsWritten = false;
    for (String key : resultMap.keySet()) {
        Map<String, String> result = resultMap.get(key);
        if (!fieldsWritten) {
            for (String field : result.keySet()) {
                ecsvp.print(field);
            }
            ecsvp.println("");
            fieldsWritten = true;
        }
        for (String value : result.values()) {
            ecsvp.print(value);
        }
        ecsvp.println("");
    }

    PrintWriter out = new PrintWriter("result.csv");
    out.write(sw.toString());
    out.close();
    in.close();

    System.out.println("Done.");
}

From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxy.java

/**
 * @see AbstractController#handleRequestInternal(HttpServletRequest,
 *      HttpServletResponse)//w  w w.j  av a2 s.c o m
 *      @throws Exception .
 */
public final void handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {

    final URLResult r = urlRequestMapper.getProxiedURLFromRequest(request);
    if (r.hasResult()) {
        final HttpMethod method = buildRequest(request, r);
        InputStream is = null;
        try {
            // Entity enclosing requests cannot be redirected 
            // without user intervention according to RFC 2616
            if (!method.getName().equalsIgnoreCase("post") && !method.getName().equalsIgnoreCase("put")) {
                method.setFollowRedirects(followRedirects);
            }
            httpClient.executeMethod(method);
            updateResponseCode(request, response, method);
            proxyHeaders(response, method);
            addOtherHeaders(response, method);

            is = method.getResponseBodyAsStream();

            if (is != null) {
                if (contentTransformer.getContentType() != null) {
                    response.setContentType(contentTransformer.getContentType());
                }

                try {
                    String uri = request.getPathInfo();
                    // When using this component as a Filter,  the path info
                    // can be null, but the same information is available at
                    // getServletPath
                    if (uri == null) {
                        uri = request.getServletPath();
                    }

                    contentTransformer.transform(is, response.getOutputStream(),
                            new InmutableContentMetadata(uri, getContentType(method), method.getStatusCode()));
                } finally {
                    is.close();
                }
            }
        } catch (final ConnectException e) {
            onConnectionException(request, response, method, e);
        } finally {
            if (is != null) {
                is.close();
            }
            method.releaseConnection();
        }
    } else {
        onNoMapping(request, response);
    }
}

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

private HttpResponse execute(HttpMethod method) throws IOException {
    if (isOauthEnabled) {
        signWithOAuth(method);/*from w w w.jav a  2  s.co  m*/
    }

    applyHeadersCommonToAllRequests(method);

    byte[] responseBody;
    StatusLine statusLine = null;
    LOG.startTimer(TIMER_NAME);
    try {

        client.executeMethod(method);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        if (responseBodyAsStream != null) {
            IOUtils.copy(responseBodyAsStream, outputStream);
        }

        responseBody = outputStream.toByteArray();
        statusLine = method.getStatusLine();

        return new HttpResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase(), responseBody);

    } finally {
        method.releaseConnection();
        LOG.stopTimer(TIMER_NAME);
    }
}

From source file:fedora.client.test.PerformanceTests.java

/**
 * @return total time to run all iterations
 *//*w w w .  j a  v a  2 s.c o m*/
public long runGetDatastreamRestTest() throws Exception {
    long totalTime = 0;
    long startTime = 0;
    long stopTime = 0;
    runIngest(FOXML[0]);
    HttpMethod httpMethod = getHttpMethod(PIDS[0]);
    HttpClient client = getHttpClient();
    startTime = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        client.executeMethod(httpMethod);
        InputStream in = httpMethod.getResponseBodyAsStream();
        int input = in.read();
        while (input > 0) {
            input = in.read();
        }
    }
    stopTime = System.currentTimeMillis();
    totalTime = (stopTime - startTime);
    runPurgeObject(PIDS[0]);

    return totalTime;
}

From source file:application.draw.SaveButtonDebuggFrame.java

private void connectButtonActionPerformed(ActionEvent evt) {
    System.out.println("connectButton.actionPerformed, event=" + evt);
    //TODO add your code for connectButton.actionPerformed

    client = new HttpClient();
    String url = this.urlField.getText();
    this.messageTextArea.append(url + "\n");
    try {//from   w ww.  j  a v a 2s  . c o m
        HttpMethod method = new GetMethod(url);
        //          method.getParams().setContentCharset("UTF-8");
        int status = client.executeMethod(method);
        if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        } else {
            //          String txt=method.getResponseBodyAsString();
            InputStream is = method.getResponseBodyAsStream();
            InputStreamReader isr = new InputStreamReader(is, this.charset);
            //               InputStreamReader isr=new InputStreamReader(is,"UTF-8");
            //               InputStreamReader isr=new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = "";
            while (true) {
                line = br.readLine();
                if (line == null)
                    break;
                this.messageTextArea.append(line + "\n");
                //                 System.out.println(line);
            }
        }
    } catch (Exception e) {
        this.messageTextArea.append(e.toString() + "\n");
        e.printStackTrace();
    }

}