Skip to content

Commit 2ea12b6

Browse files
committed
Resolve #240: merge branch 'philboeselager-lobid-organisations/#issue49'
Pull request with commits: - Enable RestMap and deliver test - RestMap: catch all exceptions by returning null
2 parents 9c7e543 + a72b80c commit 2ea12b6

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/main/java/org/culturegraph/mf/morph/maps/RestMap.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,69 @@
1515
*/
1616
package org.culturegraph.mf.morph.maps;
1717

18+
import java.io.BufferedReader;
1819
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.InputStreamReader;
22+
import java.net.URI;
23+
import java.net.URISyntaxException;
1924
import java.net.URL;
20-
import java.net.URLConnection;
25+
import java.nio.charset.Charset;
2126
import java.util.regex.Matcher;
2227
import java.util.regex.Pattern;
2328

24-
import org.culturegraph.mf.exceptions.MetafactureException;
25-
2629
/**
27-
* NOT WORKING YET
28-
*
29-
* @author "Markus Michael Geipel"
30+
* @author "Markus Michael Geipel", "Philipp v. Böselager"
3031
*
3132
*/
3233
public final class RestMap extends AbstractReadOnlyMap<String, String> {
3334

34-
private static final Pattern VAR_PATTERN = Pattern.compile("${key}",
35-
Pattern.LITERAL);
35+
private static final Pattern VAR_PATTERN = Pattern.compile("${key}", Pattern.LITERAL);
36+
private String charsetName = "UTF-8";
3637
private String url;
3738

38-
public void setUrl(final String url) {
39+
public RestMap() {
40+
}
41+
42+
public RestMap(String url) {
3943
this.url = url;
4044
}
4145

4246
@Override
4347
public String get(final Object key) {
4448
final Matcher matcher = VAR_PATTERN.matcher(url);
4549
try {
46-
final URL url = new URL(matcher.replaceAll(key.toString()));
47-
final URLConnection con = url.openConnection();
48-
// TODO correctly read from connection!
49-
return (String) con.getContent();
50+
String urlString = matcher.replaceAll(key.toString());
51+
return readFromUrl(urlString);
52+
} catch (IOException | URISyntaxException e) {
53+
// There was no data result for the given URL
54+
return null;
55+
}
56+
}
5057

51-
} catch (final IOException e) {
52-
throw new MetafactureException(e);
58+
private String readFromUrl(final String url) throws IOException, URISyntaxException {
59+
InputStream inputStream = new URL(new URI(url.replace(" ", "%20")).toASCIIString()).openConnection()
60+
.getInputStream();
61+
try {
62+
BufferedReader reader = new BufferedReader(
63+
new InputStreamReader(inputStream, Charset.forName(charsetName)));
64+
StringBuilder stringBuffer = new StringBuilder();
65+
int value;
66+
while ((value = reader.read()) != -1) {
67+
stringBuffer.append((char) value);
68+
}
69+
return stringBuffer.toString();
70+
} finally {
71+
inputStream.close();
5372
}
5473
}
5574

75+
public void setUrl(final String url) {
76+
this.url = url;
77+
}
78+
79+
public void setCharsetName(String name) {
80+
charsetName = name;
81+
}
82+
5683
}

0 commit comments

Comments
 (0)