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:net.sourceforge.eclipsetrader.directaworld.Feed.java

private int update() {
    int i, requiredDelay = -1;
    String inputLine;//from w w w . j av a2  s.  c  om

    nf.setGroupingUsed(true);
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(0);

    pf.setGroupingUsed(true);
    pf.setMinimumFractionDigits(4);
    pf.setMaximumFractionDigits(4);

    try {
        // Legge la pagina contenente gli ultimi prezzi
        String host = "registrazioni.directaworld.it";
        StringBuffer url = new StringBuffer("http://" + host + "/cgi-bin/qta?idx=alfa&modo=t&appear=n"); //$NON-NLS-1$
        i = 0;
        for (Iterator iter = map.values().iterator(); iter.hasNext();)
            url.append("&id" + (++i) + "=" + (String) iter.next()); //$NON-NLS-1$ //$NON-NLS-2$
        for (; i < 30; i++)
            url.append("&id" + (i + 1) + "="); //$NON-NLS-1$ //$NON-NLS-2$
        url.append("&u=" + userName + "&p=" + password); //$NON-NLS-1$ //$NON-NLS-2$

        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        BundleContext context = DirectaWorldPlugin.getDefault().getBundle().getBundleContext();
        ServiceReference reference = context.getServiceReference(IProxyService.class.getName());
        if (reference != null) {
            IProxyService proxy = (IProxyService) context.getService(reference);
            IProxyData data = proxy.getProxyDataForHost(host, IProxyData.HTTP_PROXY_TYPE);
            if (data != null) {
                if (data.getHost() != null)
                    client.getHostConfiguration().setProxy(data.getHost(), data.getPort());
                if (data.isRequiresAuthentication())
                    client.getState().setProxyCredentials(AuthScope.ANY,
                            new UsernamePasswordCredentials(data.getUserId(), data.getPassword()));
            }
        }

        HttpMethod method = new GetMethod(url.toString());
        method.setFollowRedirects(true);
        client.executeMethod(method);

        BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        while ((inputLine = in.readLine()) != null) {
            if (inputLine.indexOf("<!--QT START HERE-->") != -1) //$NON-NLS-1$
            {
                while ((inputLine = in.readLine()) != null) {
                    if (inputLine.indexOf("<!--QT STOP HERE-->") != -1) //$NON-NLS-1$
                        break;
                    parseLine(inputLine);
                }
            } else if (inputLine.indexOf("Sara' possibile ricaricare la pagina tra") != -1) //$NON-NLS-1$
            {
                int beginIndex = inputLine.indexOf("tra ") + 4; //$NON-NLS-1$
                int endIndex = inputLine.indexOf("sec") - 1; //$NON-NLS-1$
                try {
                    requiredDelay = Integer.parseInt(inputLine.substring(beginIndex, endIndex)) + 1;
                } catch (Exception e) {
                    CorePlugin.logException(e);
                }
            }
        }
        in.close();
    } catch (Exception e) {
        CorePlugin.logException(e);
    }

    return requiredDelay;
}

From source file:fedora.test.integration.TestLargeDatastreams.java

private long exportAPIALite(String dsId) throws Exception {
    String url = apia.describeRepository().getRepositoryBaseURL() + "/get/" + pid + "/" + dsId;
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.setDoAuthentication(true);
    httpMethod.getParams().setParameter("Connection", "Keep-Alive");

    HttpClient client = fedoraClient.getHttpClient();
    client.executeMethod(httpMethod);/*  w w w.j av a2  s  .  c  o  m*/
    BufferedInputStream dataStream = new BufferedInputStream(httpMethod.getResponseBodyAsStream());

    long bytesRead = 0;
    while (dataStream.read() >= 0) {
        ++bytesRead;
    }

    return bytesRead;
}

From source file:ensen.controler.DBpediaLookupClient.java

public String getOneResource(String query, String classes, int numberOfRes) throws Exception {

    HttpClient client = new HttpClient();
    // client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
    String query2 = URLEncoder.encode(query, "utf-8");
    HttpMethod method = new GetMethod(
            gatway + "QueryString=" + query2 + "&QueryClass=" + classes + "&MaxHits=" + numberOfRes);
    // method.setFollowRedirects(true);
    try {/*from  w w w.  j  a  va2 s  .  c  o  m*/
        client.executeMethod(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.out.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.out.println("Unable to connect to lookup.dbpedia.org");
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    method.releaseConnection();

    String resource = "";
    int num_results = Results.size();
    if (num_results > 0) {
        for (DBpediaLookupResModel res : Results) {
            resource = URLDecoder.decode(res.uri);
            /*
             * if (res.uri.contains("http://")) { Resource O =
             * model.createResource(res.uri); Property P =
             * model.createProperty("http://ensen.org/data#has-a"); Literal
             * O1 = model.createLiteral(res.label + ""); Literal O2 =
             * model.createLiteral(res.desc + ""); Property P1 =
             * model.createProperty
             * ("http://www.w3.org/2000/01/rdf-schema#label"); Property P2 =
             * model.createProperty(
             * "http://www.w3.org/1999/02/22-rdf-syntax-ns#description");
             * O.addProperty(P1, O1); O.addProperty(P2, O2);
             * 
             * for (String cat : res.cats) { Resource OO =
             * model.createResource(cat); Property PP =
             * model.createProperty("http://purl.org/dc/terms/subject");
             * O.addProperty(PP, OO); } for (String c : res.classes) {
             * Resource OO = model.createResource(c); Property PP =
             * model.createProperty
             * ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
             * O.addProperty(PP, OO); } core.addProperty(P, O);
             * 
             * }
             */
        }
    }

    return resource;
}

From source file:ensen.controler.DBpediaLookupClient.java

public Model qetEntities(String query, String classes, int numberOfRes) throws Exception {

    HttpClient client = new HttpClient();
    // client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
    String query2 = URLEncoder.encode(query, "utf-8");
    HttpMethod method = new GetMethod(
            gatway + "QueryString=" + query2 + "&QueryClass=" + classes + "&MaxHits=" + numberOfRes);
    // method.setFollowRedirects(true);
    try {//  w w w  .  j  av  a2s  . co m
        client.executeMethod(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.out.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.out.println("Unable to connect to lookup.dbpedia.org");
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    method.releaseConnection();

    Model model = ModelFactory.createDefaultModel();
    Resource core = model.createResource("http://ensen.org/data#q-" + query2);

    int num_results = Results.size();
    if (num_results > 0) {
        for (DBpediaLookupResModel res : Results) {
            // System.out.println("URI: " + res.uri);
            if (res.uri.contains("http://")) {
                Resource O = model.createResource(res.uri);
                Property P = model.createProperty("http://ensen.org/data#has-a");
                Literal O1 = model.createLiteral(res.label + "");
                Literal O2 = model.createLiteral(res.desc + "");
                Property P1 = model.createProperty("http://www.w3.org/2000/01/rdf-schema#label");
                Property P2 = model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#description");
                O.addProperty(P1, O1);
                O.addProperty(P2, O2);

                for (String cat : res.cats) {
                    Resource OO = model.createResource(cat);
                    Property PP = model.createProperty("http://purl.org/dc/terms/subject");
                    O.addProperty(PP, OO);
                }
                for (String c : res.classes) {
                    Resource OO = model.createResource(c);
                    Property PP = model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
                    O.addProperty(PP, OO);
                }
                core.addProperty(P, O);
            }
        }
    }

    return model;
}

From source file:com.zimbra.cs.dav.client.CalDavClient.java

private String getCurrentUserPrincipal() {
    DavRequest propfind = DavRequest.PROPFIND("/.well-known/caldav");
    propfind.addRequestProp(DavElements.E_CURRENT_USER_PRINCIPAL);
    HttpMethod m = null;
    try {//from  w  w  w.ja v a  2 s  .co m
        m = executeFollowRedirect(propfind);
        int status = m.getStatusCode();
        if (status >= 400) {
            return null;
        }
        Document doc = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(m.getResponseBodyAsStream());
        Element top = doc.getRootElement();
        for (Object obj : top.elements(DavElements.E_RESPONSE)) {
            if (obj instanceof Element) {
                DavObject davObject = new DavObject((Element) obj);
                Element e = davObject.getProperty(DavElements.E_CURRENT_USER_PRINCIPAL);
                if (e != null) {
                    return e.getStringValue().trim();
                }
            }
        }
    } catch (Exception e) {
        ZimbraLog.dav.debug("Exception thrown getting Current User Principal", e);
        return null;
    } finally {
        if (m != null) {
            m.releaseConnection();
        }
    }
    return null;
}

From source file:be.ibridge.kettle.trans.step.http.HTTP.java

private Value callHttpService(Row row) throws KettleException {
    String url = determineUrl(row);
    try {//from   w ww.  j ava  2s . c  o  m
        logDetailed("Connecting to : [" + url + "]");

        // Prepare HTTP get
        // 
        HttpClient httpclient = new HttpClient();
        HttpMethod method = new GetMethod(url);

        // Execute request
        // 
        try {
            int result = httpclient.executeMethod(method);

            // The status code
            log.logDebug(toString(), "Response status code: " + result);

            // the response
            InputStream inputStream = method.getResponseBodyAsStream();
            StringBuffer bodyBuffer = new StringBuffer();
            int c;
            while ((c = inputStream.read()) != -1)
                bodyBuffer.append((char) c);
            inputStream.close();

            String body = bodyBuffer.toString();
            log.logDebug(toString(), "Response body: " + body);

            return new Value(meta.getFieldName(), body);
        } finally {
            // Release current connection to the connection pool once you are done
            method.releaseConnection();
        }
    } catch (Exception e) {
        throw new KettleException("Unable to get result from specified URL :" + url, e);
    }
}

From source file:mitm.common.security.ca.handlers.comodo.ApplyCustomClientCert.java

private void handleResponse(int statusCode, HttpMethod httpMethod) throws IOException {
    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("Error applying for Custom Client Cert. Message: " + httpMethod.getStatusLine());
    }//from   www  .  j a  v a  2 s. c  om

    InputStream input = httpMethod.getResponseBodyAsStream();

    if (input == null) {
        throw new IOException("Response body is null.");
    }

    /*
     * we want to set a max on the number of bytes to download. We do not want a rogue server to return 1GB.
     */
    InputStream limitInput = new SizeLimitedInputStream(input, MAX_HTTP_RESPONSE_SIZE);

    String response = IOUtils.toString(limitInput, CharEncoding.US_ASCII);

    if (logger.isDebugEnabled()) {
        logger.debug("Response:\r\n" + response);
    }

    Map<String, String[]> parameters = NetUtils.parseQuery(response);

    String errorCodeParam = getValue(parameters, "errorCode");

    if (!StringUtils.isEmpty(errorCodeParam)) {
        errorCode = CustomClientStatusCode.fromCode(errorCodeParam);

        error = true;

        errorMessage = getValue(parameters, "errorMessage");
    } else {
        error = false;

        orderNumber = getValue(parameters, "orderNumber");
        collectionCode = getValue(parameters, "collectionCode");

        if (StringUtils.isEmpty(orderNumber)) {
            throw new IOException(new CustomClientCertException("orderNumber is missing."));
        }
    }
}

From source file:com.springsource.insight.plugin.apache.http.hc3.HttpClientExecutionCollectionAspectTest.java

private Operation handleResponse(String testName, String uri, HttpMethod method, int response,
        boolean checkHeaders) throws IOException {
    InputStream body = method.getResponseBodyAsStream();
    try {/*from w  w  w . java  2  s.co m*/
        String content = IOUtils.toString(body);
        assertEquals("Mismatched content", createResponseContent(testName), content);
    } finally {
        body.close();
    }

    return assertExecutionResult(uri, method, response, checkHeaders);
}

From source file:com.lazerycode.ebselen.customhandlers.FileDownloader.java

public String downloader(WebElement element, String attribute) throws Exception {
    //Assuming that getAttribute does some magic to return a fully qualified URL
    String downloadLocation = element.getAttribute(attribute);
    if (downloadLocation.trim().equals("")) {
        throw new Exception("The element you have specified does not link to anything!");
    }//from   w w w  . ja  va 2  s . co  m
    URL downloadURL = new URL(downloadLocation);
    HttpClient client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
    client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort()));
    client.setState(mimicCookieState(driver.manage().getCookies()));
    HttpMethod getRequest = new GetMethod(downloadURL.getPath());
    FileHandler downloadedFile = new FileHandler(
            downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true);
    try {
        int status = client.executeMethod(getRequest);
        LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm());
        BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream());
        int offset = 0;
        int len = 4096;
        int bytes = 0;
        byte[] block = new byte[len];
        while ((bytes = in.read(block, offset, len)) > -1) {
            downloadedFile.getWritableFileOutputStream().write(block, 0, bytes);
        }
        downloadedFile.close();
        in.close();
        LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile());
    } catch (Exception Ex) {
        LOGGER.error("Download failed: {}", Ex);
        throw new Exception("Download failed!");
    } finally {
        getRequest.releaseConnection();
    }
    return downloadedFile.getAbsoluteFile();
}

From source file:com.cloud.test.stress.StressTestDirectAttach.java

private static int executeEventsAndBilling(String server, String developerServer)
        throws HttpException, IOException {
    // test steps:
    // - get all the events in the system for all users in the system
    // - generate all the usage records in the system
    // - get all the usage records in the system

    // -----------------------------
    // GET EVENTS
    // -----------------------------
    String url = server + "?command=listEvents&page=1&account=" + _account.get();

    s_logger.info("Getting events for the account " + _account.get());
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    s_logger.info("get events response code: " + responseCode);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, List<String>> eventDescriptions = getMultipleValuesFromXML(is,
                new String[] { "description" });
        List<String> descriptionText = eventDescriptions.get("description");
        if (descriptionText == null) {
            s_logger.info("no events retrieved...");
        } else {//  ww w  .  ja  v a  2 s . co m
            for (String text : descriptionText) {
                s_logger.info("event: " + text);
            }
        }
    } else {
        s_logger.error(
                "list events failed with error code: " + responseCode + ". Following URL was sent: " + url);

        return responseCode;
    }
    return responseCode;
}