Example usage for javax.servlet.http HttpServletResponse addDateHeader

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

Introduction

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

Prototype

public void addDateHeader(String name, long date);

Source Link

Document

Adds a response header with the given name and date-value.

Usage

From source file:net.duckling.ddl.web.controller.file.BaseAttachController.java

private void setModifiedHeader(int age, MetaInfo meta, HttpServletResponse res) {
    if (meta.size > 0) {
        res.setHeader("Content-Length", meta.size + "");
    }//  w w w  .jav  a 2  s .com
    try {
        res.addDateHeader("last-modified", meta.lastUpdate.getTime());
    } catch (Exception e) {
    }
}

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  w  ww .  jav  a  2 s  .c  o m*/
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testSkipBecauseExpiresIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override/*  w w  w  .  java  2s  . c  o  m*/
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addDateHeader("Expires", System.currentTimeMillis());
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, null);
}

From source file:com.kolich.spring.views.AbstractKolichView.java

@Override
public void renderMergedOutputModel(final Map<String, Object> model, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    try {//from  w w w.  j  ava2  s  .  c om
        final KolichViewSerializable payload = (KolichViewSerializable) model.get(VIEW_PAYLOAD);
        // If the payload has an Expires date attached to it, then
        // we should honor that by adding the Expires: header to the
        // response.  Controllers may set this so that upstream caches
        // do not attempt to cache the resource.
        final Date expires;
        if ((expires = payload.getExpires()) != null) {
            response.addDateHeader(EXPIRES, expires.getTime());
        }
        // If we are asked to disable caching of this response,
        // then we need to append the correct Cache-Control and Pragma
        // headers here.
        if (payload.disableCaching()) {
            response.addHeader(PRAGMA, PRAGMA_NO_CACHE);
            response.addHeader(CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
        }
        // Call the custom prepare response after anything else has
        // been set so that the view can override these if necessary.
        myPrepareResponse(payload, request, response);
        myRenderMergedOutputModel(payload, request, response);
    } catch (Exception e) {
        logger__.error("Failed to render merged output model.", e);
        throw e;
    }
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testExcludedResponseStatusCode() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//  www  .  j  a v  a  2 s .com
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.addHeader("ETag", "W/\"1934-1269208821000\"");
            response.addDateHeader("Date", System.currentTimeMillis());
        }
    };

    validate(servlet, null, HttpServletResponse.SC_NOT_MODIFIED);
}

From source file:com.rabbitstewdio.build.maven.tomcat.AbstractContentServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String filename = request.getPathInfo();

    InputStream in = findResource(filename);
    if (in == null) {
        log.warn("Unable to find [" + filename + "] on " + getServletName());
        response.setStatus(404);/*from  ww  w  .j av  a2  s .  c  o m*/
        return;
    }

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(in, bout);

    response.setCharacterEncoding("UTF-8");

    String mimeType = getServletContext().getMimeType(filename);
    if (mimeType != null) {
        response.setContentType(mimeType);
    }

    response.addDateHeader("Expires", 0L);
    response.setDateHeader(LAST_MODIFIED, new Date().getTime());
    response.setContentLength(bout.size());
    response.getOutputStream().write(bout.toByteArray());
    response.flushBuffer();
}

From source file:com.ibm.sbt.service.basic.ProxyService.java

public static void writeErrorResponse(int httpstatus, String errorMessage, String[] parameters, String[] values,
        HttpServletResponse response, HttpServletRequest request) throws ServletException {
    JsonJavaObject o = new JsonJavaObject();
    o.putString("Message", errorMessage);
    List<JsonJavaObject> params = new ArrayList<JsonJavaObject>();
    if (parameters != null && parameters.length > 0) {
        for (int i = 0; i < parameters.length; i++) {
            JsonJavaObject e = new JsonJavaObject();
            e.putString(parameters[i], values[i]);
            params.add(e);//from   w  ww . j av  a  2 s.  com
        }
    }
    o.putObject("Parameters", params);

    try {
        response.setStatus(httpstatus);
        //response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.addHeader("content-type", "application/json"); // $NON-NLS-1$ $NON-NLS-2$
        response.addHeader("Server", "SBT"); // $NON-NLS-1$ $NON-NLS-2$
        response.addHeader("Connection", "close"); // $NON-NLS-1$ $NON-NLS-2$
        response.addDateHeader("Date", System.currentTimeMillis()); // $NON-NLS-1$
        response.getOutputStream().print(JsonGenerator.toJson(JsonJavaFactory.instanceEx, o));
    } catch (Exception e) {
    }
}

From source file:grails.plugin.cache.web.filter.PageFragmentCachingFilter.java

/**
 * Set the headers in the response object, excluding the Gzip header
 * @param pageInfo//from w w w.j a v  a  2 s  .  c  o m
 * @param response
 */
protected void setHeaders(final PageInfo pageInfo, final HttpServletResponse response) {

    Collection<Header<? extends Serializable>> headers = pageInfo.getHeaders();

    // Track which headers have been set so all headers of the same name
    // after the first are added
    TreeSet<String> setHeaders = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

    for (Header<? extends Serializable> header : headers) {
        String name = header.getName();

        switch (header.getType()) {
        case STRING:
            if (setHeaders.contains(name)) {
                response.addHeader(name, (String) header.getValue());
            } else {
                setHeaders.add(name);
                response.setHeader(name, (String) header.getValue());
            }
            break;
        case DATE:
            if (setHeaders.contains(name)) {
                response.addDateHeader(name, (Long) header.getValue());
            } else {
                setHeaders.add(name);
                response.setDateHeader(name, (Long) header.getValue());
            }
            break;
        case INT:
            if (setHeaders.contains(name)) {
                response.addIntHeader(name, (Integer) header.getValue());
            } else {
                setHeaders.add(name);
                response.setIntHeader(name, (Integer) header.getValue());
            }
            break;
        default:
            throw new IllegalArgumentException("No mapping for Header: " + header);
        }
    }
}