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

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

Introduction

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

Prototype

public static String getStatusText(int statusCode) 

Source Link

Document

Get the reason phrase for a particular status code.

Usage

From source file:com.tc.admin.UpdateCheckRequestTest.java

public static String getResponseBody(URL url, HttpClient client) throws ConnectException, IOException {
    GetMethod get = new GetMethod(url.toString());

    get.setFollowRedirects(true);/* w w  w.  j  a  v  a  2 s .  c o m*/
    try {
        int status = client.executeMethod(get);
        if (status != HttpStatus.SC_OK) {
            throw new ConnectException(
                    "The http client has encountered a status code other than ok for the url: " + url
                            + " status: " + HttpStatus.getStatusText(status));
        }
        return get.getResponseBodyAsString();
    } finally {
        get.releaseConnection();
    }
}

From source file:com.utest.domain.service.util.FileUploadUtil.java

public static void uploadFile(final File targetFile, final String targetURL, final String targerFormFieldName_)
        throws Exception {
    final PostMethod filePost = new PostMethod(targetURL);
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    filePost.addRequestHeader("X-Atlassian-Token", "no-check");
    try {/*from w w w  .j av a 2  s.  co  m*/
        final FilePart fp = new FilePart(targerFormFieldName_, targetFile);
        fp.setTransferEncoding(null);
        final Part[] parts = { fp };
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        final HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        final int status = client.executeMethod(filePost);
        if (status == HttpStatus.SC_OK) {
            Logger.getLogger(FileUploadUtil.class)
                    .info("Upload complete, response=" + filePost.getResponseBodyAsString());
        } else {
            Logger.getLogger(FileUploadUtil.class)
                    .info("Upload failed, response=" + HttpStatus.getStatusText(status));
        }
    } catch (final Exception ex) {
        Logger.getLogger(FileUploadUtil.class)
                .error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage(), ex);
        throw ex;
    } finally {
        Logger.getLogger(FileUploadUtil.class).debug(new String(filePost.getResponseBody()));
        filePost.releaseConnection();
    }

}

From source file:com.temenos.interaction.media.odata.xml.error.InteractionExceptionHandler.java

protected String generateLogMessage(T exception, int code) {
    String message = new StringBuilder("HTTP ").append(code).append(" ").append(HttpStatus.getStatusText(code))
            .toString();//from  w w  w.  j a  v  a 2 s  .  c o m
    String stackTrace = getStackTraceAsString(exception);
    logger.error(message, stackTrace);
    return stackTrace;
}

From source file:com.bdaum.zoom.net.core.internal.Activator.java

public static String getStatusText(int responseCode) {
    String statusText = HttpStatus.getStatusText(responseCode);
    if (statusText == null)
        statusText = "Unknown cause";//$NON-NLS-1$
    return statusText + " (" + responseCode + ')'; //$NON-NLS-1$
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.publico.FileDownload.java

@Override
public ActionForward execute(final ActionMapping mapping, final ActionForm actionForm,
        final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String oid = request.getParameter("oid");
    final File file = FenixFramework.getDomainObject(oid);
    if (file == null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();/*from w  ww . java2  s .  com*/
    } else {
        final Person person = AccessControl.getPerson();
        if (!file.isPrivate() || file.isPersonAllowedToAccess(person)) {
            response.setContentType(file.getContentType());
            response.addHeader("Content-Disposition", "attachment; filename=" + file.getFilename());
            response.setContentLength(file.getSize().intValue());
            final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
            dos.write(file.getContents());
            dos.close();
        } else if (file.isPrivate() && person == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_UNAUTHORIZED));
            response.getWriter().close();
        } else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_FORBIDDEN));
            response.getWriter().close();
        }
    }
    return null;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.publico.rss.InformaRSSAction.java

@Override
public ActionForward execute(final ActionMapping mapping, final ActionForm form,
        final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String encoding = System.getProperty("file.encoding", Charset.defaultCharset().name());
    final ChannelIF channel = getRSSChannel(request);
    if (channel != null) {
        response.setContentType("text/xml; charset=" + encoding);
        final PrintWriter writer = response.getWriter();
        final ChannelExporterIF exporter = new RSS_2_0_Exporter(writer, encoding);
        exporter.write(channel);/* w  ww  .  j a  v a  2 s  . c  o m*/
        response.flushBuffer();
    } else {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_NOT_FOUND));
        response.getWriter().close();
    }
    return null;
}

From source file:com.alibaba.antx.config.resource.http.HttpResource.java

private void load() {
    if (content != null) {
        return;/* w ww.  jav a 2s  .  c  o m*/
    }

    GetMethod httpget = new GetMethod(getURI().toString());
    httpget.setDoAuthentication(true);
    InputStream stream = null;

    try {
        ResourceContext.get().setCurrentURI(getURI().getURI());

        ((HttpSession) getSession()).getClient().executeMethod(httpget);

        if (httpget.getStatusCode() != 200) {
            throw new ResourceNotFoundException(HttpStatus.getStatusText(httpget.getStatusCode()));
        }

        // ????????
        ResourceContext.get().getVisitedURIs().remove(new ResourceKey(new ResourceURI(getURI().getURI())));

        content = httpget.getResponseBody();
        charset = httpget.getResponseCharSet();

        Header contentTypeHeader = httpget.getResponseHeader("Content-Type");
        contentType = contentTypeHeader == null ? null : contentTypeHeader.getValue();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new ConfigException(e);
    } finally {
        ResourceContext.get().setCurrentURI(null);

        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }

        httpget.releaseConnection();
    }
}

From source file:it.geosolutions.figis.requester.HTTPUtils.java

/**
* Performs an HTTP GET on the given URL.
* <BR>Basic auth is used if both username and pw are not null.
*
* @param url The URL where to connect to.
* @param username Basic auth credential. No basic auth if null.
* @param pw Basic auth credential. No basic auth if null.
* @return The HTTP response as a String if the HTTP response code was 200 (OK).
* @throws MalformedURLException//w w w  .j  av  a 2  s  .  co m
*/
public static String get(String url, String username, String pw) throws MalformedURLException {
    GetMethod httpMethod = null;
    try {
        HttpClient client = new HttpClient();
        setAuth(client, url, username, pw);
        httpMethod = new GetMethod(url);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        int status = client.executeMethod(httpMethod);
        if (status == HttpStatus.SC_OK) {
            InputStream is = httpMethod.getResponseBodyAsStream();
            String response = IOUtils.toString(is);
            if (response.trim().length() == 0) // sometime gs rest fails
            {
                LOGGER.warn("ResponseBody is empty");

                return null;
            } else {
                return response;
            }
        } else {
            LOGGER.info("(" + status + ") " + HttpStatus.getStatusText(status) + " -- " + url);
        }
    } catch (ConnectException e) {
        LOGGER.error("Couldn't connect to [" + url + "]", e);
    } catch (IOException e) {
        LOGGER.error("Error talking to [" + url + "]", e);
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }

    return null;
}

From source file:it.geosolutions.geoserver.rest.HTTPUtils.java

/**
 * Performs an HTTP GET on the given URL. <BR>
 * Basic auth is used if both username and pw are not null.
 * //  w w  w. j a  va  2  s . c o m
 * @param url The URL where to connect to.
 * @param username Basic auth credential. No basic auth if null.
 * @param pw Basic auth credential. No basic auth if null.
 * @return The HTTP response as a String if the HTTP response code was 200
 *         (OK).
 * @throws MalformedURLException
 */
public static String get(String url, String username, String pw) {

    GetMethod httpMethod = null;
    HttpClient client = new HttpClient();
    HttpConnectionManager connectionManager = client.getHttpConnectionManager();
    try {
        setAuth(client, url, username, pw);
        httpMethod = new GetMethod(url);
        connectionManager.getParams().setConnectionTimeout(5000);
        int status = client.executeMethod(httpMethod);
        if (status == HttpStatus.SC_OK) {
            InputStream is = httpMethod.getResponseBodyAsStream();
            String response = IOUtils.toString(is);
            IOUtils.closeQuietly(is);
            if (response.trim().length() == 0) { // sometime gs rest fails
                LOGGER.warn("ResponseBody is empty");
                return null;
            } else {
                return response;
            }
        } else {
            LOGGER.info("(" + status + ") " + HttpStatus.getStatusText(status) + " -- " + url);
        }
    } catch (ConnectException e) {
        LOGGER.info("Couldn't connect to [" + url + "]");
    } catch (IOException e) {
        LOGGER.info("Error talking to [" + url + "]", e);
    } finally {
        if (httpMethod != null)
            httpMethod.releaseConnection();
        connectionManager.closeIdleConnections(0);
    }

    return null;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.commons.student.ViewStudentApplicationDA.java

public ActionForward downloadDocument(final ActionMapping mapping, final ActionForm form,
        final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final IndividualCandidacyDocumentFile document = getDomainObject(request, "documentOID");
    if (document != null && isAuthorized(document)) {
        response.setContentType(document.getContentType());
        response.addHeader("Content-Disposition", "attachment; filename=\"" + document.getFilename() + "\"");
        response.setContentLength(document.getSize().intValue());
        final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
        dos.write(document.getContent());
        dos.close();//www . jav a  2  s.c o  m
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();
    }
    return null;
}