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

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

Introduction

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

Prototype

public abstract void releaseConnection();

Source Link

Usage

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

/**
 * ?diamond??//from ww  w .j a va2s .co m
 * 
 * @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.CONFIG_DEFAULT_DOMAINNAME;
            port = Constants.CONFIG_DEFAULT_PORT;
        } else {
            configServerAddress = Constants.CONFIG_DAILY_DOMAINNAME;
            port = Constants.CONFIG_DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    String serverAddressUrl = Constants.SERVER_ADDRESS_URI;

    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) && !address.startsWith("#")) {
                    if (address.contains(":")) {
                        address = address.substring(0, address.indexOf(":"));
                    }
                    if (address.contains("\\")) {
                        address = address.substring(0, address.indexOf("\\"));
                    }
                    if (address.contains("/")) {
                        address = address.substring(0, address.indexOf("/"));
                    }
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("?");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.error("??, hostsconfig.tbj.com");
        }
    } 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:ait.ffma.service.preservation.riskmanagement.TestLODFreebaseRetrieve.java

/**
 * This method evaluates Freebase description for passed guid number.
 * @param guid//from  w  w w .  j ava  2s .  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.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java

/**
 * Http//from  w w  w  . java  2 s. c  om
 * 
 * @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.intuit.tank.http.BaseRequest.java

public void sendRequest(BaseResponse response, @Nonnull HttpMethod method, String requestBody) {
    String uri = null;/*  w ww  .  j a va  2  s  .  co m*/
    long waitTime = 0L;
    try {
        this.response = response;
        uri = method.getURI().toString();
        logger.debug(
                LogUtil.getLogMessage("About to POST request to " + uri + " with requestBody  " + requestBody,
                        LogEventType.Informational));
        logRequest(uri, requestBody, method.getName(), headerInformation, httpclient, false);
        BaseRequestHandler.setHeaders(method, headerInformation);
        long startTime = System.currentTimeMillis();
        timestamp = new Date(startTime);
        httpclient.executeMethod(method);

        // read response body
        byte[] responseBody = new byte[0];
        // check for no content headers
        if (method.getStatusCode() != 203 && method.getStatusCode() != 202 && method.getStatusCode() != 204) {
            try {
                InputStream httpInputStream = method.getResponseBodyAsStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int curByte = httpInputStream.read();
                while (curByte >= 0) {
                    out.write(curByte);
                    curByte = httpInputStream.read();
                }
                responseBody = out.toByteArray();
            } catch (Exception e) {
                logger.warn("could not get response body: " + e);
            }
        }
        long endTime = System.currentTimeMillis();
        BaseRequestHandler.processResponse(responseBody, startTime, endTime, response, method.getStatusText(),
                method.getStatusCode(), method.getResponseHeaders(), httpclient.getState());
        waitTime = endTime - startTime;
    } catch (Exception ex) {
        logger.error(LogUtil.getLogMessage(
                "Could not do " + method.getName() + " to url " + uri + " |  error: " + ex.toString(),
                LogEventType.IO), ex);
        throw new RuntimeException(ex);
    } finally {
        method.releaseConnection();
    }
    if (waitTime != 0) {
        doWaitDueToLongResponse(waitTime, uri);
    }
}

From source file:davmail.exchange.ews.EwsExchangeSession.java

@Override
protected void buildSessionInfo(HttpMethod method) throws DavMailException {
    // no need to check logon method body
    if (method != null) {
        method.releaseConnection();
    }/* ww w .  j a v a2s  .c  om*/
    boolean directEws = method == null || "/ews/services.wsdl".equalsIgnoreCase(method.getPath());

    // options page is not available in direct EWS mode
    if (!directEws) {
        // retrieve email and alias from options page
        getEmailAndAliasFromOptions();
    }

    if (email == null || alias == null) {
        // OWA authentication failed, get email address from login
        if (userName.indexOf('@') >= 0) {
            // userName is email address
            email = userName;
            alias = userName.substring(0, userName.indexOf('@'));
        } else {
            // userName or domain\\username, rebuild email address
            alias = getAliasFromLogin();
            email = getAliasFromLogin() + getEmailSuffixFromHostname();
        }
    }

    currentMailboxPath = "/users/" + email.toLowerCase();

    // check EWS access
    try {
        checkEndPointUrl("/ews/exchange.asmx");
        // workaround for Exchange bug: send fake request
        internalGetFolder("");
    } catch (IOException e) {
        // first failover: retry with NTLM
        DavGatewayHttpClientFacade.addNTLM(httpClient);
        try {
            checkEndPointUrl("/ews/exchange.asmx");
            // workaround for Exchange bug: send fake request
            internalGetFolder("");
        } catch (IOException e2) {
            LOGGER.debug(e2.getMessage());
            try {
                // failover, try to retrieve EWS url from autodiscover
                checkEndPointUrl(getEwsUrlFromAutoDiscover());
                // workaround for Exchange bug: send fake request
                internalGetFolder("");
            } catch (IOException e3) {
                // autodiscover failed and initial exception was authentication failure => throw original exception
                if (e instanceof DavMailAuthenticationException) {
                    throw (DavMailAuthenticationException) e;
                }
                LOGGER.error(e2.getMessage());
                throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
            }
        }
    }

    // enable preemptive authentication on non NTLM endpoints
    if (!DavGatewayHttpClientFacade.hasNTLM(httpClient)) {
        httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
    }

    // direct EWS: get primary smtp email address with ResolveNames
    if (directEws) {
        try {
            ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod(alias);
            executeMethod(resolveNamesMethod);
            List<EWSMethod.Item> responses = resolveNamesMethod.getResponseItems();
            for (EWSMethod.Item response : responses) {
                if (alias.equalsIgnoreCase(response.get("Name"))) {
                    email = response.get("EmailAddress");
                    currentMailboxPath = "/users/" + email.toLowerCase();
                }
            }
        } catch (IOException e) {
            LOGGER.warn("Unable to get primary email address with ResolveNames", e);
        }
    }

    try {
        folderIdMap = new HashMap<String, String>();
        // load actual well known folder ids
        folderIdMap.put(internalGetFolder(INBOX).folderId.value, INBOX);
        folderIdMap.put(internalGetFolder(CALENDAR).folderId.value, CALENDAR);
        folderIdMap.put(internalGetFolder(CONTACTS).folderId.value, CONTACTS);
        folderIdMap.put(internalGetFolder(SENT).folderId.value, SENT);
        folderIdMap.put(internalGetFolder(DRAFTS).folderId.value, DRAFTS);
        folderIdMap.put(internalGetFolder(TRASH).folderId.value, TRASH);
        folderIdMap.put(internalGetFolder(JUNK).folderId.value, JUNK);
        folderIdMap.put(internalGetFolder(UNSENT).folderId.value, UNSENT);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
    }
    LOGGER.debug("Current user email is " + email + ", alias is " + alias + " on " + serverVersion);
}

From source file:edu.du.penrose.systems.fedora.ResourceIndexUtils.java

/** USED 2-2-12
 * // ww w.j  a v a2s.  com
 * The administrator is just used to get host,port,user,pwd info.
 * The call to the resource index is actually via httpClient and rest call.
 * 
 * @param topCollection such as islandora:top or codu:top
 * @param namespace the names space of collections below the topCollection. <br>
 * <br>
 * For example, to retrieve the collection names for coduDuMaps and coduDuPicture...<br> <br>
 *    islandora:top - use topCollection=islandora:top and namespace=codu <br>
 *  &nbsp;&nbsp&nbsp;codu:duMaps <br>
 *  &nbsp;&nbsp&nbsp;codu:duPictures <br>
 *  &nbsp;&nbsp&nbsp;xxxx:yyyy <br>
 *  &nbsp;&nbsp&nbsp;zzzz:wwww <br><br>
 *      OR <br><br>
 *  islandora:top <br>
 *  &nbsp;&nbsp&nbsp;codu:top - use topCollection=codu:top and namespace=codu <br>
 *  &nbsp;&nbsp&nbsp;&nbsp;&nbsp&nbsp;codu:duMaps <br>
 *  &nbsp;&nbsp&nbsp;&nbsp;&nbsp&nbsp;codu:duPictures  <br> 
 *  
 *  @param administrator  The administrator is just used to get host,port,user,password info.
 *  @param topCollection
 *  @param nameSpace
 *  
 * @return map of collection names and  titles.
 */
public static Map<String, String> getChildCollectionsMap(Administrator administrator, String topCollection,
        String namespace) {
    final String host = administrator.getHost();
    final int port = administrator.getPort();
    String userName = administrator.getUserName();
    String pwd = administrator.getUserPassword();
    String realm = null; // "realm is actually Fedora or Fedora Repository Server"

    Map<String, String> collectionMap = new LinkedHashMap<String, String>();

    String UTF_8_ENCONDING = "UTF-8";

    String type = "tuples";
    String flush = "false"; // optional default false;
    String lang = "itql";
    String format = "CSV";
    String limit = "100"; // optional default unlimited;
    String distinct = "off"; // optional default off;
    String stream = "off"; // optional default off;
    String query = "select $subject $title from <#ri> where ( $subject <fedora-model:label> $title and $subject <fedora-rels-ext:isMemberOfCollection><info:fedora/"
            + topCollection + "> ) order by $title";
    // tbd quick kludge
    String query_2 = "select $subject $title from <#ri> where ( $subject <fedora-model:label> $title and $subject <fedora-rels-ext:isMemberOf><info:fedora/"
            + topCollection + "> ) order by $title";

    String queryEncoded;
    String queryEncoded_2;
    HttpMethod method = null;
    HttpMethod method_2 = null;
    try {
        queryEncoded = URLEncoder.encode(query, UTF_8_ENCONDING);
        queryEncoded_2 = URLEncoder.encode(query_2, UTF_8_ENCONDING);

        HttpClient myClient = new HttpClient();
        method = new GetMethod("http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang=" + lang
                + "&format=" + format + "&limit=" + limit + "&distinct=" + distinct + "&stream=" + stream
                + "&query=" + queryEncoded);

        method_2 = new GetMethod("http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang="
                + lang + "&format=" + format + "&limit=" + limit + "&distinct=" + distinct + "&stream=" + stream
                + "&query=" + queryEncoded_2);

        myClient.getState().setCredentials(new AuthScope(host, port, realm),
                new UsernamePasswordCredentials(userName, pwd));
        method.setDoAuthentication(true);
        myClient.getParams().setAuthenticationPreemptive(true);

        myClient.executeMethod(method);

        String response = method.getResponseBodyAsString();
        String lines[] = response.split("\n");

        for (int i = 0; i < lines.length; i++) {
            if (!lines[i].contains("islandora") && !lines[i].equalsIgnoreCase("\"subject\",\"title\"")) {

                String singleLine = lines[i].replace("info:fedora/", "");
                if (!singleLine.startsWith(namespace)) {
                    continue;
                }
                String[] pieces = singleLine.split(",");
                String collectionName = pieces[0];
                String collectionTitle = pieces[1];

                if (isCollection(administrator, collectionName)) // collectionName is the pid
                {
                    collectionMap.put(collectionName, collectionTitle);
                }
            }
        }

        myClient.executeMethod(method_2);

        String response_2 = method.getResponseBodyAsString();
        String lines_2[] = response_2.split("\n");

        for (int i = 0; i < lines_2.length; i++) {
            if (!lines_2[i].contains("islandora") && !lines_2[i].equalsIgnoreCase("\"subject\",\"title\"")) {

                String singleLine = lines_2[i].replace("info:fedora/", "");
                if (!singleLine.startsWith(namespace)) {
                    continue;
                }
                String[] pieces = singleLine.split(",");
                String collectionName = pieces[0];
                String collectionTitle = pieces[1];

                if (isCollection(administrator, collectionName)) // collectionName is the pid
                {
                    collectionMap.put(collectionName, collectionTitle);
                }
            }
        }
    } catch (Exception e) {
        collectionMap.put("", "Unable to get Collections:" + e);
        return collectionMap;
    } finally {
        method.releaseConnection();
        method_2.releaseConnection();
    }

    return collectionMap;
}

From source file:com.cloudbees.api.BeesClient.java

/**
 * Executes an HTTP method./*from  w  w w .java2s  . c om*/
 */
private HttpReply executeRequest(HttpMethod httpMethod, Map<String, String> headers) throws IOException {
    BeesClientConfiguration conf = getBeesClientConfiguration();
    HttpClient httpClient = HttpClientHelper.createClient(conf);

    if (encodedAccountAuthorization != null)
        httpMethod.setRequestHeader("Authorization", encodedAccountAuthorization);

    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpMethod.setRequestHeader(entry.getKey(), entry.getValue());
        }
    }

    int status = 500;
    String rsp = "Error";
    try {
        status = httpClient.executeMethod(httpMethod);
        rsp = IOUtils.toString(httpMethod.getResponseBodyAsStream());
    } catch (IOException e) {
        throw (IOException) new IOException("Failed to " + httpMethod.getName() + " : " + httpMethod.getURI()
                + " : code=" + status + " response=" + e.getMessage()).initCause(e);
    } finally {
        httpMethod.releaseConnection();
    }

    trace(status + ": " + rsp);

    return new HttpReply(httpMethod, status, rsp);
}

From source file:com.foglyn.fogbugz.Request.java

private <T> T request(String url, HttpMethod method, IProgressMonitor monitor, ResponseProcessor<T> processor)
        throws FogBugzException {
    Utils.checkCancellation(monitor);/*w w  w  .  ja v  a 2  s .  c o m*/

    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, repositoryLocation,
            monitor);

    if (allowGzip) {
        method.addRequestHeader("Accept-Encoding", "gzip");
    }

    InputStream responseStream = null;
    CancellableInputStream cancellableStream = null;
    try {
        log.debug("Sending request to server");
        int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);

        log.debug("Got " + code + " response");

        if (!processor.checkHttpStatus(code)) {
            Map<String, String> headers = Utils.convertHeadersToMap(method);

            method.abort();

            throw unexpectedStatus(code, url, headers);
        }

        log.debug("Downloading data");

        responseStream = method.getResponseBodyAsStream();

        InputStream processed = responseStream;

        // may be null, for example for HEAD request
        if (processed != null) {
            Header contentEncoding = method.getResponseHeader("Content-Encoding");
            if (allowGzip && contentEncoding != null && "gzip".equals(contentEncoding.getValue())) {
                processed = new GZIPInputStream(processed);
            }

            cancellableStream = new CancellableInputStream(processed, monitor, threadFactory);
            processed = cancellableStream;
        }

        log.debug("Processing response");

        return processor.processResponse(url, method, processed);
    } catch (RuntimeException e) {
        // also catches OperationCanceledException

        // we don't know what happened to method, so we better abort processing
        method.abort();

        log.error("Error while executing request", e);

        throw e;
    } catch (IOException e) {
        // we don't know what happened... better abort connection
        method.abort();

        log.error("IO Error while executing request", e);

        throw ioError(e, url);
    } finally {
        if (cancellableStream != null) {
            cancellableStream.shutdownBackgroundThread();
        }

        // don't use cancellable stream to close responseStream -- because in case of cancelled monitor, it would ignore close request 
        Utils.close(responseStream);

        method.releaseConnection();
    }
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * /*from   w  w w .ja  va  2 s .c  o  m*/
 * @param dataId
 * @param group
 * @param timeout
 * @param skipContentCache
 *            cachegetcheckgetcache
 * @return
 */
String getConfigureInfomation(String dataId, String group, long timeout, boolean skipContentCache) {
    start();
    if (!isRun) {
        throw new RuntimeException("DiamondSubscriberConfigureInfomation");
    }
    if (null == group) {
        group = Constants.DEFAULT_GROUP;
    }
    // ==============================================
    if (MockServer.isTestMode()) {
        return MockServer.getConfigInfo(dataId, group);
    }
    // ==========================================================
    /**
     * TTLcache
     */
    if (!skipContentCache) {
        String key = makeCacheKey(dataId, group);
        String content = contentCache.get(key);
        if (content != null) {
            return content;
        }
    }

    long waitTime = 0;

    String uri = getUriString(dataId, group);
    if (log.isInfoEnabled()) {
        log.info(uri);
    }

    CacheData cacheData = getCacheData(dataId, group);

    // 
    int retryTimes = this.getDiamondConfigure().getRetrieveDataRetryTimes();
    log.info("" + retryTimes);
    // 
    int tryCount = 0;

    while (0 == timeout || timeout > waitTime) {
        // 1
        tryCount++;
        if (tryCount > retryTimes + 1) {
            log.warn("");
            break;
        }
        log.info("" + tryCount + ", waitTime:" + waitTime);

        // 
        long onceTimeOut = getOnceTimeOut(waitTime, timeout);
        waitTime += onceTimeOut;

        HttpMethod httpMethod = new GetMethod(uri);

        configureHttpMethod(skipContentCache, cacheData, onceTimeOut, httpMethod);

        try {
            int httpStatus = httpClient.executeMethod(httpMethod);

            switch (httpStatus) {

            case SC_OK: {
                String result = getSuccess(dataId, group, cacheData, httpMethod);
                return result;
            }

            case SC_NOT_MODIFIED: {
                String result = getNotModified(dataId, cacheData, httpMethod);
                return result;
            }

            case SC_NOT_FOUND: {
                log.warn("DataID:" + dataId + "");
                cacheData.setMd5(Constants.NULL);
                this.snapshotConfigInfoProcessor.removeSnapshot(dataId, group);
                return null;
            }

            case SC_SERVICE_UNAVAILABLE: {
                rotateToNextDomain();
            }
                break;

            default: {
                log.warn("HTTP State: " + httpStatus + ":" + httpClient.getState());
                rotateToNextDomain();
            }
            }
        } catch (HttpException e) {
            log.error("Http", e);
            rotateToNextDomain();
        } catch (IOException e) {

            log.error("IO", e);
            rotateToNextDomain();
        } catch (Exception e) {
            log.error("", e);
            rotateToNextDomain();
        } finally {
            httpMethod.releaseConnection();
        }
    }
    throw new RuntimeException(
            "ConfigureInfomation, DataID" + dataId + ", Group" + group + "," + timeout);
}

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

@Override
public SyndFeed retrieveFeed(final String userAgent, final URL feedUrl)
        throws IllegalArgumentException, IOException, FeedException, FetcherException {

    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }//from   w ww.ja v a 2 s.com

    final HttpClient client = new HttpClient(httpClientParams);

    if (credentialSupplier != null) {

        final HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);

        final String host = feedUrl.getHost();
        final Credentials credentials = credentialSupplier.getCredentials(null, host);
        if (credentials != null) {
            final AuthScope authScope = new AuthScope(host, -1);
            final HttpState state = client.getState();
            state.setCredentials(authScope, credentials);
        }

    }

    System.setProperty("httpclient.useragent", userAgent);

    final String urlStr = feedUrl.toString();
    final HttpMethod method = new GetMethod(urlStr);

    if (customRequestHeaders == null) {
        method.addRequestHeader("Accept-Encoding", "gzip");
        method.addRequestHeader("User-Agent", userAgent);

    } else {
        for (final Map.Entry<String, String> entry : customRequestHeaders.entrySet()) {
            method.addRequestHeader(entry.getKey(), entry.getValue());
        }
        if (!customRequestHeaders.containsKey("Accept-Encoding")) {
            method.addRequestHeader("Accept-Encoding", "gzip");
        }
        if (!customRequestHeaders.containsKey("User-Agent")) {
            method.addRequestHeader("User-Agent", userAgent);
        }
    }

    method.setFollowRedirects(true);

    if (httpClientMethodCallback != null) {
        synchronized (httpClientMethodCallback) {
            httpClientMethodCallback.afterHttpClientMethodCreate(method);
        }
    }

    final FeedFetcherCache cache = getFeedInfoCache();

    if (cache != null) {
        // retrieve feed
        try {

            if (isUsingDeltaEncoding()) {
                method.setRequestHeader("A-IM", "feed");
            }

            // try to get the feed info from the cache
            SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);

            if (syndFeedInfo != null) {

                method.setRequestHeader("If-None-Match", syndFeedInfo.getETag());

                final Object lastModifiedHeader = syndFeedInfo.getLastModified();
                if (lastModifiedHeader instanceof String) {
                    method.setRequestHeader("If-Modified-Since", (String) lastModifiedHeader);
                }

            }

            final int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode);

            syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode);

            cache.setFeedInfo(feedUrl, syndFeedInfo);

            // the feed may have been modified to pick up cached values
            // (eg - for delta encoding)
            feed = syndFeedInfo.getSyndFeed();

            return feed;

        } finally {

            method.releaseConnection();

        }

    } else {

        // cache is not in use
        try {

            final int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            return getFeed(null, urlStr, method, statusCode);

        } finally {

            method.releaseConnection();

        }

    }

}