DefaultDataProvider.java
6.86 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.PropertyUnbounded
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceNotFoundException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.forms.common.service.impl;
import com.adobe.forms.common.service.AbstractDataProvider;
import com.adobe.forms.common.service.ContentType;
import com.adobe.forms.common.service.DataOptions;
import com.adobe.forms.common.service.DataProvider;
import com.adobe.forms.common.service.FormsException;
import com.adobe.forms.common.service.PrefillData;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Binary;
import javax.jcr.Node;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceNotFoundException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=1, label="Default Prefill Service Configuration")
@Service(value={DataProvider.class})
public class DefaultDataProvider
extends AbstractDataProvider {
private Logger logger = LoggerFactory.getLogger(DefaultDataProvider.class);
private static final String ALLOWED_DATA_FILE_LOCATIONS = "alloweddataFileLocations";
@Reference
ResourceResolverHelper resourceResolverHelper;
@Property(name="alloweddataFileLocations", label="Data files locations", description="The list of paths allowed for prefill of Adaptive Forms. Paths can either be a string or a regex.", unbounded=PropertyUnbounded.ARRAY, value={"crx://.*"})
private List<Pattern> allowedLocationRegex;
private static String URL_REGEX = "(https?|ftp|file)://.+";
@Property(intValue={2000})
static final String SERVICE_RANKING = "service.ranking";
protected void activate(ComponentContext context) {
Dictionary props = context.getProperties();
String[] allowedPrefillPaths = PropertiesUtil.toStringArray(props.get("alloweddataFileLocations"));
this.allowedLocationRegex = new ArrayList<Pattern>();
for (String allowedPrefillPath : allowedPrefillPaths) {
try {
this.allowedLocationRegex.add(Pattern.compile(allowedPrefillPath));
continue;
}
catch (Exception e) {
this.logger.error("Unable to compile the regex: " + allowedPrefillPath, (Throwable)e);
}
}
}
@Override
public PrefillData getPrefillData(DataOptions options) throws FormsException {
String dataRef = options.getDataRef();
Resource formResource = options.getFormResource();
Resource aemFormContainer = options.getAemFormContainer();
ResourceResolver resolver = null;
InputStream inputStream = null;
PrefillData prefillData = null;
resolver = formResource != null ? formResource.getResourceResolver() : (aemFormContainer != null ? aemFormContainer.getResourceResolver() : this.resourceResolverHelper.getResourceResolver());
if (dataRef != null && this.isPrefillAllowedForPath(dataRef)) {
try {
if (dataRef.startsWith("crx://")) {
Binary data;
if (resolver == null) {
throw new Exception("resource resolver is null while reading resource : " + dataRef);
}
Resource fileResource = resolver.resolve(dataRef.substring("crx://".length()));
if (fileResource instanceof NonExistingResource) {
throw new ResourceNotFoundException("dataRef passed as CRX Resource does not exist at: " + dataRef);
}
Node jcrNode = (Node)fileResource.adaptTo(Node.class);
if (jcrNode.isNodeType("nt:file")) {
jcrNode = jcrNode.getNode("jcr:content");
}
if ((data = jcrNode.getProperty("jcr:data").getBinary()).getSize() < 1) {
throw new IllegalArgumentException("dataRef passed as CRX Resource is Empty at: " + dataRef);
}
inputStream = data.getStream();
} else {
URL url;
if (dataRef.matches(URL_REGEX)) {
url = new URL(dataRef);
} else {
File file = new File(dataRef);
url = file.toURI().toURL();
}
inputStream = url.openStream();
}
}
catch (Exception e) {
this.logger.error("Unable to read data for the dataRef: " + dataRef, (Throwable)e);
}
}
if (inputStream != null) {
prefillData = new PrefillData(inputStream, options.getContentType());
}
return prefillData;
}
@Override
public String getServiceName() {
return null;
}
@Override
public String getServiceDescription() {
return null;
}
public boolean isPrefillAllowedForPath(String path) {
for (Pattern regex : this.allowedLocationRegex) {
Matcher matcher = regex.matcher(path);
if (!matcher.matches()) continue;
return true;
}
return false;
}
protected void bindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
this.resourceResolverHelper = resourceResolverHelper;
}
protected void unbindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
if (this.resourceResolverHelper == resourceResolverHelper) {
this.resourceResolverHelper = null;
}
}
}