Skip to content

Commit 9926e13

Browse files
committed
Include HTTP error response body in ResourceLocatorWrapper
1 parent c72a474 commit 9926e13

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

engine/org.eclipse.birt.report.engine/src/org/eclipse/birt/report/engine/util/ResourceLocatorWrapper.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
import java.io.ByteArrayOutputStream;
1818
import java.io.IOException;
1919
import java.io.InputStream;
20+
import java.net.HttpURLConnection;
2021
import java.net.URL;
22+
import java.net.URLConnection;
23+
import java.nio.charset.StandardCharsets;
2124
import java.util.HashMap;
2225
import java.util.Map;
2326
import java.util.logging.Level;
27+
import java.util.logging.LogRecord;
2428
import java.util.logging.Logger;
2529

2630
import org.eclipse.birt.report.engine.i18n.MessageConstants;
@@ -74,15 +78,32 @@ public byte[] findResource(URL url) {
7478
}
7579
byte[] inBytes = cache.get(url);
7680
if (inBytes == null) {
81+
URLConnection connection = null;
7782
try {
78-
InputStream in = url.openStream();
83+
connection = url.openConnection();
84+
InputStream in = connection.getInputStream();
7985
inBytes = getByteArrayFromInputStream(in);
8086
in.close();
8187
cache.put(url, inBytes);
8288
} catch (IOException e) {
83-
logger.log(Level.WARNING, MessageConstants.RESOURCE_NOT_ACCESSIBLE, url.toExternalForm());
89+
LogRecord record = new LogRecord(Level.WARNING, MessageConstants.RESOURCE_NOT_ACCESSIBLE);
90+
record.setParameters(new Object[] { url.toExternalForm() });
91+
record.setLoggerName(logger.getName());
92+
record.setResourceBundle(logger.getResourceBundle());
93+
record.setResourceBundleName(logger.getResourceBundleName());
94+
if (connection instanceof HttpURLConnection httpConn) {
95+
String errorBody = extractErrorBody(httpConn);
96+
if (!errorBody.isEmpty()) {
97+
record.setThrown(new IOException(errorBody));
98+
}
99+
}
100+
logger.log(record);
84101
cache.put(url, DUMMY_BYTES);
85102
return DUMMY_BYTES;
103+
} finally {
104+
if (connection instanceof HttpURLConnection httpConn) {
105+
httpConn.disconnect();
106+
}
86107
}
87108
}
88109
return inBytes;
@@ -102,4 +123,20 @@ private byte[] getByteArrayFromInputStream(InputStream in) throws IOException {
102123
return buffer;
103124
}
104125

126+
private String extractErrorBody(HttpURLConnection httpConn) {
127+
try {
128+
InputStream errorStream = httpConn.getErrorStream();
129+
if (errorStream == null) {
130+
return "No Error-Body";
131+
}
132+
133+
try (errorStream) {
134+
String body = new String(errorStream.readAllBytes(), StandardCharsets.UTF_8);
135+
return body.length() > 1024 ? body.substring(0, 1024) + "..." : body;
136+
}
137+
} catch (Exception ex) {
138+
return "Can't read Error-Body: " + ex.getMessage();
139+
}
140+
}
141+
105142
}

0 commit comments

Comments
 (0)