EntryPreprocessorImpl.java
5.91 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Externalizer
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.tika.io.CloseShieldInputStream
* org.apache.tika.io.IOUtils
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.designimporter.impl;
import com.day.cq.commons.Externalizer;
import com.day.cq.wcm.designimporter.DesignImporterContext;
import com.day.cq.wcm.designimporter.api.EntryPreprocessor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Dictionary;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.tika.io.CloseShieldInputStream;
import org.apache.tika.io.IOUtils;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="%entry.preprocessor.name", description="%entry.preprocessor.description", metatype=1)
@Service
public class EntryPreprocessorImpl
implements EntryPreprocessor {
private static final String TOKEN_DESIGNPATH = "{designPath}";
private static final String DEFAULT_SEARCH_PATTERN = "/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])";
@Property(value={"/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])"})
private static final String PN_SEARCH_PATTERN = "search.pattern";
@Property(value={"$1{designPath}/"})
private static final String PN_REPLACE_PATTERN = "replace.pattern";
@Deprecated
@Reference
Externalizer externalizer;
private Pattern searchPattern = Pattern.compile("/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])");
private String replacePattern = "\"{designPath}";
private Logger logger = LoggerFactory.getLogger(EntryPreprocessorImpl.class);
@Override
public InputStream getProcessedStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
if (entry.matches("(?i)[^.]*\\.js")) {
return this.getProcessedScriptStream(entry, inputStream, designImporterContext);
}
if (entry.matches("(?i)[^.]*\\.css")) {
return this.getProcessedCssStream(entry, inputStream, designImporterContext);
}
if (entry.matches("(?i)[^.]*\\.html")) {
return this.getProcessedHtmlStream(entry, inputStream, designImporterContext);
}
return new CloseShieldInputStream(inputStream);
}
@Activate
protected void activate(ComponentContext context) {
try {
this.searchPattern = Pattern.compile(OsgiUtil.toString(context.getProperties().get("search.pattern"), (String)"/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])"));
}
catch (PatternSyntaxException e) {
this.logger.error("Invalid pattern encountered", (Throwable)e);
}
this.replacePattern = OsgiUtil.toString(context.getProperties().get("replace.pattern"), (String)"");
}
protected InputStream getProcessedCssStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
return this.getProcessedStream(inputStream, designImporterContext);
}
protected InputStream getProcessedHtmlStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
return this.getProcessedStream(inputStream, designImporterContext);
}
protected InputStream getProcessedScriptStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
return this.getProcessedStream(inputStream, designImporterContext);
}
private String findReplace(String input, DesignImporterContext designImporterContext) {
Matcher m = this.searchPattern.matcher(input);
String designPath = this.getDesignPath(designImporterContext);
String replacement = this.replacePattern.replace("{designPath}", designPath);
return m.replaceAll(replacement);
}
private String getDesignPath(DesignImporterContext designImporterContext) {
try {
String designPath = designImporterContext.designNode.getPath();
return designPath;
}
catch (RepositoryException e) {
this.logger.error("Error occurred while trying to access the path of the design node", (Throwable)e);
return "";
}
}
private InputStream getProcessedStream(InputStream inputStream, DesignImporterContext designImporterContext) {
StringWriter stringWriter = new StringWriter();
String encoding = "UTF-8";
try {
IOUtils.copy((InputStream)inputStream, (Writer)stringWriter, (String)encoding);
String contents = stringWriter.toString();
String processedContents = this.findReplace(contents, designImporterContext);
return new ByteArrayInputStream(processedContents.getBytes(encoding));
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
protected void bindExternalizer(Externalizer externalizer) {
this.externalizer = externalizer;
}
protected void unbindExternalizer(Externalizer externalizer) {
if (this.externalizer == externalizer) {
this.externalizer = null;
}
}
}