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