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:be.bittich.quote.security.CustomAccessDeniedHandler.java

@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {

    String respJSON = jsonifyMap(ImmutableMap.of("error", "Access forbidden."));
    response.setStatus(403);
    response.setCharacterEncoding(ENCODING_UTF8);
    response.getWriter().printf(respJSON).flush();
    response.getWriter().close();/*from   w  ww .j a v a2 s  . c o  m*/

}

From source file:com.iflytek.edu.cloud.frame.spring.OAuth2AuthenticationEntryPointExt.java

public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    if (authException.getCause() instanceof InvalidTokenException) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        MainError mainError = MainErrors.getError(MainErrorType.INVALID_ACCESS_TOKEN,
                (Locale) request.getAttribute("locale"));
        messageConverter.convertData(request, response, mainError);
    } else {//from   w  w w  .j  a va  2 s . co m
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        doHandle(request, response, authException);
    }
}

From source file:com.meltmedia.cadmium.servlets.AbstractSecureRedirectStrategy.java

/**
 * Sends a moved perminately redirect to the secure form of the request URL.
 * //from  w w  w  . j  a v a  2s .c  om
 * @request the request to make secure.
 * @response the response for the request.
 */
@Override
public void makeSecure(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", secureUrl(request, response));
    response.getOutputStream().flush();
    response.getOutputStream().close();
}

From source file:com.meltmedia.cadmium.servlets.AbstractSecureRedirectStrategy.java

/**
 * Sends a moved perminately redirect to the insecure form of the request URL.
 * //from w w w  .  j a va 2  s  .  c  o  m
 * @request the request to make secure.
 * @response the response for the request.
 */
@Override
public void makeInsecure(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", insecureUrl(request, response));
    response.getOutputStream().flush();
    response.getOutputStream().close();
}

From source file:com.aipo.social.core.oauth2.AipoOAuth2Servlet.java

/**
 * Sends an OAuth 2.0 response based on an OAuth2NormalizedResponse object.
 *
 * @param servletResp//w ww  .  j a  va2  s  . c o  m
 *          is the servlet's response object
 * @param normalizedResp
 *          maintains the headers and body fields to respond with
 */
private void sendOAuth2Response(HttpServletResponse servletResp, OAuth2NormalizedResponse normalizedResp) {
    // set status
    servletResp.setStatus(normalizedResp.getStatus());

    // set body parameters
    Map<String, String> respParams = normalizedResp.getResponseParameters();
    if (normalizedResp.isBodyReturned() && respParams != null) {
        PrintWriter out = null;
        try {
            servletResp.setHeader("Content-Type", "application/json");
            out = servletResp.getWriter();
            out.println(new JSONObject(respParams).toString());
            out.flush();
        } catch (IOException e) {
            LOG.logp(Level.WARNING, classname, "getBodyAsString", MessageKeys.INVALID_OAUTH, e);
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    // set headers
    Map<String, String> headers = normalizedResp.getHeaders();
    if (headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            servletResp.setHeader(entry.getKey(), entry.getValue());
        }
    }
}

From source file:com.sdl.odata.controller.AbstractODataController.java

/**
 * Transfers data from an {@code ODataResponse} into an {@code HttpServletResponse}.
 *
 * @param oDataResponse   The {@code ODataResponse}.
 * @param servletResponse The {@code HttpServletResponse}
 * @throws java.io.IOException If an I/O error occurs.
 *//*from  w  ww .  j  ava  2 s  . co m*/
private void fillServletResponse(ODataResponse oDataResponse, HttpServletResponse servletResponse)
        throws IOException {
    servletResponse.setStatus(oDataResponse.getStatus().getCode());

    for (Map.Entry<String, String> entry : oDataResponse.getHeaders().entrySet()) {
        servletResponse.setHeader(entry.getKey(), entry.getValue());
    }

    byte[] body = oDataResponse.getBody();
    if (body != null && body.length != 0) {
        OutputStream out = servletResponse.getOutputStream();
        out.write(oDataResponse.getBody());
        out.flush();
    }
}

From source file:gr.cti.android.experimentation.controller.BaseController.java

protected JSONObject internalServerError(final HttpServletResponse response) throws JSONException {
    response.setStatus(500);
    final JSONObject res = new JSONObject();
    res.put("status", "Internal Server Error");
    res.put("code", 5500);
    return res;/*www  .  j a  va 2  s. co  m*/
}

From source file:org.magnum.dataup.MainController.java

@RequestMapping(value = VideoSvcApi.VIDEO_DATA_PATH, method = RequestMethod.GET)
public @ResponseBody void getData(@PathVariable("id") long id, HttpServletResponse response) {

    try {/*from  ww w  .jav  a  2 s  .com*/
        if (!videos.containsKey(id)) {
            response.setStatus(404);
        } else {
            serveSomeVideo(videos.get(id), response);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.ambraproject.wombat.controller.ArticleAssetController.java

/**
 * Redirect to a given link. (We can't just return a {@link org.springframework.web.servlet.view.RedirectView} because
 * we ordinarily want to pass the raw response to {@link #forwardAssetResponse}. So we mess around with it directly.)
 */// w w  w  .j a v  a  2 s.  c  om
private void redirectTo(HttpServletResponse response, String location) {
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader(HttpHeaders.LOCATION, location);
}

From source file:com.ge.apm.web.WeChatCoreController.java

/**
 * ?webservice??????/*  w w w .  j a  v  a2 s  .c om*/
 *
 * @param request
 * @param response
 * @throws Exception
 */
@RequestMapping(value = "core")
public void wechatCore(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);

    String signature = request.getParameter("signature");
    String nonce = request.getParameter("nonce");
    String timestamp = request.getParameter("timestamp");

    if (!this.wxMpService.checkSignature(timestamp, nonce, signature)) {
        // ?????????
        response.getWriter().println("?");
        return;
    }

    String echoStr = request.getParameter("echostr");
    if (StringUtils.isNotBlank(echoStr)) {
        // ??echostr
        String echoStrOut = String.copyValueOf(echoStr.toCharArray());
        response.getWriter().println(echoStrOut);
        return;
    }
    System.out.println("current token is \n" + wxMpService.getAccessToken());
    String apiTicket = wxMpService.getJsapiTicket();
    System.out.println("current apiTicket is \n" + apiTicket);
    String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw"
            : request.getParameter("encrypt_type");

    if ("raw".equals(encryptType)) {
        // ?
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
        WxMpXmlOutMessage outMessage = this.coreService.route(inMessage);
        response.getWriter().write(outMessage == null ? "" : outMessage.toXml());
        return;
    }

    if ("aes".equals(encryptType)) {
        // aes?
        String msgSignature = request.getParameter("msg_signature");
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), this.configStorage,
                timestamp, nonce, msgSignature);
        WxMpXmlOutMessage outMessage = this.coreService.route(inMessage);
        response.getWriter().write(outMessage == null ? "" : outMessage.toEncryptedXml(this.configStorage));
        return;
    }

    response.getWriter().println("??");
    return;
}