GuidePDFSubmitHelperImpl.java
9.54 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.addon.dor.DoROptions
* com.adobe.aemds.guide.addon.dor.DoRResult
* com.adobe.aemds.guide.addon.dor.DoRService
* com.adobe.aemds.guide.service.GuideException
* com.adobe.aemds.guide.service.GuidePDFSubmitHelper
* com.adobe.aemds.guide.servlet.GuideSubmitServlet
* com.adobe.aemds.guide.submitutils.FileRequestParameter
* com.adobe.aemds.guide.submitutils.FormFieldRequestParameter
* com.adobe.forms.common.service.FileAttachmentWrapper
* com.adobe.forms.common.submitutils.CustomParameterRequest
* com.adobe.forms.common.submitutils.CustomResponse
* com.adobe.forms.common.submitutils.ParameterMap
* javax.servlet.RequestDispatcher
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestDispatcherOptions
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.addon.service.impl;
import com.adobe.aemds.guide.addon.dor.DoROptions;
import com.adobe.aemds.guide.addon.dor.DoRResult;
import com.adobe.aemds.guide.addon.dor.DoRService;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.service.GuidePDFSubmitHelper;
import com.adobe.aemds.guide.servlet.GuideSubmitServlet;
import com.adobe.aemds.guide.submitutils.FileRequestParameter;
import com.adobe.aemds.guide.submitutils.FormFieldRequestParameter;
import com.adobe.forms.common.service.FileAttachmentWrapper;
import com.adobe.forms.common.submitutils.CustomParameterRequest;
import com.adobe.forms.common.submitutils.CustomResponse;
import com.adobe.forms.common.submitutils.ParameterMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="Guide PDF Submit Helper", description="Guide PDF Submit Helper Service", immediate=1)
@Service(value={GuidePDFSubmitHelper.class})
public class GuidePDFSubmitHelperImpl
implements GuidePDFSubmitHelper {
private Logger logger = LoggerFactory.getLogger(GuidePDFSubmitHelperImpl.class);
@Reference
private DoRService doRService;
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
public ParameterMap getWrappedParameterMapForPdfSubmit(SlingHttpServletRequest request, SlingHttpServletResponse response, ParameterMap wrappedParameterMap) {
try {
byte[] fileBytes = new byte[]{};
String contentType = "";
String signedPdfUrl = wrappedParameterMap.getStringValue("_useSignedPdf");
if (signedPdfUrl != null && signedPdfUrl.length() > 0) {
ParameterMap esignMap = new ParameterMap();
CustomParameterRequest wrappedRequest = new CustomParameterRequest(request, esignMap, "GET");
CustomResponse wrappedResponse = new CustomResponse(response);
Resource forwardResource = wrappedRequest.getResourceResolver().resolve(signedPdfUrl);
if (forwardResource == null) throw new GuideException("URL is not valid");
wrappedRequest.getRequestDispatcher(signedPdfUrl, new RequestDispatcherOptions()).forward((ServletRequest)wrappedRequest, (ServletResponse)wrappedResponse);
fileBytes = wrappedResponse.getCopy();
contentType = wrappedResponse.getContentType();
} else {
String guideContainerPath = wrappedParameterMap.getStringValue("guideContainerPath");
DoROptions options = new DoROptions();
Resource guideContainerPathResouce = request.getResourceResolver().getResource(guideContainerPath);
options.setFormResource(guideContainerPathResouce);
String data = wrappedParameterMap.getStringValue("jcr:data");
if (StringUtils.isEmpty((CharSequence)data)) {
throw new GuideException("Data is not present in the Request");
}
options.setData(data);
String locale = wrappedParameterMap.getStringValue("runtimeLocale");
if (StringUtils.isBlank((CharSequence)locale)) {
locale = "en";
}
Locale local = new Locale(locale);
options.setLocale(local);
List<FileAttachmentWrapper> fileAttachments = this.getFileAttachmentWrapperList(wrappedParameterMap);
if (fileAttachments != null && fileAttachments.size() > 0) {
options.setFileAttachments(fileAttachments);
options.setIncludeAttachments(true);
}
DoRResult result = this.doRService.render(options);
fileBytes = result.getContent();
contentType = result.getContentType();
}
String pdfName = this.getPdfName(request);
RequestParameter[] pdf = new RequestParameter[]{new FileRequestParameter(pdfName, fileBytes, contentType)};
wrappedParameterMap.put((Object)pdfName, (Object)pdf);
GuideSubmitServlet.setRequestAttrPdfName((SlingHttpServletRequest)request, (String)pdfName);
String _guideAttachments = "";
if (wrappedParameterMap.containsKey((Object)"_guideAttachments")) {
_guideAttachments = wrappedParameterMap.getStringValue("_guideAttachments") + ",";
}
_guideAttachments = _guideAttachments + pdfName;
FormFieldRequestParameter guideAttachments = new FormFieldRequestParameter(_guideAttachments);
RequestParameter[] gpm = new RequestParameter[]{guideAttachments};
wrappedParameterMap.put((Object)"_guideAttachments", (Object)gpm);
return wrappedParameterMap;
}
catch (Exception e) {
throw new GuideException(e);
}
}
private List<FileAttachmentWrapper> getFileAttachmentWrapperList(ParameterMap parameterMap) {
ArrayList<FileAttachmentWrapper> fileAttachments = new ArrayList<FileAttachmentWrapper>();
for (Map.Entry param : parameterMap.entrySet()) {
RequestParameter[] rpm = (RequestParameter[])param.getValue();
if (rpm == null || rpm.length <= 0 || rpm[0].isFormField()) continue;
String newFileName = rpm[0].getFileName();
if (StringUtils.contains((CharSequence)newFileName, (CharSequence)"/")) {
newFileName = newFileName.substring(newFileName.indexOf("/") + 1);
newFileName = newFileName.replaceAll("[^a-zA-Z0-9.]", "");
}
FileAttachmentWrapper fileAttachment = new FileAttachmentWrapper(newFileName, rpm[0].getContentType(), rpm[0].get());
fileAttachments.add(fileAttachment);
}
return fileAttachments;
}
private String getPdfName(SlingHttpServletRequest request) {
String pdfName = "";
RequestPathInfo in = request.getRequestPathInfo();
String guideContainerPath = in.getResourcePath();
try {
if (guideContainerPath.lastIndexOf("jcr:content") != -1) {
Resource guideResource;
String cqPage = guideContainerPath.substring(0, guideContainerPath.lastIndexOf("jcr:content") + 11);
Resource pageRes = request.getResourceResolver().getResource(cqPage);
if (pageRes != null) {
ValueMap props = (ValueMap)pageRes.adaptTo(ValueMap.class);
pdfName = (String)props.get("jcr:title", (Object)"");
}
if (pdfName.length() == 0 && (guideResource = request.getResourceResolver().getResource(guideContainerPath)) != null) {
ValueMap props = (ValueMap)guideResource.adaptTo(ValueMap.class);
pdfName = (String)props.get("name", (Object)"");
}
}
if (pdfName.length() == 0) {
pdfName = "guideSubmitPdf";
}
pdfName = pdfName + ".pdf";
}
catch (Exception e) {
throw new GuideException(e);
}
return pdfName;
}
protected void bindDoRService(DoRService doRService) {
this.doRService = doRService;
}
protected void unbindDoRService(DoRService doRService) {
if (this.doRService == doRService) {
this.doRService = null;
}
}
}