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:cats.twitter.webapp.controller.mvc.CORSFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
        // CORS "pre-flight" request
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type");
        response.addHeader("Access-Control-Max-Age", "1800");//30 min
    }//from  w  ww  .  j  a v  a 2 s.  c o m
    filterChain.doFilter(request, response);
}

From source file:com.ace.erp.filter.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");

    ServletOutputStream out = response.getOutputStream();

    try {/*from  ww w . jav  a2  s . c o m*/
        String id = request.getSession(true).getId();
        //String id = request.getRequestedSessionId();
        BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);

        ImageIO.write(bi, "jpg", out);
        out.flush();
    } finally {
        out.close();
    }
}

From source file:com.founder.fix.fixflow.explorer.util.ResultUtils.java

/**
 * ?Header./*w w  w .java  2s  .  co m*/
 */
public void setDisableCacheHeader(HttpServletResponse response) {
    // Http 1.0 header
    response.setDateHeader("Expires", 1L);
    response.addHeader("Pragma", "no-cache");
    // Http 1.1 header
    response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * //w w  w.  ja  va2s.  c o m
 *
 * @param attachFile 
 * @param fileName   ????
 * @throws IOException
 */
public static void write(byte[] attachFile, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:cn.featherfly.web.spring.servlet.view.json.ObjectJacksonJsonView.java

@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
    setResponseContentType(request, response);
    response.setCharacterEncoding(this.getEncoding().getJavaName());
    if (cacheExpires > 0) {
        response.addHeader("Cache-Control", "max-age=" + cacheExpires);
    } else {/*from www.  j ava2 s .c  o m*/
        response.addHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "no-cache, no-store, max-age=0");
        response.addDateHeader("Expires", 1L);
    }
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response?/*from ww w  . j a va2  s .  c  o  m*/
 * ???10mdownloadCall
 *
 * @param attachFile
 * @throws IOException
 */
public static void download(byte[] attachFile, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
        resp.addHeader("Content-Length", "" + attachFile.length);
        //            resp.setContentType("application/octet-stream");
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response/* www.j  av  a2 s.  c  om*/
 * ???10mdownloadCall
 *
 * @param file
 * @throws IOException
 */
public static void download(File file, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
        //            resp.addHeader("Content-Length", "" + attachFile.length);
        //            resp.setContentType("application/octet-stream");
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(IOUtils.toByteArray(new FileInputStream(file)));
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.energyos.espi.datacustodian.web.customer.CustomerDownloadMyDataController.java

@RequestMapping(value = Routes.RETAIL_CUSTOMER_DOWNLOAD_MY_DATA_COLLECTION, method = RequestMethod.GET)
public void downloadMyDataCollection(HttpServletResponse response, @PathVariable Long retailCustomerId,
        @RequestParam Map<String, String> params) throws IOException, FeedException {

    response.setContentType(MediaType.TEXT_HTML_VALUE);
    response.addHeader("Content-Disposition", "attachment; filename=GreenButtonDownload.xml");
    try {//from   www .j  av  a  2 s  . c  om
        // TODO -- need authorization hook
        exportService.exportUsagePointsFull(0L, retailCustomerId, response.getOutputStream(),
                new ExportFilter(params));

    } catch (Exception e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:org.openwms.tms.api.TransportationController.java

@PostMapping
@ResponseStatus(HttpStatus.CREATED)/*from   ww w. j  av  a2  s  .c o  m*/
public void createTO(@RequestBody CreateTransportOrderVO vo, HttpServletRequest req, HttpServletResponse resp) {
    validatePriority(vo);
    TransportOrder to = service.create(vo.getBarcode(), vo.getTarget(),
            PriorityLevel.valueOf(vo.getPriority()));
    resp.addHeader(HttpHeaders.LOCATION, getCreatedResourceURI(req, to.getPersistentKey()));
}

From source file:org.fcrepo.http.api.ContentExposingResource.java

/**
 * Add ETag and Last-Modified cache control headers to the response
 * @param servletResponse the servlet response
 * @param resource the fedora resource//from  w  w w.  ja  v  a  2s.c  o m
 * @param session the session
 */
protected static void addCacheControlHeaders(final HttpServletResponse servletResponse,
        final FedoraResource resource, final Session session) {

    final String txId = TransactionServiceImpl.getCurrentTransactionId(session);
    if (txId != null) {
        // Do not add caching headers if in a transaction
        return;
    }

    final FedoraResource mutableResource = resource instanceof NonRdfSourceDescription
            ? ((NonRdfSourceDescription) resource).getDescribedResource()
            : resource;
    final EntityTag etag = new EntityTag(mutableResource.getEtagValue());
    final Date date = mutableResource.getLastModifiedDate();

    if (!etag.getValue().isEmpty()) {
        servletResponse.addHeader("ETag", etag.toString());
    }

    if (date != null) {
        servletResponse.addDateHeader("Last-Modified", date.getTime());
    }
}