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

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

Introduction

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

Prototype

public abstract String getResponseBodyAsString() throws IOException;

Source Link

Usage

From source file:it.drwolf.ridire.session.CrawlerManager.java

public Long getRawBytes(String jobName, User user) throws HeritrixException, CrawlingFileException {
    // this.updateJobsList(user);
    Job j = this.getPersistedJob(jobName);
    if (j == null) {
        return 0L;
    }/* w  w  w.  jav  a 2 s .  c o m*/
    if (j.getChildJobName() != null && j.getChildJobName().length() > 0) {
        jobName = j.getChildJobName();
    }
    HttpMethod method = null;
    try {
        method = new GetMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8")
                + "/jobdir/reports/crawl-report.txt");
        // TODO check status
        int status = this.httpClient.executeMethod(method);
        String report = method.getResponseBodyAsString();
        method.releaseConnection();
        long parseLong = 0L;
        Matcher m = CrawlerManager.pDownloadedBytes.matcher(report);
        if (m.find()) {
            String bytes = m.group(1);
            parseLong = Long.parseLong(bytes);
        }
        if (parseLong == 0L) {
            try {
                parseLong = this.getRawBytesFromFileSystem(jobName);
            } catch (Exception exception) {
                parseLong = 0;
            }
        }
        return parseLong;
    } catch (HttpException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.jackbe.mapreduce.EMMLRestRunner.java

protected String executeRESTCall(String scriptName, String encodedValue) {

    // "http://localhost:8080/presto/edge/api/rest/StockQuoteMapper/runMashup?x-presto-resultFormat=xml&value=<encodedValue>"
    HttpMethod httpMethod = null;
    String result = null;/*from   w  w w  .  j av a 2s  . co m*/

    if (encodedValue != null) {
        httpMethod = new GetMethod(protocol + host + ":" + port + path + scriptName + "/" + operation + "?"
                + format + "&value=" + encodedValue);
        log.debug("Invoking REST service: " + protocol + host + ":" + port + path + scriptName + "/" + operation
                + "?" + format + "&value=" + encodedValue);
    } else {
        httpMethod = new GetMethod(
                protocol + host + ":" + port + path + scriptName + "/" + operation + "?" + format);
        log.debug("Invoking REST service: " + protocol + host + ":" + port + path + scriptName + "/" + operation
                + "?" + format);
    }

    try {
        client.executeMethod(httpMethod);

        if (httpMethod.getStatusCode() != HttpStatus.SC_OK) {
            log.error("HTTP Error status connecting to Presto: " + httpMethod.getStatusCode());
            log.error("HTTP Error message connecting to Presto: " + httpMethod.getStatusText());
            return null;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Status code: " + httpMethod.getStatusCode());
                Header contentTypeHeader = httpMethod.getResponseHeader("content-type");
                log.debug("Mimetype: " + contentTypeHeader.getValue());
            }
        }

        result = httpMethod.getResponseBodyAsString();
        // log.debug(httpMethod.getStatusText());
        if (log.isDebugEnabled())
            log.debug("Response: " + result);
    } catch (Exception e) {
        log.error("Exception executing REST call: " + e, e);
    } finally {
        httpMethod.releaseConnection();
    }

    return result;

}

From source file:ait.ffma.service.preservation.riskmanagement.TestLODFreebaseRetrieve.java

/**
 * This method evaluates Freebase description for passed guid number.
 * @param guid//from  w w w  .  j  a  v  a 2 s.c  o m
 * @return description text
 */
public String getDescription(String guid) {
    String res = "";
    if (guid != null) {
        guid = guid.replaceAll(LODConstants.STRING_SEPARATOR, "");

        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
        String url = "http://hotshot.dfhuynh.user.dev.freebaseapps.com/html?id=/guid/" + guid;
        HttpMethod method = new GetMethod(url);
        method.setFollowRedirects(false);
        try {
            client.executeMethod(method);
            String responseBody = method.getResponseBodyAsString();
            log.info("Response body: " + responseBody);
            String descriptionStr = responseBody.substring(
                    responseBody.indexOf(LODConstants.DESC_BEGIN) + LODConstants.DESC_BEGIN.length(),
                    responseBody.indexOf(LODConstants.DESC_P_END));
            res = descriptionStr.replaceAll(LODConstants.DESC_P, "");
        } catch (HttpException he) {
            log.fine("Http error connecting to " + url + "");
        } catch (IOException ioe) {
            log.fine("Unable to connect to " + url + "");
        } catch (Exception e) {
            log.fine("Description retrieval exception. Unable to parse response from " + url + " (Exception: "
                    + e.getLocalizedMessage() + ")");
        }
        method.releaseConnection();
    }
    return res;
}

From source file:com.apatar.http.HttpNode.java

@Override
protected void TransformTDBtoRDB(int mode) {
    DataBaseTools.completeTransfer();/*from  www  . j  a v a2 s.c  om*/
    HttpConnection conn = (HttpConnection) ApplicationData.getProject().getProjectData(getConnectionDataID())
            .getData();
    String url = conn.getUrl();
    HttpRequestMethod httpRequestMethod = (HttpRequestMethod) conn.getMethod();

    TableInfo ti = getTiForConnection(IN_CONN_POINT_NAME);

    TableInfo tiOut = getTiForConnection(OUT_CONN_POINT_NAME);

    List<Record> selectionList = DataBaseTools.intersectionRecords(tiOut.getRecords(), ti.getRecords(), true);

    // read values from result set and put it in request
    SQLQueryString sqs = DataBaseTools.CreateSelectString(ApplicationData.getTempDataBase().getDataBaseInfo(),
            new SQLCreationData(selectionList, ti.getTableName()), null);

    if (sqs == null) {
        return;
    }

    ResultSet rs;
    try {
        rs = DataBaseTools.executeSelect(sqs, ApplicationData.getTempJDBC());

        while (rs.next()) {
            KeyInsensitiveMap rsData = DataBaseTools.GetDataFromRS(rs);

            HttpMethod hm;

            if (httpRequestMethod == HttpRequestMethod.post) {
                hm = sendPost(url, rsData);
            } else {
                hm = sendGet(url, rsData);
            }

            HttpClient client = new HttpClient();
            if (ApplicationData.httpClient.isUseProxy()) {
                HostConfiguration hostConfig = client.getHostConfiguration();
                hostConfig.setProxy(ApplicationData.httpClient.getHost(), ApplicationData.httpClient.getPort());
                String proxyUser = ApplicationData.httpClient.getUserName();
                if (proxyUser != null) {
                    client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                            proxyUser, ApplicationData.httpClient.getPassword()));
                }
            }

            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            int status = client.executeMethod(hm);

            KeyInsensitiveMap datas = new KeyInsensitiveMap();

            if (status != HttpStatus.SC_OK) {
                datas.put("Response", "Upload failed, response=" + HttpStatus.getStatusText(status));
            } else {
                datas.put("Response", hm.getResponseBodyAsString());
            }

            ti = getTiForConnection(OUT_CONN_POINT_NAME);

            List<Record> recs = getTiForConnection(AbstractDataBaseNode.OUT_CONN_POINT_NAME).getSchemaTable()
                    .getRecords();

            for (int j = 1; j < recs.size(); j++) {
                Record rec = recs.get(j);
                datas.put(rec.getFieldName(), rs.getObject(rec.getFieldName()));
            }

            DataBaseTools.insertData(new DataProcessingInfo(ApplicationData.getTempDataBase().getDataBaseInfo(),
                    ti.getTableName(), ti.getRecords(), ApplicationData.getTempJDBC()), datas);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    DataBaseTools.completeTransfer();
}

From source file:net.sourceforge.jwbf.actions.HttpActionClient.java

/**
 * Process a POST Message./*  w w  w  .ja va 2 s. c  om*/
 * 
 * @param authpost
 *            a
 * @param cp
 *            a
 * @return a returning message, not null
 * @throws IOException on problems
 * @throws ProcessException on problems
 * @throws CookieException on problems
 */
protected String post(HttpMethod authpost, ContentProcessable cp)
        throws IOException, ProcessException, CookieException {
    showCookies(client);
    authpost.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET);
    String out = "";

    client.executeMethod(authpost);

    // Header locationHeader = authpost.getResponseHeader("location");
    // if (locationHeader != null) {
    // authpost.setRequestHeader(locationHeader) ;
    // }

    // Usually a successful form-based login results in a redicrect to
    // another url

    int statuscode = authpost.getStatusCode();
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
            || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
        Header header = authpost.getResponseHeader("location");
        if (header != null) {
            String newuri = header.getValue();
            if ((newuri == null) || (newuri.equals(""))) {
                newuri = "/";
            }
            LOG.debug("Redirect target: " + newuri);
            GetMethod redirect = new GetMethod(newuri);

            client.executeMethod(redirect);
            LOG.debug("Redirect: " + redirect.getStatusLine().toString());
            // release any connection resources used by the method
            authpost.releaseConnection();
            authpost = redirect;
        }
    }

    out = authpost.getResponseBodyAsString();
    out = cp.processReturningText(out, authpost);

    cp.validateReturningCookies(client.getState().getCookies(), authpost);

    authpost.releaseConnection();
    LOG.debug(authpost.getURI() + " || " + "POST: " + authpost.getStatusLine().toString());
    return out;
}

From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java

/**
 * Http//from w w w  . ja v a 2 s .co  m
 * 
 * @param request ?
 * @param strParaFileName ???
 * @param strFilePath 
 * @return 
 * @throws HttpException, IOException 
 */
public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath)
        throws HttpException, IOException {
    HttpClient httpclient = new HttpClient(connectionManager);

    // 
    int connectionTimeout = defaultConnectionTimeout;
    if (request.getConnectionTimeout() > 0) {
        connectionTimeout = request.getConnectionTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

    // 
    int soTimeout = defaultSoTimeout;
    if (request.getTimeout() > 0) {
        soTimeout = request.getTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

    // ConnectionManagerconnection
    httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout);

    String charset = request.getCharset();
    charset = charset == null ? DEFAULT_CHARSET : charset;
    HttpMethod method = null;

    //get??
    if (request.getMethod().equals(HttpRequest.METHOD_GET)) {
        method = new GetMethod(request.getUrl());
        method.getParams().setCredentialCharset(charset);

        // parseNotifyConfig??GETrequestQueryString
        method.setQueryString(request.getQueryString());
    } else if (strParaFileName.equals("") && strFilePath.equals("")) {
        //post??
        method = new PostMethod(request.getUrl());
        ((PostMethod) method).addParameters(request.getParameters());
        method.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded; text/html; charset=" + charset);
    } else {
        //post?
        method = new PostMethod(request.getUrl());
        List<Part> parts = new ArrayList<Part>();
        for (int i = 0; i < request.getParameters().length; i++) {
            parts.add(new StringPart(request.getParameters()[i].getName(),
                    request.getParameters()[i].getValue(), charset));
        }
        //?strParaFileName???
        parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath))));

        // 
        ((PostMethod) method).setRequestEntity(
                new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams()));
    }

    // Http HeaderUser-Agent
    method.addRequestHeader("User-Agent", "Mozilla/4.0");
    HttpResponse response = new HttpResponse();

    try {
        httpclient.executeMethod(method);
        if (request.getResultType().equals(HttpResultType.STRING)) {
            response.setStringResult(method.getResponseBodyAsString());
        } else if (request.getResultType().equals(HttpResultType.BYTES)) {
            response.setByteResult(method.getResponseBody());
        }
        response.setResponseHeaders(method.getResponseHeaders());
    } catch (UnknownHostException ex) {

        return null;
    } catch (IOException ex) {

        return null;
    } catch (Exception ex) {

        return null;
    } finally {
        method.releaseConnection();
    }
    return response;
}

From source file:com.mirth.connect.connectors.http.HttpMessageDispatcher.java

private void submitHttpRequest(String address, MessageObject mo) throws Exception {
    HttpMethod httpMethod = null;

    try {//from w ww  . j  av a  2s. c om
        httpMethod = buildHttpRequest(replacer.replaceValues(address, mo), mo);

        // authentication

        if (connector.isDispatcherUseAuthentication()) {
            List<String> authenticationPreferences = new ArrayList<String>();

            if ("Digest".equalsIgnoreCase(connector.getDispatcherAuthenticationType())) {
                authenticationPreferences.add(AuthPolicy.DIGEST);
                logger.debug("using Digest authentication");
            } else {
                authenticationPreferences.add(AuthPolicy.BASIC);
                logger.debug("using Basic authentication");
            }

            client.getParams().setAuthenticationPreemptive(true);
            client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authenticationPreferences);
            Credentials credentials = new UsernamePasswordCredentials(
                    replacer.replaceValues(connector.getDispatcherUsername(), mo),
                    replacer.replaceValues(connector.getDispatcherPassword(), mo));
            client.getState().setCredentials(
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials);
            logger.debug("using authentication with credentials: " + credentials);
        }

        client.getParams().setSoTimeout(
                NumberUtils.toInt(replacer.replaceValues(connector.getDispatcherSocketTimeout()), 30000));

        // execute the method
        logger.debug(
                "executing method: type=" + httpMethod.getName() + ", uri=" + httpMethod.getURI().toString());
        int statusCode = client.executeMethod(httpMethod);
        logger.debug("received status code: " + statusCode);

        String response = null;

        if (connector.isDispatcherIncludeHeadersInResponse()) {
            HttpMessageConverter converter = new HttpMessageConverter();
            response = converter.httpResponseToXml(httpMethod.getStatusLine().toString(),
                    httpMethod.getResponseHeaders(), httpMethod.getResponseBodyAsString());
        } else {
            response = httpMethod.getResponseBodyAsString();
        }

        if (statusCode < HttpStatus.SC_BAD_REQUEST) {
            messageObjectController.setSuccess(mo, response, null);

            // send to reply channel
            if ((connector.getDispatcherReplyChannelId() != null)
                    && !connector.getDispatcherReplyChannelId().equals("sink")) {
                new VMRouter().routeMessageByChannelId(connector.getDispatcherReplyChannelId(), response, true);
            }
        } else {
            alertController.sendAlerts(connector.getChannelId(), Constants.ERROR_404,
                    "Received error response from HTTP server.", null);
            messageObjectController.setError(mo, Constants.ERROR_404, response, null, null);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}

From source file:fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient.java

private JSONObject doHttpRequest(HttpMethod req, Boolean skipError) throws OCSyncException {
    JSONObject respJSON = null;//w  w  w .j a v a  2  s.com
    int status = 0;

    // We try maximumHttpReqTries because sometimes network is slow or unstable
    int tryNb = 0;
    ConnectivityMonitor cMon = new ConnectivityMonitor(_context);

    while (tryNb < maximumHttpReqTries) {
        tryNb++;

        if (!cMon.isValid()) {
            if (tryNb == maximumHttpReqTries) {
                req.releaseConnection();
                throw new OCSyncException(R.string.err_sync_no_connection_available, OCSyncErrorType.IO);
            }
            continue;
        }

        try {
            status = _ocClient.executeMethod(req);

            Log.d(TAG, "HTTP Request done at try " + tryNb);

            // Force loop exit
            tryNb = maximumHttpReqTries;
        } catch (ConnectException e) {
            Log.e(TAG, "Unable to perform a connection to ownCloud instance", e);

            // If it's the last try
            if (tryNb == maximumHttpReqTries) {
                req.releaseConnection();
                throw new OCSyncException(R.string.err_sync_http_request_connect, OCSyncErrorType.IO);
            }
        } catch (HttpException e) {
            Log.e(TAG, "Unable to perform a connection to ownCloud instance", e);

            // If it's the last try
            if (tryNb == maximumHttpReqTries) {
                req.releaseConnection();
                throw new OCSyncException(R.string.err_sync_http_request_httpexception, OCSyncErrorType.IO);
            }
        } catch (IOException e) {
            Log.e(TAG, "Unable to perform a connection to ownCloud instance", e);

            // If it's the last try
            if (tryNb == maximumHttpReqTries) {
                req.releaseConnection();
                throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO);
            }
        }
    }

    if (status == HttpStatus.SC_OK) {
        String response = null;
        try {
            response = req.getResponseBodyAsString();
        } catch (IOException e) {
            Log.e(TAG, "Unable to parse server response", e);
            throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.IO);
        }
        //Log.d(TAG, "Successful response: " + response);

        // Parse the response
        try {
            respJSON = new JSONObject(response);
        } catch (JSONException e) {
            if (skipError == false) {
                Log.e(TAG, "Unable to parse server response", e);
                throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE);
            }
            return null;
        }

    } else if (status == HttpStatus.SC_FORBIDDEN) {
        // Authentication failed
        throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH);
    } else {
        // Unk error
        String response = null;
        try {
            response = req.getResponseBodyAsString();
        } catch (IOException e) {
            Log.e(TAG, "Unable to parse server response", e);
            throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.PARSE);
        }

        Log.e(TAG, "Server set unhandled HTTP return code " + status);
        if (response != null) {
            Log.e(TAG, "Status code: " + status + ". Response message: " + response);
        } else {
            Log.e(TAG, "Status code: " + status);
        }
        throw new OCSyncException(R.string.err_sync_http_request_returncode_unhandled,
                OCSyncErrorType.SERVER_ERROR);
    }
    return respJSON;
}

From source file:com.taobao.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

/**
 * Response/*w w  w . j ava2  s. c o  m*/
 * 
 * @param httpMethod
 * @return
 */
String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException {
    StringBuilder contentBuilder = new StringBuilder();
    if (isZipContent(httpMethod)) {
        // 
        InputStream is = null;
        GZIPInputStream gzin = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            is = httpMethod.getResponseBodyAsStream();
            gzin = new GZIPInputStream(is);
            isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // 
            br = new BufferedReader(isr);
            char[] buffer = new char[4096];
            int readlen = -1;
            while ((readlen = br.read(buffer, 0, 4096)) != -1) {
                contentBuilder.append(buffer, 0, readlen);
            }
        } catch (Exception e) {
            log.error("", e);
        } finally {
            try {
                br.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                isr.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                gzin.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                is.close();
            } catch (Exception e1) {
                // ignore
            }
        }
    } else {
        // 
        String content = null;
        try {
            content = httpMethod.getResponseBodyAsString();
        } catch (Exception e) {
            log.error("", e);
        }
        if (null == content) {
            return null;
        }
        contentBuilder.append(content);
    }
    return StringEscapeUtils.unescapeHtml(contentBuilder.toString());
}

From source file:cn.leancloud.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

/**
 * ?Response??/*from   w  ww  .j a v  a2 s  .  c  om*/
 * 
 * @param httpMethod
 * @return
 */
String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException {
    StringBuilder contentBuilder = new StringBuilder();
    if (isZipContent(httpMethod)) {
        // ???
        InputStream is = null;
        GZIPInputStream gzin = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            is = httpMethod.getResponseBodyAsStream();
            gzin = new GZIPInputStream(is);
            isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // ?????
            br = new BufferedReader(isr);
            char[] buffer = new char[4096];
            int readlen = -1;
            while ((readlen = br.read(buffer, 0, 4096)) != -1) {
                contentBuilder.append(buffer, 0, readlen);
            }
        } catch (Exception e) {
            log.error("", e);
        } finally {
            try {
                br.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                isr.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                gzin.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                is.close();
            } catch (Exception e1) {
                // ignore
            }
        }
    } else {
        // ???
        String content = null;
        try {
            content = httpMethod.getResponseBodyAsString();
        } catch (Exception e) {
            log.error("???", e);
        }
        if (null == content) {
            return null;
        }
        contentBuilder.append(content);
    }
    return StringEscapeUtils.unescapeHtml(contentBuilder.toString());
}