UrlUtil.java
5.62 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.adobe.granite.resourceresolverhelper.ResourceResolverHelper
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.datamanager.impl;
import com.adobe.aemds.datamanager.impl.CRXUrlStreamHandler;
import com.adobe.aemds.datamanager.impl.RepositoryUrlStreamHandler;
import com.adobe.aemds.datamanager.impl.ServiceContainer;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import com.adobe.service.DataManagerOperations;
import com.adobe.service.FileDataBuffer;
import com.adobe.service.InvalidSourceException;
import com.adobe.service.ResolverCallbackHelper;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.concurrent.Callable;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UrlUtil {
private static final Logger logger = LoggerFactory.getLogger(UrlUtil.class);
private static final URLStreamHandler crxHandler = new CRXUrlStreamHandler();
private static final URLStreamHandler repositoryHandler = new RepositoryUrlStreamHandler();
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static void copyFile(InputStream is, String filePath) throws IOException {
int BUFFER_SIZE = 16384;
byte[] buffer = new byte[BUFFER_SIZE];
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath), BUFFER_SIZE);
try {
int len;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
Object var7_6 = null;
}
catch (Throwable var6_8) {
Object var7_7 = null;
outStream.close();
throw var6_8;
}
outStream.close();
{
}
}
private static ResourceResolver getResourceResolver(byte[] context) throws Exception {
String ticketVal = new String(context);
return ResolverCallbackHelper.getResolver(ticketVal);
}
public static URL toURL(String url) throws IOException {
int pidx = url.indexOf(58);
if (pidx == -1) {
throw new MalformedURLException("No protocol specified in URL: " + url);
}
String p = url.substring(0, pidx);
URLStreamHandler handler = p.equals(CRXUrlStreamHandler.CRX_PROTOCOL) ? crxHandler : (p.equals(RepositoryUrlStreamHandler.REPOSITORY_PROTOCOL) ? repositoryHandler : null);
return new URL(null, url, handler);
}
private static InputStream doOpenUrlStream(String url, StringBuffer ct) throws IOException {
URL u = UrlUtil.toURL(url);
URLConnection uc = u.openConnection();
uc.connect();
ct.append(uc.getContentType());
return uc.getInputStream();
}
private static InputStream openUrlStream(final String urlStr, byte[] invocationContext, final StringBuffer contentType) throws Exception {
if (invocationContext != null) {
ResourceResolver rr = UrlUtil.getResourceResolver(invocationContext);
InputStream is = (InputStream)ServiceContainer.resourceResolverHelper.callWith(rr, (Callable)new Callable<InputStream>(){
@Override
public InputStream call() throws Exception {
return UrlUtil.doOpenUrlStream(urlStr, contentType);
}
});
return is;
}
return UrlUtil.doOpenUrlStream(urlStr, contentType);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
static FileDataBuffer createFileDataBufferFromUrl(String urlStr, DataManagerOperations dm) throws InvalidSourceException {
if (urlStr == null) {
throw new InvalidSourceException("Null URL arg in createFileDataBufferFromUrl");
}
try {
FileDataBuffer fileDataBuffer;
String filePath = dm.getTempFileName(true);
StringBuffer contentType = new StringBuffer();
InputStream is = UrlUtil.openUrlStream(urlStr, dm.getInvocationContext(), contentType);
try {
UrlUtil.copyFile(is, filePath);
FileDataBuffer db = dm.createFileDataBuffer(filePath);
db.setContentType(contentType.toString());
fileDataBuffer = db;
Object var8_8 = null;
if (is == null) return fileDataBuffer;
}
catch (Throwable var7_10) {
Object var8_9 = null;
if (is != null) {
is.close();
}
throw var7_10;
}
is.close();
return fileDataBuffer;
}
catch (Throwable e) {
logger.error("Error creating FileDataBuffer from URL " + urlStr, e);
throw new InvalidSourceException(UrlUtil.getMessage(e, urlStr, "CREATE_FILE_DATA_BUFFER"));
}
}
static String getMessage(Throwable t, String urlStr, String operation) {
return "Error of type '" + t.getClass().getName() + "' thrown on attempting operation '" + operation + "' on URL '" + urlStr + "'. Message = [" + t.getMessage() + "]";
}
}