FileRequestParameter.java
3.34 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* org.apache.commons.io.IOUtils
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.submitutils;
import com.adobe.aemds.guide.service.GuideException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileRequestParameter
implements RequestParameter {
private String fileName;
private String contentType;
private byte[] fileBytes;
private Logger logger = LoggerFactory.getLogger(FileRequestParameter.class);
public FileRequestParameter(String fileName, Resource fileResource) {
this.fileName = fileName;
try {
if (fileResource != null) {
Node jcrNode = (Node)fileResource.adaptTo(Node.class);
Node jcrContent = jcrNode.getNode("jcr:content");
InputStream is = jcrContent.getProperty("jcr:data").getBinary().getStream();
this.fileBytes = IOUtils.toByteArray((InputStream)is);
is.close();
this.contentType = jcrContent.getProperty("jcr:mimeType").getString();
}
}
catch (Exception e) {
this.logger.error("Exception in getting initialising filebytes and content type : " + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
}
public FileRequestParameter(String fileName, byte[] fileBytes, String contentType) {
this.fileBytes = (byte[])fileBytes.clone();
this.fileName = fileName;
this.contentType = contentType;
}
public InputStream getInputStream() {
ByteArrayInputStream is = null;
try {
if (this.fileBytes != null) {
is = new ByteArrayInputStream(this.fileBytes);
}
}
catch (Exception e) {
this.logger.error("Exception in getting inputstream : " + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
return is;
}
public String getFileName() {
return this.fileName;
}
public boolean isFormField() {
return false;
}
public String getName() {
return this.fileName;
}
public String getContentType() {
return this.contentType;
}
public byte[] get() {
return this.fileBytes;
}
public String getString() {
return new String(this.fileBytes);
}
public String getString(String encoding) {
String result = null;
try {
result = new String(this.fileBytes, encoding);
}
catch (UnsupportedEncodingException e) {
this.logger.error("Exception in encoding : " + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
return result;
}
public long getSize() {
byte[] bytes = this.get();
return bytes.length;
}
}