Example usage for org.apache.commons.httpclient HttpStatus SC_REQUEST_TIMEOUT

List of usage examples for org.apache.commons.httpclient HttpStatus SC_REQUEST_TIMEOUT

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_REQUEST_TIMEOUT.

Prototype

int SC_REQUEST_TIMEOUT

To view the source code for org.apache.commons.httpclient HttpStatus SC_REQUEST_TIMEOUT.

Click Source Link

Document

<tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616)

Usage

From source file:ke.go.moh.oec.adt.Daemon.java

private static boolean sendMessage(String url, String filename) {
    int returnStatus = HttpStatus.SC_CREATED;
    HttpClient httpclient = new HttpClient();
    HttpConnectionManager connectionManager = httpclient.getHttpConnectionManager();
    connectionManager.getParams().setSoTimeout(120000);

    PostMethod httpPost = new PostMethod(url);

    RequestEntity requestEntity;/*from   w w w . j  av  a  2 s. c om*/
    try {
        FileInputStream message = new FileInputStream(filename);
        Base64InputStream message64 = new Base64InputStream(message, true, -1, null);
        requestEntity = new InputStreamRequestEntity(message64, "application/octet-stream");
    } catch (FileNotFoundException e) {
        Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, "File not found.", e);
        return false;
    }
    httpPost.setRequestEntity(requestEntity);
    try {
        httpclient.executeMethod(httpPost);
        returnStatus = httpPost.getStatusCode();
    } catch (SocketTimeoutException e) {
        returnStatus = HttpStatus.SC_REQUEST_TIMEOUT;
        Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, "Request timed out.  Not retrying.", e);
    } catch (HttpException e) {
        returnStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, "HTTP exception.  Not retrying.", e);
    } catch (ConnectException e) {
        returnStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
        Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, "Service unavailable.  Not retrying.", e);
    } catch (UnknownHostException e) {
        returnStatus = HttpStatus.SC_NOT_FOUND;
        Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, "Not found.  Not retrying.", e);
    } catch (IOException e) {
        returnStatus = HttpStatus.SC_GATEWAY_TIMEOUT;
        Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, "IO exception.  Not retrying.", e);
    } finally {
        httpPost.releaseConnection();
    }
    return returnStatus == HttpStatus.SC_OK;
}

From source file:com.intellij.openapi.paths.WebReferencesAnnotatorBase.java

private static MyFetchResult doCheckUrl(String url) {
    final HttpClient client = new HttpClient();
    client.setTimeout(3000);/*  ww w. j a  v  a 2 s . c  om*/
    client.setConnectionTimeout(3000);
    // see http://hc.apache.org/httpclient-3.x/cookies.html
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    try {
        final GetMethod method = new GetMethod(url);
        final int code = client.executeMethod(method);

        return code == HttpStatus.SC_OK || code == HttpStatus.SC_REQUEST_TIMEOUT ? MyFetchResult.OK
                : MyFetchResult.NONEXISTENCE;
    } catch (UnknownHostException e) {
        LOG.info(e);
        return MyFetchResult.UNKNOWN_HOST;
    } catch (IOException e) {
        LOG.info(e);
        return MyFetchResult.OK;
    } catch (IllegalArgumentException e) {
        LOG.debug(e);
        return MyFetchResult.OK;
    }
}

From source file:net.sf.xmm.moviemanager.http.HttpUtil.java

public HTTPResult readData(URL url) throws TimeoutException, Exception {

    if (!isSetup())
        setup();/*from w w w  .j a v a  2  s . c o m*/

    GetMethod method = new GetMethod(url.toString());
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        log.debug("HTTP StatusCode not HttpStatus.SC_OK:" + method.getStatusLine());
        log.debug("For url:" + url.toString());
    }

    if (statusCode == HttpStatus.SC_REQUEST_TIMEOUT) {
        throw new TimeoutException();
    }

    //java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.InputStreamReader(method.getResponseBodyAsStream(), "ISO-8859-1"));

    StringBuffer data = new StringBuffer();

    // Saves the page data in a string buffer... 
    String chrSet = method.getResponseCharSet();
    InputStream input = method.getResponseBodyAsStream();
    ByteArrayOutputStream temp = new ByteArrayOutputStream();
    byte[] buff = new byte[1500];
    int read;
    while ((read = input.read(buff)) >= 0) {
        temp.write(buff, 0, read);
    }
    input.close();
    temp.flush();
    buff = temp.toByteArray();
    temp.close();
    data.append(new String(buff, chrSet));

    return new HTTPResult(url, statusCode == HttpStatus.SC_OK ? data : null, method.getStatusLine());
}

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

private ContextResult processPulishByDefinedServerId(String dataId, String groupName, String context,
        String serverId) {//  w  ww  . j a v  a 2 s.c  o m
    ContextResult response = new ContextResult();
    // 
    if (!login(serverId)) {
        response.setSuccess(false);
        response.setStatusMsg(",serverId");
        return response;
    }
    if (log.isDebugEnabled())
        log.debug("processPulishByDefinedServerId(" + dataId + "," + groupName + "," + context + ","
                + serverId + ")");

    String postUrl = "/diamond-server/admin.do?method=postConfig";
    PostMethod post = new PostMethod(postUrl);
    // 
    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
    try {
        NameValuePair dataId_value = new NameValuePair("dataId", dataId);
        NameValuePair group_value = new NameValuePair("group", groupName);
        NameValuePair content_value = new NameValuePair("content", context);

        // 
        post.setRequestBody(new NameValuePair[] { dataId_value, group_value, content_value });
        // 
        ConfigInfo configInfo = new ConfigInfo();
        configInfo.setDataId(dataId);
        configInfo.setGroup(groupName);
        configInfo.setContent(context);
        if (log.isDebugEnabled())
            log.debug("ConfigInfo: " + configInfo);
        // 
        response.setConfigInfo(configInfo);
        // http
        int status = client.executeMethod(post);
        response.setReceiveResult(post.getResponseBodyAsString());
        response.setStatusCode(status);
        log.info("" + status + "," + post.getResponseBodyAsString());
        if (status == HttpStatus.SC_OK) {
            response.setSuccess(true);
            response.setStatusMsg("");
            log.info(", dataId=" + dataId + ",group=" + groupName + ",content=" + context
                    + ",serverId=" + serverId);
        } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
            response.setSuccess(false);
            response.setStatusMsg(", :" + require_timeout + "");
            log.error(":" + require_timeout + ", dataId=" + dataId + ",group="
                    + groupName + ",content=" + context + ",serverId=" + serverId);
        } else {
            response.setSuccess(false);
            response.setStatusMsg(", :" + status);
            log.error(":" + response.getReceiveResult() + ",dataId=" + dataId + ",group="
                    + groupName + ",content=" + context + ",serverId=" + serverId);
        }
    } catch (HttpException e) {
        response.setStatusMsg("HttpException" + e.getMessage());
        log.error("HttpException: dataId=" + dataId + ",group=" + groupName + ",content=" + context
                + ",serverId=" + serverId, e);
    } catch (IOException e) {
        response.setStatusMsg("IOException" + e.getMessage());
        log.error("IOException: dataId=" + dataId + ",group=" + groupName + ",content=" + context
                + ",serverId=" + serverId, e);
    } finally {
        // 
        post.releaseConnection();
    }

    return response;
}

From source file:net.sf.xmm.moviemanager.gui.DialogIMDbMultiAdd.java

/**
 * Checks if the movie list should be retrived from IMDB or the local movie Database
 *///from  w  ww  .ja va  2 s. co m
void executeSearchMultipleMovies() {

    if (addInfoToExistingMovie) {
        executeEditExistingMovie(getSearchField().getText());
    } else {
        final DefaultListModel listModel = new DefaultListModel();

        int setSelectedIndex = 0;

        try {

            IMDb imdb = IMDbLib.newIMDb(MovieManager.getConfig().getHttpSettings());
            ArrayList<ModelIMDbSearchHit> hits = imdb.getSimpleMatches(getSearchField().getText());

            // Error
            if (hits == null) {
                HTTPResult res = imdb.getLastHTTPResult();

                if (res.getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
                    listModel.addElement(new ModelIMDbSearchHit("Connection timed out...")); //$NON-NLS-1$
                }
            }

            for (int i = 0; i < hits.size(); i++) {
                listModel.addElement(hits.get(i));
            }

        } catch (Exception e) {
            executeErrorMessage(e);

            e.printStackTrace();
            dispose();
        }

        if (listModel.getSize() == 0)
            listModel.addElement(
                    new ModelIMDbSearchHit(Localizer.get("DialogIMDB.list-element.messsage.no-hits-found"))); //$NON-NLS-1$

        getMoviesList().setModel(listModel);
        getMoviesList().setSelectedIndex(setSelectedIndex);

        // This delays the execution of requestFocusInWindow.
        // The reason is to avoid that the actionlistener for the choose button 
        // is invoked, which is would be if invokelater isn't used. (Experienced on Ubuntu).
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                getMoviesList().requestFocusInWindow();
            }
        });
    }
}

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

private ContextResult processPulishByDefinedServerId(String dataId, String groupName, String context,
        String serverId) {// w w w  .  java 2s.c o  m
    ContextResult response = new ContextResult();
    // 
    if (!login(serverId)) {
        response.setSuccess(false);
        response.setStatusMsg(",??serverId?");
        return response;
    }
    if (log.isDebugEnabled())
        log.debug("processPulishByDefinedServerId(" + dataId + "," + groupName + "," + context + ","
                + serverId + ")?");

    String postUrl = "/diamond-server/admin.do?method=postConfig";
    PostMethod post = new PostMethod(postUrl);
    // 
    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
    try {
        NameValuePair dataId_value = new NameValuePair("dataId", dataId);
        NameValuePair group_value = new NameValuePair("group", groupName);
        NameValuePair content_value = new NameValuePair("content", context);

        // ?
        post.setRequestBody(new NameValuePair[] { dataId_value, group_value, content_value });
        // ?
        ConfigInfo configInfo = new ConfigInfo();
        configInfo.setDataId(dataId);
        configInfo.setGroup(groupName);
        configInfo.setContent(context);
        if (log.isDebugEnabled())
            log.debug("?ConfigInfo: " + configInfo);
        // ??
        response.setConfigInfo(configInfo);
        // http??
        int status = client.executeMethod(post);
        response.setReceiveResult(post.getResponseBodyAsString());
        response.setStatusCode(status);
        log.info("??" + status + ",?" + post.getResponseBodyAsString());
        if (status == HttpStatus.SC_OK) {
            response.setSuccess(true);
            response.setStatusMsg("???");
            log.info("???, dataId=" + dataId + ",group=" + groupName + ",content=" + context
                    + ",serverId=" + serverId);
        } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
            response.setSuccess(false);
            response.setStatusMsg("??, :" + require_timeout + "");
            log.error("??:" + require_timeout + ", dataId="
                    + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId);
        } else {
            response.setSuccess(false);
            response.setStatusMsg("??, ??:" + status);
            log.error("??:" + response.getReceiveResult() + ",dataId=" + dataId + ",group="
                    + groupName + ",content=" + context + ",serverId=" + serverId);
        }
    } catch (HttpException e) {
        response.setStatusMsg("???HttpException" + e.getMessage());
        log.error("???HttpException: dataId=" + dataId + ",group=" + groupName + ",content="
                + context + ",serverId=" + serverId, e);
    } catch (IOException e) {
        response.setStatusMsg("???IOException" + e.getMessage());
        log.error("???IOException: dataId=" + dataId + ",group=" + groupName + ",content="
                + context + ",serverId=" + serverId, e);
    } finally {
        // ?
        post.releaseConnection();
    }

    return response;
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private HttpResponse executeWithTimeout(final HttpMethodBase httpMethod, int timeoutMillis) {
    client.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler(0, false));
    ExecutorService service = Executors.newSingleThreadExecutor();

    Future<HttpResponse> future = service.submit(new Callable<HttpResponse>() {
        @Override/*www. j  a va 2s . co m*/
        public HttpResponse call() throws IOException {
            return execute(httpMethod);
        }
    });

    try {
        return future.get(timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        String uriInfo = "";
        try {
            uriInfo = " for " + httpMethod.getURI();
        } catch (Exception ie) {
        }
        LOG.warn("Http connection thread was interrupted or has timed out" + uriInfo, e);
        return new HttpResponse(HttpStatus.SC_REQUEST_TIMEOUT, "Request Timeout", null);
    } finally {
        service.shutdownNow();
    }
}

From source file:net.sf.xmm.moviemanager.gui.DialogIMDB.java

void handleSearchResults(ArrayList<ModelIMDbSearchHit> hits) {

    final DefaultListModel list = new DefaultListModel();

    boolean noHits = false;

    // Error//w w w  .j a  va 2s .  c o  m
    if (hits == null) {
        HTTPResult res = imdb.getLastHTTPResult();

        if (res.getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
            list.addElement(new ModelIMDbSearchHit("Connection timed out...")); //$NON-NLS-1$
            noHits = true;
        }
    } else if (hits.size() == 0) {
        list.addElement(
                new ModelIMDbSearchHit(Localizer.get("DialogIMDB.list-element.messsage.no-hits-found"))); //$NON-NLS-1$
        noHits = true;
    } else {
        for (ModelIMDbSearchHit hit : hits)
            list.addElement(hit);
    }

    final boolean setButtonChooseEnabled = !noHits;

    // make changes on EDT
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            setListModel(list);
            listMovies.setSelectedIndex(0);
            getButtonSelect().setEnabled(setButtonChooseEnabled);
        }
    });
}

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

private ContextResult processPulishAfterModifiedByDefinedServerId(String dataId, String groupName,
        String context, String serverId) {
    ContextResult response = new ContextResult();
    // //  w  ww.ja va 2  s.  c o  m
    if (!login(serverId)) {
        response.setSuccess(false);
        response.setStatusMsg(",serverId");
        return response;
    }
    if (log.isDebugEnabled())
        log.debug("processPulishAfterModifiedByDefinedServerId(" + dataId + "," + groupName + "," + context
                + "," + serverId + ")");
    // dataId,groupName
    ContextResult result = null;
    result = queryByDataIdAndGroupName(dataId, groupName, serverId);
    if (null == result || !result.isSuccess()) {
        response.setSuccess(false);
        response.setStatusMsg("!");
        log.warn("! dataId=" + dataId + ",group=" + groupName + ",serverId="
                + serverId);
        return response;
    }
    // 
    else {
        String postUrl = "/diamond-server/admin.do?method=updateConfig";
        PostMethod post = new PostMethod(postUrl);
        // 
        post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
        try {
            NameValuePair dataId_value = new NameValuePair("dataId", dataId);
            NameValuePair group_value = new NameValuePair("group", groupName);
            NameValuePair content_value = new NameValuePair("content", context);
            // 
            post.setRequestBody(new NameValuePair[] { dataId_value, group_value, content_value });
            // 
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setDataId(dataId);
            configInfo.setGroup(groupName);
            configInfo.setContent(context);
            if (log.isDebugEnabled())
                log.debug("ConfigInfo: " + configInfo);
            // 
            response.setConfigInfo(configInfo);
            // http
            int status = client.executeMethod(post);
            response.setReceiveResult(post.getResponseBodyAsString());
            response.setStatusCode(status);
            log.info("" + status + "," + post.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                response.setSuccess(true);
                response.setStatusMsg("");
                log.info("");
            } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
                response.setSuccess(false);
                response.setStatusMsg(":" + require_timeout + "");
                log.error(":" + require_timeout + ", dataId=" + dataId
                        + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId);
            } else {
                response.setSuccess(false);
                response.setStatusMsg(
                        ",ContextResultgetReceiveResult()");
                log.error(":" + response.getReceiveResult() + ",dataId=" + dataId + ",group="
                        + groupName + ",content=" + context + ",serverId=" + serverId);
            }

        } catch (HttpException e) {
            response.setSuccess(false);
            response.setStatusMsg("HttpException" + e.getMessage());
            log.error(
                    "processPulishAfterModifiedByDefinedServerId(String dataId, String groupName, String context,String serverId)HttpExceptiondataId="
                            + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId,
                    e);
            return response;
        } catch (IOException e) {
            response.setSuccess(false);
            response.setStatusMsg("IOException" + e.getMessage());
            log.error(
                    "processPulishAfterModifiedByDefinedServerId(String dataId, String groupName, String context,String serverId)IOExceptiondataId="
                            + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId,
                    e);
            return response;
        } finally {
            // 
            post.releaseConnection();
        }

        return response;
    }
}

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

private ContextResult processPulishAfterModifiedByDefinedServerId(String dataId, String groupName,
        String context, String serverId) {
    ContextResult response = new ContextResult();
    // /*w w w.j  ava 2 s  .  c  o  m*/
    if (!login(serverId)) {
        response.setSuccess(false);
        response.setStatusMsg(",??serverId");
        return response;
    }
    if (log.isDebugEnabled())
        log.debug("processPulishAfterModifiedByDefinedServerId(" + dataId + "," + groupName + ","
                + context + "," + serverId + ")?");
    // ?dataId,groupName?
    ContextResult result = null;
    result = queryByDataIdAndGroupName(dataId, groupName, serverId);
    if (null == result || !result.isSuccess()) {
        response.setSuccess(false);
        response.setStatusMsg("????!");
        log.warn("????! dataId=" + dataId + ",group="
                + groupName + ",serverId=" + serverId);
        return response;
    }
    // ?
    else {
        String postUrl = "/diamond-server/admin.do?method=updateConfig";
        PostMethod post = new PostMethod(postUrl);
        // 
        post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
        try {
            NameValuePair dataId_value = new NameValuePair("dataId", dataId);
            NameValuePair group_value = new NameValuePair("group", groupName);
            NameValuePair content_value = new NameValuePair("content", context);
            // ?
            post.setRequestBody(new NameValuePair[] { dataId_value, group_value, content_value });
            // ?
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setDataId(dataId);
            configInfo.setGroup(groupName);
            configInfo.setContent(context);
            if (log.isDebugEnabled())
                log.debug("?ConfigInfo: " + configInfo);
            // ??
            response.setConfigInfo(configInfo);
            // http??
            int status = client.executeMethod(post);
            response.setReceiveResult(post.getResponseBodyAsString());
            response.setStatusCode(status);
            log.info("??" + status + ",?" + post.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                response.setSuccess(true);
                response.setStatusMsg("???");
                log.info("???");
            } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
                response.setSuccess(false);
                response.setStatusMsg(
                        "??:" + require_timeout + "");
                log.error("??:" + require_timeout
                        + ", dataId=" + dataId + ",group=" + groupName + ",content=" + context
                        + ",serverId=" + serverId);
            } else {
                response.setSuccess(false);
                response.setStatusMsg(
                        "??,ContextResultgetReceiveResult()");
                log.error("??:" + response.getReceiveResult() + ",dataId=" + dataId
                        + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId);
            }

        } catch (HttpException e) {
            response.setSuccess(false);
            response.setStatusMsg("??HttpException" + e.getMessage());
            log.error(
                    "?processPulishAfterModifiedByDefinedServerId(String dataId, String groupName, String context,String serverId)?HttpExceptiondataId="
                            + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId,
                    e);
            return response;
        } catch (IOException e) {
            response.setSuccess(false);
            response.setStatusMsg("??IOException" + e.getMessage());
            log.error(
                    "?processPulishAfterModifiedByDefinedServerId(String dataId, String groupName, String context,String serverId)?IOExceptiondataId="
                            + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId,
                    e);
            return response;
        } finally {
            // ?
            post.releaseConnection();
        }

        return response;
    }
}