2006/unicorn/org/w3c/unicorn/request DirectRequestPOST.java,NONE,1.1 DirectRequestGET.java,NONE,1.1 Request.java,1.1.1.1,1.2 RequestList.java,1.1.1.1,1.2 UploadRequest.java,1.1.1.1,1.2 RequestListImpl.java,1.1.1.1,NONE DirectRequest.java,1.1.1.1,NONE

Update of /sources/public/2006/unicorn/org/w3c/unicorn/request
In directory hutz:/tmp/cvs-serv25605/org/w3c/unicorn/request

Modified Files:
	Request.java RequestList.java UploadRequest.java 
Added Files:
	DirectRequestPOST.java DirectRequestGET.java 
Removed Files:
	RequestListImpl.java DirectRequest.java 
Log Message:
Add use of http method information from the observer contrat to call the observer with method POST or GET.

--- NEW FILE: DirectRequestPOST.java ---
// $Id: DirectRequestPOST.java,v 1.1 2006/09/21 16:01:19 dleroy Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.request;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import java.util.Map;
import java.util.Random;

import javax.xml.bind.JAXBException;

import org.w3c.unicorn.contract.EnumInputMethod;
import org.w3c.unicorn.generated.observationresponse.Observationresponse;
import org.w3c.unicorn.input.DirectInputModule;
import org.w3c.unicorn.input.InputModule;

/**
 * @author Damien LEROY
 *
 */
public class DirectRequestPOST extends Request {

	private static Random aRandom = new Random();

	private String sURL = null;
	private Map<String, String> mapOfParameter = null;
	private String sBoundary =
		"---------------------------" +
		DirectRequestPOST.randomString() +
		DirectRequestPOST.randomString() +
		DirectRequestPOST.randomString();
	private URLConnection aURLConnection = null;
	private OutputStream aOutputStream = null;

	private static String randomString () {
		return Long.toString(DirectRequestPOST.aRandom.nextLong(), 36);
	}

	protected DirectRequestPOST (
			final String sURL,
			final String sInputParameterName,
			final InputModule aInputModule) throws IOException {
		DirectRequestPOST.logger.trace("Constructor");
		if (DirectRequestPOST.logger.isDebugEnabled()) {
			DirectRequestPOST.logger.debug("URL : " + sURL + ".");
			DirectRequestPOST.logger.debug("Input parameter name : " + sInputParameterName + ".");
			DirectRequestPOST.logger.debug("Input module : " + aInputModule + ".");
		}
		if (!(aInputModule instanceof DirectInputModule)) {
			throw new IllegalArgumentException("InputModule : " + aInputModule.toString() + ".");
		}
		this.mapOfParameter = new Hashtable<String, String>();
		this.sURL = sURL;
		this.addParameter(sInputParameterName, aInputModule.getStringContent());
	}

	@Override
	public void addParameter (final String sName, final String sValue) throws IOException {
		DirectRequestPOST.logger.trace("addParameter");
		if (DirectRequestPOST.logger.isDebugEnabled()) {
			DirectRequestPOST.logger.debug("Name :" + sName + ".");
			DirectRequestPOST.logger.debug("Value :" + sValue + ".");
		}
		this.mapOfParameter.put(sName, sValue);
	}

	@Override
	public Observationresponse doRequest () throws JAXBException, IOException {
		DirectRequestPOST.logger.trace("doRequest");
		final URL aURL = new URL(sURL);
		this.aURLConnection = aURL.openConnection();
		this.aURLConnection.setDoOutput(true);
		this.aURLConnection.setRequestProperty(
				"Content-Type",
				"multipart/form-data; boundary=" + sBoundary);
		this.aURLConnection.setRequestProperty("Accept-Language", this.sLang);
		if (null == this.aOutputStream) {
			this.aOutputStream = this.aURLConnection.getOutputStream();
		}
		for (final String sName : this.mapOfParameter.keySet()) {
			final String sValue = this.mapOfParameter.get(sName);
			DirectRequestPOST.logger.trace("addParameter");
			if (DirectRequestPOST.logger.isDebugEnabled()) {
				DirectRequestPOST.logger.debug("Name :" + sName + ".");
				DirectRequestPOST.logger.debug("Value :" + sValue + ".");
			}
			DirectRequestPOST.logger.debug("--");
			DirectRequestPOST.logger.debug(this.sBoundary);
			DirectRequestPOST.logger.debug("\r\n");
			DirectRequestPOST.logger.debug("Content-Disposition: form-data; name=\"");
			DirectRequestPOST.logger.debug(sName);
			DirectRequestPOST.logger.debug('"');
			DirectRequestPOST.logger.debug("\r\n");
			DirectRequestPOST.logger.debug("\r\n");
			DirectRequestPOST.logger.debug(sValue);
			DirectRequestPOST.logger.debug("\r\n");
			// boundary
			this.aOutputStream.write("--".getBytes());
			this.aOutputStream.write(this.sBoundary.getBytes());
			// writeName
			this.aOutputStream.write("\r\n".getBytes());
			this.aOutputStream.write("Content-Disposition: form-data; name=\"".getBytes());
			this.aOutputStream.write(sName.getBytes());
			this.aOutputStream.write('"');
			// newline
			this.aOutputStream.write("\r\n".getBytes());
			// newline
			this.aOutputStream.write("\r\n".getBytes());
			// writeln
			this.aOutputStream.write(sValue.getBytes());
			this.aOutputStream.write("\r\n".getBytes());
		}
		DirectRequestPOST.logger.debug("--");
		DirectRequestPOST.logger.debug(this.sBoundary);
		DirectRequestPOST.logger.debug("--");
		DirectRequestPOST.logger.debug("\r\n");
		this.aOutputStream.write("--".getBytes());
		this.aOutputStream.write(this.sBoundary.getBytes());
		this.aOutputStream.write("--".getBytes());
		this.aOutputStream.write("\r\n".getBytes());
		this.aOutputStream.close();/*
		final InputStream aInputStream = aURLConnection.getInputStream();
		String sResponse = "";
		byte[] tByte = new byte[500];
		while (0 < aInputStream.available()) {
			aInputStream.read(tByte);
			sResponse += new String(tByte);
		}
		DirectRequestPOST.logger.debug(sResponse);*/
		return (Observationresponse) DirectRequestPOST.aUnmarshaller.unmarshal(aURLConnection.getInputStream());
	}

	@Override
	public EnumInputMethod getInputMethod () {
		DirectRequestPOST.logger.trace("getInputMethod");
		return EnumInputMethod.DIRECT;
	}

	public String toString () {
		final int iStringBufferSize = 1000;
		//final String sVariableSeparator = " ";
		final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize);

		aStringBuffer.append("url:").append(this.sURL);
		//aStringBuffer.append(sVariableSeparator);
		//aStringBuffer.append("param:").append(this.sParameter);
		

		return aStringBuffer.toString();
	}

}

--- NEW FILE: DirectRequestGET.java ---
// $Id: DirectRequestGET.java,v 1.1 2006/09/21 16:01:21 dleroy Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.request;

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

import javax.xml.bind.JAXBException;

import org.w3c.unicorn.contract.EnumInputMethod;
import org.w3c.unicorn.generated.observationresponse.Observationresponse;
import org.w3c.unicorn.input.DirectInputModule;
import org.w3c.unicorn.input.InputModule;
import org.w3c.unicorn.util.Property;

/**
 * 
 * @author Damien LEROY
 */
public class DirectRequestGET extends Request {

	private String sURL = null;
	private String sParameter = null;

	protected DirectRequestGET (
			final String sURL,
			final String sInputParameterName,
			final InputModule aInputModule) throws IOException {
		DirectRequestGET.logger.trace("Constructor");
		if (DirectRequestGET.logger.isDebugEnabled()) {
			DirectRequestGET.logger.debug("URL : " + sURL + ".");
			DirectRequestGET.logger.debug("Input parameter name : " + sInputParameterName + ".");
			DirectRequestGET.logger.debug("Input module : " + aInputModule + ".");
		}
		if (!(aInputModule instanceof DirectInputModule)) {
			throw new IllegalArgumentException("InputModule : " + aInputModule.toString() + ".");
		}
		this.sURL = sURL;
		this.addParameter(sInputParameterName, aInputModule.getStringContent());
	}

	@Override
	public void addParameter (final String sName, final String sValue) throws IOException {
		DirectRequestGET.logger.trace("addParameter");
		if (DirectRequestGET.logger.isDebugEnabled()) {
			DirectRequestGET.logger.debug("Name :" + sName + ".");
			DirectRequestGET.logger.debug("Value :" + sValue + ".");
		}
		if (null == this.sParameter) {
			this.sParameter = "";
		} else {
			this.sParameter += "&";
		}
		this.sParameter += sName + "=" + URLEncoder.encode(sValue, Property.get("UNICORN_ENCODING"));
		DirectRequestGET.logger.debug("Parameters : "+this.sParameter+".");
	}

	@Override
	public Observationresponse doRequest () throws JAXBException, IOException {
		DirectRequestGET.logger.trace("doRequest");
		final URL aURL;
		if (null == this.sParameter) {
			aURL = new URL(this.sURL);
		} else {
			DirectRequestGET.logger.debug(this.sParameter);
			aURL = new URL(this.sURL + "?" + this.sParameter);
		}
		final URLConnection aURLConnection = aURL.openConnection();
		aURLConnection.setRequestProperty("Accept-Language", this.sLang);
		return (Observationresponse) DirectRequestGET.aUnmarshaller.unmarshal(aURLConnection.getInputStream());
	}

	@Override
	public EnumInputMethod getInputMethod () {
		DirectRequestGET.logger.trace("getInputMethod");
		return EnumInputMethod.DIRECT;
	}

	public String toString () {
		final int iStringBufferSize = 1000;
		final String sVariableSeparator = " ";
		final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize);

		aStringBuffer.append("url:").append(this.sURL);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("param:").append(this.sParameter);

		return aStringBuffer.toString();
	}

}

Index: UploadRequest.java
===================================================================
RCS file: /sources/public/2006/unicorn/org/w3c/unicorn/request/UploadRequest.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- UploadRequest.java	31 Aug 2006 09:09:25 -0000	1.1.1.1
+++ UploadRequest.java	21 Sep 2006 16:01:22 -0000	1.2
@@ -6,6 +6,8 @@
 
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.util.Hashtable;
+import java.util.Map;
 
 import javax.xml.bind.JAXBException;
 
@@ -21,7 +23,11 @@
  */
 public class UploadRequest extends Request {
 
+	private String sURL = null;
+	private String sInputParameterName = null;
 	private ClientHttpRequest aClientHttpRequest = null;
+	private UploadInputModule aUploadInputModule = null;
+	private Map<String, String> mapOfParameter = null;
 
 	protected UploadRequest (
 			final String sURL,
@@ -36,12 +42,15 @@
 		if (!(aInputModule instanceof UploadInputModule)) {
 			throw new IllegalArgumentException("InputModule : " + aInputModule.toString() + ".");
 		}
-		this.aClientHttpRequest = new ClientHttpRequest(sURL);
-		final UploadInputModule aUploadInputModule = (UploadInputModule) aInputModule;
-		this.aClientHttpRequest.setParameter(
-				sInputParameterName,
-				aUploadInputModule.getFileName(),
-				aUploadInputModule.getInputStream());
+		this.sURL = sURL;
+		this.sInputParameterName = sInputParameterName;
+		this.aUploadInputModule = (UploadInputModule) aInputModule;
+		this.mapOfParameter = new Hashtable<String, String>();
+		//this.aClientHttpRequest = new ClientHttpRequest(sURL);
+		/*this.aClientHttpRequest.setParameter(
+				this.sInputParameterName,
+				this.aUploadInputModule.getFileName(),
+				this.aUploadInputModule.getInputStream());*/
 	}
 
 	@Override
@@ -51,12 +60,29 @@
 			UploadRequest.logger.debug("Name :" + sName + ".");
 			UploadRequest.logger.debug("Value :" + sValue + ".");
 		}
-		this.aClientHttpRequest.setParameter(sName, sValue);
+		this.mapOfParameter.put(sName, sValue);
+		//this.aClientHttpRequest.setParameter(sName, sValue);
 	}
 
 	@Override
 	public Observationresponse doRequest() throws JAXBException, IOException {
 		UploadRequest.logger.trace("doRequest");
+		this.aClientHttpRequest = new ClientHttpRequest(sURL);
+		UploadRequest.logger.debug("Lang : "+this.sLang+".");
+		this.aClientHttpRequest.setLang(sLang); // meme place que pour directpost
+		this.aClientHttpRequest.setParameter(
+				this.sInputParameterName,
+				this.aUploadInputModule.getFileName(),
+				this.aUploadInputModule.getInputStream());
+		for (final String sName : this.mapOfParameter.keySet()) {
+			final String sValue = this.mapOfParameter.get(sName);
+			DirectRequestPOST.logger.trace("addParameter");
+			if (DirectRequestPOST.logger.isDebugEnabled()) {
+				DirectRequestPOST.logger.debug("Name :" + sName + ".");
+				DirectRequestPOST.logger.debug("Value :" + sValue + ".");
+			}
+			this.aClientHttpRequest.setParameter(sName, sValue);
+		}
 		final Observationresponse aObservationResponse;
 		aObservationResponse = (Observationresponse) UploadRequest.aUnmarshaller.unmarshal(this.aClientHttpRequest.post());
 		return aObservationResponse;
@@ -68,10 +94,6 @@
 		return EnumInputMethod.UPLOAD;
 	}
 
-	public void setLang (final String sLang) {
-		this.aClientHttpRequest.setLang(sLang);
-	}
-
 	public String toString () {
 		final int iStringBufferSize = 1000;
 		final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize);

Index: RequestList.java
===================================================================
RCS file: /sources/public/2006/unicorn/org/w3c/unicorn/request/RequestList.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- RequestList.java	31 Aug 2006 09:09:25 -0000	1.1.1.1
+++ RequestList.java	21 Sep 2006 16:01:22 -0000	1.2
@@ -4,6 +4,8 @@
 // Please first read the full copyright statement in file COPYRIGHT.html
 package org.w3c.unicorn.request;
 
+import java.io.IOException;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.commons.logging.Log;
@@ -14,19 +16,104 @@
  * @author Damien LEROY
  *
  */
-public interface RequestList {
+public class RequestList {
 
-	public static final Log logger = LogFactory.getLog("org.w3c.unicorn.request");
+	private static final Log logger = LogFactory.getLog("org.w3c.unicorn.request");
 
-	public abstract Request getRequest (final String sObserverID);
+	private String sLang = null;
 
-	public abstract Map<String, Request> getRequest (final TPriority aTPriority);
+	public RequestList (final String sLang) {
+		RequestList.logger.debug("Lang : "+sLang+".");
+		this.sLang = sLang;
+	}
 
-	public abstract Request getRequest (final TPriority aTPriority, final String sObserverName);
+	/**
+	 * Map of request about the observer who handle the current mime type with a LOW priority.
+	 */
+	private final Map<String, Request> mapOfRequestLOW = new LinkedHashMap<String, Request>();
 
-	public abstract void addRequest (
+	/**
+	 * Map of request about the observer who handle the current mime type with a MEDIUM priority.
+	 */
+	private final Map<String, Request> mapOfRequestMEDIUM = new LinkedHashMap<String, Request>();
+
+	/**
+	 * Map of request about the observer who handle the current mime type with a HIGH priority.
+	 */
+	private final Map<String, Request> mapOfRequestHIGH = new LinkedHashMap<String, Request>();
+
+	public Request getRequest (final String sObserverID) {
+		RequestList.logger.trace("getRequest");
+		if (RequestList.logger.isDebugEnabled()) {
+			RequestList.logger.debug("Observer ID : " + sObserverID + ".");
+		}
+		Request aRequest = null;
+		aRequest = this.mapOfRequestHIGH.get(sObserverID);
+		if (null != aRequest) return aRequest;
+		aRequest = this.mapOfRequestMEDIUM.get(sObserverID);
+		if (null != aRequest) return aRequest;
+		return this.mapOfRequestLOW.get(sObserverID);
+	}
+
+	public Map<String, Request> getRequest (final TPriority aTPriority) {
+		RequestList.logger.trace("getRequest");
+		if (RequestList.logger.isDebugEnabled()) {
+			RequestList.logger.debug("Priority : " + aTPriority + ".");
+		}
+		switch (aTPriority) {
+			case HIGH :
+				return this.mapOfRequestHIGH;
+			case MEDIUM :
+				return this.mapOfRequestMEDIUM;
+			case LOW :
+				return this.mapOfRequestLOW;
+		}
+		return null;
+	}
+
+	public Request getRequest (final TPriority aTPriority, final String sObserverID) {
+		RequestList.logger.trace("getRequest");
+		if (RequestList.logger.isDebugEnabled()) {
+			RequestList.logger.debug("Priority : " + aTPriority + ".");
+			RequestList.logger.debug("Observer ID : " + sObserverID + ".");
+		}
+		return this.getRequest(aTPriority).get(sObserverID);
+	}
+
+	public void addRequest(
 			final Request aRequest,
 			final TPriority aTPriority,
-			final String sObserverID);
+			final String sObserverID) throws IOException {
+		RequestList.logger.trace("addRequest");
+		if (RequestList.logger.isDebugEnabled()) {
+			RequestList.logger.debug("Request : " + aRequest + ".");
+			RequestList.logger.debug("TPriority : " + aTPriority + ".");
+			RequestList.logger.debug("String observer ID : " + sObserverID + ".");
+		}
+		aRequest.setLang(this.sLang);
+		switch (aTPriority) {
+			case HIGH :
+				this.mapOfRequestHIGH.put(sObserverID, aRequest);
+				return;
+			case MEDIUM :
+				this.mapOfRequestMEDIUM.put(sObserverID, aRequest);
+				return;
+			case LOW :
+				this.mapOfRequestLOW.put(sObserverID, aRequest);
+				return;
+		}
+	}
+
+	public String toString () {
+		final int iStringBufferSize = 1000;
+		final String sVariableSeparator = " ";
+		final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize);
+		aStringBuffer.append("HIGH:").append(this.mapOfRequestHIGH);
+		aStringBuffer.append(sVariableSeparator);
+		aStringBuffer.append("MEDIUM:").append(this.mapOfRequestMEDIUM);
+		aStringBuffer.append(sVariableSeparator);
+		aStringBuffer.append("LOW:").append(this.mapOfRequestLOW);
+		return aStringBuffer.toString();
+	}
 
 }

--- DirectRequest.java DELETED ---

Index: Request.java
===================================================================
RCS file: /sources/public/2006/unicorn/org/w3c/unicorn/request/Request.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- Request.java	31 Aug 2006 09:09:25 -0000	1.1.1.1
+++ Request.java	21 Sep 2006 16:01:18 -0000	1.2
@@ -11,6 +11,7 @@
 import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.w3c.unicorn.contract.EnumInputMethod;
 import org.w3c.unicorn.generated.observationresponse.Observationresponse;
 import org.w3c.unicorn.input.InputModule;
@@ -21,7 +22,7 @@
  */
 public abstract class Request {
 
-	protected static final Log logger = RequestList.logger;
+	protected static final Log logger = LogFactory.getLog("org.w3c.unicorn.request");
 
 	public static Unmarshaller aUnmarshaller = null;
 	protected static JAXBContext aJAXBContext = null;
@@ -39,7 +40,8 @@
 
 	protected String sLang = null;
 
-	public void setLang (final String sLang) {
+	public void setLang (final String sLang) throws IOException {
+		Request.logger.debug("setLang("+sLang+")");
 		this.sLang = sLang;
 	}
 
@@ -52,16 +54,22 @@
 	public static Request createRequest (
 			final InputModule aInputModule,
 			final String sURL,
-			final String sInputParameterName) throws IOException {
+			final String sInputParameterName,
+			final boolean bIsPost) throws IOException {
 		Request.logger.trace("createRequest");
 		if (Request.logger.isDebugEnabled()) {
 			Request.logger.debug("InputModule : " + aInputModule + ".");
 			Request.logger.debug("URL : " + sURL + ".");
 			Request.logger.debug("Input parameter name : " + sInputParameterName + ".");
+			Request.logger.debug("POST method : " + bIsPost + ".");
 		}
 		switch (aInputModule.getEnumInputMethod()) {
 			case DIRECT :
-				return new DirectRequest(sURL, sInputParameterName, aInputModule);
+				if ( bIsPost) {
+					return new DirectRequestPOST(sURL, sInputParameterName, aInputModule);
+				} else {
+					return new DirectRequestGET(sURL, sInputParameterName, aInputModule);
+				}
 			case UPLOAD :
 				return new UploadRequest(sURL, sInputParameterName, aInputModule);
 			case URI :

--- RequestListImpl.java DELETED ---

Received on Thursday, 21 September 2006 16:01:42 UTC