Example usage for javax.servlet.http HttpServletResponse addHeader

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

Introduction

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

Prototype

public void addHeader(String name, String value);

Source Link

Document

Adds a response header with the given name and value.

Usage

From source file:com.sishuok.chapter3.web.controller.StreamingController.java

@RequestMapping("/async3")
public void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setHeader("Connection", "Keep-Alive");
    resp.addHeader("Cache-Control", "private");
    resp.addHeader("Pragma", "no-cache");
    resp.setContentType("text/html;charset=utf-8");
    resp.flushBuffer();/*from   w ww. j  av a  2  s.  c  om*/

    //1??
    final AsyncContext asyncContext = req.startAsync();

    asyncContext.addListener(new AsyncListener() {
        @Override
        public void onComplete(final AsyncEvent event) throws IOException {
            queue.remove(event.getAsyncContext());
        }

        @Override
        public void onTimeout(final AsyncEvent event) throws IOException {
            event.getAsyncContext().complete();
            queue.remove(event.getAsyncContext());
        }

        @Override
        public void onError(final AsyncEvent event) throws IOException {
            queue.remove(event.getAsyncContext());
        }

        @Override
        public void onStartAsync(final AsyncEvent event) throws IOException {
        }
    });

    //????
    queue.add(asyncContext);

}

From source file:com.iflytek.edu.cloud.frame.web.filter.CORSFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    String origin = request.getHeader("Origin");
    if (allowedOrigins.contains(origin)) {
        response.addHeader("Access-Control-Allow-Origin", origin);
        response.addHeader("Access-Control-Allow-Methods", "GET, POST");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type");
    }//from  w w w .ja  va 2s.c  o m

    filterChain.doFilter(request, response);
}

From source file:org.jasig.cas.web.flow.AbstractLogoutAction.java

/**
 * Prevent caching by adding the appropriate headers.
 * Copied from the <code>preventCaching</code> method in the {@link WebContentGenerator} class.
 *
 * @param response the HTTP response./*from  www.  j a  v a 2 s  .c o m*/
 */
protected final void preventCaching(final HttpServletResponse response) {
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 1L);
    response.setHeader("Cache-Control", "no-cache");
    response.addHeader("Cache-Control", "no-store");
}

From source file:com.sentinel.config.TokenAuthenticationService.java

public String addAuthentication(HttpServletResponse response, UserAuthentication authentication) {
    LOG.debug("adding auth to request");
    final User user = authentication.getDetails();
    String token = tokenHandler.createTokenForUser(user);
    response.addHeader(AUTH_HEADER_NAME, token);
    return token;
}

From source file:Core.Security.CustomEntryPoint.java

public void commence(final HttpServletRequest request, final HttpServletResponse response,
        final AuthenticationException authException) throws IOException, ServletException {
    //Authentication failed, send error response.
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName() + "");
    PrintWriter writer = response.getWriter();
    writer.println("HTTP Status 401 : " + authException.getMessage());
}

From source file:com.nesscomputing.jmx.jolokia.JolokiaServlet.java

private void sendResponse(final HttpServletResponse resp, final String contentType, final String jsonTxt)
        throws IOException {
    resp.setCharacterEncoding("utf-8");
    resp.addHeader("Access-Control-Allow-Origin", "*");
    resp.setContentType(contentType);/*from w w w . jav  a2 s  .c o  m*/
    resp.setStatus(HttpServletResponse.SC_OK);
    final PrintWriter writer = resp.getWriter();
    writer.write(jsonTxt);
    writer.flush();
}

From source file:com.threewks.thundr.route.cors.CorsFilter.java

@Override
public <T> T before(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) {
    String origin = origins == null ? "*" : origin(req, origins);
    resp.addHeader(Header.Vary, StringUtils.join(vary(), ", "));
    if (origin != null) {
        resp.addHeader(Header.AccessControlAllowOrigin, origin);
        if (withCredentials) {
            resp.addHeader(Header.AccessControlAllowCredentials, "true");
        }//w  ww .j ava 2s  .c o  m
        List<String> allowedHeaders = determineAllowedHeaders(headers, req);
        if (Expressive.isNotEmpty(allowedHeaders)) {
            resp.addHeader(Header.AccessControlAllowHeaders, StringUtils.join(allowedHeaders, ", "));
        }
        String requestedMethod = req.getHeader(Header.AccessControlRequestMethod);
        if (StringUtils.isNotBlank(requestedMethod)) {
            resp.addHeader(Header.AccessControlAllowMethods, requestedMethod);
        }
    }
    return null;
}

From source file:org.zht.framework.filter.jcaptcha.JCaptchaFilter.java

/**
 * ???./*from   w  w  w  .j a  v a 2 s. c  o m*/
 */
protected void genernateCaptchaImage(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException {

    response.setDateHeader("Expires", 0);
    response.addHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setContentType("image/jpeg");

    ServletOutputStream out = response.getOutputStream();
    try {
        String captchaId = request.getSession(true).getId();
        BufferedImage challenge = (BufferedImage) captchaService.getChallengeForID(captchaId,
                request.getLocale());
        ImageIO.write(challenge, "jpg", out);
        out.flush();
    } catch (CaptchaServiceException e) {
        logger.error(e.getMessage(), e);
    } finally {
        out.close();
    }
}

From source file:org.openwms.core.uaa.api.UsersController.java

/**
 * Takes a newly created <tt>User</tt> instance and persists it. <p> <p> <table> <tr> <td>URI</td> <td>/users</td> </tr> <tr>
 * <td>Verb</td> <td>POST</td> </tr> <tr> <td>Auth</td> <td>YES</td> </tr> <tr> <td>Header</td> <td></td> </tr> </table> </p> <p>
 * Request Body <p>/*from  ww  w.java  2s . c  o  m*/
 * <pre>
 *   {
 *     "username" : "testuser"
 *   }
 * </pre>
 * <p> Parameters: <ul> <li>username (String):</li> The unique username. </ul> </p> <p> Response Body <p>
 * <pre>
 *   {
 *     "id" : 4711,
 *     "username" : "testuser",
 *     "token" : "876sjh36ejwhd",
 *     "version" : 1
 *   }
 * </pre>
 * <p> <ul> <li>id (Integer (32bit)):</li> The internal unique technical key for the stored instance. <li>username (String):</li> The
 * unique username. <li>token (String):</li> A generated token that is used to authenticate each request. <li>version (Integer
 * (32bit)):</li> A version number used internally for optimistic locking. </ul> </p>
 *
 * @param user The user to create
 * @return a responseVO
 */
@PostMapping
@ResponseBody
public ResponseEntity<Response<UserVO>> create(@RequestBody @Valid @NotNull UserVO user, HttpServletRequest req,
        HttpServletResponse resp) {
    User createdUser = service.create(m.map(user, User.class));
    resp.addHeader(HttpHeaders.LOCATION, getLocationForCreatedResource(req, createdUser.getPk().toString()));
    return buildResponse(HttpStatus.CREATED, translate(Messages.CREATED), Messages.CREATED);
}

From source file:com.fanya.p2p.front.user.jcaptcha.JCaptchaFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    response.setDateHeader("Expires", 0L);
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");
    response.setContentType("image/jpeg");

    String id = request.getRequestedSessionId();
    BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);

    ServletOutputStream out = response.getOutputStream();

    ImageIO.write(bi, "jpg", out);
    try {/*from   w  ww .  j  a v a2 s.  c  o m*/
        out.flush();
    } finally {
        out.close();
    }
}