Example usage for javax.servlet.http HttpServletResponse setHeader

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

Introduction

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

Prototype

public void setHeader(String name, String value);

Source Link

Document

Sets a response header with the given name and value.

Usage

From source file:org.openmrs.module.logmanager.web.view.AutocompleteView.java

@SuppressWarnings("rawtypes")
@Override/*  w ww.j  av a 2 s .c  o  m*/
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    PrintWriter writer = response.getWriter();
    Object source = model.get(sourceKey);

    // Disable caching
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");
    response.setContentType("application/json");

    writer.write("[");

    if (source != null) {
        if (source instanceof Collection) {
            Collection<?> collection = (Collection<?>) source;
            Object[] items = collection.toArray();
            for (int i = 0; i < items.length; i++) {
                Object item = items[i];
                String label = (item instanceof LoggerProxy) ? ((LoggerProxy) item).getName() : item.toString();

                if (i > 0)
                    writer.write(',');

                writer.write("{\"label\":\"" + label + "\", \"value\":\"" + label + "\"}");
            }
        }
    } else
        writer.write("\"ERROR: Source object is null\"");

    writer.write("]");
}

From source file:com.zte.gu.webtools.web.download.DownloadController.java

@RequestMapping(method = RequestMethod.GET)
public void download(HttpSession session, HttpServletResponse response) {
    String filePath = (String) session.getAttribute("filePath");
    String fileName = (String) session.getAttribute("fileName");
    if (filePath != null) {
        response.reset();//www . j a  v  a 2s .com
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        InputStream in = null;
        try {
            in = new FileInputStream(filePath);
            IOUtils.copy(in, response.getOutputStream());
        } catch (Exception e) {
            LoggerFactory.getLogger(DownloadController.class).warn("download error,", e);
        } finally {
            IOUtils.closeQuietly(in);
            session.removeAttribute("filePath");
            session.removeAttribute("fileName");
        }
    }
}

From source file:com.allanditzel.springframework.security.web.csrf.CsrfTokenResponseHeaderBindingFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        javax.servlet.FilterChain filterChain) throws ServletException, IOException {
    CsrfToken token = (CsrfToken) request.getAttribute(REQUEST_ATTRIBUTE_NAME);

    if (token != null) {
        response.setHeader(RESPONSE_HEADER_NAME, token.getHeaderName());
        response.setHeader(RESPONSE_PARAM_NAME, token.getParameterName());
        response.setHeader(RESPONSE_TOKEN_NAME, token.getToken());
    }/* www.j a  v a 2s.  co m*/

    filterChain.doFilter(request, response);
}

From source file:cn.vlabs.umt.ui.servlet.filters.P3PConfigFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    if (resp instanceof HttpServletResponse) {
        HttpServletResponse re = (HttpServletResponse) resp;
        String s = p3p;//  w ww. j  av  a2 s  .  c  o  m
        if (StringUtils.isEmpty(s)) {
            s = "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"";
        }
        re.setHeader("P3P", s);
    }
    chain.doFilter(request, resp);
}

From source file:com.silverpeas.silvercrawler.servlets.SilverCrawlerFileServer.java

private void sendFile(HttpServletResponse response, File file) throws IOException {
    response.setContentType(FileUtil.getMimeType(file.getName()));
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
    SilverTrace.debug("peasUtil", "SilverCrawlerFileServer.sendFile()", "root.MSG_GEN_ENTER_METHOD",
            " file: " + file.getAbsolutePath());
    try {//from   w ww .  j  av a 2 s  .  c  o  m
        FileUtils.copyFile(file, response.getOutputStream());
        response.getOutputStream().flush();
        SilverTrace.debug("peasUtil", "SilverCrawlerFileServer.sendFile()", "root.MSG_GEN_ENTER_METHOD",
                " File was sent");
    } catch (IOException e) {
        SilverTrace.error("peasUtil", "SilverCrawlerFileServer.sendFile", "root.EX_CANT_READ_FILE",
                " file: " + file.getAbsolutePath(), e);
        displayWarningHtmlCode(response);
    }
}

From source file:ar.com.zauber.commons.spring.servlet.view.PermanentlyRedirectView.java

/** @see RedirectView#sendRedirect() */
@Override/*  w  w  w  .java 2  s . com*/
protected final void sendRedirect(final HttpServletRequest request, final HttpServletResponse response,
        final String targetUrl, final boolean http10Compatible) throws IOException {

    if (request.getMethod().toLowerCase().equals("get")) {
        response.setStatus(301);
        response.setHeader("Location", response.encodeRedirectURL(targetUrl));
    } else {
        super.sendRedirect(request, response, targetUrl, http10Compatible);
    }
}

From source file:com.portal.security.controller.JCaptchaController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String captchaId = request.getSession().getId();
    BufferedImage challenge = captchaService.getImageChallengeForID(captchaId, request.getLocale());

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

    ImageIO.write(challenge, "jpeg", response.getOutputStream());

    /*//from ww w  .j a va 2  s. co  m
     * byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new
     * ByteArrayOutputStream();
     * 
     * JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
     * jpegEncoder.encode(challenge);
     * 
     * captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); ServletOutputStream
     * responseOutputStream = response.getOutputStream();
     * 
     * responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush();
     * responseOutputStream.close();
     */
    return null;
}

From source file:com.adito.keystore.actions.ShowKeyStoreDispatchAction.java

/**
 * @param mapping// w  ww . j  a va  2  s  .  c om
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 * @throws Exception
 */
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "must-revalidate");
    CoreUtil.clearFlow(request);

    ((ShowKeyStoreForm) form).initialize(request.getSession());
    return mapping.findForward("display");
}

From source file:net.smktarunabhakti.penjualan.ui.controller.BarangController.java

@RequestMapping(value = "/barang", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)//from w  w  w .  j  av a  2 s  .  c o  m
public void create(@RequestBody @Valid Barang x, HttpServletRequest request, HttpServletResponse response) {
    appService.simpanBarang(x);
    String requesturl = request.getRequestURL().toString();
    URI uri = new UriTemplate("{requestUrl}/{id}").expand(requesturl, x.getId());
    response.setHeader("location", uri.toASCIIString());
}

From source file:ru.org.linux.user.UserEventApiController.java

@ResponseBody
@RequestMapping(value = "/yandex-tableau", method = RequestMethod.GET, produces = { "application/json" })
public Map<String, Integer> getYandexWidget(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Template tmpl = Template.getTemplate(request);
    if (!tmpl.isSessionAuthorized()) {
        return ImmutableMap.of();
    } else {/*from  w  w w  .ja  va  2  s  .com*/
        response.setHeader("Cache-control", "no-cache");

        return ImmutableMap.of("notifications", tmpl.getCurrentUser().getUnreadEvents());
    }
}