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 Dec 14, 2018
1 parent f181678 commit 33e28a3
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 72 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(NetworkUtil.openConnection(url))) {
try (InputStream inputStream = NetworkUtil.getUrlInputStream(url.openConnection(), BundleTools.SESSION_TAGS_FILE)) {
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 @@ -12,6 +12,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

Expand All @@ -23,6 +24,7 @@
import org.weasis.core.api.explorer.model.AbstractFileModel;
import org.weasis.core.api.gui.util.AppProperties;
import org.weasis.core.api.gui.util.GuiExecutor;
import org.weasis.core.api.service.BundleTools;
import org.weasis.core.api.util.FileUtil;
import org.weasis.core.api.util.NetworkUtil;

Expand All @@ -41,7 +43,9 @@ 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(NetworkUtil.openConnection( new URL(url)), outFile);
InputStream httpCon =
NetworkUtil.getUrlInputStream(new URL(url).openConnection(), BundleTools.SESSION_TAGS_FILE);
FileUtil.writeStreamWithIOException(httpCon, 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 @@ -20,7 +20,6 @@
import java.io.OutputStream;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -267,31 +266,6 @@ public static boolean isFileExtensionMatching(File file, String[] extensions) {
return false;
}

/**
* Write URL content into a file
*
* @param urlConnection
* @param outFile
* @throws StreamIOException
*/
public static void writeStreamWithIOException(URLConnection urlConnection, File outFile) throws StreamIOException {
try (InputStream urlInputStream = NetworkUtil.getUrlInputStream(urlConnection);
FileOutputStream outputStream = new FileOutputStream(outFile)) {
byte[] buf = new byte[FILE_BUFFER];
int offset;
while ((offset = urlInputStream.read(buf)) > 0) {
outputStream.write(buf, 0, offset);
}
outputStream.flush();
} catch (StreamIOException e) {
FileUtil.delete(outFile);
throw e;
} catch (IOException e) {
FileUtil.delete(outFile);
throw new StreamIOException(e);
}
}

/**
* Write inputStream content into a file
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

private static final int MAX_REDIRECTS = 3;

private NetworkUtil() {
Expand All @@ -56,20 +58,35 @@ 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),
return getUrlInputStream(urlConnection, null);
}

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

public static InputStream getUrlInputStream(URLConnection urlConnection, int connectTimeout, int readTimeout)
public static InputStream getUrlInputStream(URLConnection urlConnection, Map<String, String> headers, int connectTimeout, int readTimeout)
throws StreamIOException {
if (headers != null && headers.size() > 0) {
for (Iterator<Entry<String, String>> iter = headers.entrySet().iterator(); iter.hasNext();) {
Entry<String, String> element = iter.next();
urlConnection.addRequestProperty(element.getKey(), element.getValue());
}
}
urlConnection.setConnectTimeout(connectTimeout);
urlConnection.setReadTimeout(readTimeout);
if (urlConnection instanceof HttpURLConnection) {
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
try {
int responseCode = httpURLConnection.getResponseCode();
if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
LOGGER.warn("http Status {} - {}", responseCode, httpURLConnection.getResponseMessage());// $NON-NLS-1$ //$NON-NLS-1$
int code = httpURLConnection.getResponseCode();
if (code < HttpURLConnection.HTTP_OK || code >= HttpURLConnection.HTTP_MULT_CHOICE) {
if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_SEE_OTHER) {
return getRedirectionStream(httpURLConnection, headers);
}

LOGGER.warn("http Status {} - {}", code, httpURLConnection.getResponseMessage());// $NON-NLS-1$ //$NON-NLS-1$

// Following is only intended LOG more info about Http Server Error
if (LOGGER.isTraceEnabled()) {
Expand All @@ -91,18 +108,25 @@ public static InputStream getUrlInputStream(URLConnection urlConnection, int con
}
}

public static URLConnection openConnection(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
public static InputStream getRedirectionStream(URLConnection urlConnection, Map<String, String> headers) throws IOException {
String redirect = urlConnection.getHeaderField("Location");
for (int i = 0; i < MAX_REDIRECTS; i++) {
if (redirect != null) {
String cookies = urlConnection.getHeaderField("Set-Cookie");
urlConnection = new URL(redirect).openConnection();
urlConnection.setRequestProperty("Cookie", cookies);
if (headers != null && headers.size() > 0) {
for (Iterator<Entry<String, String>> iter = headers.entrySet().iterator(); iter.hasNext();) {
Entry<String, String> element = iter.next();
urlConnection.addRequestProperty(element.getKey(), element.getValue());
}
}
redirect = urlConnection.getHeaderField("Location");
} else {
break;
}
}
return urlConnection;
return urlConnection.getInputStream();
}

private static void writeErrorResponse(HttpURLConnection httpURLConnection) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.weasis.core.api.gui.util.AbstractItemDialogPage;
import org.weasis.core.api.gui.util.AppProperties;
import org.weasis.core.api.gui.util.FileFormatFilter;
import org.weasis.core.api.service.BundleTools;
import org.weasis.core.api.util.FileUtil;
import org.weasis.core.api.util.NetworkUtil;
import org.weasis.core.api.util.StringUtil;
Expand Down Expand Up @@ -146,7 +147,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(NetworkUtil.openConnection(u.toURL()));
stream = NetworkUtil.getUrlInputStream(u.toURL().openConnection(), BundleTools.SESSION_TAGS_FILE);
FileUtil.writeStreamWithIOException(stream, tempFile);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -259,19 +257,12 @@ public static Collection<LoadSeries> buildDicomSeriesFromXml(URI uri, final Dico
XMLInputFactory xmlif = XMLInputFactory.newInstance();

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

if (BundleTools.SESSION_TAGS_MANIFEST.size() > 0) {
for (Iterator<Entry<String, String>> iter =
BundleTools.SESSION_TAGS_MANIFEST.entrySet().iterator(); iter.hasNext();) {
Entry<String, String> element = iter.next();
urlConnection.setRequestProperty(element.getKey(), element.getValue());
}
}

URLConnection urlConnection = uri.toURL().openConnection();
urlConnection.setUseCaches(false);

LOGGER.info("Downloading XML manifest: {}", path); //$NON-NLS-1$
InputStream urlInputStream = NetworkUtil.getUrlInputStream(urlConnection, StringUtil.getInt(System.getProperty("UrlConnectionTimeout"), 7000) , StringUtil.getInt(System.getProperty("UrlReadTimeout"), 15000) * 2);
InputStream urlInputStream = NetworkUtil.getUrlInputStream(urlConnection, BundleTools.SESSION_TAGS_MANIFEST, StringUtil.getInt(System.getProperty("UrlConnectionTimeout"), 7000) , StringUtil.getInt(System.getProperty("UrlReadTimeout"), 15000) * 2);

if (path.endsWith(".gz")) { //$NON-NLS-1$
stream = new BufferedInputStream(new GZIPInputStream(urlInputStream));
Expand Down Expand Up @@ -413,7 +404,7 @@ private static void readQuery(XMLStreamReader xmler, ReaderParams params, final
String httpkey = TagUtil.getTagAttribute(xmler, "key", null); //$NON-NLS-1$
String httpvalue = TagUtil.getTagAttribute(xmler, "value", null); //$NON-NLS-1$
wadoParameters.addHttpTag(httpkey, httpvalue);
// <Message> tag
// <Message> tag
} else if ("Message".equals(key)) { //$NON-NLS-1$
final String title = TagUtil.getTagAttribute(xmler, "title", null); //$NON-NLS-1$
final String message = TagUtil.getTagAttribute(xmler, "description", null); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -54,6 +56,7 @@
import org.weasis.core.api.media.data.TagW.TagType;
import org.weasis.core.api.media.data.Thumbnail;
import org.weasis.core.api.service.AuditLog;
import org.weasis.core.api.service.BundleTools;
import org.weasis.core.api.util.FileUtil;
import org.weasis.core.api.util.NetworkUtil;
import org.weasis.core.api.util.StreamIOException;
Expand Down Expand Up @@ -382,7 +385,7 @@ private Boolean startDownload() {
request.append(instance.getDirectDownloadFile());
}
request.append(wado.getAdditionnalParameters());
urlConnection = initConnection(new URL(request.toString()), wado);
urlConnection = new URL(request.toString()).openConnection();
} catch (MalformedURLException e) {
LOGGER.error("Invalid URL", e); //$NON-NLS-1$
continue;
Expand All @@ -392,7 +395,7 @@ private Boolean startDownload() {
continue;
}
LOGGER.debug("Download DICOM instance {} index {}.", urlConnection, k); //$NON-NLS-1$
Download ref = new Download(urlConnection);
Download ref = new Download(urlConnection, wado);
tasks.add(ref);
}

Expand All @@ -407,19 +410,25 @@ private Boolean startDownload() {
return true;
}

private static URLConnection initConnection(URL url, WadoParameters wadoParameters) throws IOException {
// If there is a proxy, it should be already configured
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$
}
if (!wadoParameters.getHttpTaglist().isEmpty()) {
for (HttpTag tag : wadoParameters.getHttpTaglist()) {
urlConnection.setRequestProperty(tag.getKey(), tag.getValue());
private static Map<String, String> getHttpTags(WadoParameters wadoParameters) {
boolean hasBundleTags = !BundleTools.SESSION_TAGS_FILE.isEmpty();
boolean hasWadoTags = wadoParameters != null && wadoParameters.getHttpTaglist() != null;
boolean hasWadoLogin = wadoParameters != null && wadoParameters.getWebLogin() != null;

if (hasWadoTags || hasWadoLogin || hasBundleTags) {
HashMap<String, String> map = new HashMap<>(BundleTools.SESSION_TAGS_FILE);
if (hasWadoTags) {
for (HttpTag tag : wadoParameters.getHttpTaglist()) {
map.put(tag.getKey(), tag.getValue());
}
}
if (hasWadoLogin) {
// Set http login (no protection, only convert in base64)
map.put("Authorization", "Basic " + wadoParameters.getWebLogin());
}
return map;
}
return urlConnection;
return null;
}

public void startDownloadImageReference(final WadoParameters wadoParameters) {
Expand Down Expand Up @@ -470,8 +479,11 @@ 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(NetworkUtil.openConnection(new URL(wadoParameters.getBaseURL() + thumURL)), outFile);
if(outFile.length() == 0) {
InputStream httpCon = NetworkUtil.getUrlInputStream(
new URL(wadoParameters.getBaseURL() + thumURL).openConnection(),
getHttpTags(wadoParameters));
FileUtil.writeStreamWithIOException(httpCon, outFile);
if (outFile.length() == 0) {
throw new IllegalStateException("Thumbnail file is empty"); //$NON-NLS-1$
}
file = outFile;
Expand Down Expand Up @@ -542,11 +554,11 @@ public File getJPEGThumnails(WadoParameters wadoParameters, String StudyUID, Str
+ "&objectUID=" + SOPInstanceUID + "&contentType=image/jpeg&imageQuality=70" + "&rows=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ Thumbnail.MAX_SIZE + "&columns=" + Thumbnail.MAX_SIZE + wadoParameters.getAdditionnalParameters()); //$NON-NLS-1$

URLConnection httpCon = initConnection(url, wadoParameters);
InputStream httpCon = NetworkUtil.getUrlInputStream(url.openConnection(), getHttpTags(wadoParameters));
File outFile = File.createTempFile("tumb_", ".jpg", Thumbnail.THUMBNAIL_CACHE_DIR); //$NON-NLS-1$ //$NON-NLS-2$
LOGGER.debug("Start to download JPEG thbumbnail {} to {}.", url, outFile.getName()); //$NON-NLS-1$
FileUtil.writeStreamWithIOException(httpCon, outFile);
if(outFile.length() == 0) {
if (outFile.length() == 0) {
throw new IllegalStateException("Thumbnail file is empty"); //$NON-NLS-1$
}
return outFile;
Expand Down Expand Up @@ -592,10 +604,12 @@ private int[] generateDownladOrder(final int size) {
class Download implements Callable<Boolean> {

private final URLConnection urlConnection; // download URL
private final WadoParameters wado;
private Status status; // current status of download

public Download(URLConnection urlConnection) {
public Download(URLConnection urlConnection, WadoParameters wado) {
this.urlConnection = urlConnection;
this.wado = wado;
this.status = Status.DOWNLOADING;
}

Expand Down Expand Up @@ -636,8 +650,7 @@ private InputStream replaceToDefaultTSUID() throws IOException {
buffer.append(TransferSyntax.EXPLICIT_VR_LE.getTransferSyntaxUID());
}

final WadoParameters wado = (WadoParameters) dicomSeries.getTagValue(TagW.WadoParameters);
return NetworkUtil.getUrlInputStream(initConnection(new URL(buffer.toString()), wado));
return NetworkUtil.getUrlInputStream(new URL(buffer.toString()).openConnection(), getHttpTags(wado));
}

@Override
Expand Down Expand Up @@ -676,7 +689,7 @@ private boolean process() throws IOException, URISyntaxException {
File tempFile = null;
DicomMediaIO dicomReader = null;

try (InputStream stream = NetworkUtil.getUrlInputStream(urlConnection)) {
try (InputStream stream = NetworkUtil.getUrlInputStream(urlConnection, getHttpTags(wado))) {

if (!writeInCache && getUrl().startsWith("file:")) { //$NON-NLS-1$
cache = false;
Expand Down

0 comments on commit 33e28a3

Please sign in to comment.