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

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

Introduction

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

Prototype

int SC_INTERNAL_SERVER_ERROR

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

Click Source Link

Document

<tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.broadleafcommerce.core.web.api.BroadleafSpringRestExceptionMapper.java

@ExceptionHandler(Exception.class)
public @ResponseBody ErrorWrapper handleException(HttpServletRequest request, HttpServletResponse response,
        Exception ex) {/*from  w  w w  . j  a  va  2s. co m*/
    ErrorWrapper errorWrapper = (ErrorWrapper) context.getBean(ErrorWrapper.class.getName());
    Locale locale = null;
    BroadleafRequestContext requestContext = BroadleafRequestContext.getBroadleafRequestContext();
    if (requestContext != null) {
        locale = requestContext.getJavaLocale();
    }

    LOG.error("An error occured invoking a REST service", ex);
    if (locale == null) {
        locale = Locale.getDefault();
    }
    errorWrapper.setHttpStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    response.setStatus(resolveResponseStatusCode(ex, errorWrapper));
    ErrorMessageWrapper errorMessageWrapper = (ErrorMessageWrapper) context
            .getBean(ErrorMessageWrapper.class.getName());
    errorMessageWrapper.setMessageKey(resolveClientMessageKey(BroadleafWebServicesException.UNKNOWN_ERROR));
    errorMessageWrapper.setMessage(messageSource.getMessage(BroadleafWebServicesException.UNKNOWN_ERROR, null,
            BroadleafWebServicesException.UNKNOWN_ERROR, locale));
    errorWrapper.getMessages().add(errorMessageWrapper);
    return errorWrapper;
}

From source file:org.codehaus.mojo.delicious.DeliciousService.java

/**
 * Invokes the delicous service defined by the supplied url and query.
 * The username and password are supplied for http-auth basic authorisation.
 * According to the guidelines at http://del.icio.us/doc/api this method:
 * <ul>/*from w w  w  . jav  a2  s.  com*/
 * <li>waits 1 second before properly executing</li>
 * <li>bails out on receipt of a 503 error</li>
 * </ul>
 * For an ordinairy internal server error this method will try up to
 * a maximum number of times before bailing out.
 * @param category TODO
 * @param command
 * @param formFields
 * @throws IOException
 * @throws InterruptedException
 */
public void doService(String category, String command, HashMap formFields)
        throws IOException, InterruptedException {
    int tryCount = 0;
    boolean shouldTry = true;
    while (shouldTry) {
        tryCount++;
        try {
            doServiceImpl(category, command, formFields);
            shouldTry = false;
        } catch (RuntimeException e) {
            if (code == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                logger.warn("got an internal server error");
                if (tryCount == 3) {
                    logger.warn("giving up");
                    throw e;
                }
                logger.warn("will try again");
            } else {
                throw e;
            }
        }
    }
}

From source file:org.codehaus.mojo.delicious.DeliciousServiceTest.java

/**
 * Inserts an http connector that only reports internal server error.
 * @throws InterruptedException /*from w  ww  . j  a v  a  2  s  . c om*/
 * @throws IOException 
 *
 */
public void testInternalServerError() throws IOException, InterruptedException {
    DeliciousService service = new DeliciousService(new FailingConnection(-1));
    try {
        service.setUser("", "");
        service.addBookmark(new Bookmark(), Boolean.TRUE);
    } catch (RuntimeException e) {
        assertEquals(service.getCode(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.easyj.http.RESTHttpClient.java

/**
 * Executes the wrapped method after configuring the request headers and parameters.
 * Builds a {@code EasyRESTResponse} and returns it.
 *
 * @param uri URI of the resource to be requested
 * @return Wrapped response for the resource requested
 *//*from   ww  w .  j a v  a  2 s. co  m*/
protected RESTHttpClient execute(String uri) {
    status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    responseBody = null;

    setURI(uri);
    setMethodRequestHeaders();
    setMethodQueryString();
    setMethodParameters();

    if (method.getRequestHeader("Accept") == null) {
        method.addRequestHeader("Accept", "application/json");
    }

    try {
        status = client.executeMethod(method);

        if (status == HttpStatus.SC_OK) {
            responseBody = method.getResponseBodyAsString();
        }
    } catch (HttpException ex) {
        logger.error("HttpException: problem executing HTTP Request: [{}]", method, ex);
        exception = ex;
    } catch (IOException ex) {
        logger.error("IOException: problem executing HTTP Request or trying to read the Response: [{}]", method,
                ex);
        exception = ex;
    }
    return this;
}

From source file:org.eclipse.buckminster.jnlp.MaterializationUtils.java

/**
 * Checks HTTP response code and throws JNLPException if there is a problem
 * /* ww w.  ja v  a 2  s  .  c  o m*/
 * @param connection
 * @throws JNLPException
 * @throws IOException
 */
public static void checkConnection(int status, String originalURL) throws JNLPException {
    if (status != HttpStatus.SC_OK) {
        String errorCode;

        switch (status) {
        case HttpStatus.SC_FORBIDDEN:

            errorCode = ERROR_CODE_403_EXCEPTION;
            break;

        case HttpStatus.SC_NOT_FOUND:

            errorCode = ERROR_CODE_404_EXCEPTION;
            break;

        case HttpStatus.SC_INTERNAL_SERVER_ERROR:

            errorCode = ERROR_CODE_500_EXCEPTION;
            break;

        default:
            errorCode = ERROR_CODE_REMOTE_IO_EXCEPTION;
            break;
        }

        throw new JNLPException(Messages.cannot_read_materialization_specification, errorCode,
                BuckminsterException.fromMessage("%s - %s", originalURL, HttpStatus.getStatusText(status))); //$NON-NLS-1$
    }
}

From source file:org.eclipse.buckminster.jnlp.p2.MaterializationUtils.java

/**
 * Checks HTTP response code and throws JNLPException if there is a problem
 * /*from  ww  w .  j  a  v a 2 s.c  o m*/
 * @param connection
 * @throws JNLPException
 * @throws IOException
 */
public static void checkConnection(int status, String originalURL) throws JNLPException {
    if (status != HttpStatus.SC_OK) {
        String errorCode;

        switch (status) {
        case HttpStatus.SC_FORBIDDEN:

            errorCode = ERROR_CODE_403_EXCEPTION;
            break;

        case HttpStatus.SC_NOT_FOUND:

            errorCode = ERROR_CODE_404_EXCEPTION;
            break;

        case HttpStatus.SC_INTERNAL_SERVER_ERROR:

            errorCode = ERROR_CODE_500_EXCEPTION;
            break;

        default:
            errorCode = ERROR_CODE_REMOTE_IO_EXCEPTION;
            break;
        }

        throw new JNLPException("Cannot read materialization specification", errorCode,
                BuckminsterException.fromMessage("%s - %s", originalURL, HttpStatus.getStatusText(status)));
    }
}

From source file:org.eclipse.e4.opensocial.container.internal.browserHandlers.core.MakeXmlHttpRequestHandler.java

@Override
public Object handle(final Browser browser, final Object[] arguments) {
    /*/*from  w ww. j a v  a 2 s .  co  m*/
     * function('makeXmlHttpRequest', url, callback, method, contentType,
     * headers, postData)
     */
    try {
        // Retrieve arguments
        String url = (String) arguments[1];
        String callback = (String) arguments[2];
        String method = (String) arguments[3];
        String contentType = (String) arguments[4];
        // headers format :
        // headerName#headerValue\nheader2Name#header2Value
        String headers = (String) arguments[5];
        String postData = (String) arguments[6];

        HttpClient httpClient = new HttpClient();
        // Create GET or POST http method
        HttpMethod httpMethod = createMethod(url, method);

        // Add needed headers
        if (!"".equals(headers))
            addHeaders(httpMethod, headers);

        // Handle PostData if needed
        if (postData != null) {
            ((PostMethod) httpMethod).setRequestEntity(new StringRequestEntity(postData, "text/xml", "utf-8"));
        }

        int status = httpClient.executeMethod(httpMethod);

        String responseBodyAsString = retrieveResponseBody(httpMethod);

        // FIXME: at the moment, remove single quotes causing js
        // problems.
        responseBodyAsString = responseBodyAsString.replaceAll("'", " ");

        String responseScript = "\n";
        // Create and fill response object
        responseScript += "var response = new Object();\n";
        responseScript += "response.rc=" + status + ";\n";
        responseScript += "response.text='" + responseBodyAsString + "';\n";

        // Create the response's data property
        if ("DOM".equals(contentType)) {
            responseScript = createDOMResponseData(responseScript);
        } else if ("JSON".equals(contentType)) {
            responseScript = createJSONResponseData(responseBodyAsString, responseScript);
        } else if ("TEXT".equals(contentType)) {
            responseScript = createTEXTReponseData(responseScript);
        } else if ("FEED".equals(contentType)) {
            // FIXME : Not implemented for the moment.
            responseScript = createFEEDResponseData(responseScript);
        }

        // Instanciate and call callback
        responseScript += "(" + callback + ")(response);\n";

        final String script = responseScript;
        Display.getDefault().asyncExec(new Runnable() {
            public void run() {
                browser.execute(script);
            }
        });
        return status;
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return HttpStatus.SC_INTERNAL_SERVER_ERROR;
}

From source file:org.eclipse.e4.ui.internal.gadgets.opensocial.browserfunctions.MakeXmlHttpRequest.java

@Override
public Object function(Object[] arguments) {
    /*//from w w w . j  a v  a  2s .  c o m
     * e4_makeXmlHttpRequest : function(url, callback, method, contentType,
     * headers, postData)
     */
    try {
        // Retrieve arguments
        String url = (String) arguments[0];
        String callback = (String) arguments[1];
        String method = (String) arguments[2];
        String contentType = (String) arguments[3];
        // headers format :
        // headerName#headerValue\nheader2Name#header2Value
        String headers = (String) arguments[4];
        String postData = (String) arguments[5];

        HttpClient httpClient = new HttpClient();
        // Create GET or POST http method
        HttpMethod httpMethod = createMethod(url, method);

        // Add needed headers
        if (!"".equals(headers))
            addHeaders(httpMethod, headers);

        // Handle PostData if needed
        if (postData != null) {
            ((PostMethod) httpMethod).setRequestEntity(new StringRequestEntity(postData, "text/xml", "utf-8"));
        }

        int status = httpClient.executeMethod(httpMethod);

        String responseBodyAsString = retrieveResponseBody(httpMethod);

        // FIXME: at the moment, remove single quotes causing js
        // problems.
        responseBodyAsString = responseBodyAsString.replaceAll("'", " ");

        String responseScript = "\n";
        // Create and fill response object
        responseScript += "var response = new Object();\n";
        responseScript += "response.rc=" + status + ";\n";
        responseScript += "response.text='" + responseBodyAsString + "';\n";

        // Create the response's data property
        if ("DOM".equals(contentType)) {
            responseScript = createDOMResponseData(responseScript);
        } else if ("JSON".equals(contentType)) {
            responseScript = createJSONResponseData(responseBodyAsString, responseScript);
        } else if ("TEXT".equals(contentType)) {
            responseScript = createTEXTReponseData(responseScript);
        } else if ("FEED".equals(contentType)) {
            // FIXME : Not implemented for the moment.
            responseScript = createFEEDResponseData(responseScript);
        }

        // Instanciate and call callback
        responseScript += "var callback = " + callback + ";\n" + "callback(response);\n";

        final String script = responseScript;
        Display.getDefault().asyncExec(new Runnable() {
            public void run() {
                getBrowser().execute(script);
            }
        });
        return status;
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return HttpStatus.SC_INTERNAL_SERVER_ERROR;
}

From source file:org.fao.geonet.util.XslUtil.java

/**
 * Returns the HTTP code  or error message if error occurs during URL connection.
 *
 * @param url       The URL to ckeck.//  w w w .j  ava2  s  .  c  o m
 * @param tryNumber the number of remaining tries.
 */
public static String getUrlStatus(String url, int tryNumber) {
    if (tryNumber < 1) {
        // protect against redirect loops
        return "ERR_TOO_MANY_REDIRECTS";
    }
    HttpHead head = new HttpHead(url);
    GeonetHttpRequestFactory requestFactory = ApplicationContextHolder.get()
            .getBean(GeonetHttpRequestFactory.class);
    ClientHttpResponse response = null;
    try {
        response = requestFactory.execute(head, new Function<HttpClientBuilder, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable HttpClientBuilder originalConfig) {
                RequestConfig.Builder config = RequestConfig.custom().setConnectTimeout(1000)
                        .setConnectionRequestTimeout(3000).setSocketTimeout(5000);
                RequestConfig requestConfig = config.build();
                originalConfig.setDefaultRequestConfig(requestConfig);

                return null;
            }
        });
        //response = requestFactory.execute(head);
        if (response.getRawStatusCode() == HttpStatus.SC_BAD_REQUEST
                || response.getRawStatusCode() == HttpStatus.SC_METHOD_NOT_ALLOWED
                || response.getRawStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            // the website doesn't support HEAD requests. Need to do a GET...
            response.close();
            HttpGet get = new HttpGet(url);
            response = requestFactory.execute(get);
        }

        if (response.getStatusCode().is3xxRedirection() && response.getHeaders().containsKey("Location")) {
            // follow the redirects
            return getUrlStatus(response.getHeaders().getFirst("Location"), tryNumber - 1);
        }

        return String.valueOf(response.getRawStatusCode());
    } catch (IOException e) {
        Log.error(Geonet.GEONETWORK, "IOException validating  " + url + " URL. " + e.getMessage(), e);
        return e.getMessage();
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

From source file:org.fusesource.restygwt.server.FailingConnectionServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    log.fine("GET: failingMODE");

    log.fine("respond code: " + (HttpStatus.SC_INTERNAL_SERVER_ERROR + currentNumberOfServerFailures)
            + " with purpose");
    response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR + currentNumberOfServerFailures++);
}