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) throws IOException;

Source Link

Document

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

Usage

From source file:de.iteratec.iteraplan.presentation.ajax.DojoUtils.java

/**
 * Sets the response status to bad request
 * //w  w  w .j  a  v a 2 s. co m
 * @param response
 */
public static void doBadRequestResponse(HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}

From source file:de.iteratec.iteraplan.presentation.ajax.DojoUtils.java

/**
 * Sets the response status to bad request
 * //w ww .  j  ava2s.c  om
 * @param response
 */
public static void doErrorResponse(HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}

From source file:net.sf.appstatus.web.pages.Resources.java

public static void doGet(StatusWebHandler webHandler, HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    String location = null;/*from  ww w  . j  a  va 2  s  .  co m*/
    String id = req.getParameter("resource");
    if (id == null) {
        id = req.getParameter("icon");
    }

    if (resources.containsKey(id)) {
        resp.setContentType(resources.get(id).getMimeType());
        location = resources.get(id).getLocation();
        InputStream is = Resources.class.getResourceAsStream(location);
        IOUtils.copy(is, resp.getOutputStream());
    } else {
        resp.sendError(404);
    }

}

From source file:com.ericsson.eif.hansoft.utils.HttpUtils.java

/**
 * @param response/*from w w  w  .  jav  a2 s.  c om*/
 * @param e - unauthorized exception
 * @throws IOException
 * @throws ServletException
 */
public static void sendUnauthorizedResponse(HttpServletResponse response, UnauthorizedException e)
        throws IOException, ServletException {
    if (e instanceof HansoftOAuthException) {
        OAuthServlet.handleException(response, e, HansoftManager.REALM);
    } else {
        // Accept basic access or OAuth authentication.
        response.addHeader(WWW_AUTHENTICATE_HEADER, OAUTH_AUTHENTICATION_CHALLENGE);
        response.addHeader(WWW_AUTHENTICATE_HEADER, BASIC_AUTHENTICATION_CHALLENGE);
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
}

From source file:com.betfair.tornjak.monitor.overlay.AuthUtils.java

/**
 * Returns null if user is not authenticated or authorised, otherwise returns Auth object.
 * /*from  w  w w .j ava 2  s  .co m*/
 */
public static Auth checkAuthorised(final HttpServletRequest request, HttpServletResponse response,
        ServletContext servletContext) throws IOException {

    RolePerms rolePerms = getOrCreateRolePerms(servletContext);

    Auth auth = new Auth(new Auth.Validator() {

        @Override
        public boolean isUserInRole(String role) {
            return request.isUserInRole(role);
        }

        @Override
        public boolean isAuthenticated() {
            return request.getUserPrincipal() != null;
        }
    }, rolePerms);

    switch (auth.check()) {
    case UNAUTHORISED:
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        return null;
    case FORBIDDEN:
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return null;
    default:
        return auth;
    }
}

From source file:com.googlecode.jsonplugin.JSONUtil.java

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

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

    if (TextUtils.stringSet(serializationParams.getWrapSuffix()))
        stringBuilder.append(serializationParams.getWrapSuffix());

    String json = stringBuilder.toString();

    if (log.isDebugEnabled()) {
        log.debug("[JSON]" + json);
    }/*from   w  w  w.jav  a  2 s.  c o 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
    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());
            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.belajar_filter.config.FilterBeanConfig.java

public static Filter myFilter() {
    Filter filter = new Filter() {

        private ServletContext context;

        @Override//from   ww w  .jav  a 2  s. c om
        public void init(FilterConfig filterConfig) throws ServletException {
            log.debug("initiate general filter config");
            this.context = filterConfig.getServletContext();
            this.context.log("AuthenticationFilter initialized");
        }

        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
                throws IOException, ServletException {
            log.debug("execute do filter ... ");
            HttpServletResponse response = (HttpServletResponse) res;
            HttpServletRequest request = (HttpServletRequest) req;

            String getParam = request.getParameter("name");
            String urlRequest = request.getRequestURI();
            log.debug("intercept url request : " + urlRequest);
            log.debug("intercept param : " + getParam);

            if ("aji".equals(getParam)) {
                log.debug("is aji");
                fc.doFilter(req, res);
            } else {
                log.debug("is not aji");
                response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
            }

        }

        @Override
        public void destroy() {

        }
    };

    return filter;
}

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 va  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: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);
    }// w w w. j av a2  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
    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.concursive.connect.web.webdav.WebdavManager.java

public static void askForAuthentication(HttpServletResponse res) throws Exception {
    String nonce = DefaultServlet.generateNonce();
    // determine the 'opaque' value which should be returned as-is by the client
    String opaque = DefaultServlet.generateOpaque();
    res.setHeader("WWW-Authenticate", "Digest realm=\"" + WebdavServlet.USER_REALM + "\", " + "nonce=\"" + nonce
            + "\", " + "opaque=\"" + opaque + "\"");
    res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}