Example usage for org.apache.wicket.request.http WebResponse setLastModifiedTime

List of usage examples for org.apache.wicket.request.http WebResponse setLastModifiedTime

Introduction

In this page you can find the example usage for org.apache.wicket.request.http WebResponse setLastModifiedTime.

Prototype

public void setLastModifiedTime(final Time time) 

Source Link

Document

Set the contents last modified time, if appropriate in the subclass.

Usage

From source file:com.gitblit.wicket.pages.BasePage.java

License:Apache License

/**
 * Sets the last-modified header field and the expires field.
 *
 * @param when/*from w ww.j ava2 s  . c o  m*/
 */
protected final void setLastModified(Date when) {
    if (when == null) {
        return;
    }

    if (when.before(app().getBootDate())) {
        // last-modified can not be before the Gitblit boot date
        // this helps ensure that pages are properly refreshed after a
        // server config change
        when = app().getBootDate();
    }

    int expires = app().settings().getInteger(Keys.web.pageCacheExpires, 0);
    WebResponse response = (WebResponse) getResponse();
    response.setLastModifiedTime(Time.valueOf(when));
    response.addHeader("Expires",
            String.valueOf(System.currentTimeMillis() + Duration.minutes(expires).getMilliseconds()));
}

From source file:de.alpharogroup.wicket.base.util.WicketComponentExtensions.java

License:Apache License

/**
 * Disables caching from a WebPage. To disable the cache override the WebPage.setHeader() and
 * invoke this method. For instance:<code>
 * protected void setHeaders(WebResponse response) {
 * &nbsp;&nbsp;&nbsp;&nbsp;
 * WicketComponentExtensions.disableCaching(response);
 * }//w  w  w .  jav  a  2  s .com
 * </code>
 *
 * @param response
 *            the response
 */
public static void disableCaching(final WebResponse response) {
    response.setLastModifiedTime(Time.now());
    final HttpServletResponse httpServletResponse = getHttpServletResponse(response);
    if (httpServletResponse != null) {
        httpServletResponse.addHeader("Cache-Control", "max-age=0");
        httpServletResponse.setDateHeader("Expires", 0);
    }
}

From source file:fiftyfive.wicket.resource.MergedResourceRequestHandler.java

License:Apache License

public void respond(IRequestCycle requestCycle) {
    WebRequest origRequest = (WebRequest) requestCycle.getRequest();

    // Explicitly set the last modified header of the response based on the last modified
    // time of the aggregate. Do this on the original response because our wrapped response
    // ignores the last modified headers contributed by each individual resource.
    WebResponse origResponse = (WebResponse) requestCycle.getResponse();
    if (this.lastModified != null) {
        origResponse.setLastModifiedTime(this.lastModified);
    }//w w  w.ja va  2s. c o m

    try {
        // Make a special response object that merges the contributions of each resource,
        // but maintains a single set of headers.
        MergedResponse merged = new MergedResponse(origResponse);
        requestCycle.setResponse(merged);

        // Make a special request object that tweaks the If-Modified-Since header to ensure
        // we don't end up in a situation where some resources respond 200 and others 304.
        // Yes, calling RequestCycle#setRequest() is frowned upon so this is a bit of a hack.
        ((RequestCycle) requestCycle).setRequest(new MergedRequest(origRequest));

        for (ResourceReference ref : this.resources) {
            ResourceRequestHandler handler = new ResourceRequestHandler(ref.getResource(), this.pageParameters);
            handler.respond(requestCycle);

            // If first resource sent 304 Not Modified that means all will.
            // We can therefore skip the rest.
            if (304 == merged.status) {
                break;
            }
        }
    } finally {
        // Restore the original request once we're done. We don't need to restore the
        // original response because Wicket takes care of that automatically.
        ((RequestCycle) requestCycle).setRequest(origRequest);
    }
}

From source file:org.brixcms.plugin.site.resource.ResourceNodeHandler.java

License:Apache License

@Override
public void respond(IRequestCycle requestCycle) {
    boolean save = (this.save != null) ? this.save
            : Strings.isTrue(RequestCycle.get().getRequest().getRequestParameters()
                    .getParameterValue(SAVE_PARAMETER).toString());

    BrixFileNode node = (BrixFileNode) this.node.getObject();

    if (!SitePlugin.get().canViewNode(node, Action.Context.PRESENTATION)) {
        throw Brix.get().getForbiddenException();
    }//from   ww  w.j  a v a2  s  . co  m

    WebResponse response = (WebResponse) RequestCycle.get().getResponse();

    response.setContentType(node.getMimeType());

    Date lastModified = node.getLastModified();
    response.setLastModifiedTime(Time.valueOf(lastModified));

    try {
        final HttpServletRequest r = (HttpServletRequest) requestCycle.getRequest().getContainerRequest();
        String since = r.getHeader("If-Modified-Since");
        if (!save && since != null) {
            Date d = new Date(r.getDateHeader("If-Modified-Since"));

            // the weird toString comparison is to prevent comparing
            // milliseconds
            if (d.after(lastModified) || d.toString().equals(lastModified.toString())) {
                response.setContentLength(node.getContentLength());
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            }
        }
        String fileName = node.getName();
        long length = node.getContentLength();
        HttpServletResponse httpServletResponse = (HttpServletResponse) response.getContainerResponse();
        InputStream stream = node.getDataAsStream();

        new Streamer(length, stream, fileName, save, r, httpServletResponse).stream();
    } catch (Exception e) {
        log.error("Error writing resource data to content", e);
    }
}

From source file:org.hippoecm.frontend.dialog.NodeInfo.java

License:Apache License

/**
 * @see org.apache.wicket.request.IRequestHandler#respond(org.apache.wicket.request.IRequestCycle)
 *///from   w w  w  . j  a  v  a  2s . c  o  m
public void respond(IRequestCycle requestCycle) {
    final Application app = Application.get();

    // Determine encoding
    final String encoding = app.getRequestCycleSettings().getResponseRequestEncoding();

    // Set content type based on markup type for page
    final WebResponse response = (WebResponse) requestCycle.getResponse();
    response.setContentType("text/xml; charset=" + encoding);

    // Make sure it is not cached by a client
    response.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
    response.setHeader("Cache-Control", "no-cache, must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setLastModifiedTime(Time.now());

    // set filename
    response.setAttachmentHeader(FILE_NAME);

    UserSession session = UserSession.get();

    try {

        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        try {
            PrintStream ps = new PrintStream(fos);

            ps.println("Hippo CMS Error report");
            ps.println("========================================");

            ps.print("version : ");
            ps.print(getCMSVersion());
            ps.println();

            ps.print("time    : ");
            ps.print(Time.now().toString());
            ps.println();

            javax.jcr.Session jcrSession = session.getJcrSession();
            if (jcrSession != null) {
                ps.print("user    : ");
                ps.print(session.getJcrSession().getUserID());
                ps.println();
            }

            HttpServletRequest hsr = (HttpServletRequest) RequestCycle.get().getRequest().getContainerRequest();
            ps.print("server  : ");
            ps.print(hsr.getServerName());
            ps.println();

            String node = System.getProperty("org.apache.jackrabbit.core.cluster.node_id");
            if (node != null) {
                ps.print("node    : ");
                ps.print(hsr.getServerName());
                ps.println();
            }

            ps.print("java    : ");
            ps.print(System.getProperty("java.version"));
            ps.println();

            ps.println();

            ps.println("error   :");
            ps.print(ex.getClass().getName());
            ps.print(": ");
            ps.print(ex.getLocalizedMessage());
            ps.println();

            ex.printStackTrace(ps);
            ps.println();

            if (jcrSession != null) {
                ps.println("session :");

                Map<NodePath, NodeInfo> modificationTree = getModificationTree(jcrSession);
                int widths[] = new int[Column.values().length];
                for (Map.Entry<NodePath, NodeInfo> entry : modificationTree.entrySet()) {
                    NodeInfo info = entry.getValue();
                    for (Column col : Column.values()) {
                        int width = col.getWidth(info);
                        if (width > widths[col.ordinal()]) {
                            widths[col.ordinal()] = width;
                        }
                    }
                }
                int row = 0;
                for (Map.Entry<NodePath, NodeInfo> entry : modificationTree.entrySet()) {
                    NodeInfo info = entry.getValue();
                    if (row % 5 == 0 && row != 0) {
                        ps.println();
                    }
                    ps.print("  ");
                    for (Column col : Column.values()) {
                        String val = col.render(info);
                        ps.print(val);
                        int fill = widths[col.ordinal()] - val.length();
                        for (int i = 0; i < fill; i++) {
                            ps.print(' ');
                        }
                        ps.print("   ");
                    }
                    ps.println();
                    row++;
                }
            }
            ps.println();

            ps.println("========================================");

        } finally {
            fos.close();
        }
        response.write(fos.toByteArray());
    } catch (FileNotFoundException e) {
        log.error("Tempfile missing during export", e);
    } catch (IOException e) {
        log.error("IOException during export", e);
    }
}

From source file:org.jaulp.wicket.base.util.WicketComponentUtils.java

License:Apache License

/**
 * Disables caching from a WebPage. To disable the cache override the WebPage.setHeader() and
 * invoke this method. For instance:<code>
 * protected void setHeaders(WebResponse response) {
 * &nbsp;&nbsp;&nbsp;&nbsp;WicketComponentUtils.disableCaching(response);
 * }//w  w  w.j  a va2 s  .c om
 * </code>
 * 
 * @param response
 *            the response
 */
public static void disableCaching(WebResponse response) {
    response.setLastModifiedTime(Time.now());
    HttpServletResponse httpServletResponse = getHttpServletResponse(response);
    if (httpServletResponse != null) {
        httpServletResponse.addHeader("Cache-Control", "max-age=0");
        httpServletResponse.setDateHeader("Expires", 0);
    }
}