1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
62
63
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
90 if (LinkUtil.EXTERNAL_LINK_PATTERN.matcher(link).matches()) {
91 return link;
92 }
93 else {
94
95
96
97
98
99
100 final String linkPrefix;
101 if (MgnlContext.isWebContext()) {
102
103 Content page = MgnlContext.getAggregationState().getMainContent();
104
105 String fullLinkToPage = templatingFunctions.link(page.getJCRNode());
106 String pagePath = uri2RepositoryManager.getURI(LinkFactory.createLink(page));
107
108
109
110
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 }