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.webbfontaine.valuewebb.model.util.Utils.java

public static void sendBytes(byte[] bytes, String filename, String contentType) {
    if (bytes != null) {
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();

        response.setContentType(contentType);
        response.addHeader("Content-disposition", "attachment; filename=" + filename);
        response.addHeader("Content-Length", String.valueOf(bytes.length));
        ServletOutputStream os = null;/*from   w  w w . j a va  2  s .  com*/
        try {
            os = response.getOutputStream();
            os.write(bytes);
            os.flush();
            FacesContext.getCurrentInstance().responseComplete();
        } catch (IOException e) {
            LOGGER.error("Sending content failed {0}", filename, e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    LOGGER.error("Failed to close stream after sending", e);
                }
            }
        }
    }
}

From source file:com.feilong.servlet.http.ResponseDownloadUtil.java

/**
 *  download response header./*w  w w  .  ja  v  a  2  s  . c o m*/
 *
 * @param saveFileName
 *            the save file name
 * @param contentLength
 *            the content length
 * @param contentType
 *            ?,;,? <code>null</code>,,?? {@link #resolverContentType(String, String)}
 * @param contentDisposition
 *            ?,;,? <code>null</code>,,?? {@link #resolverContentDisposition(String, String)}
 * @param response
 *            the response
 */
private static void setDownloadResponseHeader(String saveFileName, Number contentLength, String contentType,
        String contentDisposition, HttpServletResponse response) {

    //response
    //getResponsegetWriter()??,??,?response.resetresetBuffer.
    //getOutputStream() has already been called for this response
    //jsp??,response.getOutputStream()??:java.lang.IllegalStateException:getOutputStream() has already been called for this response,Exception
    response.reset();

    //*************************************************************************
    response.addHeader(HttpHeaders.CONTENT_DISPOSITION,
            resolverContentDisposition(saveFileName, contentDisposition));

    // ===================== Default MIME Type Mappings =================== -->
    //??,?,????.????,???,
    response.setContentType(resolverContentType(saveFileName, contentType));

    if (isNotNullOrEmpty(contentLength)) {
        response.setContentLength(contentLength.intValue());
    }

    //************************about buffer***********************************************************

    //?:??,?,.
    //??,?:
    //JSP??
    //
    //JSPout.flush()response.flushbuffer()

    //:?,?,???,???.
    //XXX ?? response.setBufferSize(10240); ^_^

    //see org.apache.commons.io.IOUtils.copyLarge(InputStream, OutputStream) javadoc
    //This method buffers the input internally, so there is no need to use a BufferedInputStream
}

From source file:org.openmrs.module.webservices.rest.web.RestUtil.java

/**
 * Sets the HTTP status for UPDATED and (if 'updated' has a uri) the Location header attribute
 * /*  w w w  .  jav  a  2  s . c  o  m*/
 * @param response
 * @param updated
 * @return the object passed in
 */
public static Object updated(HttpServletResponse response, Object updated) {
    response.setStatus(HttpServletResponse.SC_OK);
    try {
        String uri = (String) PropertyUtils.getProperty(updated, "uri");
        response.addHeader("Location", uri);
    } catch (Exception ex) {
    }
    return updated;
}

From source file:org.openmrs.module.webservices.rest.web.RestUtil.java

/**
 * Sets the HTTP status for CREATED and (if 'created' has a uri) the Location header attribute
 * /*w  w w  .j a v a2s. c  o m*/
 * @param response
 * @param created
 * @return the object passed in
 */
public static Object created(HttpServletResponse response, Object created) {
    response.setStatus(HttpServletResponse.SC_CREATED);
    try {
        String uri = (String) PropertyUtils.getProperty(created, "uri");
        response.addHeader("Location", uri);
    } catch (Exception ex) {
    }
    return created;
}

From source file:net.shopxx.filter.AccessDeniedFilter.java

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    response.addHeader(new String(Base64.decodeBase64("UG93ZXJlZEJ5"), "utf-8"),
            new String(Base64.decodeBase64("Y2hlbmd6aGFuZy5jbw=="), "utf-8"));
    response.sendError(HttpServletResponse.SC_FORBIDDEN, ERROR_MESSAGE);
}

From source file:nl.surfnet.coin.api.oauth.Http401UnauthorizedEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.addHeader("WWW-Authenticate", "Bearer realm=\"api.surfconext\"");
    httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");

}

From source file:com.cnd.greencube.web.base.filter.AccessDeniedFilter.java

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    response.addHeader(new String(Base64.decodeBase64("UG93ZXJlZEJ5"), "utf-8"),
            new String(Base64.decodeBase64("VG9wIFRlYW0="), "utf-8"));
    response.sendError(HttpServletResponse.SC_FORBIDDEN, ERROR_MESSAGE);
}

From source file:com.sammyun.filter.AccessDeniedFilter.java

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    response.addHeader(new String(Base64.decodeBase64("UG93ZXJlZEJ5"), "utf-8"),
            new String(Base64.decodeBase64("U2hvcHh4Lm5ldA=="), "utf-8"));
    response.sendError(HttpServletResponse.SC_FORBIDDEN, ERROR_MESSAGE);
}

From source file:com.hbs.common.josn.JSONUtil.java

public static void writeJSONToResponse(HttpServletResponse response, String encoding, boolean wrapWithComments,
        String serializedJSON, boolean smd, boolean gzip, String contentType) throws IOException {
    String json = serializedJSON == null ? "" : serializedJSON;
    if (wrapWithComments) {
        StringBuilder sb = new StringBuilder("/* ");
        sb.append(json);/*from  w w  w .  j av a 2  s .co m*/
        sb.append(" */");
        json = sb.toString();
    }
    if (log.isDebugEnabled()) {
        log.debug("[JSON]" + json);
    }

    if (contentType == null) {
        contentType = "application/json";
    }
    response.setContentType((smd ? "application/json-rpc;charset=" : contentType + ";charset=") + encoding);
    if (gzip) {
        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(encoding).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:com.navercorp.arcus.util.CrossOriginResourceSharingFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse servletResponse = (HttpServletResponse) response;
    servletResponse.addHeader("Access-Control-Allow-Origin", "*");
    servletResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
    servletResponse.addHeader("Access-Control-Allow-Headers", "Content-Type");
    chain.doFilter(request, response);//from   w  w  w .  j  a va 2  s  .co  m
}