/* $Id: HttpRequest.java,v 1.1 2004/01/09 16:55:42 burkhard Exp $
 * Created on 09.05.2003 by sell
 *
 */
package de.skyrix.zsp.logic.httpclient;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

/**Request Objekt fuer eine HTTP Anfrage
 * 
 * @author sell
 * @version 
 */
public class HttpRequest {
	/**Verwendete HTTP Methode (z.B. GET / POST / OPTIONS / PROPFIND / ...)*/
	private String method = "PROPFIND";

	/**Header Properties*/
	private Properties properties;

	/**Body Daten*/
	private String data;

	/**Adresse der Anfrage**/
	private URL url;

	/**Nutzername bei Authentifizierung.*/
	private String userName = null;

	/**Password fuer Authentifizierung.*/
	private String password = null;

	/**Soll Basic Authentification benutzt werden?*/
	private boolean useBasicAuthentication = false;

	private String contentType = "application/xml";

	public HttpRequest(String url) throws MalformedURLException {
		//Eliminate "//" items
		String urlStr = null;
		if (url.indexOf("://") >= 0) {
			try {
				String dummy = url.substring(url.indexOf("://") + 3);
				while (dummy.indexOf("//") >= 0) {
					dummy = replaceString(dummy, "//", "/");
				}
				urlStr = url.substring(0, url.indexOf("://") + 3) + dummy;
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (urlStr == null)
			urlStr = url;

		this.url = new URL(url);
	}

	public HttpRequest(String url, String method) throws MalformedURLException {
		this(url);
		this.method = method;
	}

	public HttpRequest(
		String url,
		String method,
		String userName,
		String password)
		throws MalformedURLException {
		this(url, method);
		this.userName = userName;
		this.password = password;
		this.useBasicAuthentication = true;
	}

	private String replaceString(String s, String search, String repl) {
		String ret;
		if (search.length() <= 0) {
			ret = s;
		}
		else {
			ret = "";
			int pos;
			while ((pos = s.indexOf(search)) != -1) {
				ret += s.substring(0, pos) + repl;
				s = s.substring(pos + search.length());
			}
			ret += s;
		}
		return ret;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public Properties getRequestProperties() {
		return properties;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getData() {
		return data;
	}

	public void setURL(String url) throws MalformedURLException {
		this.url = new URL(url);
	}

	public URL getURL() {
		return url;
	}

	/**Erstellt den Authentifikationsstring fuer Basic Authentication.
	   * 
	   * @param username - Username
	   * @param password - Passwort
	   * 
	   * @return String - Authentifikationsstring
	   */
	private String getBasicAuthString() {
		/*System.out.println("Determining authKey");
		if ("true".equals(ZSPConfig.getProperty("detectAuthKey"))) {
			System.out.println(
				"should use grabbedKey: " + System.getProperty("zsp.ProxyAuthKey"));
			String authKey = System.getProperty("zsp.ProxyAuthKey");
			if (authKey != null && !"".equals(authKey.trim())) {
				System.out.println("using authKey: " + authKey);

				return authKey;
			}
		}
		System.out.println("constructing authKey");*/
		String sRet = getUserName() + ":" + getPassword();
		sRet = new sun.misc.BASE64Encoder().encode(sRet.getBytes());
		return "Basic " + sRet;
	}

	public void setRequestProperty(String key, String value) {
		if (properties == null)
			properties = new Properties();

		properties.setProperty(key, value);
	}

	public void setRequestProperties(Properties properties) {
		this.properties = properties;
	}

	/**Erstellt aus den Headerdaten einen korrekten HTTP Header
	 * 
	 * @return String - HTTP Header
	 */
	public String getHeader() {

		String pathStr = replaceString(url.getPath(), "//", "/");

		StringBuffer header =
			new StringBuffer(method + " " + pathStr + " HTTP/1.0\n");

		if (properties != null && !properties.isEmpty()) {
			Enumeration propNames = properties.propertyNames();
			while (propNames.hasMoreElements()) {
				String key = (String) propNames.nextElement();
				String value = properties.getProperty(key);
				if (value != null) {
					header.append(key);
					header.append(": ");
					header.append(value);
					header.append("\n");
				}
			}

			header.append("Content-Type: ");
			header.append(contentType);
			header.append("\n");

			if (isUseBasicAuthentication()) {
				header.append("Authorization: ");
				header.append(getBasicAuthString());
				header.append("\n");
			}

			header.append("Content-length: ");
			if (data != null)
				header.append(data.length());
			else
				header.append("0\n");
		}

		return header.toString();
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setUseBasicAuthentication(boolean useBasicAuthentication) {
		this.useBasicAuthentication = useBasicAuthentication;
	}

	public boolean isUseBasicAuthentication() {
		return useBasicAuthentication;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentType() {
		return contentType;
	}
}
