From ba19f0891ffffe2a6222d8145b1868b825b351c6 Mon Sep 17 00:00:00 2001 From: Jodee90 Date: Tue, 28 Jan 2020 14:36:34 +0200 Subject: [PATCH] - Fixing redirect issues - Updated version to 0.7 --- pom.xml | 6 +-- .../se/kb/oai/ore/AggregatedResource.java | 54 ++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 0f757e2..f223008 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ se.kb oai4j jar - 0.6-SNAPSHOT + 0.7-SNAPSHOT oai4j http://maven.apache.org @@ -34,8 +34,8 @@ maven-compiler-plugin 2.0 - 1.5 - 1.5 + 1.6 + 1.6 diff --git a/src/main/java/se/kb/oai/ore/AggregatedResource.java b/src/main/java/se/kb/oai/ore/AggregatedResource.java index edd1908..fb8d2df 100644 --- a/src/main/java/se/kb/oai/ore/AggregatedResource.java +++ b/src/main/java/se/kb/oai/ore/AggregatedResource.java @@ -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 InputStream for the content held in this - * AggreagatedResource. - * + * Get an InputStream for the content held in this + * AggreagatedResource. + * * @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];