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:org.fejoa.server.Portal.java

@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest,
        HttpServletResponse response) throws IOException, ServletException {
    response.setContentType("text/plain;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    request.setHandled(true);/*from  ww  w  . j a  v  a2s . co m*/

    final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(
            System.getProperty("java.io.tmpdir"));
    if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) {
        request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
    }

    Session session = new Session(baseDir, httpServletRequest.getSession());
    ResponseHandler responseHandler = new ResponseHandler(response);

    Part messagePart = request.getPart(HTMLRequest.MESSAGE_KEY);
    Part data = request.getPart(HTMLRequest.DATA_KEY);

    if (messagePart == null) {
        responseHandler.setResponseHeader("empty request!");
        responseHandler.finish();
        return;
    }

    StringWriter stringWriter = new StringWriter();
    StreamHelper.copy(messagePart.getInputStream(), stringWriter);

    String error = handleJson(responseHandler, stringWriter.toString(),
            (data != null) ? data.getInputStream() : null, session);

    if (!responseHandler.isHandled() || error != null)
        responseHandler.setResponseHeader(error);

    responseHandler.finish();
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(AuthenticationException.class)
@ResponseBody// w ww .j a  va2s  . co m
protected Map handleAuthenticationException(AuthenticationException ex, WebRequest request,
        HttpServletResponse response) {
    // Set response status
    response.setStatus(401);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Add error message
    errors.put("error", ex.getMessage());
    // Return error
    return errors;
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseBody/*from  w w  w .  j ava2  s  .c o  m*/
protected Map handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request,
        HttpServletResponse response) {
    // Set response status
    response.setStatus(404);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Add error message
    errors.put("error", ex.getMessage());
    // Return error
    return errors;
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(OperationFailedException.class)
@ResponseBody/*from  www .ja  v a2  s.  c om*/
protected Map handleOperationFailedException(OperationFailedException ex, WebRequest request,
        HttpServletResponse response) {
    // Set response status
    response.setStatus(500);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Add error message
    errors.put("error", ex.getMessage());
    // Return error
    return errors;
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
@ResponseBody/* w  ww.ja v  a 2s . c o m*/
protected Map handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex,
        WebRequest request, HttpServletResponse response) {
    // Set response status
    response.setStatus(415);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Add error message
    errors.put("error", ex.getMessage());
    // Return error
    return errors;
}

From source file:org.magnum.mobilecloud.video.Assign2Controller.java

@RequestMapping(value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}", method = RequestMethod.GET)
public @ResponseBody Video getVideoById(@PathVariable("id") long id, HttpServletResponse response) {
    Video v = null;//from w  w  w . j a  v  a  2s.c o  m
    if (videos.findOne(id) == null) {
        response.setStatus(404);
    } else {
        v = videos.findOne(id);
    }
    return v;
}

From source file:bixo.fetcher.RedirectResponseHandler.java

@Override
public void handle(String pathInContext, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws HttpException, IOException {
    if (pathInContext.equalsIgnoreCase(_originalPath)) {
        response.sendRedirect(_redirectUrl);
    } else if (_redirectUrl.contains(pathInContext)) {
        response.setStatus(HttpStatus.SC_OK);
        response.setContentType("text/plain");

        String content = "redirected content";
        response.setContentLength(content.length());
        response.getOutputStream().write(content.getBytes());
    } else {/*from  w  w  w .  j  a v  a2s .co m*/
        response.setStatus(HttpStatus.SC_OK);
        response.setContentType("text/plain");

        String content = "other content";
        response.setContentLength(content.length());
        response.getOutputStream().write(content.getBytes());
    }
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody/*from   ww  w.  j av  a2  s  .  co m*/
protected Map handleHttpMessageNotReadableException(HttpMessageNotReadableException ex, WebRequest request,
        HttpServletResponse response) {
    // Set response status
    response.setStatus(400);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Get message
    String message = messageSource.getMessage("rest.message.not.readable", null, null);
    // Add error message
    errors.put("error", message);
    // Return error
    return errors;
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(TypeMismatchException.class)
@ResponseBody/*from www . j  a v  a 2 s.co m*/
protected Map handleTypeMismatchException(TypeMismatchException ex, WebRequest request,
        HttpServletResponse response) {
    // Set response status
    response.setStatus(400);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Get message
    String message = messageSource.getMessage("rest.invalid.resource.id", null, null);
    // Add error message
    errors.put("error", message);
    // Return error
    return errors;
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
@ResponseBody/*w  w  w.  j  av  a  2 s.c  o  m*/
protected Map handleNoSuchRequestHandlingMethodException(NoSuchRequestHandlingMethodException ex,
        WebRequest request, HttpServletResponse response) {
    // Set response status
    response.setStatus(404);
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Get message
    String message = messageSource.getMessage("rest.invalid.resource.url", null, null);
    // Add error message
    errors.put("error", message);
    // Return error
    return errors;
}