View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.templatingkit.resources;
35  
36  import info.magnolia.cms.beans.config.URI2RepositoryManager;
37  import info.magnolia.cms.beans.config.URI2RepositoryMapping;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.jcr.util.SessionUtil;
41  import info.magnolia.link.LinkUtil;
42  import info.magnolia.module.templatingkit.sites.SiteManager;
43  import info.magnolia.objectfactory.Components;
44  import info.magnolia.templating.functions.TemplatingFunctions;
45  
46  import java.util.Calendar;
47  
48  import javax.inject.Inject;
49  import javax.jcr.Node;
50  import javax.jcr.PathNotFoundException;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.ValueFormatException;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.apache.commons.lang.time.FastDateFormat;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Resource Bean Used to encapsulate Physical resources as JS, or CSS files.
61   */
62  public class Resource {
63  
64      private final static Logger log = LoggerFactory.getLogger(Resource.class);
65  
66      private static final FastDateFormat TIME_STAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd-HH-mm-ss-SSS");
67  
68      private boolean farFutureCaching = false;
69  
70      private String link;
71  
72      private String conditionalComment;
73  
74      private URI2RepositoryManager uri2RepositoryManager;
75  
76      private TemplatingFunctions templatingFunctions;
77  
78      private SiteManager siteManager = Components.getComponent(SiteManager.class);
79  
80      @Inject
81      public Resource(URI2RepositoryManager uri2RepositoryManager, TemplatingFunctions templatingFunctions) {
82          this.templatingFunctions = templatingFunctions;
83          this.uri2RepositoryManager = uri2RepositoryManager;
84      }
85  
86      public String getLink() {
87          // TODO handle null link
88          if (LinkUtil.EXTERNAL_LINK_PATTERN.matcher(link).matches()) {
89              return link;
90          }
91          String linkPrefix = siteManager.getLinkPrefix(MgnlContext.getAggregationState().getMainContent().getJCRNode());
92          String currentLink = linkPrefix + link;
93  
94          if (farFutureCaching) {
95              String timeStamp = null;
96              try {
97                  timeStamp = getFarFutureCachingTimeStamp(currentLink);
98              } catch (Exception e) {
99                  log.error("Could not determind the Modified or creation date for " + currentLink, e);
100             }
101             if (timeStamp != null) {
102                 String ext = StringUtils.substringAfterLast(currentLink, ".");
103                 String withoutExt = StringUtils.substringBeforeLast(currentLink, ".");
104                 return withoutExt + "." + timeStamp + ".cache." + ext;
105             }
106         }
107         return currentLink;
108     }
109 
110     protected String getFarFutureCachingTimeStamp(String currentLink) throws PathNotFoundException, ValueFormatException, RepositoryException {
111         URI2RepositoryMapping mapping = uri2RepositoryManager.getMapping(link);
112         if (mapping != null) {
113             Node content = SessionUtil.getNode(mapping.getRepository(), mapping.getHandle(link));
114             if (content != null) {
115                 Calendar date = NodeTypes.LastModified.getLastModified(content);
116                 if (date != null) {
117                     return TIME_STAMP_FORMAT.format(date);
118                 }
119             }
120         }
121         return null;
122     }
123 
124     public void setLink(String link) {
125         this.link = link;
126     }
127 
128     public boolean isFarFutureCaching() {
129         return farFutureCaching;
130     }
131 
132     public void setFarFutureCaching(boolean farFutureCaching) {
133         this.farFutureCaching = farFutureCaching;
134     }
135 
136     public String getConditionalComment() {
137         return conditionalComment;
138     }
139 
140     public void setConditionalComment(String conditionalComment) {
141         this.conditionalComment = conditionalComment;
142     }
143 
144 }