From: Santi B. <san...@us...> - 2007-08-04 11:37:59
|
Update of /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/pipeline/stage In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv1742/modules/web/src/com/babeldoc/web/pipeline/stage Modified Files: HttpClientPipelineStage.java Log Message: 2007-08-04 Added readme/doc with javadoc generation 2007-03-15 Many changes: - MailboxScanner can filter by field TO - Can debug MailboxScanner - Add timeout connection parameter to MailboxScanner - MailboxScanner obtain the value of the properties: 'to', 'from' and 'reply to' - Add three parameters that they allowed to deny a regular expression from filters (to,from and reply) - Add encoding parameter to Pipeline Writefile - Pipeline HttpClient save cookie information - Add parameter to pipeline HttpClient. It indicates if we want copy attributes in the new document - Pipeline HttpClient can send files in the parameters of POST method. To Manage the MIME types automatically (from the extension of the file) when adding attached files - Names of attached files in SmtpWriterPipelineStage don't contains absolute path - When decompressing a file in DecompressionPipeline, the name and the extension of the decompressed file it's original name Index: HttpClientPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/pipeline/stage/HttpClientPipelineStage.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** HttpClientPipelineStage.java 15 Jul 2003 22:46:56 -0000 1.2 --- HttpClientPipelineStage.java 4 Aug 2007 11:37:29 -0000 1.3 *************** *** 76,85 **** import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.pipeline.*; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; ! import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Collection; --- 76,97 ---- import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.pipeline.*; + import com.babeldoc.core.Pair; + import com.babeldoc.core.LogService; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; + //import org.apache.commons.httpclient.methods.MultipartPostMethod; + import org.apache.commons.httpclient.methods.multipart.FilePart;//add for Multipart + import org.apache.commons.httpclient.methods.multipart.StringPart;//add for Multipart + import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;//add for Multipart + import org.apache.commons.httpclient.methods.multipart.Part;//add for Multipart + import org.apache.commons.httpclient.methods.StringRequestEntity;//add for Multipart + import org.apache.commons.httpclient.HttpVersion;//add for Multipart + import org.apache.commons.httpclient.params.HttpMethodParams; ! import org.apache.commons.httpclient.Cookie; ! ! import java.io.File; import java.util.ArrayList; import java.util.Collection; *************** *** 88,93 **** * Pipeline stage for http/https client * ! * @author Jonathan Leech ! * @version 1.0 */ public class HttpClientPipelineStage extends PipelineStage { --- 100,105 ---- * Pipeline stage for http/https client * ! * @author paspes ! * @version 1.3 */ public class HttpClientPipelineStage extends PipelineStage { *************** *** 108,111 **** --- 120,125 ---- /* the post parameters as name value pairs */ public final static String HTTP_POST_PARAMETERS = "parameters"; + /* the post file parameters as name value pairs */ + public final static String HTTP_POST_FILE_PARAMETERS = "fileParameters"; /** *************** *** 135,139 **** options.add(new ConfigOption(HTTP_HEADERS, ccot, null, false, I18n.get("web.207"))); options.add(new ConfigOption(HTTP_POST_PARAMETERS, ccot, null, false, I18n.get("web.208"))); ! return options; } }); --- 149,155 ---- options.add(new ConfigOption(HTTP_HEADERS, ccot, null, false, I18n.get("web.207"))); options.add(new ConfigOption(HTTP_POST_PARAMETERS, ccot, null, false, I18n.get("web.208"))); ! options.add(new ConfigOption(HTTP_POST_FILE_PARAMETERS, ccot, null, false, I18n.get("web.209"))); ! options.add(new ConfigOption(SPLIT_ATTRIBUTES, IConfigOptionType.BOOLEAN, "true", false,I18n.get("web.210"))); ! return options; } }); *************** *** 162,165 **** --- 178,183 ---- NameValuePair[] headers = getOptionList(new String[]{HTTP_HEADERS}); NameValuePair[] postParameters = getOptionList(new String[]{HTTP_POST_PARAMETERS}); + NameValuePair[] postFileParameters = getOptionList(new String[]{HTTP_POST_FILE_PARAMETERS}); + boolean fileparams = (postFileParameters != null) && (postFileParameters.length > 0); // Create an instance of HttpClient. *************** *** 169,173 **** HttpMethodBase httpMethod; if ("post".equalsIgnoreCase(method)) { ! httpMethod = new PostMethod(url); } else if ("get".equalsIgnoreCase(method)) { httpMethod = new GetMethod(url); --- 187,191 ---- HttpMethodBase httpMethod; if ("post".equalsIgnoreCase(method)) { ! httpMethod = new PostMethod(url); } else if ("get".equalsIgnoreCase(method)) { httpMethod = new GetMethod(url); *************** *** 187,194 **** // set the rest of the options httpMethod.setFollowRedirects(followRedirects); - httpMethod.setHttp11(oneDotOne); // this might be changing the default for HttpClient-what is the default? - httpMethod.setStrictMode(strictMode); ! // set the request headers if applicable if (headers != null) { for (int i = 0; i < headers.length; i++) { --- 205,222 ---- // set the rest of the options httpMethod.setFollowRedirects(followRedirects); ! HttpMethodParams optionsParams = new HttpMethodParams(); ! //httpMethod.setHttp11(oneDotOne); // this might be changing the default for HttpClient-what is the default? ! if(oneDotOne) ! optionsParams.setVersion(HttpVersion.HTTP_1_1); ! //httpMethod.setStrictMode(strictMode); ! if(strictMode) ! optionsParams.makeStrict(); ! else ! optionsParams.makeLenient(); ! httpMethod.setParams(optionsParams); ! ! ! // set the request headers if applicable if (headers != null) { for (int i = 0; i < headers.length; i++) { *************** *** 199,207 **** if ("post".equalsIgnoreCase(method)) { // if post parameters are passed, use those as the body, else use the document ! if ((postParameters != null) && (postParameters.length > 0)) { ! ((PostMethod) httpMethod).setRequestBody(babeldocToHttpClient(postParameters)); } else { ! ByteArrayInputStream bais = new ByteArrayInputStream(document.getBytes()); ! ((PostMethod) httpMethod).setRequestBody(bais); } } --- 227,238 ---- if ("post".equalsIgnoreCase(method)) { // if post parameters are passed, use those as the body, else use the document ! boolean params = (postParameters != null) && (postParameters.length > 0); ! if (params || fileparams) { ! addPostParams(httpMethod,postParameters,postFileParameters); } else { ! // deprecated ! //ByteArrayInputStream bais = new ByteArrayInputStream(document.getBytes()); ! //((PostMethod) httpMethod).setRequestBody(bais); ! ((PostMethod) httpMethod).setRequestEntity(new StringRequestEntity(new String(document.getBytes()))); } } *************** *** 214,217 **** --- 245,257 ---- byte[] responseBody = httpMethod.getResponseBody(); + //Get all the cookies + Cookie[] cookies = client.getState().getCookies(); + ArrayList alAtr = new ArrayList(); + for(int i=0; i<cookies.length; i++){ + LogService.getInstance().logDebug("HttpClientPipelineStage.process.debug Cookie found, name: " + cookies[i].getName() + " value: " + cookies[i].getValue() ); + Pair atr = new Pair("cookie_" + cookies[i].getName(),cookies[i].getValue()); + alAtr.add(atr); + } + // Release the connection. httpMethod.releaseConnection(); *************** *** 219,223 **** String[] results = new String[]{new String(responseBody)}; // todo: should this be the content type of the response? ! return super.processHelper("text/xml", results); } catch (Exception e) { throw new com.babeldoc.core.pipeline.PipelineException("HTTPWriter", e); --- 259,263 ---- String[] results = new String[]{new String(responseBody)}; // todo: should this be the content type of the response? ! return super.processHelper("text/xml", results[0],alAtr); } catch (Exception e) { throw new com.babeldoc.core.pipeline.PipelineException("HTTPWriter", e); *************** *** 226,229 **** --- 266,362 ---- /** + * Add post params + * + * @param httpMethod Object POST + * @param postParameters parameters (name,value) from add to post method + * @param postFileParameters file type parameters (name,value) from add to post method + * + * + **/ + private void addPostParams(HttpMethodBase httpMethod, NameValuePair[] postParameters, NameValuePair[] postFileParameters) throws PipelineException { + boolean fileparams = (postFileParameters != null) && (postFileParameters.length > 0); + boolean params = (postParameters != null) && (postParameters.length > 0); + LogService.getInstance().logDebug("addPostParams"); + + if(params){ + if(fileparams){ + Part[] parts = new Part[postParameters.length+postFileParameters.length]; + //add Params + for(int i=0;i<postParameters.length;i++){ + parts[i] = new StringPart(postParameters[i].getName(), postParameters[i].getValue()); + LogService.getInstance().logDebug("addPostParam, name:" +postParameters[i].getName() +" value:" +postParameters[i].getValue()); + } + //add FileParams + for(int i=0;i<postFileParameters.length;i++){ + File f1 = new File(postFileParameters[i].getValue()); + if(f1.isFile()){ + try{ + // + String mime = PipelineDocument.getMimeTypeForFile(f1.getName()); + // / + parts[postParameters.length+i] = new FilePart(postFileParameters[i].getName(), postFileParameters[i].getValue(),f1,mime,null); + LogService.getInstance().logDebug("addPostFileParam, name:" +postFileParameters[i].getName() +" value:" +postFileParameters[i].getValue()+" file:"+f1.getName()+" mime:"+mime); + }catch (java.io.FileNotFoundException fe){ + LogService.getInstance().logError("Invalid file path: " + postFileParameters[i].getValue()+ "."+ fe.getMessage(),fe ); + throw new PipelineException("Invalid file path: " + postFileParameters[i].getValue()); + } + } + else{ + throw new PipelineException("Invalid file path. File not found: " + postFileParameters[i].getValue()); + } + } + ((PostMethod) httpMethod).setRequestEntity(new MultipartRequestEntity(parts, httpMethod.getParams() )); + + } + else{ + //Nomes Params + ((PostMethod) httpMethod).addParameters(babeldocToHttpClient(postParameters)); + LogService.getInstance().logDebug(postParameters.length + " addPostsParams"); + } + } + else{ + if(fileparams){ + //Nomes fileparams + Part[] parts = new Part[postFileParameters.length]; + for(int i=0;i<postFileParameters.length;i++){ + File f1 = new File(postFileParameters[i].getValue()); + if(f1.isFile()){ + try{ + parts[i] = new FilePart(postFileParameters[i].getName(), postFileParameters[i].getValue(),f1); + LogService.getInstance().logDebug("addPostFileParam, name:" +postFileParameters[i].getName() +" value:" +postFileParameters[i].getValue()); + }catch (java.io.FileNotFoundException fe){ + LogService.getInstance().logError("Invalid file path: " + postFileParameters[i].getValue()+ "."+ fe.getMessage(),fe ); + throw new PipelineException("Invalid file path: " + postFileParameters[i].getValue()); + } + } + else{ + throw new PipelineException("Invalid file path. File not found: " + postFileParameters[i].getValue()); + } + } + ((PostMethod) httpMethod).setRequestEntity(new MultipartRequestEntity(parts, httpMethod.getParams() )); + } + // else No hi ha parametres + } + } + + /** + * + * This method could debug info of post params + * + * @param httpMethod + */ + private void debugParams(PostMethod httpMethod){ + LogService.getInstance().logDebug("DEBUG PostParams***************"); + org.apache.commons.httpclient.NameValuePair[] queParametresTinc = httpMethod.getParameters(); + for(int i=0;i<queParametresTinc.length;i++){ + LogService.getInstance().logDebug("Have PostParam["+i+"], name:" +queParametresTinc[i].getName() +" value:" +queParametresTinc[i].getValue()); + } + if(queParametresTinc.length==0){ + LogService.getInstance().logDebug("Don't have any PostParams"); + + } + } + + /** * convert babeldoc NameValuePair[] to HttpClient NameValuePair **/ |