/*
 * Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 * Licensed under the Apache License, Version 2.0 the "License";
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.culturegraph.mf.morph.maps;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.culturegraph.mf.exceptions.MetafactureException;

/**
 * NOT WORKING YET
 *
 * @author "Markus Michael Geipel"
 *
 */
public final class RestMap extends AbstractReadOnlyMap<String, String> {

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

	public void setUrl(final 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();

		} catch (final IOException e) {
			throw new MetafactureException(e);
		}
	}

}

@@ -664,27 +664,158 @@
 java.io.
-IOException
+BufferedReader;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader
 ;%0Aimport
@@ -826,17 +826,17 @@
 a.net.UR
-L
+I
 ;%0Aimport
@@ -847,27 +847,86 @@
 a.net.UR
-LConnection
+ISyntaxException;%0Aimport java.net.URL;%0Aimport java.nio.charset.Charset
 ;%0Aimport
@@ -1053,30 +1053,8 @@
 /**%0A
- * NOT WORKING YET%0A *%0A
  * @
@@ -1083,16 +1083,40 @@
  Geipel%22
+, %22Philipp v. B%C3%B6selager%22
 %0A *%0A */%0A
@@ -1258,20 +1258,17 @@
 $%7Bkey%7D%22,
-%0A%09%09%09
+ 
 Pattern.
@@ -1297,40 +1297,93 @@
 ing 
-url;%0A%0A%09public void setUrl(final 
+charsetName = %22UTF-8%22;%0A%09private String url;%0A%0A%09public RestMap() %7B%0A%09%7D%0A%0A%09public RestMap(
 Stri
@@ -1531,32 +1531,27 @@
 %0A%09%09%09
-final URL url = new URL(
+String urlString = 
 matc
@@ -1584,94 +1584,434 @@
 g())
-)
 ;%0A%09%09%09
-final URLConnection con = url.openConnec
+return readFromUrl(urlString);%0A%09%09%7D catch (IOException %7C URISyntaxException e) %7B%0A%09%09%09if (e instanceof FileNotFoundException) %7B%0A%09%09%09%09// There was no data result for the given URL%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%09throw new MetafactureExcep
 tion(
+e
 );%0A%09%09
-%09// TODO correctly read from c
+%7D%0A%09%7D%0A%0A%09private String readFromUrl(final String url) throws IOException, URISyntaxException %7B%0A%09%09InputStream inputStream = new URL(new URI(url.replace(%22 %22, %22%2520%22)).toASCIIString()).openC
 onne
@@ -2019,123 +2019,522 @@
 tion
-!
+()
 %0A%09%09%09
-return (String) con.getContent();%0A%0A%09%09%7D catch (final IOException e
+%09.getInputStream();%0A%09%09try %7B%0A%09%09%09BufferedReader reader = new BufferedReader(%0A%09%09%09%09%09new InputStreamReader(inputStream, Charset.forName(charsetName)));%0A%09%09%09StringBuilder stringBuffer = new StringBuilder();%0A%09%09%09int value;%0A%09%09%09while ((value = reader.read()) != -1) %7B%0A%09%09%09%09stringBuffer.append((char) value);%0A%09%09%09%7D%0A%09%09%09return stringBuffer.toString();%0A%09%09%7D finally %7B%0A%09%09%09inputStream.close();%0A%09%09%7D%0A%09%7D%0A%0A%09public void setUrl(final String url
 ) %7B%0A%09%09
-%09
 th
-row new MetafactureException(e);%0A%09%09%7D
+is.url = url;%0A%09%7D%0A%0A%09public void setCharsetName(String name) %7B%0A%09%09charsetName = name;
 %0A%09%7D%0A

Enable RestMap and deliver test