Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<targetJdk>1.6</targetJdk>
<targetJdk>1.8</targetJdk>
<antlr.version>3.5.2</antlr.version>
</properties>

Expand Down
57 changes: 42 additions & 15 deletions src/main/java/org/culturegraph/mf/morph/maps/RestMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,69 @@
*/
package org.culturegraph.mf.morph.maps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.culturegraph.mf.exceptions.MetafactureException;

/**
* NOT WORKING YET
*
* @author "Markus Michael Geipel"
* @author "Markus Michael Geipel", "Philipp v. Böselager"
*
*/
public final class RestMap extends AbstractReadOnlyMap<String, String> {

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

public void setUrl(final String url) {
public RestMap() {
}

public RestMap(String url) {
this.url = url;
}

@Override
public String get(final Object key) {
final Matcher matcher = VAR_PATTERN.matcher(url);
try {
final URL url = new URL(matcher.replaceAll(key.toString()));
final URLConnection con = url.openConnection();
// TODO correctly read from connection!
return (String) con.getContent();
String urlString = matcher.replaceAll(key.toString());
return readFromUrl(urlString);
} catch (IOException | URISyntaxException e) {
// There was no data result for the given URL
return null;
}
}

} catch (final IOException e) {
throw new MetafactureException(e);
private String readFromUrl(final String url) throws IOException, URISyntaxException {
InputStream inputStream = new URL(new URI(url.replace(" ", "%20")).toASCIIString()).openConnection()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the url.replace(" ", "%20") necessary here? The test data does not seem to contain a space, or am I misunderstanding something?

If in general characters that need encoding should be supported here, wouldn't a general approach like calling URLEncoder.encode(String s, String enc) with key.toString() when building the URL be safer?

And can we get rid of the .toASCIIString() to support keys like organisations?q=Köln, if that makes any sense?

.getInputStream();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, Charset.forName(charsetName)));
StringBuilder stringBuffer = new StringBuilder();
int value;
while ((value = reader.read()) != -1) {
stringBuffer.append((char) value);
}
return stringBuffer.toString();
} finally {
inputStream.close();
}
}

public void setUrl(final String url) {
this.url = url;
}

public void setCharsetName(String name) {
charsetName = name;
}

}
28 changes: 28 additions & 0 deletions src/test/java/org/culturegraph/mf/morph/maps/RestMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.culturegraph.mf.morph.maps;

import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;

/**
* Tests {@link RestMap}.
*
* @author Philipp v. Böselager
*
*/
public final class RestMapTest {

private static final String BASE_URL_01 = "http://beta.lobid.org/${key}";
private static final String TEST_CASE_01 = "organisations/DE-6#!";

@Test
public void testGetDatasource() throws IOException {
final RestMap map = new RestMap();
map.setUrl(BASE_URL_01);
String result = map.get(TEST_CASE_01);
assertTrue(result.contains("51.96286"));
}

}