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:com.github.rnewson.couchdb.lucene.util.ServletUtils.java

public static void sendJsonError(final HttpServletRequest request, final HttpServletResponse response,
        final int code, final JSONObject error) throws IOException, JSONException {
    setResponseContentTypeAndEncoding(request, response);
    response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
    response.setStatus(code);/*from   w ww.j  a  v a  2 s.  c  o  m*/
    error.put("code", code);

    final Writer writer = response.getWriter();
    try {
        writer.write(error.toString());
        writer.write("\r\n");
    } finally {
        writer.close();
    }
}

From source file:com.indeed.imhotep.web.ResultServlet.java

static void setContentType(HttpServletResponse resp, boolean avoidFileSave, boolean csv, boolean progress) {
    if (avoidFileSave) {
        resp.setContentType("text/plain");
    } else if (csv) {
        resp.setContentType("text/csv");
        resp.setHeader("Content-Disposition", "inline; filename=\"iqlresult.csv\"");
    } else if (progress) {
        resp.setContentType("text/event-stream");
        resp.setHeader("Cache-Control", "no-cache");
    } else {//from   w w w . j ava  2 s  . c o m
        resp.setContentType("text/tab-separated-values");
        resp.setHeader("Content-Disposition", "inline; filename=\"iqlresult.tsv\"");
    }
}

From source file:com.handpay.ibenefit.framework.util.WebUtils.java

public static void setNoCacheHeader(HttpServletResponse response) {
    // Http 1.0 header
    response.setDateHeader("Expires", 0);
    // Http 1.1 header
    response.setHeader("Cache-Control", "no-cache");
}

From source file:org.ambraproject.wombat.util.ReproxyUtil.java

/**
 * Apply reproxying information for a requested asset to a response.
 * <p/>/*from w w  w  . ja v  a 2s . c  o  m*/
 * The request may not support the reproxying protocol, and there may not be any reproxy URLs for the asset. In either
 * case, the response is not touched. The return value indicates this.
 * <p/>
 * Note that both {@code request} and {@code response} are for the client. Neither is for the service that reproxies
 * the asset. It is assumed that the caller has already queried the service that reproxies the asset, which provides
 * the {@code reproxyUrls} argument.
 *
 * @param request     a request from the client
 * @param response    a response to the client
 * @param reproxyUrls a list of available reproxy URLs for the requested asset (empty and {@code null} are allowed,
 *                    indicating none are available)
 * @param cacheFor    number of seconds to instruct the client to cache the reproxy information
 * @return {@code true} if the response was filled with reproxy information, in which case nothing should be written
 * to the response body; {@code false} if not, in which case the response body should be written
 */
public static boolean applyReproxy(HttpServletRequest request, HttpServletResponse response,
        List<String> reproxyUrls, int cacheFor) {
    if (reproxyUrls != null && !reproxyUrls.isEmpty() && supportsReproxy(request)) {
        response.setStatus(HttpStatus.SC_OK);
        response.setHeader(X_REPROXY_URL, REPROXY_URL_JOINER.join(reproxyUrls));
        response.setHeader(X_REPROXY_CACHE_FOR, cacheFor + "; Last-Modified Content-Type Content-Disposition");
        return true;
    }
    return false;
}

From source file:io.wcm.wcm.commons.caching.CacheHeader.java

/**
 * Set expires header to given date./*www  . j  a  v  a  2  s  . c  om*/
 * @param response Response
 * @param date Expires date
 */
public static void setExpires(HttpServletResponse response, Date date) {
    if (date == null) {
        response.setHeader(HEADER_EXPIRES, "-1");
    } else {
        response.setHeader(HEADER_EXPIRES, formatDate(date));
    }
}

From source file:com.voa.weixin.utils.HttpUtils.java

public static void write(HttpServletResponse response, String message) throws IOException {
    PrintWriter writer = null;/* w  w  w.ja  v  a  2  s.  co  m*/
    try {
        response.setHeader("Charset", "UTF-8");
        response.setCharacterEncoding("UTF-8");
        writer = response.getWriter();

        writer.write(message);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ???/*from   ww  w  .  ja v a 2s  .  co  m*/
 * @param request
 * @param response
 */
public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 
    int width = 60, height = 20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // ?
    Graphics g = image.getGraphics();

    // ??
    Random random = new Random();

    // 
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);

    // 
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

    // 
    // g.setColor(new Color());
    // g.drawRect(0,0,width-1,height-1);

    // ?155?????
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }

    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789";
    // ????(4?)
    String sRand = "";
    for (int i = 0; i < 4; i++) {
        int start = random.nextInt(base.length());
        String rand = base.substring(start, start + 1);
        sRand = sRand.concat(rand);
        // ??
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(rand, 13 * i + 6, 16);
    }

    // ??SESSION
    request.getSession().setAttribute("entrymrand", sRand);

    // 
    g.dispose();
    OutputStream out = response.getOutputStream();
    // ?
    ImageIO.write(image, "JPEG", out);

    out.flush();
    out.close();
}

From source file:com.shz.foundation.utils.Servlets.java

/**
 * /*from www.jav a2  s  .c o  m*/
 * @param response
 * @param image 
 * @param sufix ?
 */
public static void writeBufferedImage(HttpServletResponse response, BufferedImage image, String sufix)
        throws IOException {
    response.setHeader("Cache-Control", "private,no-cache,no-store");
    response.setContentType("image/" + sufix);
    ImageIO.write(image, sufix, response.getOutputStream());
    response.getOutputStream().close();
}

From source file:com.yiji.openapi.sdk.util.Servlets.java

public static void setHeader(HttpServletResponse response, String name, String value) {
    response.setHeader(name, value);
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

public static void showFile(HttpServletResponse response, String path, String fileName, String contentType)
        throws IOException {
    File file = new File(path);
    response.setBufferSize((int) file.length());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    response.setContentType(contentType);
    response.setContentLength((int) file.length());

    byte[] bytes = new byte[(int) file.length()];
    FileInputStream fis = new FileInputStream(file);
    fis.read(bytes);/*w  w  w  .j a va 2s . com*/
    fis.close();

    FileCopyUtils.copy(bytes, response.getOutputStream());
}