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.cms.core.Content;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.SessionUtil;
42  import info.magnolia.link.LinkFactory;
43  import info.magnolia.link.LinkUtil;
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  /**
61   * Resource Bean Used to encapsulate Physical resources as JS, or CSS files.
62   */
63  public class Resource {
64  
65      private final static Logger log = LoggerFactory.getLogger(Resource.class);
66  
67      private static final FastDateFormat TIME_STAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd-HH-mm-ss-SSS");
68  
69      private boolean farFutureCaching = false;
70  
71      private String link;
72  
73      private String conditionalComment;
74  
75      private URI2RepositoryManager uri2RepositoryManager;
76  
77      private TemplatingFunctions templatingFunctions;
78  
79      @Inject
80      public Resource(URI2RepositoryManager uri2RepositoryManager , TemplatingFunctions templatingFunctions) {
81          this.templatingFunctions = templatingFunctions;
82          this.uri2RepositoryManager = uri2RepositoryManager;
83      }
84  
85      public String getLink() {
86          //TODO handle null link
87          if (LinkUtil.EXTERNAL_LINK_PATTERN.matcher(link).matches()) {
88              return link;
89          }
90          else {
91              // FIXME: this is a fix for MGNLSTK-636, a better fix depends on MGNLETK-30
92  
93              // we have to ensure that we are using the same prefix as used for the current page
94              // based on the domain, used locale or if we server a subsite this prefix can look
95              // differently
96              // to avoid duplication of the code we use the following trick
97              final String linkPrefix;
98              if (MgnlContext.isWebContext()) {
99                  // the content we are serving -> the current page
100                 Content page = MgnlContext.getAggregationState().getMainContent();
101 
102                 String fullLinkToPage = templatingFunctions.link(page.getJCRNode());
103                 String pagePath = uri2RepositoryManager.getURI(LinkFactory.createLink(page));
104 
105                 // remove the content related part from the full uri
106                 // now we can create the link safely and we are sure that the multisite filter will
107                 // detect the correct site
108                 linkPrefix = StringUtils.removeEnd(fullLinkToPage, pagePath);
109             }
110             else {
111                 linkPrefix = MgnlContext.getContextPath();
112             }
113             String currentLink = linkPrefix + link;
114 
115             if(farFutureCaching){
116                 String timeStamp = null;
117                 try {
118                     timeStamp = getFarFutureCachingTimeStamp(currentLink);
119                 } catch (Exception  e) {
120                     log.error("Could not determind the Modified or creation date for "+currentLink,e);
121                 }
122                 if(timeStamp != null){
123                     String ext = StringUtils.substringAfterLast(currentLink, ".");
124                     String withoutExt = StringUtils.substringBeforeLast(currentLink, ".");
125                     return withoutExt + "." + timeStamp + ".cache." + ext;
126                 }
127             }
128             return currentLink;
129         }
130     }
131 
132     protected String getFarFutureCachingTimeStamp(String currentLink) throws PathNotFoundException, ValueFormatException, RepositoryException {
133         URI2RepositoryMapping mapping = uri2RepositoryManager.getMapping(link);
134         if(mapping != null){
135             Node content = SessionUtil.getNode(mapping.getRepository(), mapping.getHandle(link));
136             if(content != null){
137                 Calendar date = NodeTypes.LastModified.getLastModified(content);
138                 if (date != null){
139                     return TIME_STAMP_FORMAT.format(date);
140                 }
141             }
142         }
143         return null;
144     }
145 
146     public void setLink(String link) {
147         this.link = link;
148     }
149 
150 
151     public boolean isFarFutureCaching() {
152         return farFutureCaching;
153     }
154 
155     public void setFarFutureCaching(boolean farFutureCaching) {
156         this.farFutureCaching = farFutureCaching;
157     }
158 
159 
160     public String getConditionalComment() {
161         return conditionalComment;
162     }
163 
164 
165     public void setConditionalComment(String conditionalComment) {
166         this.conditionalComment = conditionalComment;
167     }
168 
169 }