|
15 | 15 | */ |
16 | 16 | package org.culturegraph.mf.morph.maps; |
17 | 17 |
|
| 18 | +import java.io.BufferedReader; |
18 | 19 | import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.InputStreamReader; |
| 22 | +import java.net.URI; |
| 23 | +import java.net.URISyntaxException; |
19 | 24 | import java.net.URL; |
20 | | -import java.net.URLConnection; |
| 25 | +import java.nio.charset.Charset; |
21 | 26 | import java.util.regex.Matcher; |
22 | 27 | import java.util.regex.Pattern; |
23 | 28 |
|
24 | | -import org.culturegraph.mf.exceptions.MetafactureException; |
25 | | - |
26 | 29 | /** |
27 | | - * NOT WORKING YET |
28 | | - * |
29 | | - * @author "Markus Michael Geipel" |
| 30 | + * @author "Markus Michael Geipel", "Philipp v. Böselager" |
30 | 31 | * |
31 | 32 | */ |
32 | 33 | public final class RestMap extends AbstractReadOnlyMap<String, String> { |
33 | 34 |
|
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"; |
36 | 37 | private String url; |
37 | 38 |
|
38 | | - public void setUrl(final String url) { |
| 39 | + public RestMap() { |
| 40 | + } |
| 41 | + |
| 42 | + public RestMap(String url) { |
39 | 43 | this.url = url; |
40 | 44 | } |
41 | 45 |
|
42 | 46 | @Override |
43 | 47 | public String get(final Object key) { |
44 | 48 | final Matcher matcher = VAR_PATTERN.matcher(url); |
45 | 49 | 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 | + } |
50 | 57 |
|
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(); |
53 | 72 | } |
54 | 73 | } |
55 | 74 |
|
| 75 | + public void setUrl(final String url) { |
| 76 | + this.url = url; |
| 77 | + } |
| 78 | + |
| 79 | + public void setCharsetName(String name) { |
| 80 | + charsetName = name; |
| 81 | + } |
| 82 | + |
56 | 83 | } |
0 commit comments