- From: Jean-Guilhem Rouel via cvs-syncmail <cvsmail@w3.org>
- Date: Fri, 28 Aug 2009 12:39:50 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/request In directory hutz:/tmp/cvs-serv22368/src/org/w3c/unicorn/request Added Files: URIRequest.java Request.java UploadRequest.java RequestList.java DirectRequestPOST.java DirectRequestGET.java Log Message: Merging dev2 in HEAD --- NEW FILE: DirectRequestPOST.java --- // $Id: DirectRequestPOST.java,v 1.2 2009/08/28 12:39:48 jean-gui 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.InputStream; 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 org.w3c.unicorn.contract.EnumInputMethod; import org.w3c.unicorn.input.DirectInputModule; import org.w3c.unicorn.input.InputModule; import org.w3c.unicorn.response.Response; /** * Class to make a request directly using POST method * * @author Damien LEROY */ public class DirectRequestPOST extends Request { /** * generate a random number */ private static Random aRandom = new Random(); /** * URL for the post direct request */ private String sURL = null; /** * Data Structure for the parameters */ private Map<String, String> mapOfParameter = null; /** * Random string for hazardous purpose */ private String sBoundary = "---------------------------" + DirectRequestPOST.randomString() + DirectRequestPOST.randomString() + DirectRequestPOST.randomString(); /** * URL to connect */ private URLConnection aURLConnection = null; /** * Output stream for the post */ private OutputStream aOutputStream = null; /** * Generate random strings * * @return a random string */ private static String randomString() { return Long.toString(DirectRequestPOST.aRandom.nextLong(), 36); } /** * Constructor for a direct request using post method * * @param sURL * URL to connect * @param sInputParameterName * parameter name * @param aInputModule * input module for the request * @param responseType * type of the response of the observer * @throws IOException * odd error occurs */ protected DirectRequestPOST(final String sURL, final String sInputParameterName, final InputModule aInputModule, final String responseType) throws IOException { super(); Request.logger.trace("Constructor"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("URL : " + sURL + "."); Request.logger.debug("Input parameter name : " + sInputParameterName + "."); Request.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()); this.setResponseType(responseType); } @Override public void addParameter(final String sName, final String sValue) throws IOException { Request.logger.trace("addParameter"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("Name :" + sName + "."); Request.logger.debug("Value :" + sValue + "."); } this.mapOfParameter.put(sName, sValue); } @Override public Response doRequest() throws IOException { Request.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); Request.logger.trace("addParameter"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("Name :" + sName + "."); Request.logger.debug("Value :" + sValue + "."); } Request.logger.debug("--"); Request.logger.debug(this.sBoundary); Request.logger.debug("\r\n"); Request.logger.debug("Content-Disposition: form-data; name=\""); Request.logger.debug(sName); Request.logger.debug('"'); Request.logger.debug("\r\n"); Request.logger.debug("\r\n"); Request.logger.debug(sValue); Request.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()); } Request.logger.debug("--"); Request.logger.debug(this.sBoundary); Request.logger.debug("--"); Request.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(); InputStream is = aURLConnection.getInputStream(); return streamToResponse(is); } @Override public EnumInputMethod getInputMethod() { Request.logger.trace("getInputMethod"); return EnumInputMethod.DIRECT; } @Override public String toString() { final int iStringBufferSize = 1000; final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize); aStringBuffer.append("url:").append(this.sURL); return aStringBuffer.toString(); } } --- NEW FILE: DirectRequestGET.java --- // $Id: DirectRequestGET.java,v 1.2 2009/08/28 12:39:48 jean-gui 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.InputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import org.w3c.unicorn.contract.EnumInputMethod; import org.w3c.unicorn.input.DirectInputModule; import org.w3c.unicorn.input.InputModule; import org.w3c.unicorn.response.Response; import org.w3c.unicorn.util.Property; /** * Class to make a request directly using GET method * * @author Damien LEROY */ public class DirectRequestGET extends Request { /** * URL of for the request */ private String sURL = null; /** * Parameter for the request */ private String sParameter = null; /** * Constructor for the direct request with GET method * * @param sURL * URL for the request * @param sInputParameterName * name of the parameter * @param aInputModule * input module to do the request * @param responseType * type of the response of the observer * @throws IOException * odd error occured */ protected DirectRequestGET(final String sURL, final String sInputParameterName, final InputModule aInputModule, final String responseType) throws IOException { super(); Request.logger.trace("Constructor"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("URL : " + sURL + "."); Request.logger.debug("Input parameter name : " + sInputParameterName + "."); Request.logger.debug("Input module : " + aInputModule + "."); } if (!(aInputModule instanceof DirectInputModule)) { throw new IllegalArgumentException("InputModule : " + aInputModule.toString() + "."); } this.sURL = sURL; this.addParameter(sInputParameterName, aInputModule.getStringContent()); this.setResponseType(responseType); } @Override public void addParameter(final String sName, final String sValue) throws IOException { Request.logger.trace("addParameter"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("Name :" + sName + "."); Request.logger.debug("Value :" + sValue + "."); } if (null == this.sParameter) { this.sParameter = ""; } else { this.sParameter += "&"; } this.sParameter += sName + "=" + URLEncoder.encode(sValue, Property.get("UNICORN_ENCODING")); Request.logger.debug("Parameters : " + this.sParameter + "."); } @Override public Response doRequest() throws IOException { Request.logger.trace("doRequest"); final URL aURL; if (null == this.sParameter) { aURL = new URL(this.sURL); } else { Request.logger.debug(this.sParameter); aURL = new URL(this.sURL + "?" + this.sParameter); } final URLConnection aURLConnection = aURL.openConnection(); aURLConnection.setRequestProperty("Accept-Language", this.sLang); InputStream is = aURLConnection.getInputStream(); return streamToResponse(is); } @Override public EnumInputMethod getInputMethod() { Request.logger.trace("getInputMethod"); return EnumInputMethod.DIRECT; } @Override 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: UploadRequest.java --- // $Id: UploadRequest.java,v 1.2 2009/08/28 12:39:48 jean-gui 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.MalformedURLException; import java.util.Hashtable; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.InputStreamBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.w3c.unicorn.contract.EnumInputMethod; import org.w3c.unicorn.input.InputModule; import org.w3c.unicorn.input.UploadInputModule; import org.w3c.unicorn.response.Response; /** * Class to deal with the upload request * * @author Damien LEROY */ public class UploadRequest extends Request { /** * URL for the request */ private String sURL = null; /** * Name of the parameter */ private String sInputParameterName = null; /** * An input module with upload */ private UploadInputModule aUploadInputModule = null; /** * Data structure for the parameters */ private Map<String, String> mapOfParameter = null; /** * Create a upload request * * @param sURL * URL for the request * @param sInputParameterName * name of the parameter * @param aInputModule * module for the input of the request * @param responseType * type of the response * @throws MalformedURLException * error if the URL is not well formed * @throws IOException * odd error occured */ protected UploadRequest(final String sURL, final String sInputParameterName, final InputModule aInputModule, final String responseType) throws MalformedURLException, IOException { super(); Request.logger.trace("Constructor"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("URL : " + sURL + "."); Request.logger.debug("Input parameter name : " + sInputParameterName + "."); Request.logger.debug("Input module : " + aInputModule + "."); } if (!(aInputModule instanceof UploadInputModule)) { throw new IllegalArgumentException("InputModule : " + aInputModule.toString() + "."); } this.sURL = sURL; this.sInputParameterName = sInputParameterName; this.aUploadInputModule = (UploadInputModule) aInputModule; this.mapOfParameter = new Hashtable<String, String>(); this.setResponseType(responseType); } @Override public void addParameter(final String sName, final String sValue) throws IOException { Request.logger.trace("addParameter"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("Name :" + sName + "."); Request.logger.debug("Value :" + sValue + "."); } this.mapOfParameter.put(sName, sValue); } @Override public Response doRequest() throws IOException { Request.logger.trace("doRequest"); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost method = new HttpPost(sURL); method.setHeader("Accept-Language", sLang); MultipartEntity entity = new MultipartEntity(); for (final String sName : this.mapOfParameter.keySet()) { final String sValue = this.mapOfParameter.get(sName); Request.logger.trace("addParameter"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("Name :" + sName + "."); Request.logger.debug("Value :" + sValue + "."); } entity.addPart(sName, new StringBody(sValue)); //entity.addPart(sName, new StringBody(sValue, Charset.forName("UTF-8"))); } InputStreamBody file = new InputStreamBody(this.aUploadInputModule.getInputStream(), this.aUploadInputModule.getMimeType().toString(), this.aUploadInputModule.getFileName()); entity.addPart(this.sInputParameterName, file); method.setEntity(entity); HttpResponse response = httpclient.execute(method); return streamToResponse(response.getEntity().getContent()); } @Override public EnumInputMethod getInputMethod() { Request.logger.trace("getInputMethod"); return EnumInputMethod.UPLOAD; } /*@Override public String toString() { final int iStringBufferSize = 1000; final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize); aStringBuffer.append("ClientHttpRequest:").append( this.aClientHttpRequest); return aStringBuffer.toString(); }*/ } --- NEW FILE: RequestList.java --- // $Id: RequestList.java,v 1.2 2009/08/28 12:39:48 jean-gui 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.util.LinkedHashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author Damien LEROY * */ public class RequestList { /** * Object used for complex logging purpose */ private static final Log logger = LogFactory.getLog(RequestList.class); /** * Language of the list */ private String sLang = null; /** * Constructor of the list * * @param sLang * language of the list */ public RequestList(final String sLang) { RequestList.logger.debug("Lang : " + sLang + "."); this.sLang = sLang; } /** * Map of request about the observer who handle the current mime type with a * LOW priority. */ private final Map<String, Request> mapOfRequest = new LinkedHashMap<String, Request>(); /** * * @return */ public Map<String, Request> getRequestMap() { RequestList.logger.trace("getRequestMap"); return this.mapOfRequest; } /** * Gives an observer placed in the map * * @param String * sNodeID the ID of the node into which we'll search the Request * @return */ public Request getRequest(final String sNodeID) { RequestList.logger.trace("getRequest"); if (RequestList.logger.isDebugEnabled()) { RequestList.logger.debug("Observer ID : " + sNodeID + "."); } return this.mapOfRequest.get(sNodeID); } /** * Adds a request to the map * * @param aRequest * @param sNodeID * The ID of the corresponding node * @throws IOException */ public void addRequest(final Request aRequest, final String sNodeID) throws IOException { RequestList.logger.trace("addRequest"); if (RequestList.logger.isDebugEnabled()) { RequestList.logger.debug("Request : " + aRequest + "."); RequestList.logger.debug("String node ID : " + sNodeID + "."); } aRequest.setLang(this.sLang); this.mapOfRequest.put(sNodeID, aRequest); } /** * Prints the object */ @Override public String toString() { final int iStringBufferSize = 1000; final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize); aStringBuffer.append(mapOfRequest); return aStringBuffer.toString(); } } --- NEW FILE: URIRequest.java --- // $Id: URIRequest.java,v 1.2 2009/08/28 12:39:48 jean-gui 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.InputStream; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import org.w3c.unicorn.contract.EnumInputMethod; import org.w3c.unicorn.input.InputModule; import org.w3c.unicorn.input.URIInputModule; import org.w3c.unicorn.response.Response; /** * Use to handle a request to a observer. * * @author Damien LEROY */ public class URIRequest extends Request { /** * URL for the request */ private String sURL = null; /** * Parameter of the request */ private String sParameter = null; /** * Create an URI request * * @param sURL * URL for the request * @param sInputParameterName * name of the parameter for the request * @param aInputModule * input module for the request * @param responseType * type of the response by the observer * @throws IOException * odd error occured */ public URIRequest(final String sURL, final String sInputParameterName, final InputModule aInputModule, final String responseType) throws IOException { super(); Request.logger.trace("Constructor"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("URL : " + sURL + "."); Request.logger.debug("Input parameter name : " + sInputParameterName + "."); Request.logger.debug("Input module : " + aInputModule + "."); } if (!(aInputModule instanceof URIInputModule)) { throw new IllegalArgumentException("InputModule : " + aInputModule.toString() + "."); } this.sURL = sURL; final URIInputModule aURIInputModule = (URIInputModule) aInputModule; this.addParameter(sInputParameterName, aURIInputModule.getURI()); this.setResponseType(responseType); } /** * Add a parameter to the request * * @param sName * name of the parameter to add * @param sValue * value of the parameter to add */ @Override public void addParameter(final String sName, final String sValue) throws UnsupportedEncodingException { Request.logger.trace("addParameter"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("sName : " + sName + "."); Request.logger.debug("sValue : " + sValue + "."); } if (null == this.sParameter) { this.sParameter = ""; } else { this.sParameter += "&"; } this.sParameter += sName + "=" + URLEncoder.encode(sValue, "UTF-8"); Request.logger.debug("Parameters : " + this.sParameter + "."); } /** * Do the request to the observer * * @throws IOException * odd error occured */ @Override public Response doRequest() throws IOException { Request.logger.trace("doRequest"); if (Request.logger.isDebugEnabled()) { Request.logger.debug("URL : " + this.sURL + " ."); Request.logger.debug("Parameters : " + this.sParameter + " ."); } final URL aURL; if (null == this.sParameter) { aURL = new URL(this.sURL); } else { Request.logger.debug(this.sParameter); aURL = new URL(this.sURL + "?" + this.sParameter); } Request.logger.debug("URL : " + aURL + " ."); final URLConnection aURLConnection = aURL.openConnection(); aURLConnection.setRequestProperty("Accept-Language", this.sLang); InputStream is = aURLConnection.getInputStream(); return streamToResponse(is); } @Override public EnumInputMethod getInputMethod() { Request.logger.trace("getInputMethod"); return EnumInputMethod.URI; } /** * Prints the object */ @Override 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: Request.java --- // $Id: Request.java,v 1.2 2009/08/28 12:39:48 jean-gui 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.InputStream; import java.io.InputStreamReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.unicorn.contract.EnumInputMethod; import org.w3c.unicorn.input.InputModule; import org.w3c.unicorn.response.Response; import org.w3c.unicorn.response.parser.ResponseParserFactory; /** * * @author Damien LEROY */ public abstract class Request { /** * Object used for complex logging purpose */ protected static final Log logger = LogFactory.getLog(Request.class); /** * Language of the request */ protected String sLang = null; /** * Type of the response for the request */ protected String responseType = null; /** * Sets the language of the request * * @param sLang * new language to set * @throws IOException * odd error occured */ public void setLang(final String sLang) throws IOException { Request.logger.debug("setLang(" + sLang + ")"); this.sLang = sLang; } /** * Add a parameter to the request * * @param sName * name of the parameter * @param sValue * value of the parameter * @throws IOException * odd error occured */ public abstract void addParameter(final String sName, final String sValue) throws IOException; /** * Do the request to the observer * * @return the response of the observer * @throws IOException * odd error occured */ public abstract org.w3c.unicorn.response.Response doRequest() throws IOException; public abstract EnumInputMethod getInputMethod(); /** * Create a request for the observer * * @param aInputModule * input module used for the request * @param sURL * url for the request * @param sInputParameterName * name of the parameter of the request * @param bIsPost * to know whether the request is sent or not * @param responseType * type of the response * @return a request ready to be done * @throws IOException * odd error occurred */ public static Request createRequest(final InputModule aInputModule, final String sURL, final String sInputParameterName, final boolean bIsPost, final String responseType) 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: if (bIsPost) { return new DirectRequestPOST(sURL, sInputParameterName, aInputModule, responseType); } else { return new DirectRequestGET(sURL, sInputParameterName, aInputModule, responseType); } case UPLOAD: return new UploadRequest(sURL, sInputParameterName, aInputModule, responseType); case URI: return new URIRequest(sURL, sInputParameterName, aInputModule, responseType); } return null; } @Override public String toString() { return "Abstract class org.w3c.unicorn.request.Request, toString function must be overrided."; } public String getResponseType() { return responseType; } public void setResponseType(String responseType) { this.responseType = responseType; } protected Response streamToResponse(InputStream is) throws IOException { StringBuilder builder = new StringBuilder(); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); char[] chararray = new char[8192]; int readLength = 0; while ((readLength = isr.read(chararray, 0, 8192)) > -1) { builder.append(chararray, 0, readLength); } Response res = ResponseParserFactory.parse(builder.toString(), this .getResponseType()); res.setXml(builder); return res; } }
Received on Friday, 28 August 2009 12:40:02 UTC