ImporterResource.java
7.59 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.io.IOUtils
* org.apache.http.HttpEntity
* org.apache.http.StatusLine
* org.apache.http.client.methods.CloseableHttpResponse
* org.apache.http.client.methods.HttpGet
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.impl.client.CloseableHttpClient
*/
package com.day.cq.wcm.siteimporter.internal.resource;
import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResourceVisitor;
import com.day.cq.wcm.siteimporter.internal.resource.ReferenceLocation;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
public abstract class ImporterResource {
protected URL location;
protected String content;
protected String contentType;
protected String downloadLocation;
protected ArrayList<ReferenceLocation> references;
protected ImporterContext ctx;
public static final LineProcessor NULL_LINE_PROCESSOR = new LineProcessor(){
@Override
public String onLine(String line) {
return line;
}
};
public ImporterResource(URL location, ImporterContext ctx) {
this.location = location;
this.downloadLocation = "";
this.content = "";
this.ctx = ctx;
}
public ImporterContext getContext() {
return this.ctx;
}
public boolean isDownloaded() {
return !"".equals(this.downloadLocation);
}
public String getDownloadLocation() {
return this.downloadLocation;
}
public void setDownloadLocation(String downloadLocation) {
this.downloadLocation = downloadLocation;
}
public URL getLocation() {
return this.location;
}
public String getMimeType() throws IOException {
String mimeType = this.getContentType();
if (mimeType.contains(";")) {
for (String s : mimeType.split(";")) {
if (s.contains("=")) continue;
mimeType = s.trim();
break;
}
}
return mimeType;
}
public String getCharset() throws IOException {
String charset = "UTF-8";
String contentType = this.getContentType();
if (contentType.contains("charset=")) {
for (String s : contentType.split(";")) {
if (!s.contains("charset=")) continue;
charset = s.trim().split("=")[1];
break;
}
}
return charset;
}
public String getBasePath() {
return this.getLocation().getPath();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public InputStream getDownloadStream() throws IOException {
String fragment;
HttpGet get = null;
CloseableHttpClient client = this.getContext().getHttpClient();
try {
StringBuilder url = new StringBuilder(this.location.toExternalForm());
fragment = "";
int anchorIndex = url.indexOf("#");
if (anchorIndex > 0) {
fragment = url.substring(anchorIndex);
url.delete(anchorIndex, url.length());
}
URL buildUrl = new URL(url.toString());
URI uri = null;
try {
uri = new URI(buildUrl.getProtocol(), buildUrl.getUserInfo(), buildUrl.getHost(), buildUrl.getPort(), buildUrl.getPath(), buildUrl.getQuery(), buildUrl.getRef());
}
catch (URISyntaxException e) {
InputStream inputStream = null;
if (get != null) {
get.releaseConnection();
}
return inputStream;
}
String encodedUrl = uri.toASCIIString() + fragment;
get = new HttpGet(encodedUrl);
CloseableHttpResponse httpResp = client.execute((HttpUriRequest)get);
if (httpResp != null && httpResp.getStatusLine().getStatusCode() == 200) {
byte[] bytes = IOUtils.toByteArray((InputStream)httpResp.getEntity().getContent());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
return byteArrayInputStream;
}
InputStream bytes = null;
return bytes;
}
catch (IllegalArgumentException e) {
fragment = null;
return fragment;
}
finally {
if (get != null) {
get.releaseConnection();
}
}
}
public abstract ArrayList<ReferenceLocation> getExternalReferences();
public String getContent() throws IOException {
return this.getContent(NULL_LINE_PROCESSOR);
}
public String getContent(LineProcessor lineProcessor) throws IOException {
if (!"".equals(this.content)) {
return this.content;
}
byte[] byteArray = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream is = this.getDownloadStream();
if (is != null) {
int count;
while ((count = is.read(byteArray, 0, byteArray.length)) > 0) {
out.write(byteArray, 0, count);
}
is.close();
}
return this.encodeContent(out);
}
String encodeContent(ByteArrayOutputStream out) throws IOException {
String charset = this.getCharset();
this.content = charset != null ? new String(out.toByteArray(), charset) : new String(out.toByteArray());
return this.content;
}
public String reload() throws IOException {
return this.reload(NULL_LINE_PROCESSOR);
}
public String reload(LineProcessor lineProcessor) throws IOException {
this.content = "";
this.contentType = null;
return this.getContent(lineProcessor);
}
public URL toAbsoluteReference(String original) throws MalformedURLException {
if (original.startsWith("http://") || original.startsWith("https://")) {
return new URL(original);
}
URL u = new URL(new URL(this.location, this.getBasePath()), original);
return u;
}
public abstract Object triggerAction(ImporterResourceVisitor var1);
String getContentType() throws IOException {
if (this.contentType == null) {
InputStream is;
URLConnection connection = this.location.openConnection();
this.contentType = connection.getContentType();
if (this.contentType == null) {
this.contentType = URLConnection.guessContentTypeFromName(this.location.getPath());
}
if (this.contentType == null && (is = this.getDownloadStream()) != null) {
this.contentType = URLConnection.guessContentTypeFromStream(is);
}
if (this.contentType == null) {
this.contentType = "application/octet-stream";
}
}
return this.contentType;
}
public static interface LineProcessor {
public String onLine(String var1);
}
}