Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
fixes #1 - UrlDownloaderUsingNtlmCredentials now detects download err…
Browse files Browse the repository at this point in the history
…ors and throws an appropriate exception
  • Loading branch information
danhaywood committed Nov 21, 2016
1 parent 5a0404a commit 5736d12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ To use "out-of-the-box":
<dependency>
<groupId>org.incode.module.docrendering</groupId>
<artifactId>incode-module-docrendering-stringinterpolator-dom</artifactId>
<version>1.13.0</version>
<version>1.13.1</version>
</dependency>
----

Expand Down Expand Up @@ -206,6 +206,7 @@ If you want to use the current `-SNAPSHOT`, then the steps are the same as above

== Change Log

* `1.13.1` - released against Isis 1.13.0, fixes link:https://github.com/incodehq/incode-module-docrendering-stringinterpolator/issues/1[#1]
* `1.13.0` - released against Isis 1.13.0.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
Expand All @@ -14,8 +15,10 @@

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.io.CharStreams;

import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
Expand All @@ -26,6 +29,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.isis.applib.ApplicationException;
import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;

Expand Down Expand Up @@ -111,6 +115,36 @@ public byte[] download(final URL url) throws IOException {

HttpGet httpGet = new HttpGet(url.getFile());
try (final CloseableHttpResponse httpResponse = httpclient.execute(target, httpGet, context)) {
final StatusLine statusLine = httpResponse.getStatusLine();
if(statusLine == null) {
throw new ApplicationException(String.format(
"Could not obtain response statusLine for %s", url.toExternalForm()));
}
final int statusCode = statusLine.getStatusCode();
if(statusCode != 200) {

// try to read content of entity, but ignore any exceptions
// because we are simply trying to get extra data to report in the exception below
InputStreamReader inputStreamReader = null;
String entityContent = "";
try {
inputStreamReader = new InputStreamReader(httpResponse.getEntity().getContent());
entityContent = CharStreams.toString(inputStreamReader);
} catch (java.lang.Throwable ex) {
// ignore
} finally {
if(inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
// ignore
}
}
}

throw new ApplicationException(String.format(
"Failed to download from '%s': %d %s\n%s", url.toExternalForm(), statusCode, statusLine.getReasonPhrase(), entityContent));
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(baos);
return baos.toByteArray();
Expand Down

0 comments on commit 5736d12

Please sign in to comment.