Example usage for javax.servlet.http HttpServletResponse setStatus

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

Introduction

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

Prototype

public void setStatus(int sc);

Source Link

Document

Sets the status code for this response.

Usage

From source file:dtu.ds.warnme.ws.rest.json.AbstractRestWS.java

@ExceptionHandler(ServiceException.class)
@ResponseBody/*from   w  w  w  .  j  a v a  2  s . c om*/
String handleServiceExceptions(Exception ex, HttpServletRequest request, HttpServletResponse response) {
    log.info(ex.getMessage(), ex);
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    String responseBody = getMessage(ex, request.getLocale());
    RestUtils.decorateResponseHeaderWithMD5(response, responseBody);
    RestUtils.decorateResponseHeaderForJsonContentType(response);
    return responseBody;
}

From source file:net.duckling.ddl.web.controller.BaseController.java

/**
 * ???/*w  ww  .  j  a  v  a 2  s.  co  m*/
 * @param request
 * @param response
 */
protected void sendForbidden(HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}

From source file:com.ge.predix.acs.commons.web.RestErrorHandler.java

/**
 * Handles the given exception and generates a response with error code and message description.
 *
 * @param e//from   w  w  w .  j  a  v  a2  s.  c o m
 *            Given exception
 * @param request
 *            The http request
 * @param response
 *            The http response
 * @return The model view with the error response
 */
@SuppressWarnings("nls")
public ModelAndView createApiErrorResponse(final Exception e, final HttpServletRequest request,
        final HttpServletResponse response) {
    LOGGER.error(e.getMessage(), e);
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    RestApiErrorResponse restApiErrorResponse = new RestApiErrorResponse();

    if (e instanceof RestApiException) {
        RestApiException restEx = (RestApiException) e;
        response.setStatus(restEx.getHttpStatusCode().value());
        restApiErrorResponse.setErrorMessage(restEx.getMessage());
        restApiErrorResponse.setErrorCode(restEx.getAppErrorCode());
    } else if (IllegalArgumentException.class.isAssignableFrom(e.getClass())) {
        // Illegal argument exceptions mapped to 400 errors by default
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        restApiErrorResponse.setErrorMessage(e.getMessage());
    } else if (UntrustedIssuerException.class.isAssignableFrom(e.getClass())
            || SecurityException.class.isAssignableFrom(e.getClass())) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        restApiErrorResponse.setErrorMessage(e.getMessage());
    } else if (HttpMessageNotReadableException.class.isAssignableFrom(e.getClass())) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        restApiErrorResponse.setErrorMessage("Malformed JSON syntax. " + e.getLocalizedMessage());
    }

    return new ModelAndView(new MappingJackson2JsonView(), "ErrorDetails", restApiErrorResponse);
}

From source file:de.knightsoftnet.validators.server.security.AuthFailureHandler.java

@Override
public void onAuthenticationFailure(final HttpServletRequest prequest, final HttpServletResponse presponse,
        final AuthenticationException pexception) throws IOException, ServletException {
    presponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    final PrintWriter writer = presponse.getWriter();
    writer.write(pexception.getMessage());
    writer.flush();//from w  w  w.  j  a v  a2s .c o  m
}

From source file:org.openxdata.server.servlet.StudyExportServlet.java

private void setBadRequest(HttpServletResponse response, String message) throws IOException {
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    response.setContentType("text/plain");
    response.getOutputStream().println(message);
}

From source file:gsn.http.OutputStructureHandler.java

public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    String vsName = request.getParameter("name");
    VSensorConfig sensorConfig = Mappings.getVSensorConfig(vsName);
    logger.info(new StringBuilder().append("Structure request for *").append(vsName).append("* received.")
            .toString());//from ww  w .j  ava  2 s .  c om
    StringBuilder sb = new StringBuilder("<virtual-sensor name=\"").append(vsName).append("\">\n");
    sb.append(
            "<field name=\"time\" type=\"string\" description=\"The timestamp associated with the stream element\" unit=\"\"/>\n");
    for (DataField df : sensorConfig.getOutputStructure()) {
        sb.append("<field name=\"").append(df.getName()).append("\" ").append("type=\"").append(df.getType())
                .append("\" ").append("description=\"")
                .append(StringEscapeUtils.escapeXml(df.getDescription()));
        if (df.getUnit() != null && df.getUnit().trim().length() != 0)
            sb.append("\" ").append("unit=\"").append(df.getUnit());
        else
            sb.append("\" ").append("unit=\"").append("");
        sb.append("\" />\n");
    }
    sb.append("</virtual-sensor>");
    response.setHeader("Cache-Control", "no-store");
    response.setDateHeader("Expires", 0);
    response.setHeader("Pragma", "no-cache");
    response.getWriter().write(sb.toString());
}

From source file:org.smigo.user.password.PasswordController.java

@RequestMapping(value = "/change-password", method = RequestMethod.POST)
@ResponseBody/*w w  w . ja v a 2s .  co m*/
public List<ObjectError> changePassword(@RequestBody @Valid Password password, BindingResult result,
        @AuthenticationPrincipal AuthenticatedUser user, HttpServletResponse response) {
    if (result.hasErrors()) {
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        return result.getAllErrors();
    }
    userHandler.updatePassword(user.getId(), password.getNewPassword());
    return Collections.emptyList();
}

From source file:org.mitre.openid.connect.view.ExceptionAsJSONView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest requesr,
        HttpServletResponse response) {

    response.setContentType("application/json");

    response.setStatus(HttpStatus.BAD_REQUEST.value());

    final JsonObject jsonObject = new JsonObject();

    Object ex = model.get("exception");

    jsonObject.addProperty("error", ex.getClass().getName());
    jsonObject.addProperty("error_description", ((Exception) ex).getMessage());

    try {/*from w  w  w .j a va 2s .  c o m*/

        response.getWriter().write(jsonObject.toString());

    } catch (IOException e) {

        logger.error("IOException in ExceptionAsJSONView.java: ", e);

    }
}

From source file:be.dnsbelgium.rdap.DomainController.java

@ExceptionHandler(value = RDAPError.NotAuthoritative.class)
@ResponseBody/*from www .  ja  v a 2s.c  o m*/
protected RDAPError handleResourceNotFoundException(RDAPError.NotAuthoritative error,
        HttpServletResponse response) throws UnsupportedEncodingException {
    response.setStatus(error.getErrorCode());
    String location = baseRedirectURL + "/domain/" + URLEncoder.encode(error.getDomainName(), "UTF-8");
    response.addHeader(Controllers.LOCATION_HEADER, location);
    return error;
}

From source file:com.hp.autonomy.frontend.find.idol.web.IdolGlobalExceptionHandler.java

@ExceptionHandler(AciErrorException.class)
@ResponseBody/* w w  w  .  ja  v a 2 s.c  o  m*/
public ErrorResponse authenticationFailedHandler(final AciErrorException exception,
        final HttpServletResponse response) {
    if (SECURITY_INFO_TOKEN_EXPIRED_ID.equals(exception.getErrorId())) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return new ErrorResponse("Security Info has expired");
    }

    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return handler(exception);
}