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:com.thoughtworks.go.agent.launcher.ServerBinaryDownloader.java

private boolean download() throws Exception {
    HttpClient httpClient = new HttpClient();
    HttpMethod method = new GetMethod(checkUrl());
    InputStream body = null;/*from  w  w  w  .  ja v a  2  s . c om*/
    OutputStream outputFile = null;
    httpClient.setConnectionTimeout(ServerCall.HTTP_TIMEOUT_IN_MILLISECONDS);
    try {
        LOG.info("download started at " + new Date());
        final int status = httpClient.executeMethod(method);
        if (status != 200) {
            throw new Exception("Got status " + status + " " + method.getStatusText() + " from server");
        }
        body = new BufferedInputStream(method.getResponseBodyAsStream());
        LOG.info("got server response at " + new Date());
        outputFile = new BufferedOutputStream(
                new FileOutputStream(new File(downloadableFile.getLocalFileName())));
        IOUtils.copy(body, outputFile);
        LOG.info("pipe the stream to " + downloadableFile + " at " + new Date());
        return true;
    } catch (Exception e) {
        String message = "Couldn't access Go Server with base url: " + downloadableFile.url(urlGenerator) + ": "
                + e.toString();
        LOG.error(message);
        throw new Exception(message, e);
    } finally {
        IOUtils.closeQuietly(body);
        IOUtils.closeQuietly(outputFile);
        method.releaseConnection();
    }
}

From source file:com.rometools.fetcher.impl.HttpClientFeedFetcher.java

private SyndFeed retrieveFeed(final String urlStr, final HttpMethod method)
        throws IOException, HttpException, FetcherException, FeedException {

    final Header contentEncodingHeader = method.getResponseHeader("Content-Encoding");

    final InputStream stream;
    if (contentEncodingHeader != null && "gzip".equalsIgnoreCase(contentEncodingHeader.getValue())) {
        stream = new GZIPInputStream(method.getResponseBodyAsStream());
    } else {/*  w ww.j  a  v a  2s . c o m*/
        stream = method.getResponseBodyAsStream();
    }

    try {

        final Header contentTypeHeader = method.getResponseHeader("Content-Type");

        final XmlReader reader;
        if (contentTypeHeader != null) {
            reader = new XmlReader(stream, contentTypeHeader.getValue(), true);
        } else {
            reader = new XmlReader(stream, true);
        }

        final SyndFeedInput syndFeedInput = new SyndFeedInput();
        syndFeedInput.setPreserveWireFeed(isPreserveWireFeed());

        return syndFeedInput.build(reader);

    } finally {

        IO.close(stream);

    }

}

From source file:de.innovationgate.webgate.api.query.rss.WGDatabaseImpl.java

private Document retrievePage(String url) {

    try {/*from   w  w w .  j  a v  a 2  s. c  om*/

        // Try to retrieve from cache
        CachedPage page = (CachedPage) this.cachedPages.get(url);
        if (page != null) {
            return page.getDocument();
        }

        SAXReader reader = new SAXReader();

        // Retrieve from web
        HttpClient client = WGFactory.getHttpClientFactory().createHttpClient();
        client.setConnectionTimeout(5000);
        HttpMethod method = new GetMethod(url);
        method.setFollowRedirects(true);
        method.setStrictMode(false);
        client.executeMethod(method);

        // Read response. Wrap content decoder if necessary.
        InputStream inStream = method.getResponseBodyAsStream();

        // Not necessary - HttpClient decodes automatically
        /*Header contentEncoding = method.getResponseHeader("Content-Encoding");
        if (contentEncoding != null) {
           if (contentEncoding.getValue().equals("gzip")) {
              inStream = new GZIPInputStream(inStream);
           }
        }*/
        Document doc = reader.read(inStream);

        /*HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
        Document doc = reader.read(conn.getInputStream());*/

        // Put into cache, if cache latency is set
        if (cacheLatency > 0) {
            this.cachedPages.put(url, new CachedPage(doc));
        }

        return doc;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.htmlhifive.tools.jslint.engine.download.AbstractDownloadEngineSupport.java

@Override
public EngineInfo getEngineInfo(IProgressMonitor monitor) throws IOException {
    IProgressMonitor actualMonitor = monitor;
    if (monitor == null) {
        actualMonitor = new NullProgressMonitor();
    }// ww w. j a v a  2  s .  co  m
    actualMonitor.setTaskName(Messages.T0009.getText());
    HttpClient client = createHttpClient(getEngineSourceUrl());
    HttpMethod getMethod = new GetMethod(getEngineSourceUrl());
    int result = client.executeMethod(getMethod);
    if (result != HttpStatus.SC_OK) {
        // TODO 
        return null;
    }
    StringBuilder licenseSb = new StringBuilder();
    StringBuilder rawSource = new StringBuilder();
    Header header = getMethod.getResponseHeader("Content-Length");
    int content = Integer.valueOf(header.getValue());
    actualMonitor.beginTask(Messages.T0010.getText(), content);
    BufferedReader reader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));
    String temp = reader.readLine();
    int progress = 0;
    while (!isEndLicenseLine(temp)) {
        progress += temp.length();
        actualMonitor.subTask(Messages.T0011.format(progress, content));
        actualMonitor.worked(temp.length());
        rawSource.append(temp);
        temp = StringUtils.trim(temp);
        temp = StringUtils.substring(temp, 2);
        temp = StringUtils.trim(temp);
        licenseSb.append(temp);
        licenseSb.append(System.getProperty("line.separator"));
        rawSource.append(System.getProperty("line.separator"));
        temp = reader.readLine();
    }
    EngineInfo info = new EngineInfo();
    info.setLicenseStr(licenseSb.toString());

    while ((temp = reader.readLine()) != null) {
        progress += temp.length();
        actualMonitor.subTask(Messages.T0011.format(progress, content));
        actualMonitor.worked(temp.length());
        rawSource.append(temp);
        rawSource.append(System.getProperty("line.separator"));
    }
    info.setMainSource(rawSource.toString());
    monitor.done();
    return info;
}

From source file:ch.lipsch.subsonic4j.internal.SubsonicServiceImpl.java

private void fetchAsyncStream(String url, final StreamListener listener)
        throws IOException, JAXBException, SubsonicException {
    final HttpMethod method = new GetMethod(url);
    httpClient.executeMethod(method);/*from   w ww  .j a  v a  2  s . co m*/

    final InputStream responseStream = method.getResponseBodyAsStream();
    Header contentTypeHeader = method.getResponseHeader(HTTP_RESPONSE_HEADER_CONTENT_TYPE);

    if (contentTypeHeader.getValue().startsWith(HTTP_CONTENT_TYPE_TEXT_XML)) {
        // There was an error
        Response response = unmarshalResponse(responseStream);
        SubsonicUtil.throwExceptionIfNecessary(response);
    } else {
        new Thread("StreamDeliverer") {
            @Override
            public void run() {
                try {
                    listener.receivedStream(responseStream);
                } finally {
                    method.releaseConnection();
                }
            };
        }.start();
    }
}

From source file:com.taobao.diamond.client.processor.ServerAddressProcessor.java

/**
 * diamond/*w w w . java2s.  c  om*/
 * 
 * @param acquireCount
 *            01
 * @return
 */
private boolean acquireServerAddressOnce(int acquireCount) {
    HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration();
    String configServerAddress;
    int port;
    if (null != diamondConfigure.getConfigServerAddress()) {
        configServerAddress = diamondConfigure.getConfigServerAddress();
        port = diamondConfigure.getConfigServerPort();
    } else {
        if (acquireCount == 0) {
            configServerAddress = Constants.DEFAULT_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        } else {
            configServerAddress = Constants.DAILY_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    String serverAddressUrl = Constants.CONFIG_HTTP_URI_FILE;

    HttpMethod httpMethod = new GetMethod(serverAddressUrl);
    // HttpMethod
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(diamondConfigure.getOnceTimeout());
    // ///////////////////////
    httpMethod.setParams(params);

    try {
        if (SC_OK == configHttpClient.executeMethod(httpMethod)) {
            InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String address = null;
            List<String> newDomainNameList = new LinkedList<String>();
            while ((address = bufferedReader.readLine()) != null) {
                address = address.trim();
                if (StringUtils.isNotBlank(address)) {
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.warn("");
        }
    } catch (HttpException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (IOException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (Exception e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } finally {
        httpMethod.releaseConnection();
    }
    return false;
}

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

private static Integer executeDeployment(String server, String developerServer, String username)
        throws HttpException, IOException {
    // test steps:
    // - create user
    // - deploy Windows VM
    // - deploy Linux VM
    // - associate IP address
    // - create two IP forwarding rules
    // - create load balancer rule
    // - list IP forwarding rules
    // - list load balancer rules

    // -----------------------------
    // CREATE USER
    // -----------------------------
    String encodedUsername = URLEncoder.encode(username, "UTF-8");
    String encryptedPassword = createMD5Password(username);
    String encodedPassword = URLEncoder.encode(encryptedPassword, "UTF-8");

    String url = server + "?command=createUser&username=" + encodedUsername + "&password=" + encodedPassword
            + "&firstname=Test&lastname=Test&email=test@vmops.com&domainId=1&accounttype=0";
    if (accountName != null) {
        url = server + "?command=createUser&username=" + encodedUsername + "&password=" + encodedPassword
                + "&firstname=Test&lastname=Test&email=test@vmops.com&domainId=1&accounttype=0&account="
                + accountName;// www  .  j  ava2  s.  c  om
    }
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    long userId = -1;
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> userIdValues = getSingleValueFromXML(is, new String[] { "id", "account" });
        String userIdStr = userIdValues.get("id");
        s_logger.info("created user " + username + " with id " + userIdStr);
        if (userIdStr != null) {
            userId = Long.parseLong(userIdStr);
            _userId.set(userId);
            _account.set(userIdValues.get("account"));
            if (userId == -1) {
                s_logger.error("create user (" + username
                        + ") failed to retrieve a valid user id, aborting depolyment test");
                return -1;
            }
        }
    } else {
        s_logger.error("create user test failed for user " + username + " with error code :" + responseCode);
        return responseCode;
    }

    _secretKey.set(executeRegistration(server, username, username));

    if (_secretKey.get() == null) {
        s_logger.error("FAILED to retrieve secret key during registration, skipping user: " + username);
        return -1;
    } else {
        s_logger.info("got secret key: " + _secretKey.get());
        s_logger.info("got api key: " + _apiKey.get());
    }

    // ---------------------------------
    // CREATE NETWORK GROUP AND ADD INGRESS RULE TO IT
    // ---------------------------------
    String networkAccount = null;
    if (accountName != null) {
        networkAccount = accountName;
    } else {
        networkAccount = encodedUsername;
    }
    String encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
    String requestToSign = "apikey=" + encodedApiKey + "&command=createSecurityGroup&name=" + encodedUsername;
    requestToSign = requestToSign.toLowerCase();
    String signature = signRequest(requestToSign, _secretKey.get());
    String encodedSignature = URLEncoder.encode(signature, "UTF-8");
    url = developerServer + "?command=createSecurityGroup&name=" + encodedUsername + "&apikey=" + encodedApiKey
            + "&signature=" + encodedSignature;
    method = new GetMethod(url);
    responseCode = client.executeMethod(method);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> values = getSingleValueFromXML(is, new String[] { "id" });

        if (values.get("id") == null) {
            s_logger.info("Create network rule response code: 401");
            return 401;
        } else {
            s_logger.info("Create security group response code: " + responseCode);
        }
    } else {
        s_logger.error("Create security group failed with error code: " + responseCode
                + ". Following URL was sent: " + url);
        return responseCode;
    }

    String encodedCidr = URLEncoder.encode("192.168.1.143/32", "UTF-8");
    url = server + "?command=authorizeSecurityGroupIngress&cidrlist=" + encodedCidr + "&endport=22&"
            + "securitygroupname=" + encodedUsername + "&protocol=tcp&startport=22&account=" + networkAccount
            + "&domainid=1";

    method = new GetMethod(url);
    responseCode = client.executeMethod(method);
    if (responseCode == 200) {
        InputStream input = method.getResponseBodyAsStream();
        Element el = queryAsyncJobResult(server, input);
        Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });

        if (values.get("id") == null) {
            s_logger.info("Authorise security group ingress response code: 401");
            return 401;
        } else {
            s_logger.info("Authorise security group ingress response code: " + responseCode);
        }
    } else {
        s_logger.error("Authorise security group ingress failed with error code: " + responseCode
                + ". Following URL was sent: " + url);
        return responseCode;
    }

    // ---------------------------------
    // DEPLOY LINUX VM
    // ---------------------------------
    {
        long templateId = 2;
        String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
        String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
        String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
        encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
        requestToSign = "apikey=" + encodedApiKey + "&command=deployVirtualMachine&securitygrouplist="
                + encodedUsername + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid="
                + encodedTemplateId + "&zoneid=" + encodedZoneId;
        requestToSign = requestToSign.toLowerCase();
        signature = signRequest(requestToSign, _secretKey.get());
        encodedSignature = URLEncoder.encode(signature, "UTF-8");
        url = developerServer + "?command=deployVirtualMachine&securitygrouplist=" + encodedUsername
                + "&zoneid=" + encodedZoneId + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid="
                + encodedTemplateId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;

        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            Map<String, String> values = getSingleValueFromXML(el, new String[] { "id", "ipaddress" });

            if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
                s_logger.info("deploy linux vm response code: 401");
                return 401;
            } else {
                s_logger.info("deploy linux vm response code: " + responseCode);
                long linuxVMId = Long.parseLong(values.get("id"));
                s_logger.info("got linux virtual machine id: " + linuxVMId);
                _linuxVmId.set(values.get("id"));
                _linuxIP.set(values.get("ipaddress"));
                _linuxPassword.set("rs-ccb35ea5");
            }
        } else {
            s_logger.error("deploy linux vm failed with error code: " + responseCode
                    + ". Following URL was sent: " + url);
            return responseCode;
        }
    }

    //Create a new volume
    {
        url = server + "?command=createVolume&diskofferingid=" + diskOfferingId + "&zoneid=" + zoneId
                + "&name=newvolume&account=" + _account.get() + "&domainid=1";
        s_logger.info("Creating volume....");
        client = new HttpClient();
        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });

            if (values.get("id") == null) {
                s_logger.info("create volume response code: 401");
                return 401;
            } else {
                s_logger.info("create volume response code: " + responseCode);
                String volumeId = values.get("id");
                s_logger.info("got volume id: " + volumeId);
                _newVolume.set(volumeId);
            }
        } else {
            s_logger.error("create volume failed with error code: " + responseCode
                    + ". Following URL was sent: " + url);
            return responseCode;
        }
    }

    //attach a new volume to the vm
    {
        url = server + "?command=attachVolume&id=" + _newVolume.get() + "&virtualmachineid=" + _linuxVmId.get();
        s_logger.info("Attaching volume with id " + _newVolume.get() + " to the vm " + _linuxVmId.get());
        client = new HttpClient();
        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        s_logger.info("Attach data volume response code: " + responseCode);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });

            if (values.get("id") == null) {
                s_logger.info("Attach volume response code: 401");
                return 401;
            } else {
                s_logger.info("Attach volume response code: " + responseCode);
            }
        } else {
            s_logger.error("Attach volume failed with error code: " + responseCode
                    + ". Following URL was sent: " + url);
            return responseCode;
        }
    }

    //DEPLOY SECOND VM, ADD VOLUME TO IT

    // ---------------------------------
    // DEPLOY another linux vm
    // ---------------------------------
    {
        long templateId = 2;
        String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
        String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
        String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
        encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
        requestToSign = "apikey=" + encodedApiKey + "&command=deployVirtualMachine&securitygrouplist="
                + encodedUsername + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid="
                + encodedTemplateId + "&zoneid=" + encodedZoneId;
        requestToSign = requestToSign.toLowerCase();
        signature = signRequest(requestToSign, _secretKey.get());
        encodedSignature = URLEncoder.encode(signature, "UTF-8");
        url = developerServer + "?command=deployVirtualMachine&securitygrouplist=" + encodedUsername
                + "&zoneid=" + encodedZoneId + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid="
                + encodedTemplateId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;

        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            Map<String, String> values = getSingleValueFromXML(el, new String[] { "id", "ipaddress" });

            if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
                s_logger.info("deploy linux vm response code: 401");
                return 401;
            } else {
                s_logger.info("deploy linux vm response code: " + responseCode);
                long linuxVMId = Long.parseLong(values.get("id"));
                s_logger.info("got linux virtual machine id: " + linuxVMId);
                _linuxVmId1.set(values.get("id"));
            }
        } else {
            s_logger.error("deploy linux vm failed with error code: " + responseCode
                    + ". Following URL was sent: " + url);
            return responseCode;
        }
    }

    //Create a new volume
    {
        url = server + "?command=createVolume&diskofferingid=" + diskOfferingId1 + "&zoneid=" + zoneId
                + "&name=newvolume1&account=" + _account.get() + "&domainid=1";
        s_logger.info("Creating volume....");
        client = new HttpClient();
        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });

            if (values.get("id") == null) {
                s_logger.info("create volume response code: 401");
                return 401;
            } else {
                s_logger.info("create volume response code: " + responseCode);
                String volumeId = values.get("id");
                s_logger.info("got volume id: " + volumeId);
                _newVolume1.set(volumeId);
            }
        } else {
            s_logger.error("create volume failed with error code: " + responseCode
                    + ". Following URL was sent: " + url);
            return responseCode;
        }
    }

    //attach a new volume to the vm
    {
        url = server + "?command=attachVolume&id=" + _newVolume1.get() + "&virtualmachineid="
                + _linuxVmId1.get();
        s_logger.info("Attaching volume with id " + _newVolume1.get() + " to the vm " + _linuxVmId1.get());
        client = new HttpClient();
        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        s_logger.info("Attach data volume response code: " + responseCode);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });

            if (values.get("id") == null) {
                s_logger.info("Attach volume response code: 401");
                return 401;
            } else {
                s_logger.info("Attach volume response code: " + responseCode);
            }
        } else {
            s_logger.error("Attach volume failed with error code: " + responseCode
                    + ". Following URL was sent: " + url);
            return responseCode;
        }
    }
    return 200;
}

From source file:edu.ucsb.eucalyptus.admin.server.extensions.store.ImageStoreServiceImpl.java

public String requestJSON(String sessionId, Method method, String uri, Parameter[] params) {

    UserInfoWeb user;//from   w  ww . j  av  a 2  s.c om
    try {
        user = EucalyptusWebBackendImpl.getUserRecord(sessionId);
    } catch (Exception e) {
        return errorJSON("Session authentication error: " + e.getMessage());
    }

    NameValuePair[] finalParams;
    try {
        finalParams = getFinalParameters(method, uri, params, user);
    } catch (MalformedURLException e) {
        return errorJSON("Malformed URL: " + uri);
    }

    HttpClient client = new HttpClient();

    HttpMethod httpMethod;
    if (method == Method.GET) {
        GetMethod getMethod = new GetMethod(uri);
        httpMethod = getMethod;
        getMethod.setQueryString(finalParams);
    } else if (method == Method.POST) {
        PostMethod postMethod = new PostMethod(uri);
        httpMethod = postMethod;
        postMethod.addParameters(finalParams);
    } else {
        throw new UnsupportedOperationException("Unknown method");
    }

    try {
        int statusCode = client.executeMethod(httpMethod);
        String str = "";
        InputStream in = httpMethod.getResponseBodyAsStream();
        byte[] readBytes = new byte[1024];
        int bytesRead = -1;
        while ((bytesRead = in.read(readBytes)) > 0) {
            str += new String(readBytes, 0, bytesRead);
        }
        return str;
    } catch (HttpException e) {
        return errorJSON("Protocol error: " + e.getMessage());
    } catch (IOException e) {
        return errorJSON("Proxy error: " + e.getMessage());
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:cn.leancloud.diamond.client.processor.ServerAddressProcessor.java

/**
 * ?diamond??/*w w w.jav a  2s  .  c om*/
 * 
 * @param acquireCount
 *            ?01?
 * @return
 */
private boolean acquireServerAddressOnce(int acquireCount) {
    HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration();
    String configServerAddress;
    int port;
    if (null != diamondConfigure.getConfigServerAddress()) {
        configServerAddress = diamondConfigure.getConfigServerAddress();
        port = diamondConfigure.getConfigServerPort();
    } else {
        if (acquireCount == 0) {
            configServerAddress = Constants.DEFAULT_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        } else {
            configServerAddress = Constants.DAILY_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    String serverAddressUrl = Constants.CONFIG_HTTP_URI_FILE;

    HttpMethod httpMethod = new GetMethod(serverAddressUrl);
    // HttpMethod?
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(diamondConfigure.getOnceTimeout());
    // ///////////////////////
    httpMethod.setParams(params);

    try {
        if (SC_OK == configHttpClient.executeMethod(httpMethod)) {
            InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String address = null;
            List<String> newDomainNameList = new LinkedList<String>();
            while ((address = bufferedReader.readLine()) != null) {
                address = address.trim();
                if (StringUtils.isNotBlank(address)) {
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("?");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.warn("??");
        }
    } catch (HttpException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (IOException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (Exception e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } finally {
        httpMethod.releaseConnection();
    }
    return false;
}

From source file:cuanto.api.CuantoConnector.java

/**
 * This is here to substitute for HttpMethod.getResponseBodyAsString(), which logs an annoying error message each time
 * it's called./*from ww w .  ja  v  a2  s .c o m*/
 *
 * @param method The method for which to get the response.
 * @return The full response body as a String.
 * @throws IOException If something bad happened.
 */
private String getResponseBodyAsString(HttpMethod method) throws IOException {
    InputStreamReader reader = new InputStreamReader(method.getResponseBodyAsStream());
    StringWriter writer = new StringWriter();
    int in;
    while ((in = reader.read()) != -1) {
        writer.write(in);
    }
    reader.close();
    return writer.toString();
}