- Fixing redirect issues

- Updated version to 0.7
This commit is contained in:
Jodee90 2020-01-28 14:36:34 +02:00
parent 18d9a1542a
commit ba19f0891f
2 changed files with 50 additions and 10 deletions

View File

@ -4,7 +4,7 @@
<groupId>se.kb</groupId>
<artifactId>oai4j</artifactId>
<packaging>jar</packaging>
<version>0.6-SNAPSHOT</version>
<version>0.7-SNAPSHOT</version>
<name>oai4j</name>
<url>http://maven.apache.org</url>
@ -34,8 +34,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>

View File

@ -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,
@ -78,8 +83,43 @@ public class AggregatedResource extends AggregateBase {
* @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];