View Javadoc

1   /**
2    * This file Copyright (c) 2008-2013 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.MetaDataUtil;
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   * @author pbracher
63   * @version $Id$
64   */
65  public class Resource {
66  
67      private final static Logger log = LoggerFactory.getLogger(Resource.class);
68  
69      private static final FastDateFormat TIME_STAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd-HH-mm-ss-SSS");
70  
71      private boolean farFutureCaching = false;
72  
73      private String link;
74  
75      private String conditionalComment;
76  
77      private URI2RepositoryManager uri2RepositoryManager;
78  
79      private TemplatingFunctions templatingFunctions;
80  
81      private SiteManager siteManager = Components.getComponent(SiteManager.class);
82  
83      @Inject
84      public Resource(URI2RepositoryManager uri2RepositoryManager, TemplatingFunctions templatingFunctions) {
85          this.templatingFunctions = templatingFunctions;
86          this.uri2RepositoryManager = uri2RepositoryManager;
87      }
88  
89      public String getLink() {
90          // TODO handle null link
91          if (LinkUtil.EXTERNAL_LINK_PATTERN.matcher(link).matches()) {
92              return link;
93          }
94          String linkPrefix = siteManager.getLinkPrefix(MgnlContext.getAggregationState().getMainContent().getJCRNode());
95          String currentLink = linkPrefix + link;
96  
97          if (farFutureCaching) {
98              String timeStamp = null;
99              try {
100                 timeStamp = getFarFutureCachingTimeStamp(currentLink);
101             } catch (Exception e) {
102                 log.error("Could not determind the Modified or creation date for " + currentLink, e);
103             }
104             if (timeStamp != null) {
105                 String ext = StringUtils.substringAfterLast(currentLink, ".");
106                 String withoutExt = StringUtils.substringBeforeLast(currentLink, ".");
107                 return withoutExt + "." + timeStamp + ".cache." + ext;
108             }
109         }
110         return currentLink;
111     }
112 
113     protected String getFarFutureCachingTimeStamp(String currentLink) throws PathNotFoundException, ValueFormatException, RepositoryException {
114         URI2RepositoryMapping mapping = uri2RepositoryManager.getMapping(link);
115         if (mapping != null) {
116             Node content = SessionUtil.getNode(mapping.getRepository(), mapping.getHandle(link));
117             if (content != null) {
118                 Calendar date = MetaDataUtil.getLastModification(content);
119                 if (date == null) {
120                     date = MetaDataUtil.getMetaData(content).getCreationDate();
121                 }
122                 if (date != null) {
123                     return TIME_STAMP_FORMAT.format(date);
124                 }
125             }
126         }
127         return null;
128     }
129 
130     public void setLink(String link) {
131         this.link = link;
132     }
133 
134     public boolean isFarFutureCaching() {
135         return farFutureCaching;
136     }
137 
138     public void setFarFutureCaching(boolean farFutureCaching) {
139         this.farFutureCaching = farFutureCaching;
140     }
141 
142     public String getConditionalComment() {
143         return conditionalComment;
144     }
145 
146     public void setConditionalComment(String conditionalComment) {
147         this.conditionalComment = conditionalComment;
148     }
149 
150 }