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.pentaho.platform.dataaccess.client.ConnectionServiceClient.java

/**
 * Submits an HTTP result with the provided HTTPMethod and returns a dom4j document of the
 * response/*  w  ww . ja v  a 2s .c o m*/
 * @param callMethod
 * @return
 * @throws ConnectionServiceException
 */
protected Document getResultDocument(HttpMethod callMethod) throws ConnectionServiceException {

    try {
        HttpClient client = getClient();
        // execute the HTTP call
        int status = client.executeMethod(callMethod);
        if (status != HttpStatus.SC_OK) {
            throw new ConnectionServiceException("Web service call failed with code " + status); //$NON-NLS-1$
        }
        // get the result as a string
        InputStream in = callMethod.getResponseBodyAsStream();
        byte buffer[] = new byte[2048];
        int n = in.read(buffer);
        StringBuilder sb = new StringBuilder();
        while (n != -1) {
            sb.append(new String(buffer, 0, n));
            n = in.read(buffer);
        }
        String result = sb.toString();
        // convert to XML
        return DocumentHelper.parseText(result);
    } catch (IOException e) {
        throw new ConnectionServiceException(e);
    } catch (DocumentException e) {
        throw new ConnectionServiceException(e);
    }

}

From source file:org.pentaho.platform.util.client.ClientUtil.java

/**
 * Submits an HTTP result with the provided HTTPMethod and returns a dom4j document of the response
 * /*from w ww  .java2 s.  c  om*/
 * @param callMethod
 * @return
 * @throws ConnectionServiceException
 */
public static org.dom4j.Document getResultDom4jDocument(HttpClient client, HttpMethod callMethod)
        throws ServiceException {

    try {
        // execute the HTTP call
        int status = client.executeMethod(callMethod);
        if (status != HttpStatus.SC_OK) {
            throw new ServiceException("Web service call failed with code " + status); //$NON-NLS-1$
        }
        // get the result as a string
        InputStream in = callMethod.getResponseBodyAsStream();
        byte[] buffer = new byte[2048];
        int n = in.read(buffer);
        StringBuilder sb = new StringBuilder();
        while (n != -1) {
            sb.append(new String(buffer, 0, n));
            n = in.read(buffer);
        }
        String result = sb.toString();
        // convert to XML
        return DocumentHelper.parseText(result);
    } catch (IOException e) {
        throw new ServiceException(e);
    } catch (DocumentException e) {
        throw new ServiceException(e);
    }

}

From source file:org.proteomecommons.io.gpmdb.GPMDBPeptideReader.java

/**
 * A helper to parse one page of the gpmdb and update the internal peptides and possible links lists.
 * @param url the location to parse/*  ww  w.j a  v  a  2  s. c  o  m*/
 */
private void parse(String url) {
    //create a singular HttpClient object
    HttpClient client = new HttpClient();
    client.getHostConfiguration().setHost("gpmdb-us.thegpm.org");

    //establish a connection within 25 seconds
    client.getHttpConnectionManager().getParams().setConnectionTimeout(25000);

    //create a method object
    HttpMethod method = new GetMethod(url);
    method.setFollowRedirects(true);

    //System.out.println("Getting " + url);

    //execute the method
    try {
        client.executeMethod(method);
        InputStream responseBody = method.getResponseBodyAsStream();
        if (responseBody != null) {
            //if(!url.endsWith("xml")){
            //kludge through the html

            //go through the html and look for various elements like peptide sequences, or links to more about this hit
            BufferedReader html = new BufferedReader(new InputStreamReader(responseBody));

            String line = html.readLine();

            //some parsing state
            String proteinModel = "";
            boolean inModel = false;
            String proteinID = null;
            int tableStringCount = 0;
            //generic loop to process any html that we might see on the gpmdb.. from any of the page generating scripts.
            while (line != null) {
                //check the line for links to "GPM - protein model:" pages... but don't look for more links if we're already at a protein.pl
                //for example, if they clicked on an accession link
                if (line.indexOf("protein.pl") != -1 && url.indexOf("protein.pl") == -1) {
                    //System.out.println("Contains a link to a protein xml doc: " + line);
                    //split on links
                    String[] splitLinks = line.split("href=\"*\"");
                    //look over all of the split results to find good links
                    for (int index = 0; index < splitLinks.length; index++) {
                        //System.out.println("possible " + index + " " + splitLinks[index]);
                        if (splitLinks[index].indexOf("archive") != -1
                                && splitLinks[index].indexOf("protein.pl") != -1) {
                            splitLinks[index] = splitLinks[index].substring(0,
                                    splitLinks[index].indexOf("\">"));
                            if (DEBUG)
                                System.out.println("Found more peptide model pages.. Adding " + index + " "
                                        + splitLinks[index]);
                            possibleLinks.add(splitLinks[index]);
                        }
                    }
                }

                //check for links to "Search Results for:" if we've done a keyword search
                if (url.indexOf("dblist_keyword.pl") != -1 && line.indexOf("dblist_label.pl") != -1) {
                    //                        System.out.println("Expandling links by accessions");

                    String[] splitLinks = line.split("href=\"*\"");
                    //look over all of the split results to find good links
                    for (int index = 0; index < splitLinks.length; index++) {
                        //System.out.println("possible " + index + " " + splitLinks[index]);
                        if (splitLinks[index].indexOf("dblist_label.pl") != -1) {
                            splitLinks[index] = splitLinks[index].substring(0,
                                    splitLinks[index].indexOf("\">"));
                            //                                System.out.println("Found more protein search result pages.. Adding " + index + " " + splitLinks[index]);
                            possibleLinks.add(splitLinks[index]);
                        }
                    }
                }

                //look for the actual pepetides and model sequence if we're on a protein model page
                if (url.indexOf("protein.pl") != -1) {
                    //                        System.out.println("Handling Ling: "+line);

                    // pull out protein id if appropriate
                    if (line.indexOf("<BR>protein model: ") != -1) {
                        String[] parts = line.split("<BR>protein model: |<BR><BR>");
                        System.out.println("Setting ID: " + parts[1]);
                        proteinID = parts[1];
                    }

                    // fix me!
                    if (line.indexOf("<table") != -1) {
                        tableStringCount++;
                    }

                    //look for the protein model... and parse it
                    if (tableStringCount == 5 && line.indexOf("</table>") == -1) {
                        inModel = true;

                        //grab all of the AminoAcid chars on these lines
                        String chars = removeAllTags(line);
                        //                            System.out.println("Adding: "+filterAminoAcidChars(chars));
                        proteinModel = proteinModel + filterAminoAcidChars(chars);

                    }

                    if ((tableStringCount > 4 && line.indexOf("</table>") != -1)) {
                        //                            System.out.println("Flagging out of model.");
                        inModel = false;
                        tableStringCount++;
                        possibleProteins.add(proteinModel);
                        //                            System.out.println("Added protein model " + proteinModel);
                        proteinModel = "";
                    }

                    //                        //look for the protein model... and parse it
                    //                        if(line.indexOf("<pre>") != -1 || tableStringCount == 5){
                    //                            inModel = true;
                    //                        } else if(line.indexOf("</pre>") != -1 || (tableStringCount==5 && line.indexOf("</table>")!=-1)){
                    //                            inModel = false;
                    //                            tableStringCount++;
                    //                            possibleProteins.add(proteinModel);
                    //                            if(DEBUG) System.out.println("Added protein model " + proteinModel);
                    //                            proteinModel = "";
                    //                        } else if (inModel){
                    //                            //grab all of the AminoAcid chars on these lines
                    //                            String chars = removeAllTags(line);
                    //
                    //                            proteinModel = proteinModel + filterAminoAcidChars(chars);
                    //
                    //                        } else
                    if (line.indexOf("spectrum") != -1 && line.indexOf("sequence") != -1) {
                        //found the table with peptide sequences... parse the peptides out of it
                        String[] rows = line.split("<tr>");

                        //process rows and columns in the table.  There are peptide sequences in there.
                        for (int row = 0; row < rows.length; row++) {
                            String[] columns = rows[row].split("</td>");
                            for (int column = 0; column < columns.length; column++) {
                                String possiblePep = removeAllTags(columns[column]);
                                possiblePep = filterAminoAcidChars(possiblePep);
                                //System.out.println(possiblePep);

                                //give all detagged and filtered strings that are above a specified size a chance at becoming a peptide.
                                if (possiblePep.length() > 4 && columns[column].indexOf("nbsp") == -1) {
                                    try {
                                        //convert the possible pep into a real live peptide sequence
                                        String pepSeq = validatePeptideSequence(possiblePep);
                                        peptides.add(new GPMDBPeptide(pepSeq, proteinID));
                                    } catch (Exception e) {
                                        System.err.println(
                                                "Tried to make a peptide from " + possiblePep + " but failed");
                                        System.err.println("The possible string was " + columns[column]);
                                    }
                                }
                            }
                        }
                    }
                }

                //and look for more
                line = html.readLine();
            }
            //   } else {
            /*
            //setup an xml reader to parse the document
                     
            try {
                    XMLInputFactory factory = XMLInputFactory.newInstance();
                    parser = factory.createXMLStreamReader(responseBody);
                     
                    //state for parsing the document
                    boolean inPeptide = false; //in a 'peptide' tag?
                     
                    //walk through the document, and buffer some peptides
                    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()){
                            if(event == XMLStreamConstants.START_ELEMENT && parser.getLocalName().equals("domain")){
                                    String pepSeq = parser.getAttributeValue(null, "seq");
                                    //TODO do mods too
                                    try{
                                            peptides.add(new Peptide(pepSeq));
                                    } catch (Exception e){
                                            System.out.println("couldn't get a pepseq from the xml...");
                                    }
                            } else if (event == XMLStreamConstants.CHARACTERS && inPeptide){
                                    //System.out.println("Found characters in the doc in  peptide tag");
                                    char[] chars = parser.getTextCharacters();
                                    String fixed = "";
                                    for(int character = 0; character < chars.length; character++){
                                            if(chars[character] == 'A' ||
                                                            chars[character] == 'R' ||
                                                            chars[character] == 'N' ||
                                                            chars[character] == 'D' ||
                                                            chars[character] == 'C' ||
                                                            chars[character] == 'E' ||
                                                            chars[character] == 'Q' ||
                                                            chars[character] == 'G' ||
                                                            chars[character] == 'H' ||
                                                            chars[character] == 'I' ||
                                                            chars[character] == 'L' ||
                                                            chars[character] == 'K' ||
                                                            chars[character] == 'M' ||
                                                            chars[character] == 'F' ||
                                                            chars[character] == 'P' ||
                                                            chars[character] == 'S' ||
                                                            chars[character] == 'T' ||
                                                            chars[character] == 'W' ||
                                                            chars[character] == 'Y' ||
                                                            chars[character] == 'V'){
                                                    fixed = fixed + chars[character];
                                            }
                                    }
                                    if(!possibleProteins.contains(fixed)){
                                            possibleProteins.add(fixed);
                                    }
                                    //System.out.println(fixed);
                            } else if (event == XMLStreamConstants.START_ELEMENT && parser.getLocalName().equals("peptide")){
                                    inPeptide = true;
                            } else if(event == XMLStreamConstants.END_ELEMENT && parser.getLocalName().equals("peptide")){
                                    inPeptide = false;
                            }
                    }
            } catch (XMLStreamException ex) {
                    System.out.println(ex);
            }
             */
            //   }
        }
    } catch (HttpException he) {
        System.err.println("Could not connect to '" + url + "'");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to the GPMDB.");
    }

    //clean up the connection resources
    method.releaseConnection();
}

From source file:org.sakaiproject.entitybroker.util.http.HttpRESTUtils.java

/**
 * Fire off a request to a URL using the specified method but reuse the client for efficiency,
 * include optional params and data in the request,
 * the response data will be returned in the object if the request can be carried out
 * /*from  ww  w  .  jav  a2 s .com*/
 * @param httpClientWrapper (optional) allows the http client to be reused for efficiency,
 * if null a new one will be created each time, use {@link #makeReusableHttpClient(boolean, int)} to
 * create a reusable instance
 * @param URL the url to send the request to (absolute or relative, can include query params)
 * @param method the method to use (e.g. GET, POST, etc.)
 * @param params (optional) params to send along with the request, will be encoded in the query string or in the body depending on the method
 * @param params (optional) headers to send along with the request, will be encoded in the headers
 * @param data (optional) data to send along in the body of the request, this only works for POST and PUT requests, ignored for the other types
 * @param guaranteeSSL if this is true then the request is sent in a mode which will allow self signed certs to work,
 * otherwise https requests will fail if the certs cannot be centrally verified
 * @return an object representing the response, includes data about the response
 * @throws HttpRequestException if the request cannot be processed for some reason (this is unrecoverable)
 */
@SuppressWarnings("deprecation")
public static HttpResponse fireRequest(HttpClientWrapper httpClientWrapper, String URL, Method method,
        Map<String, String> params, Map<String, String> headers, Object data, boolean guaranteeSSL) {
    if (guaranteeSSL) {
        // added this to attempt to force the SSL self signed certs to work
        Protocol myhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
        Protocol.registerProtocol("https", myhttps);
    }

    if (httpClientWrapper == null || httpClientWrapper.getHttpClient() == null) {
        httpClientWrapper = makeReusableHttpClient(false, 0, null);
    }

    HttpMethod httpMethod = null;
    if (method.equals(Method.GET)) {
        GetMethod gm = new GetMethod(URL);
        // put params into query string
        gm.setQueryString(mergeQueryStringWithParams(gm.getQueryString(), params));
        // warn about data being set
        if (data != null) {
            System.out.println(
                    "WARN: data cannot be passed in GET requests, data will be ignored (org.sakaiproject.entitybroker.util.http.HttpUtils#fireRequest)");
        }
        gm.setFollowRedirects(true);
        httpMethod = gm;
    } else if (method.equals(Method.POST)) {
        PostMethod pm = new PostMethod(URL);
        // special handling for post params
        if (params != null) {
            for (Entry<String, String> entry : params.entrySet()) {
                if (entry.getKey() == null || entry.getValue() == null) {
                    System.out.println("WARN: null value supplied for param name (" + entry.getKey()
                            + ") or value (" + entry.getValue()
                            + ") (org.sakaiproject.entitybroker.util.http.HttpUtils#fireRequest)");
                }
                pm.addParameter(entry.getKey(), entry.getValue());
            }
        }
        // handle data
        handleRequestData(pm, data);
        httpMethod = pm;
    } else if (method.equals(Method.PUT)) {
        PutMethod pm = new PutMethod(URL);
        // put params into query string
        pm.setQueryString(mergeQueryStringWithParams(pm.getQueryString(), params));
        // handle data
        handleRequestData(pm, data);
        httpMethod = pm;
    } else if (method.equals(Method.DELETE)) {
        DeleteMethod dm = new DeleteMethod(URL);
        // put params into query string
        dm.setQueryString(mergeQueryStringWithParams(dm.getQueryString(), params));
        // warn about data being set
        if (data != null) {
            System.out.println(
                    "WARN: data cannot be passed in DELETE requests, data will be ignored (org.sakaiproject.entitybroker.util.http.HttpUtils#fireRequest)");
        }
        httpMethod = dm;
    } else {
        throw new IllegalArgumentException("Cannot handle method: " + method);
    }
    // set the headers for the request
    if (headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            httpMethod.addRequestHeader(entry.getKey(), entry.getValue());
        }
    }

    HttpResponse response = null;
    try {
        int responseCode = httpClientWrapper.getHttpClient().executeMethod(httpMethod);
        response = new HttpResponse(responseCode);

        // Avoid DOS because of large responses using up all memory in the system - https://jira.sakaiproject.org/browse/SAK-20405
        InputStream is = httpMethod.getResponseBodyAsStream();
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = is.read(b)) != -1;) {
            out.append(new String(b, 0, n));
            if (out.length() > MAX_RESPONSE_SIZE_CHARS) {
                // die if the response exceeds the maximum chars allowed
                throw new HttpRequestException("Response size (" + out.length() + " chars) from url (" + URL
                        + ") exceeded the maximum allowed batch response size (" + MAX_RESPONSE_SIZE_CHARS
                        + " chars) while processing the response");
            }
        }
        String body = out.toString();

        //String body = httpMethod.getResponseBodyAsString();
        //         byte[] responseBody = httpMethod.getResponseBody();
        //         if (responseBody != null) {
        //            body = new String(responseBody, "UTF-8");
        //         }
        response.setResponseBody(body);
        response.setResponseMessage(httpMethod.getStatusText());
        // now get the headers
        HashMap<String, String[]> responseHeaders = new HashMap<String, String[]>();
        Header[] respHeaders = httpMethod.getResponseHeaders();
        for (int i = 0; i < respHeaders.length; i++) {
            Header header = respHeaders[i];
            // now we convert the headers from these odd pairs into something more like servlets expect
            HeaderElement[] elements = header.getElements();
            if (elements == null || elements.length == 0) {
                continue;
            } else if (elements.length >= 1) {
                String[] values = new String[elements.length];
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < elements.length; j++) {
                    sb.setLength(0); // clear the StringBuilder
                    sb.append(elements[j].getName());
                    if (elements[j].getValue() != null) {
                        sb.append("=");
                        sb.append(elements[j].getValue());
                    }
                    values[j] = sb.toString();
                }
                responseHeaders.put(header.getName(), values);
            }
        }
        response.setResponseHeaders(responseHeaders);
    } catch (HttpException he) {
        // error contained in he.getMessage()
        throw new HttpRequestException(
                "Fatal HTTP Request Error: " + "Could not sucessfully fire request to url (" + URL
                        + ") using method (" + method + ")  :: " + he.getMessage(),
                he);
    } catch (IOException ioe) {
        // other exception
        throw new HttpIOException(
                "IOException (transport/connection) Error: " + "Could not sucessfully fire request to url ("
                        + URL + ") using method (" + method + ")  :: " + ioe.getMessage(),
                ioe);
    } finally {
        httpMethod.releaseConnection();
    }
    return response;
}

From source file:org.sakaiproject.nakamura.grouper.changelog.util.NakamuraHttpUtils.java

/**
 * Prepare an HTTP request to Sakai OAE and parse the response (if JSON).
 * @param client an {@link HttpClient} to execute the request.
 * @param method an HTTP method to send//from w  w w  . j  av a2  s.  com
 * @return a JSONObject of the response if the request returns JSON
 * @throws GroupModificationException if there was an error updating the group information.
 */
public static JSONObject http(HttpClient client, HttpMethod method) throws GroupModificationException {

    method.setRequestHeader("User-Agent", HTTP_USER_AGENT);
    method.setRequestHeader("Referer", HTTP_REFERER);

    String errorMessage = null;
    String responseString = null;
    JSONObject responseJSON = null;

    boolean isJSONRequest = !method.getPath().toString().endsWith(".html");

    if (log.isDebugEnabled() && method instanceof PostMethod) {
        log.debug(method.getName() + " " + method.getPath() + " params:");
        for (NameValuePair nvp : ((PostMethod) method).getParameters()) {
            log.debug(nvp.getName() + " = " + nvp.getValue());
        }
    }

    int responseCode = -1;
    try {
        responseCode = client.executeMethod(method);
        responseString = StringUtils.trimToNull(IOUtils.toString(method.getResponseBodyAsStream()));

        if (isJSONRequest) {
            responseJSON = parseJSONResponse(responseString);
        }

        if (log.isDebugEnabled()) {
            log.debug(responseCode + " " + method.getName() + " " + method.getPath());
        }
        if (log.isTraceEnabled()) {
            log.trace("reponse: " + responseString);
        }

        switch (responseCode) {

        case HttpStatus.SC_OK: // 200
        case HttpStatus.SC_CREATED: // 201
            break;
        case HttpStatus.SC_BAD_REQUEST: // 400
        case HttpStatus.SC_UNAUTHORIZED: // 401
        case HttpStatus.SC_NOT_FOUND: // 404
        case HttpStatus.SC_INTERNAL_SERVER_ERROR: // 500
            if (isJSONRequest && responseJSON != null) {
                errorMessage = responseJSON.getString("status.message");
            }
            if (errorMessage == null) {
                errorMessage = "Empty " + responseCode + " error. Check the logs on the Sakai OAE server.";
            }
            break;
        default:
            errorMessage = "Unknown HTTP response " + responseCode;
            break;
        }
    } catch (Exception e) {
        errorMessage = "An exception occurred communicatingSakai OAE. " + e.toString();
    } finally {
        method.releaseConnection();
    }

    if (errorMessage != null) {
        log.error(errorMessage);
        errorToException(responseCode, errorMessage);
    }
    return responseJSON;
}

From source file:org.siberia.image.searcher.impl.GoogleImageSearcher.java

/** search */
public void search() {

    //        http://images.google.fr/images?imgsz=xxlarge&gbv=2&hl=fr&q=b+e&btnG=Recherche+d%27images
    //        http://images.google.fr/images?imgsz=xxlarge&hl=fr&q=b+e

    Runnable run = new Runnable() {
        public void run() {
            fireSearchHasBegan(new ImageSearcherEvent(GoogleImageSearcher.this));

            StringBuffer buffer = new StringBuffer(50);

            if (getCriterions() != null) {
                boolean oneTokenAlreadyApplied = false;

                for (int i = 0; i < getCriterions().length; i++) {
                    String current = getCriterions()[i];

                    if (current != null) {
                        if (oneTokenAlreadyApplied) {
                            buffer.append("+");
                        }//  w  w w  . j av a 2  s. c  o  m

                        buffer.append(current);

                        oneTokenAlreadyApplied = true;
                    }
                }
            }

            Locale locale = getLocale();
            if (locale == null) {
                locale = Locale.getDefault();
            }

            if (logger.isDebugEnabled()) {
                logger.debug("uri : " + buffer.toString());
            }

            HttpClient client = new HttpClient();

            HttpMethod method = new GetMethod(GOOGLE_URL);

            NameValuePair[] pairs = new NameValuePair[3];
            pairs[0] = new NameValuePair("imgsz", convertImageSizeCriterion(getImageSize()));
            pairs[1] = new NameValuePair("hl", locale.getCountry().toLowerCase());
            pairs[2] = new NameValuePair("q", buffer.toString());
            method.setQueryString(pairs);

            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(3, false));

            InputStream stream = null;

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

                if (statusCode == HttpStatus.SC_OK) {
                    /** on recherche  partir des motifs suivants
                     *  la premire occurrence de http://  partir de l, on prend jusqu'au prochaine espace ou '>' :
                     *
                     *  exemple :
                     *      <img src=http://tbn0.google.com/images?q=tbn:GIJo-j_dSy4FiM:http://www.discogs.com/image/R-378796-1136999170.jpeg width=135 height=135>
                     *
                     *  on trouve le motif, puis, on prend  partir de http://www.discogs jusqu'au prochain espace...
                     *
                     *  --> http://www.discogs.com/image/R-378796-1136999170.jpeg
                     */
                    String searchMotif = "<img src=http://tbn0.google.com/images?q";
                    String urlMotif = "http://";

                    int indexInSearchMotif = -1;
                    int indexInUrlMotif = -1;
                    boolean motifFound = false;
                    boolean foundUrl = false;

                    StringBuffer urlBuffer = new StringBuffer(50);

                    // Read the response body.
                    byte[] bytes = new byte[1024 * 8];
                    stream = method.getResponseBodyAsStream();

                    if (stream != null) {
                        int read = -1;

                        int linksRetrieved = 0;

                        while ((read = stream.read(bytes)) != -1) {
                            for (int i = 0; i < read; i++) {
                                byte currentByte = bytes[i];

                                if (motifFound) {
                                    if (foundUrl) {
                                        if (currentByte == ' ' || currentByte == '>') {
                                            /* add current url to list of result */
                                            try {
                                                URL url = new URL(urlBuffer.toString());

                                                fireImageFound(new ImageFoundEvent(GoogleImageSearcher.this,
                                                        url, linksRetrieved));
                                                linksRetrieved++;

                                                if (linksRetrieved >= getMaximumLinksRetrieved()) {
                                                    break;
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            } finally {
                                                urlBuffer.delete(0, urlBuffer.length());

                                                foundUrl = false;
                                                motifFound = false;
                                            }
                                        } else {
                                            /* add current byte to url buffer */
                                            urlBuffer.append((char) currentByte);
                                        }
                                    } else {
                                        if (indexInUrlMotif == urlMotif.length() - 1) {
                                            urlBuffer.append(urlMotif);
                                            urlBuffer.append((char) currentByte);
                                            foundUrl = true;
                                            indexInUrlMotif = -1;
                                        }

                                        /* if the current byte is the same as that attempted on the url motif let's continue */
                                        if (((char) currentByte) == urlMotif.charAt(indexInUrlMotif + 1)) {
                                            indexInUrlMotif++;
                                        } else {
                                            indexInUrlMotif = -1;
                                        }
                                    }
                                } else {
                                    if (indexInSearchMotif == searchMotif.length() - 1) {
                                        motifFound = true;
                                        indexInSearchMotif = -1;
                                    }

                                    if (((char) currentByte) == searchMotif.charAt(indexInSearchMotif + 1)) {
                                        indexInSearchMotif++;
                                    } else {
                                        indexInSearchMotif = -1;
                                    }
                                }
                            }
                            if (linksRetrieved >= getMaximumLinksRetrieved()) {
                                break;
                            }
                        }
                    }
                } else {
                    System.err.println("Method failed: " + method.getStatusLine());
                }
            } catch (HttpException e) {
                System.err.println("Fatal protocol violation: " + e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException ex) {
                        System.err.println("Fatal transport error: " + ex.getMessage());
                    }
                }
                System.err.println("Fatal transport error: " + e.getMessage());
                e.printStackTrace();
            } finally { // Release the connection.
                method.releaseConnection();

                fireSearchFinished(new ImageSearcherEvent(GoogleImageSearcher.this));
            }
        }
    };

    this.service.submit(run);
}

From source file:org.soa4all.dashboard.gwt.module.wsmolite.server.WsmoLiteDataServiceImpl.java

@Override
public String proxifyRequest(String method, String url, Map<String, String> headers) throws Exception {

    HttpMethod httpMtd;
    if (method.equalsIgnoreCase("post")) {
        httpMtd = new PostMethod(url);
    }//from   ww  w  .j a va 2  s  .  c o m
    if (method.equalsIgnoreCase("put")) {
        httpMtd = new PutMethod(url);
    }
    if (method.equalsIgnoreCase("delete")) {
        httpMtd = new DeleteMethod(url);
    } else {
        httpMtd = new GetMethod(url);
    }

    if (headers != null) {
        for (String key : headers.keySet()) {
            httpMtd.addRequestHeader(key, headers.get(key));
        }
    }

    HttpClient httpclient = new HttpClient();
    try {
        int result = httpclient.executeMethod(httpMtd);
        if (result != 200) {
            if (httpMtd.getResponseHeader("Error") != null) {
                throw new Exception(httpMtd.getResponseHeader("Error").getValue());
            }
            throw new Exception("Service error code - " + result);
        }
        BufferedInputStream response = new BufferedInputStream(httpMtd.getResponseBodyAsStream());
        ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();

        byte[] buffer = new byte[1000];
        int i;
        do {
            i = response.read(buffer);
            if (i > 0) {
                resultBuffer.write(buffer, 0, i);
            }
        } while (i > 0);
        httpMtd.releaseConnection();

        return resultBuffer.toString("UTF-8");
    } catch (Exception exc) {
        httpMtd.releaseConnection();
        throw new Exception(exc.getClass().getSimpleName() + " : " + exc.getMessage());
    }
}

From source file:org.sonar.report.pdf.util.SonarAccess.java

public Document getUrlAsDocument(String urlPath) throws HttpException, IOException, DocumentException {
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(this.sonarUrl + urlPath);
    int status = 0;

    Logger.debug("HTTP Request: " + this.sonarUrl + urlPath);
    if (this.username != null) {
        Logger.debug("Setting authentication with username: " + this.username);
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(new AuthScope(this.host, this.port),
                new UsernamePasswordCredentials(this.username, this.password));
        method.setDoAuthentication(true);
    }/*w  w w .j ava2s.c om*/
    status = client.executeMethod(method);
    if (!(status == HttpStatus.SC_OK)) {
        Logger.error("Cant access to Sonar or project doesn't exist on Sonar instance. HTTP KO to "
                + this.sonarUrl + urlPath);
        throw new IOException("Cant access to Sonar or project doesn't exist on Sonar instance.");
    }
    Logger.debug("Received response.");
    SAXReader reader = new SAXReader();
    return reader.read(method.getResponseBodyAsStream());
}

From source file:org.sonar.wsclient.connectors.HttpClient3Connector.java

private String getResponseBodyAsString(HttpMethod method) {
    BufferedReader reader = null;
    try {//from ww w . ja v a 2 s . c  o m
        final InputStream inputStream = method.getResponseBodyAsStream();
        reader = new BufferedReader(new InputStreamReader(inputStream));
        final StringBuilder sb = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();

    } catch (IOException e) {
        throw new ConnectionException("Can not read response", e);

    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
                // wsclient does not have logging ability -> silently ignore
            }
        }
    }
}

From source file:org.sonarqube.ws.connectors.HttpClient3Connector.java

private String getResponseBodyAsString(HttpMethod method) throws ConnectionException {

    try (InputStream inputStream = method.getResponseBodyAsStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream, Charset.defaultCharset()));) {

        final StringBuilder sb = new StringBuilder();
        String line;/*w ww  .j  av a  2 s  .  c o m*/

        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();

    } catch (IOException e) {
        throw new ConnectionException("Can not read response", e);

    }
}