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:com.runwaysdk.controller.ErrorUtility.java

public static void prepareAjaxThrowable(Throwable t, HttpServletResponse resp) throws IOException {
    while (t instanceof InvocationTargetException) {
        t = t.getCause();/*w ww .  j a  v  a  2s  .  c o m*/
    }

    if (t instanceof ProblemExceptionDTO) {
        JSONProblemExceptionDTO jsonE = new JSONProblemExceptionDTO((ProblemExceptionDTO) t);
        resp.setStatus(500);
        resp.getWriter().print(jsonE.getJSON());
    } else {
        JSONRunwayExceptionDTO jsonE = new JSONRunwayExceptionDTO(t);
        resp.setStatus(500);
        resp.getWriter().print(jsonE.getJSON());
    }
}

From source file:com.erudika.para.rest.RestUtils.java

/**
 * A generic JSON response returning an object. Status code is always {@code 200}.
 * @param response the response to write to
 * @param obj an object/*ww w.j  a v  a 2 s  . com*/
 */
public static void returnObjectResponse(HttpServletResponse response, Object obj) {
    if (response == null) {
        return;
    }
    PrintWriter out = null;
    try {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType(MediaType.APPLICATION_JSON);
        out = response.getWriter();
        ParaObjectUtils.getJsonWriter().writeValue(out, obj);
    } catch (Exception ex) {
        logger.error(null, ex);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.erudika.para.rest.RestUtils.java

/**
 * A generic JSON response handler//w w w.j  a v a2  s .  co m
 * @param response the response to write to
 * @param status status code
 * @param message error message
 */
public static void returnStatusResponse(HttpServletResponse response, int status, String message) {
    if (response == null) {
        return;
    }
    PrintWriter out = null;
    try {
        response.setStatus(status);
        response.setContentType(MediaType.APPLICATION_JSON);
        out = response.getWriter();
        ParaObjectUtils.getJsonWriter().writeValue(out,
                getStatusResponse(Response.Status.fromStatusCode(status), message).getEntity());
    } catch (Exception ex) {
        logger.error(null, ex);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

private static void textError(final ActiveCall call, final int status, final String message)
        throws IOException {
    final HttpServletResponse r = call.httpResponse;
    r.setStatus(status);
    r.setContentType("text/plain; charset=" + ENC);

    final Writer w = new OutputStreamWriter(r.getOutputStream(), ENC);
    try {//from ww  w. j a  va  2 s  .  c o  m
        w.write(message);
    } finally {
        w.close();
    }
}

From source file:com.xqdev.jam.MLJAM.java

private static void sendNoResponse(HttpServletResponse res) {
    res.setStatus(HttpServletResponse.SC_NO_CONTENT);
}

From source file:com.wxxr.nirvana.json.JSONUtil.java

public static void writeJSONToResponse(SerializationParams serializationParams) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    if (StringUtils.isNotBlank(serializationParams.getSerializedJSON()))
        stringBuilder.append(serializationParams.getSerializedJSON());

    if (StringUtils.isNotBlank(serializationParams.getWrapPrefix()))
        stringBuilder.insert(0, serializationParams.getWrapPrefix());
    else if (serializationParams.isWrapWithComments()) {
        stringBuilder.insert(0, "/* ");
        stringBuilder.append(" */");
    } else if (serializationParams.isPrefix())
        stringBuilder.insert(0, "{}&& ");

    if (StringUtils.isNotBlank(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (LOG.isDebugEnabled()) {
        LOG.debug("[JSON]" + json);
    }/*from   w  w  w  . j a  v a 2  s .co m*/

    HttpServletResponse response = serializationParams.getResponse();

    // status or error code
    if (serializationParams.getStatusCode() > 0)
        response.setStatus(serializationParams.getStatusCode());
    else if (serializationParams.getErrorCode() > 0)
        response.sendError(serializationParams.getErrorCode());

    // content type
    response.setContentType(
            serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding());

    if (serializationParams.isNoCache()) {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("Pragma", "No-cache");
    }

    if (serializationParams.isGzip()) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes(serializationParams.getEncoding()));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *//*from   w ww. ja  v a2  s  .  co  m*/
public static boolean sendPDFInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        File reportFile = new File(reportFilePath);
        String mimeType = new MimetypesFileTypeMap().getContentType(reportFile);
        log.debug("Sending mimetype: " + mimeType);
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "attachment;filename=" + reportFile.getName());
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setHeader("apprisk", reportStatus.name());

        FileInputStream inputStream = new FileInputStream(reportFile); //read the file

        try {
            int c;
            while ((c = inputStream.read()) != -1) {
                response.getWriter().write(c);
            }
        } finally {
            if (inputStream != null)
                inputStream.close();
            response.getWriter().close();
        }

        //         PrintWriter out = response.getWriter();
        //         out.println(reportText);
        //         out.flush();
        //         out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}

From source file:com.struts2ext.json.JSONUtil.java

public static void writeJSONToResponse(SerializationParams serializationParams) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    if (StringUtils.isNotBlank(serializationParams.getSerializedJSON()))
        stringBuilder.append(serializationParams.getSerializedJSON());

    if (StringUtils.isNotBlank(serializationParams.getWrapPrefix()))
        stringBuilder.insert(0, serializationParams.getWrapPrefix());
    else if (serializationParams.isWrapWithComments()) {
        stringBuilder.insert(0, "/* ");
        stringBuilder.append(" */");
    } else if (serializationParams.isPrefix())
        stringBuilder.insert(0, "{}&& ");

    if (StringUtils.isNotBlank(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (LOG.isDebugEnabled()) {
        LOG.debug("[JSON]" + json);
    }/*from  w ww.  j av  a 2s.com*/

    HttpServletResponse response = serializationParams.getResponse();

    // status or error code
    if (serializationParams.getStatusCode() > 0)
        response.setStatus(serializationParams.getStatusCode());
    else if (serializationParams.getErrorCode() > 0)
        response.sendError(serializationParams.getErrorCode());

    // content type
    if (serializationParams.isSmd())
        response.setContentType("application/json-rpc;charset=" + serializationParams.getEncoding());
    else
        response.setContentType(
                serializationParams.getContentType() + ";charset=" + serializationParams.getEncoding());

    if (serializationParams.isNoCache()) {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("Pragma", "No-cache");
    }

    if (serializationParams.isGzip()) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes(serializationParams.getEncoding()));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(serializationParams.getEncoding()).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:com.runwaysdk.controller.ErrorUtility.java

public static boolean prepareThrowable(Throwable t, HttpServletRequest req, OutputStream out,
        HttpServletResponse resp, Boolean isAsynchronus, boolean ignoreNotifications) throws IOException {
    t = ErrorUtility.filterServletException(t);

    if (isAsynchronus) {
        if (t instanceof ProblemExceptionDTO) {
            JSONProblemExceptionDTO jsonE = new JSONProblemExceptionDTO((ProblemExceptionDTO) t);
            resp.setStatus(500);
            out.write(jsonE.getJSON().getBytes());
        } else {// ww w .j a va  2  s .  c  o  m
            JSONRunwayExceptionDTO jsonE = new JSONRunwayExceptionDTO(t);
            resp.setStatus(500);
            out.write(jsonE.getJSON().getBytes());
        }

        return true;
    } else {
        if (t instanceof AttributeReadPermissionExceptionDTO) {
            throw (AttributeReadPermissionExceptionDTO) t;
        } else if (t instanceof ReadTypePermissionExceptionDTO) {
            throw (ReadTypePermissionExceptionDTO) t;
        } else if (t instanceof ProblemExceptionDTO) {
            ErrorUtility.prepareProblems((ProblemExceptionDTO) t, req, ignoreNotifications);
        } else {
            ErrorUtility.prepareThrowable(t, req);
        }
    }

    return false;
}

From source file:com.runwaysdk.controller.ErrorUtility.java

/**
 * Handles errors that are generated from a request sent asynchronously from the RunwayControllerForm js widget.
 * // ww w.  ja v a2s  . com
 * @param t
 * @param req
 * @param resp
 * @throws IOException
 * @returns boolean Whether or not a redirect is required. A redirect is required if and only if the error is inlined.
 */
public static boolean handleFormError(Throwable t, HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    t = ErrorUtility.filterServletException(t);

    if (t instanceof ProblemExceptionDTO) {
        ErrorUtility.prepareProblems((ProblemExceptionDTO) t, req, false);
        return true;
    } else {
        JSONRunwayExceptionDTO jsonE = new JSONRunwayExceptionDTO(t);
        resp.setStatus(500);
        resp.getWriter().print(jsonE.getJSON());
        return false;
    }
}