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

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

Introduction

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

Prototype

public abstract StatusLine getStatusLine();

Source Link

Usage

From source file:it.intecs.pisa.openCatalogue.solr.SolrHandler.java

public int postDocument(InputStream stream) throws IOException, Exception {
    HttpClient client = new HttpClient();
    HttpMethod method;
    String urlStr = solrHost + "/update?commit=true";
    Log.debug("Ingesting a new document to: " + urlStr);
    method = new PostMethod(urlStr);
    RequestEntity entity = new InputStreamRequestEntity(stream);
    ((PostMethod) method).setRequestEntity(entity);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    method.setRequestHeader("Content-Type", "text/xml");
    method.setRequestHeader("charset", "utf-8");

    // Execute the method.
    int statusCode = client.executeMethod(method);
    SaxonDocument solrResponse = new SaxonDocument(method.getResponseBodyAsStream());

    Log.debug(solrResponse.getXMLDocumentString());
    if (statusCode != HttpStatus.SC_OK) {
        Log.error("Method failed: " + method.getStatusLine());
        Log.error(solrResponse.getXMLDocumentString());
    } else/*from   ww w .jav  a 2s  .c om*/
        Log.debug(solrResponse.getXMLDocumentString());
    return statusCode;
}

From source file:com.worldline.easycukes.rest.client.RestService.java

/**
 * Gets the result of the execution of a get request. the attempt will be
 * repeated several times in case of failures
 *
 * @param path the path on which the request should be sent
 * @throws Exception if something's going wrong...
 *//*from  www .  ja  v a 2 s  .c o m*/
public void retryGetRequestUntilSucceed(@NonNull String path) throws Exception {
    String fullpath = path;
    if (path.startsWith("/"))
        fullpath = baseUrl + path;

    log.debug("Sending GET request to " + fullpath + " with several attemps");

    final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY));
    final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY));

    final HttpMethod method = new GetMethod(fullpath);
    try {
        for (final Map.Entry<String, String> header : requestHeaders.entrySet())
            method.setRequestHeader(header.getKey(), header.getValue());

        String responseAsString = null;
        int statusCode;
        int attempts = 0;
        boolean success = false;
        do {
            // waiting timeToWait seconds
            Thread.sleep(timeToWait * 1000);
            statusCode = httpClient.executeMethod(method);
            attempts++;
            // check for status code 200
            if (statusCode == HttpStatus.SC_OK) {
                responseAsString = method.getResponseBodyAsString();
                success = true;
                log.info("The result is available! ");
            } else
                log.warn("unsuccessful GET request : " + method.getStatusLine() + " | Waiting " + timeToWait
                        + " seconds ...");
        } while (!success && maxAttempts > attempts);
        response = new ResponseWrapper(responseAsString, statusCode);
    } catch (final Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    } finally {
        method.releaseConnection();
    }
}

From source file:edu.uci.ics.pregelix.example.util.TestExecutor.java

private int executeHttpMethod(HttpMethod method) throws Exception {
    HttpClient client = new HttpClient();
    int statusCode;
    try {//from   w  ww  .ja v a  2  s .  co  m
        statusCode = client.executeMethod(method);
    } catch (Exception e) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
        e.printStackTrace();
        throw e;
    }
    if (statusCode != HttpStatus.SC_OK) {
        // QQQ For now, we are indeed assuming we get back JSON errors.
        // In future this may be changed depending on the requested
        // output format sent to the servlet.
        String errorBody = method.getResponseBodyAsString();
        JSONObject result = new JSONObject(errorBody);
        String[] errors = { result.getJSONArray("error-code").getString(0), result.getString("summary"),
                result.getString("stacktrace") };
        GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, errors[2]);
        throw new Exception("HTTP operation failed: " + errors[0] + "\nSTATUS LINE: " + method.getStatusLine()
                + "\nSUMMARY: " + errors[1] + "\nSTACKTRACE: " + errors[2]);
    }
    return statusCode;
}

From source file:com.dlecan.agreg.AgregResultsBot.java

public boolean isResultatsDisponibles(String type) throws Exception {
    boolean resultatsDisponibles = false;

    String urlAppelee = URL_PUBLINET_PREFIX + type + URL_PUBLINET_SUFFIX;

    HttpMethod getUrlPublinet = new GetMethod(urlAppelee);
    try {//from w w  w  .j a  va2s .  c o m
        int status = client.executeMethod(getUrlPublinet);

        if (status == HttpStatus.SC_OK) {
            InputStream streamPage = getUrlPublinet.getResponseBodyAsStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(streamPage));

            String line;
            while ((line = reader.readLine()) != null) {

                if (line.toUpperCase().contains("AUCUN CANDIDAT ADMIS")) {
                    resultatsDisponibles = false;
                    break;
                } else if (line.toUpperCase()
                        .contains("Cliquez sur une des lettres de l'alphabet".toUpperCase())) {
                    resultatsDisponibles = true;
                    break;
                } else {
                    // Le systme dconne
                }
            }
            if (resultatsDisponibles) {
                while ((line = reader.readLine()) != null) {

                    if (line.toUpperCase().contains("VALADE")) {
                        recue = true;
                        messageAEnvoyer = urlAppelee + "\n\n" + line;
                        break;
                    } else {
                        // Le systme dconne
                    }
                }
            }
        } else {
            logger.error("Method failed: {}", getUrlPublinet.getStatusLine());
        }

    } finally {
        getUrlPublinet.releaseConnection();
    }
    return resultatsDisponibles;
}

From source file:it.haefelinger.flaka.util.HttpUpload.java

/**
 * Evaluate whether uploading went well or failed.
 * /*from   ww  w  .  ja va2 s  .  c  om*/
 * A upload on Jdepot may fail cause a connection could not be established or
 * cause there was an application specific problem (like artifact exits
 * already or Manifest does not contain required or wrong attributes).
 * 
 * @param meth
 *          not null
 * @return true if all went well
 */
protected boolean eval(HttpMethod meth) {
    Matcher regex;
    String response = null;
    StringBuilder buf = new StringBuilder("");
    String errmsg = null;
    String errtyp = null; // no error

    buf.append("<upload");
    xmlattr(buf, "testonly", get("testonly", TESTONLY));
    xmlattr(buf, "endpoint", get("endpoint", ENDPOINT));
    xmlattr(buf, "timeout", get("timeout", TIMEOUT));
    xmlattr(buf, "user", get("user", USER));
    xmlattr(buf, "loc", get("filepath", null));
    xmlattr(buf, "size", get("filesize", null));

    /* If there was already an error, handle it now */
    if (getError() != null) {
        syslog(getError());
        /* must be a transport error at this point */
        xmlattr(buf, "error", "transport-error");
        buf.append(">");
        xmldata(buf, "error", errmsg);
        buf.append("</upload>");
        set("xmlbuf", buf.toString());
        return false;
    }

    /* Handle any HTTP error */
    if (meth.getStatusCode() / 100 != 2) {
        int stat;
        String line;

        stat = meth.getStatusCode();
        line = meth.getStatusLine().toString();

        errmsg = line + " [" + HttpStatusText.explain(stat) + "]";
        setError(errmsg);
        xmlattr(buf, "error", "http-error");
        buf.append("</upload>");
        xmldata(buf, "error", errmsg);
        set("xmlbuf", buf.toString());
        return false;
    }

    /* Fetch response text from server and run a check */
    response = getResponseFrom(meth);
    /* save response for later usage */
    this.set("resbuf", response);

    /*
     * We assume that everything went well, if respone text contains an
     * acceptance message.
     */
    regex = SUBMI_P.matcher(response);

    /* do we have a confirmation message? */
    if (regex.find() == false) {
        // scan for errors? No - this should be handled on another layer.
        errtyp = "storage-error";
        errmsg = "unknown storage error";
        this.setError(errmsg);
        xmlattr(buf, "error", errtyp);
        buf.append(">");
        xmldata(buf, "error", errmsg);
        /* wrap entire response in any case */
        xmldata(buf, "cdata", response);
        buf.append("</upload>");
        set("xmlbuf", buf.toString());
        return false;
    }

    /* Finally it's looking good ... */
    String g1 = regex.group(1);
    xmlattr(buf, "href", g1);
    buf.append(">");
    /* wrap entire response in any case */
    xmldata(buf, "cdata", response);
    /* finish */
    buf.append("</upload>");
    set("xmlbuf", buf.toString());
    return true;
}

From source file:mitm.common.security.crl.HTTPCRLDownloadHandler.java

private Collection<? extends CRL> downloadCRLs(URI uri, TaskScheduler watchdog)
        throws IOException, HttpException, CRLException, FileNotFoundException {
    Collection<? extends CRL> crls = null;

    HttpClient httpClient = new HttpClient();

    HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();

    params.setConnectionTimeout((int) downloadParameters.getConnectTimeout());
    params.setSoTimeout((int) downloadParameters.getReadTimeout());

    if (proxyInjector != null) {
        try {/*from   w w w  . j  a  v  a  2 s  . c o m*/
            proxyInjector.setProxy(httpClient);
        } catch (ProxyException e) {
            throw new IOException(e);
        }
    }

    HttpMethod getMethod = new GetMethod(uri.toString());

    getMethod.setFollowRedirects(true);
    getMethod.setRequestHeader("User-Agent", NetUtils.HTTP_USER_AGENT);

    /* 
     * Add watchdog that will interrupt the thread on timeout. we want the abort to fire first so add 50% 
     */
    Task threadWatchdogTask = new ThreadInterruptTimeoutTask(Thread.currentThread(), watchdog.getName());
    watchdog.addTask(threadWatchdogTask, (long) (downloadParameters.getTotalTimeout() * 1.5));

    /* 
     * Add watchdog that will abort the HTTPMethod on timeout. we want to close the input first so add 20% 
     */
    Task httpMethodAbortTimeoutTask = new HTTPMethodAbortTimeoutTask(getMethod, watchdog.getName());
    watchdog.addTask(httpMethodAbortTimeoutTask, (long) (downloadParameters.getTotalTimeout() * 1.2));

    try {
        logger.debug("Setting up a connection to: " + uri);

        int statusCode = 0;

        try {
            statusCode = httpClient.executeMethod(getMethod);
        } catch (IllegalArgumentException e) {
            /* 
             * HttpClient can throw IllegalArgumentException when the host is not set 
             */
            throw new CRLException(e);
        }

        if (statusCode != HttpStatus.SC_OK) {
            throw new IOException("Error getting CRL. Message: " + getMethod.getStatusLine());
        }

        InputStream urlStream = getMethod.getResponseBodyAsStream();

        if (urlStream == null) {
            throw new IOException("Response body is null.");
        }

        /* 
         * add a timeout watchdog on the input 
         */
        Task inputWatchdogTask = new InputStreamTimeoutTask(urlStream, watchdog.getName());

        watchdog.addTask(inputWatchdogTask, downloadParameters.getTotalTimeout());

        /*
         * we want to set a max on the number of bytes to download. We do not want
         * a rogue server to provide us with a 1 TB CRL.
         */
        InputStream limitInputStream = new SizeLimitedInputStream(urlStream, downloadParameters.getMaxBytes());

        ReadableOutputStreamBuffer output = new ReadableOutputStreamBuffer(memThreshold);

        try {
            IOUtils.copy(limitInputStream, output);

            if (threadWatchdogTask.hasRun() || httpMethodAbortTimeoutTask.hasRun()
                    || inputWatchdogTask.hasRun()) {
                /* a timeout has occurred */
                throw new IOException(TIMEOUT_ERROR + uri);
            }

            try {
                InputStream input = output.getInputStream();

                try {
                    crls = CRLUtils.readCRLs(input);
                } finally {
                    IOUtils.closeQuietly(input);
                }

                if (crls == null || crls.size() == 0) {
                    logger.debug("No CRLs found in the downloaded stream.");
                }
            } catch (CertificateException e) {
                throw new CRLException(e);
            } catch (NoSuchProviderException e) {
                throw new CRLException(e);
            } catch (SecurityFactoryFactoryException e) {
                throw new CRLException(e);
            }
        } finally {
            /* 
             * we need to close ReadableOutputStreamBuffer to prevent temp file leak 
             */
            IOUtils.closeQuietly(output);
        }
    } finally {
        getMethod.releaseConnection();
    }

    return crls;
}

From source file:io.hops.hopsworks.api.admin.HDFSUIProxyServlet.java

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {

    if (servletRequest.getUserPrincipal() == null) {
        servletResponse.sendError(403, "User is not logged in");
        return;/*from w  w w. j  av  a 2 s  .c  o  m*/
    }
    if (!servletRequest.isUserInRole("HOPS_ADMIN")) {
        servletResponse.sendError(Response.Status.BAD_REQUEST.getStatusCode(),
                "You don't have the access right for this service");
        return;
    }
    if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
        servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
    }
    if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) {
        servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
    }

    // Make the Request
    // note: we won't transfer the protocol version because I'm not 
    // sure it would truly be compatible
    String proxyRequestUri = rewriteUrlFromRequest(servletRequest);

    try {
        String[] targetHost_port = settings.getHDFSWebUIAddress().split(":");
        File keyStore = new File(baseHadoopClientsService.getSuperKeystorePath());
        File trustStore = new File(baseHadoopClientsService.getSuperTrustStorePath());
        // Assume that KeyStore password and Key password are the same
        Protocol httpsProto = new Protocol("https",
                new CustomSSLProtocolSocketFactory(keyStore,
                        baseHadoopClientsService.getSuperKeystorePassword(),
                        baseHadoopClientsService.getSuperKeystorePassword(), trustStore,
                        baseHadoopClientsService.getSuperTrustStorePassword()),
                Integer.parseInt(targetHost_port[1]));
        Protocol.registerProtocol("https", httpsProto);
        // Execute the request
        HttpClientParams params = new HttpClientParams();
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpClient client = new HttpClient(params);
        HostConfiguration config = new HostConfiguration();
        InetAddress localAddress = InetAddress.getLocalHost();
        config.setLocalAddress(localAddress);

        HttpMethod m = new GetMethod(proxyRequestUri);
        Enumeration<String> names = servletRequest.getHeaderNames();
        while (names.hasMoreElements()) {
            String headerName = names.nextElement();
            String value = servletRequest.getHeader(headerName);
            if (PASS_THROUGH_HEADERS.contains(headerName)) {
                //hdfs does not send back the js if encoding is not accepted
                //but we don't want to accept encoding for the html because we
                //need to be able to parse it
                if (headerName.equalsIgnoreCase("accept-encoding") && (servletRequest.getPathInfo() == null
                        || !servletRequest.getPathInfo().contains(".js"))) {
                    continue;
                } else {
                    m.setRequestHeader(headerName, value);
                }
            }
        }
        String user = servletRequest.getRemoteUser();
        if (user != null && !user.isEmpty()) {
            m.setRequestHeader("Cookie", "proxy-user" + "=" + URLEncoder.encode(user, "ASCII"));
        }

        client.executeMethod(config, m);

        // Process the response
        int statusCode = m.getStatusCode();

        // Pass the response code. This method with the "reason phrase" is 
        //deprecated but it's the only way to pass the reason along too.
        //noinspection deprecation
        servletResponse.setStatus(statusCode, m.getStatusLine().getReasonPhrase());

        copyResponseHeaders(m, servletRequest, servletResponse);

        // Send the content to the client
        copyResponseEntity(m, servletResponse);

    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        if (e instanceof ServletException) {
            throw (ServletException) e;
        }
        //noinspection ConstantConditions
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new RuntimeException(e);

    }
}

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

/**
 * Process a POST Message./*from  w w  w  . j  a v  a2s  .co  m*/
 * 
 * @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:ch.ksfx.web.services.spidering.http.HttpClientHelper.java

private HttpMethod executeMethod(HttpMethod httpMethod) {
    for (Header header : this.headers.getHeaders()) {
        httpMethod.setRequestHeader(header);
    }//from  w ww  .j  ava2s .c om

    httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new KsfxHttpRetryHandler(retryCount, retryDelay));

    try {
        int tryCount = 0;
        int statusCode;
        do {
            if (tryCount > 1) {
                httpMethod = createNewHttpMethod(httpMethod);
                try {
                    if (retryDelay == 0) {
                        retryDelay = DEFAULT_RETRY_DELAY;
                    }
                    Thread.sleep(retryDelay);
                } catch (InterruptedException e) {
                    logger.severe("InterruptedException");
                }
            }

            //PROXY Configuration
            /*
            if (torify) {
                    
            String proxyHost = "";
            Integer proxyPort = 0;
                    
            try {
                    proxyHost = SpiderConfiguration.getConfiguration().getString("torifyHost");
                    proxyPort = SpiderConfiguration.getConfiguration().getInt("torifyPort");
            } catch (Exception e) {
                logger.severe("Cannot get Proxy information");
            }
                    
            this.httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
            }
            */

            statusCode = this.httpClient.executeMethod(httpMethod);
            tryCount++;
        } while (!(statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_FORBIDDEN
                || statusCode == HttpStatus.SC_NOT_FOUND) && tryCount < retryCount);
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("HTTP method failed: " + httpMethod.getStatusLine() + " - "
                    + httpMethod.getURI().toString());
        }
    } catch (HttpException e) {
        e.printStackTrace();
        httpMethod.abort();
        try {
            logger.log(Level.SEVERE, "Redirrex " + e.getClass(), e);
            if (e.getClass().equals(RedirectException.class)) {
                logger.log(Level.SEVERE, "Is real redirect exception", e);
                throw new RuntimeException("HttpRedirectException");
            }
            logger.log(Level.SEVERE, "HTTP protocol error for URL: " + httpMethod.getURI().toString(), e);
        } catch (URIException e1) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "URI exception", e);
        }
        throw new RuntimeException("HttpException");
    } catch (IOException e) {
        try {
            e.printStackTrace();
            logger.log(Level.SEVERE, "HTTP transport error for URL: " + httpMethod.getURI().toString(), e);

        } catch (URIException e1) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "URI exception", e);

        }
        throw new RuntimeException("IOException");
    }
    return httpMethod;
}

From source file:com.worldline.easycukes.rest.client.RestService.java

/**
 * Gets the result of the execution of a get request. the attempt will be
 * repeated until obtain the exepected result
 *
 * @param path       the path on which the request should be executed
 * @param expression the result that should be returned by the GET request, which
 *                   allows to know if that request is completely processed or not
 * @throws Exception if something's going wrong...
 *///from  w  w w .  ja v a 2 s. c  o m
public void retryGetRequestUntilObtainExpectedResponse(@NonNull String path, @NonNull String expression)
        throws Exception {
    String fullpath = path;
    if (path.startsWith("/"))
        fullpath = baseUrl + path;
    log.debug("Sending GET request to " + fullpath + " with several attemps");

    final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY));
    final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY));

    final HttpMethod method = new GetMethod(fullpath);
    try {
        for (final Map.Entry<String, String> header : requestHeaders.entrySet())
            method.setRequestHeader(header.getKey(), header.getValue());

        String responseAsString = null;
        String toCheck = null;
        String expected = expression;
        String prop = null;
        final int idx = expression.indexOf("=");
        if (idx > 0) {
            prop = expression.substring(0, idx);
            expected = expression.substring(idx + 1);
        }
        int statusCode;
        int attempts = 0;
        boolean success = false;
        do {
            // waiting timeToWait seconds
            Thread.sleep(timeToWait * 1000);
            statusCode = httpClient.executeMethod(method);
            attempts++;
            if (statusCode == HttpStatus.SC_OK) {
                responseAsString = method.getResponseBodyAsString();
                toCheck = responseAsString;
                if (prop != null)
                    toCheck = JSONHelper.getPropertyValue(responseAsString, prop);
                if (toCheck.contains(expected)) {
                    success = true;
                    log.debug("The result is available! ");
                } else
                    log.warn("The result is not yet available! | Waiting " + timeToWait + " seconds ...");
            } else
                log.warn("unsuccessful GET request : " + method.getStatusLine() + " | Waiting " + timeToWait
                        + " seconds ...");
        } while (!success && maxAttempts > attempts);
        response = new ResponseWrapper(responseAsString, statusCode);
    } catch (final Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    } finally {
        method.releaseConnection();
    }
}