package se.repos.restclient.base;

import java.io.IOException;
import java.net.URL;

import se.repos.restclient.HttpStatusError;
import se.repos.restclient.ResponseHeaders;
import se.repos.restclient.RestClient;
import se.repos.restclient.RestResponse;

/**
 * Helper class for implementing the single host {@link RestClient} interface 
 * using a library that operates on full URLs rather than resources at a predefined server root.
 */
public abstract class RestClientMultiHostBase implements RestClient {

	private String root;

	/**
	 * @param serverRootUrl protocol, domain and possibly port number, no trailing slash
	 */
	public RestClientMultiHostBase(String serverRootUrl) {
		if (serverRootUrl == null) {
			throw new IllegalArgumentException("Server root URL is required, this is a single host implementation");
		}
		if (serverRootUrl.length() == 0) {
			throw new IllegalArgumentException("Server root URL can not be empty");
		}
		this.root = serverRootUrl;
	}	
	
	@Override
	public void get(String uri, RestResponse response) throws IOException,
			HttpStatusError {
		if (!uri.startsWith("/")) {
			throw new IllegalArgumentException("URIs must be relative to server root starting with slash. Got " + uri);
		} 
		get(new URL(root + uri), response);
	}
	
	public abstract void get(URL url, RestResponse response)
			throws IOException, HttpStatusError;

	@Override
	public ResponseHeaders head(String uri) throws IOException {
		if (!uri.startsWith("/")) {
			throw new IllegalArgumentException("URIs must be relative to server root starting with slash");
		}
		return head(new URL(root + uri));
	}
	
	public abstract ResponseHeaders head(URL url) throws IOException;

}

@@ -56,16 +56,55 @@
 eption;%0A
+import java.net.MalformedURLException;%0A
 import j
@@ -975,16 +975,417 @@
 %22);%0A%09%09%7D%0A
+%09%09if (serverRootUrl.endsWith(%22/%22)) %7B%0A%09%09%09// Be picky as we deal with strings and URIs must start with slash according to interface%0A%09%09%09throw new IllegalArgumentException(%22Server root URL must not end with slash, got %22 + serverRootUrl);%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09new URL(serverRootUrl);%0A%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09throw new IllegalArgumentException(%22Invalid server root URL %22 + serverRootUrl, e);%0A%09%09%7D%0A
 %09%09this.r
@@ -1407,17 +1407,16 @@
 tUrl;%0A%09%7D
-%09
 %0A%09%0A%09@Ove

Complete validation of server root URL in base constructor