Example usage for javax.servlet.http HttpServletResponse sendError

List of usage examples for javax.servlet.http HttpServletResponse sendError

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse sendError.

Prototype

public void sendError(int sc, String msg) throws IOException;

Source Link

Document

Sends an error response to the client using the specified status and clears the buffer.

Usage

From source file:nl.dtls.fairdatapoint.api.controller.utils.HttpHeadersUtils.java

/**
 * Set response header for the internal server errors
 * /*from   w w w .j  ava 2 s .  c  om*/
 * @param response  Http response
 * @param ex    Server exception
 * @return returns null (as a response body)
 */
public static String set500ResponseHeaders(HttpServletResponse response, Exception ex) {
    String errorMessage = ("Internal server error; Error message : " + ex.getMessage());
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    try {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage);
    } catch (IOException ex1) {
        LOGGER.warn(
                "Error setting error message for internal server " + "error; The error : " + ex1.getMessage());
    }
    response.setContentType(MediaType.TEXT_PLAIN_VALUE);
    return null;
}

From source file:org.trustedanalytics.utils.errorhandling.ErrorLogger.java

public static void logAndSendErrorResponse(Logger logger, HttpServletResponse response, HttpStatus status,
        String reason, Exception ex) throws IOException {
    long errorId = ErrorIdGenerator.getNewId();
    String errorMessage = ErrorFormatter.formatErrorMessage(reason, errorId);

    logger.error(errorMessage, ex);/*from   ww w  .jav a  2s  .co  m*/
    response.sendError(status.value(), errorMessage);
}

From source file:org.trustedanalytics.user.common.WebErrorHandlers.java

private static void logAndSendErrorResponse(HttpServletResponse response, HttpStatus status, String reason,
        Exception ex) throws IOException {
    long errorId = generateErrorId();
    String errorMessage = ErrorFormatter.formatErrorMessage(reason, errorId);

    LOGGER.error(errorMessage, ex);//from w w w.ja v a  2s . co  m
    response.sendError(status.value(), errorMessage);
}

From source file:com.amazonaws.services.dynamodbv2.replication.server.ReplicationGroupHandler.java

private static void sendError(HttpServletResponse response, int sc, String msg) {
    try {/*from w w  w.j  ava  2  s  .co  m*/
        response.sendError(sc, msg);
    } catch (IOException e) {
        LOGGER.error("Error writing error response", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.OAuthAction.java

private static ActionForward serverError(HttpServletResponse response, Exception e) {
    try {/*ww  w  .  j  av a 2  s  .  c  om*/
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Something went wrong. Please contact support team.");
    } catch (IOException e1) {
        logger.error(e1.getMessage(), e1);
    }
    logger.error(e.getMessage(), e);
    return null;
}

From source file:org.openmrs.module.metadatasharing.web.controller.PublishController.java

private static void sendErrorResponseAfterDelay(HttpServletResponse response, int httpCode, String errorMessage)
        throws IOException {

    try {/*from  ww  w.  j  a va2 s  .  com*/
        //delay in case of a password brute force
        Thread.sleep(5000);
    } catch (InterruptedException ie) {
    }

    response.sendError(httpCode, errorMessage);
}

From source file:csiro.pidsvc.helper.Http.java

/**
 * Return specific HTTP response code and add a message to HTTP header.
 * //from  w w  w.  ja  v  a2 s . c  o  m
 * @param response HttpServletResponse object.
 * @param httpResponseCode HTTP response code.
 * @param exception Exception object.
 */
public static void returnErrorCode(HttpServletResponse response, int httpResponseCode, Exception exception) {
    try {
        String message = (exception.getMessage() == null ? "" : exception.getMessage())
                + (exception.getCause() == null ? "" : "\n" + exception.getCause().getMessage());

        StringWriter sw = new StringWriter();
        exception.printStackTrace(new PrintWriter(sw));

        response.addHeader(PID_SERVICE_ERROR_HTTP_HEADER, message);
        response.addHeader(PID_SERVICE_MSG_HTTP_HEADER, sw.toString());
        response.sendError(httpResponseCode, exception.getMessage());
    } catch (IOException e) {
        _logger.debug(e);
    }
}

From source file:com.zimbra.cs.servlet.util.AuthUtil.java

public static AuthToken getAuthTokenFromHttpReq(HttpServletRequest req, HttpServletResponse resp,
        boolean isAdminReq, boolean doNotSendHttpError) throws IOException {
    AuthToken authToken = null;//from  w  w w.j  a va2  s  .  com
    try {
        authToken = AuthProvider.getAuthToken(req, isAdminReq);
        if (authToken == null) {
            if (!doNotSendHttpError)
                resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "no authtoken cookie");
            return null;
        }

        if (authToken.isExpired() || !authToken.isRegistered()) {
            if (!doNotSendHttpError)
                resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "authtoken expired");
            return null;
        }
        return authToken;
    } catch (AuthTokenException e) {
        if (!doNotSendHttpError)
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "unable to parse authtoken");
        return null;
    }
}

From source file:com.zimbra.cs.servlet.ZimbraServlet.java

public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, Server server,
        String uri, AuthToken authToken) throws IOException, ServiceException {
    if (server == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "cannot find remote server");
        return;//from w  w  w .  j  a v a  2 s. c o m
    }
    HttpMethod method;
    String url = getProxyUrl(req, server, uri);
    mLog.debug("Proxy URL = %s", url);
    if (req.getMethod().equalsIgnoreCase("GET")) {
        method = new GetMethod(url.toString());
    } else if (req.getMethod().equalsIgnoreCase("POST") || req.getMethod().equalsIgnoreCase("PUT")) {
        PostMethod post = new PostMethod(url.toString());
        post.setRequestEntity(new InputStreamRequestEntity(req.getInputStream()));
        method = post;
    } else {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "cannot proxy method: " + req.getMethod());
        return;
    }
    HttpState state = new HttpState();
    String hostname = method.getURI().getHost();
    if (authToken != null) {
        authToken.encode(state, false, hostname);
    }
    try {
        proxyServletRequest(req, resp, method, state);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.zimbra.cs.service.AutoDiscoverServlet.java

private static void sendError(HttpServletResponse resp, int errCode, String reason) throws IOException {
    log.warn("HTTP/1.1 " + errCode + " " + reason);
    resp.sendError(errCode, reason);
}