|
|
|
@ -16,11 +16,16 @@
|
|
|
|
|
|
|
|
|
|
package se.kb.oai.ore; |
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HostnameVerifier; |
|
|
|
|
import javax.net.ssl.HttpsURLConnection; |
|
|
|
|
import javax.net.ssl.SSLSession; |
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.net.URISyntaxException; |
|
|
|
|
import java.net.URL; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* An aggregated resource is a resource, that together with other resources, |
|
|
|
@ -70,16 +75,51 @@ public class AggregatedResource extends AggregateBase {
|
|
|
|
|
public void setMimeType(String mimetype) { |
|
|
|
|
this.mimetype = mimetype; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get an <code>InputStream</code> for the content held in this |
|
|
|
|
* <code>AggreagatedResource</code>. |
|
|
|
|
* |
|
|
|
|
* Get an <code>InputStream</code> for the content held in this |
|
|
|
|
* <code>AggreagatedResource</code>. |
|
|
|
|
* |
|
|
|
|
* @return a stream to the content |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
public InputStream getContent() throws IOException { |
|
|
|
|
return id.toURL().openStream(); |
|
|
|
|
public InputStream getContent() throws IOException, URISyntaxException { |
|
|
|
|
URL url; |
|
|
|
|
String uri_schema; |
|
|
|
|
HttpURLConnection con = null; |
|
|
|
|
boolean redirect = true; |
|
|
|
|
|
|
|
|
|
HostnameVerifier hostnameVerifier = new HostnameVerifier() { |
|
|
|
|
public boolean verify(String hostname, SSLSession session) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
while (redirect) { |
|
|
|
|
redirect = false; |
|
|
|
|
uri_schema = id.getScheme(); |
|
|
|
|
url = id.toURL(); |
|
|
|
|
if (uri_schema.equals("https")) { |
|
|
|
|
HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); |
|
|
|
|
https.setHostnameVerifier(hostnameVerifier); |
|
|
|
|
con = https; |
|
|
|
|
} else if (uri_schema.equals("http")) { |
|
|
|
|
con = (HttpURLConnection) url.openConnection(); |
|
|
|
|
} |
|
|
|
|
int status = con.getResponseCode(); |
|
|
|
|
if (status != HttpURLConnection.HTTP_OK) { |
|
|
|
|
if (status == HttpURLConnection.HTTP_MOVED_TEMP |
|
|
|
|
|| status == HttpURLConnection.HTTP_MOVED_PERM |
|
|
|
|
|| status == HttpURLConnection.HTTP_SEE_OTHER) { |
|
|
|
|
redirect = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (redirect) { |
|
|
|
|
new URI(con.getHeaderField("Location")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return con.getInputStream(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -89,7 +129,7 @@ public class AggregatedResource extends AggregateBase {
|
|
|
|
|
* @return a stream to the content |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
public String getContentAsString() throws IOException { |
|
|
|
|
public String getContentAsString() throws IOException, URISyntaxException { |
|
|
|
|
InputStream in = getContent(); |
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
|
|
|
byte[] bytes = new byte[4 * 1024]; |
|
|
|
|