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.ewcms.component.comment.web.CounterServlet.java

private void initResponseHeader(final HttpServletResponse response, final String encoding) {
    response.setHeader("Content-Language", "zh-CN");
    String fullContentType = JSON_TYPE + ";charset=" + encoding;
    response.setContentType(fullContentType);
    //set on cache
    ///Http 1.0 header
    response.setDateHeader("Expires", 0);
    response.addHeader("Pragma", "no-cache");
    //Http 1.1 header
    response.setHeader("Cache-Control", "no-cache");
}

From source file:org.overlord.apiman.gateway.http.HTTPGateway.java

/** Copy proxied response headers back to the servlet client. */
protected void copyResponseHeaders(Response proxyResponse, HttpServletResponse servletResponse) {
    for (int i = 0; i < proxyResponse.getHeaders().size(); i++) {
        org.overlord.apiman.NameValuePair nvp = proxyResponse.getHeaders().get(i);

        if (hopByHopHeaders.containsHeader(nvp.getName())) {
            continue;
        }//  ww  w  . j  a v a2 s  .co  m

        servletResponse.addHeader(nvp.getName(), (String) nvp.getValue());
    }
}

From source file:org.eclipse.jgit.lfs.server.fs.ObjectDownloadListener.java

/**
 * @param repository//from w  w  w . j  av a  2 s.  c o  m
 *            the repository storing large objects
 * @param context
 *            the servlet asynchronous context
 * @param response
 *            the servlet response
 * @param id
 *            id of the object to be downloaded
 * @throws IOException
 */
public ObjectDownloadListener(FileLfsRepository repository, AsyncContext context, HttpServletResponse response,
        AnyLongObjectId id) throws IOException {
    this.context = context;
    this.response = response;
    this.in = repository.getReadChannel(id);
    this.out = response.getOutputStream();
    this.outChannel = Channels.newChannel(out);

    response.addHeader(HttpSupport.HDR_CONTENT_LENGTH, String.valueOf(repository.getSize(id)));
    response.setContentType(Constants.HDR_APPLICATION_OCTET_STREAM);
}

From source file:com.ecs.cms.web.mvc.view.MappingJacksonJsonView.java

@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType(getContentType());
    response.setCharacterEncoding(this.encoding.getJavaName());
    if (this.disableCaching) {
        response.addHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "no-cache, no-store, max-age=0");
        response.addDateHeader("Expires", 1L);
    }//from ww w. j  ava 2  s. co m
}

From source file:com.ewcms.component.counter.web.CounterServlet.java

private void initResponseHeader(final HttpServletResponse response, final String encoding) {
    response.setHeader("Content-Language", "zh-CN");
    String fullContentType = JS_TYPE + ";charset=" + encoding;
    response.setContentType(fullContentType);
    //set on cache
    ///Http 1.0 header
    response.setDateHeader("Expires", 0);
    response.addHeader("Pragma", "no-cache");
    //Http 1.1 header
    response.setHeader("Cache-Control", "no-cache");
}

From source file:com.sample.JavaHTTPResource.java

public void execute(HttpHost host, HttpUriRequest req, HttpServletResponse resultResponse)
        throws IOException, IllegalStateException, SAXException {
    HttpResponse RSSResponse = client.execute(host, req);
    ServletOutputStream os = resultResponse.getOutputStream();
    if (RSSResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        resultResponse.addHeader("Content-Type", "application/json");
        //String json = IOUtils.toString(RSSResponse.getEntity().getContent());
        InputStream in = RSSResponse.getEntity().getContent();
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String read;/*from  w w  w.  ja v a2  s.  c om*/

        while ((read = br.readLine()) != null) {
            //System.out.println(read);
            sb.append(read);
        }

        br.close();
        os.write(sb.toString().getBytes(Charset.forName("UTF-8")));

    } else {
        resultResponse.setStatus(RSSResponse.getStatusLine().getStatusCode());
        RSSResponse.getEntity().getContent().close();
        os.write(RSSResponse.getStatusLine().getReasonPhrase().getBytes());
    }
    os.flush();
    os.close();
}

From source file:com.google.nigori.server.NigoriServlet.java

/**
 * Enable cors: http://enable-cors.org/server.html to allow access from javascript/dart clients
 * using code from a different domain//  w  ww .  ja v  a 2 s  .c o m
 * 
 * @param resp the response to add the headers to
 */
private void addCorsHeaders(HttpServletResponse resp) {
    resp.addHeader("Access-Control-Allow-Origin", "*");
    resp.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
    resp.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}

From source file:de.yaio.services.plantuml.server.controller.PlantumProvider.java

protected void addHeaderForCache(HttpServletResponse response) {
    long today = System.currentTimeMillis();
    // Add http headers to force the browser to cache the image
    response.addDateHeader("Expires", today + 31536000000L);
    // today + 1 year
    response.addDateHeader("Last-Modified", 1261440000000L);
    // 2009 dec 22 constant date in the past
    response.addHeader("Cache-Control", "public");
}

From source file:org.jetbrains.webdemo.handlers.ServerHandler.java

private void writeResponse(HttpServletRequest request, HttpServletResponse response, String responseBody,
        int statusCode) {
    try {//from  www  .  j  a  v  a 2 s . c  om
        response.addHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(statusCode);
        if (!responseBody.equals("")) {
            try (PrintWriter writer = response.getWriter()) {
                writer.write(responseBody);
            }
        }
    } catch (IOException e) {
        //This is an exception we can't send data to client
        ErrorWriter.ERROR_WRITER.writeExceptionToExceptionAnalyzer(e, "UNKNOWN", request.getHeader("Origin"),
                "null");
    }
}

From source file:com.vmware.identity.openidconnect.protocol.HttpResponse.java

public void applyTo(HttpServletResponse httpServletResponse) throws IOException {
    Validate.notNull(httpServletResponse, "httpServletResponse");

    for (Cookie cookie : this.cookies) {
        httpServletResponse.addCookie(cookie);
    }/* w w  w.j a  va  2 s . c om*/

    for (Header header : this.headers) {
        httpServletResponse.addHeader(header.getName(), header.getValue());
    }

    if (this.errorObject != null) {
        httpServletResponse.sendError(this.errorObject.getStatusCode().getValue(),
                this.errorObject.getErrorCode().getValue() + ": " + this.errorObject.getDescription());
    } else if (this.redirectTarget != null) {
        httpServletResponse.sendRedirect(this.redirectTarget.toString());
    } else if (this.htmlContent != null) {
        httpServletResponse.setStatus(this.statusCode.getValue());
        httpServletResponse.setHeader("Cache-Control", "no-cache, no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.print(this.htmlContent);
        writer.close();
    } else if (this.jsonContent != null) {
        httpServletResponse.setStatus(this.statusCode.getValue());
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.print(this.jsonContent.toString());
        writer.close();
    } else {
        throw new IllegalStateException("unexpected");
    }
}