Commit 26bd1f7d authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Replaced custom reading of url content by apache commons ioutils

parent e754a6ee
......@@ -91,6 +91,11 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
......
......@@ -16,19 +16,20 @@
*/
package de.cebitec.common.dbxref.api;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
/**
*
* @author cb
*/
public abstract class AbstractResource implements Resource {
protected final URI uri;
public AbstractResource(URI uri) {
......@@ -47,32 +48,19 @@ public abstract class AbstractResource implements Resource {
openConnection.connect();
int responseCode = openConnection.getResponseCode();
openConnection.disconnect();
if (responseCode >= 400)
if (responseCode >= 400) {
throw new UnsupportedOperationException("Invalid url");
}
} catch (IOException ex) {
Logger.getLogger(AbstractResource.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
String s = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(uri.toURL().openStream()))) {
//s = "content: "+uri.toURL().openConnection().getContent().toString();
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
s = sb.toString();
} catch (Exception ex) {
// applikations exception werfen
try (InputStream stream = uri.toURL().openStream()) {
return IOUtils.toString(stream);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
//return
return s;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment