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.openxdata.server.servlet.FormOpenServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", -1);
    response.setHeader("Cache-Control", "no-store");

    response.setContentType(OpenXDataConstants.HTTP_HEADER_CONTENT_TYPE_XML);
    response.getOutputStream().print((String) request.getSession().getAttribute(KEY_FILE_CONTENTS));
}

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 ww  w  .  j a  v a  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
    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:it.cilea.osd.jdyna.web.controller.AutocompleteUsersView.java

protected void renderMergedOutputModel(Map model, HttpServletRequest httpServletRequest,
        HttpServletResponse response) throws Exception {

    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");

    List<DTOAutocomplete> users = (List<DTOAutocomplete>) model.get("result");

    String xml = new AjaxXmlBuilder().addItems(users, "display", "id", true).toString();

    ServletOutputStream out = response.getOutputStream();
    out.print(xml);//from   w  w w  .  ja  va2 s  .  c  o  m
    out.close();

}

From source file:net.sourceforge.subsonic.filter.RESTFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    try {/* w ww .ja v a2 s.com*/
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        chain.doFilter(req, res);
    } catch (Throwable x) {
        handleException(x, (HttpServletRequest) req, (HttpServletResponse) res);
    }
}

From source file:org.openxdata.server.servlet.ImportServlet.java

private void writeErrorsToResponse(String errors, HttpServletResponse response) throws IOException {
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", -1);
    response.setHeader("Cache-Control", "no-store");

    response.setHeader("Content-Type", "text/csv;charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename=importErrros.csv");

    response.getWriter().write(errors);/* ww w . jav a 2s  .c o m*/
}

From source file:be.fedict.eid.idp.webapp.XSSProtectionFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    Boolean xssProtection = this.configuration.getValue(ConfigProperty.XSS_PROTECTION, Boolean.class);
    if (null != xssProtection) {
        if (xssProtection.equals(Boolean.TRUE)) {
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            httpServletResponse.setHeader("X-XSS-Protection", "1; mode=block");
        }/*  w w  w  .  ja  v  a2 s .co  m*/
    }
    chain.doFilter(request, response);
}

From source file:com.zoe.weiya.security.WwwAuthFilter.java

private void needAuthenticate(final HttpServletRequest request, final HttpServletResponse response) {
    response.setStatus(401);//from w ww .j av  a 2s  .c  o  m
    response.setHeader("Cache-Control", "no-store");
    response.setDateHeader("Expires", 0);
    response.setHeader("WWW-authenticate", AUTH_PREFIX + "Realm=\"WeiYa Auth\"");
}

From source file:com.cloudseal.spring.client.namespace.CloudSealLogoutImageFilter.java

protected void preventCaching(HttpServletResponse response) {
    Date now = new Date();
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Date", now.getTime());
    response.setDateHeader("Expires", now.getTime() - 86400000L); // one day old   
    response.setHeader("Cache-Control", "no-cache");
    response.addHeader("Cache-Control", "no-store");
    response.addHeader("Cache-Control", "must-revalidate");
}

From source file:com.tce.spring.oauth2.filters.CorsFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type, Authorization");
    response.setHeader("Access-Control-Max-Age", "3600");
    if (!"OPTIONS".equals(request.getMethod())) {
        filterChain.doFilter(request, response);
    } else {//from w w w.  j  a v  a  2 s  . c o m
    }
}

From source file:de.knurt.fam.template.controller.json.JSONController2.java

/**
 * print out json with the key and values got from
 * {@link JSONController2#getKeyAndValues(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
 * /*w  ww  .  ja v a 2  s. c o m*/
 * @param rq
 *            request
 * @param rs
 *            response
 * @return null
 */
public ModelAndView handleRequest(HttpServletRequest rq, HttpServletResponse rs) {
    PrintWriter pw = null;
    try {
        rs.setHeader("Content-type", "application/json");
        pw = rs.getWriter();
        IOUtils.write(this.getJSONObject().toString(), pw);
    } catch (IOException ex) {
        this.onIOException(ex);
    } finally {
        IOUtils.closeQuietly(pw);
    }
    return null;
}