Skip to content

Commit

Permalink
Http redirection not supported when downloading images #46
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Nov 27, 2018
1 parent bd626e5 commit 17f26d3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ private static byte[] getURIContent(URI uri) {

// note: fastest way to convert inputStream to string according to :
// http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
try (InputStream inputStream = NetworkUtil.getUrlInputStream(url.openConnection())) {
try (InputStream inputStream = NetworkUtil.getUrlInputStream(NetworkUtil.openConnection(url))) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.weasis.core.api.gui.util.AppProperties;
import org.weasis.core.api.gui.util.GuiExecutor;
import org.weasis.core.api.util.FileUtil;
import org.weasis.core.api.util.NetworkUtil;

// TODO required to change the static ref
//@org.osgi.service.component.annotations.Component(immediate = false, property = {
Expand All @@ -40,7 +41,7 @@ private File getFile(String url) {
try {
outFile = File.createTempFile("img_", FileUtil.getExtension(url), IMAGE_CACHE_DIR); // $NON-NLS-2$ //$NON-NLS-1$
LOGGER.debug("Start to download image {} to {}.", url, outFile.getName()); //$NON-NLS-1$
FileUtil.writeStreamWithIOException(new URL(url).openConnection(), outFile);
FileUtil.writeStreamWithIOException(NetworkUtil.openConnection( new URL(url)), outFile);
} catch (IOException e) {
LOGGER.error("Dowloading image", e); //$NON-NLS-1$
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class NetworkUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(NetworkUtil.class);

private static final int MAX_REDIRECTS = 3;

private NetworkUtil() {
}
Expand All @@ -54,7 +56,8 @@ public static URI getURI(String pathOrUri) throws MalformedURLException, URISynt
}

public static InputStream getUrlInputStream(URLConnection urlConnection) throws StreamIOException {
return getUrlInputStream(urlConnection, StringUtil.getInt(System.getProperty("UrlConnectionTimeout"), 5000) , StringUtil.getInt(System.getProperty("UrlReadTimeout"), 15000));
return getUrlInputStream(urlConnection, StringUtil.getInt(System.getProperty("UrlConnectionTimeout"), 5000),
StringUtil.getInt(System.getProperty("UrlReadTimeout"), 15000));
}

public static InputStream getUrlInputStream(URLConnection urlConnection, int connectTimeout, int readTimeout)
Expand Down Expand Up @@ -88,6 +91,20 @@ public static InputStream getUrlInputStream(URLConnection urlConnection, int con
}
}

public static URLConnection openConnection(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
String redirect = urlConnection.getHeaderField("Location");
for (int i = 0; i < MAX_REDIRECTS; i++) {
if (redirect != null) {
urlConnection = new URL(redirect).openConnection();
redirect = urlConnection.getHeaderField("Location");
} else {
break;
}
}
return urlConnection;
}

private static void writeErrorResponse(HttpURLConnection httpURLConnection) throws IOException {
InputStream errorStream = httpURLConnection.getErrorStream();
if (errorStream != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static void loadDicomZip(String uri, DicomModel dicomModel) {
tempFile = new File(u.getPath());
} else {
tempFile = File.createTempFile("dicom_", ".zip", AppProperties.APP_TEMP_DIR); //$NON-NLS-1$ //$NON-NLS-2$
stream = NetworkUtil.getUrlInputStream(u.toURL().openConnection());
stream = NetworkUtil.getUrlInputStream(NetworkUtil.openConnection(u.toURL()));
FileUtil.writeStreamWithIOException(stream, tempFile);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public static Collection<LoadSeries> buildDicomSeriesFromXml(URI uri, final Dico
XMLInputFactory xmlif = XMLInputFactory.newInstance();

String path = uri.getPath();
URLConnection urlConnection = uri.toURL().openConnection();
URLConnection urlConnection = NetworkUtil.openConnection(uri.toURL());

if (BundleTools.SESSION_TAGS_MANIFEST.size() > 0) {
for (Iterator<Entry<String, String>> iter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private Boolean startDownload() {

private static URLConnection initConnection(URL url, WadoParameters wadoParameters) throws IOException {
// If there is a proxy, it should be already configured
URLConnection urlConnection = url.openConnection();
URLConnection urlConnection = NetworkUtil.openConnection(url);
// Set http login (no protection, only convert in base64)
if (wadoParameters.getWebLogin() != null) {
urlConnection.setRequestProperty("Authorization", "Basic " + wadoParameters.getWebLogin()); //$NON-NLS-1$ //$NON-NLS-2$
Expand Down Expand Up @@ -470,8 +470,7 @@ public void loadThumbnail(SopInstance instance, WadoParameters wadoParameters) {
try {
File outFile = File.createTempFile("tumb_", FileUtil.getExtension(thumURL), //$NON-NLS-1$
Thumbnail.THUMBNAIL_CACHE_DIR);
FileUtil.writeStreamWithIOException(
new URL(wadoParameters.getBaseURL() + thumURL).openConnection(), outFile);
FileUtil.writeStreamWithIOException(NetworkUtil.openConnection(new URL(wadoParameters.getBaseURL() + thumURL)), outFile);
if(outFile.length() == 0) {
throw new IllegalStateException("Thumbnail file is empty"); //$NON-NLS-1$
}
Expand Down

0 comments on commit 17f26d3

Please sign in to comment.