|
From: <jon...@us...> - 2003-07-14 20:49:51
|
Update of /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/pipeline/stage In directory sc8-pr-cvs1:/tmp/cvs-serv30021/src/com/babeldoc/web/pipeline/stage Added Files: HttpClientPipelineStage.java Log Message: Initial version of HttpClientPipelineStage supporting http get, post. --- NEW FILE: HttpClientPipelineStage.java --- package com.babeldoc.web.pipeline.stage; // TODO: http authentication // TODO: handle cookies // TODO: handle all http methods import java.util.ArrayList; import java.util.Collection; import java.io.*; import java.net.*; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import com.babeldoc.core.I18n; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigInfo; import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.option.ComplexConfigOptionType; import com.babeldoc.core.pipeline.PipelineDocument; import com.babeldoc.core.pipeline.PipelineException; import com.babeldoc.core.pipeline.PipelineStage; import com.babeldoc.core.pipeline.PipelineStageInfo; import com.babeldoc.core.pipeline.PipelineStageResult; import com.babeldoc.core.NameValuePair; /** * Pipeline stage for http/https client * * @author Jonathan Leech * @version 1.0 */ public class HttpClientPipelineStage extends PipelineStage { /* http method: get,post,etc. */ public final static String HTTP_METHOD = "method"; /* the URL */ public final static String HTTP_URL = "URL"; /* the query string is option list of name value pairs */ public final static String HTTP_QUERY_STRING = "queryString"; /* whether to automatically follow redirects */ public final static String HTTP_FOLLOW_REDIRECTS = "followRedirects"; /* whether to use http 1.1 protocol */ public final static String HTTP_1_1 = "http1.1"; /* whether to use strict mode */ public final static String HTTP_STRICT_MODE = "strictMode"; /* the headers as option list of name value pairs */ public final static String HTTP_HEADERS = "headers"; /* the post parameters as name value pairs */ public final static String HTTP_POST_PARAMETERS = "parameters"; /** * Return this stages info */ public HttpClientPipelineStage() { super(new PipelineStageInfo() { public String getName() { return "HttpClient"; } public String getDescription() { return I18n.get("web.200"); } public Collection getTypeSpecificOptions() { ArrayList options = new ArrayList(); // add specific options ComplexConfigOptionType ccot = new ComplexConfigOptionType((ConfigOption [])null); // no constant for this in IConfigOptionType options.add(new ConfigOption(HTTP_METHOD, IConfigOptionType.STRING, null, false, I18n.get("web.201"))); options.add(new ConfigOption(HTTP_URL, IConfigOptionType.URL, null, false, I18n.get("web.202"))); options.add(new ConfigOption(HTTP_QUERY_STRING, ccot, null, false, I18n.get("web.203"))); options.add(new ConfigOption(HTTP_FOLLOW_REDIRECTS, IConfigOptionType.BOOLEAN, null, false, I18n.get("web.204"))); options.add(new ConfigOption(HTTP_1_1, IConfigOptionType.BOOLEAN, null, false, I18n.get("web.205"))); options.add(new ConfigOption(HTTP_STRICT_MODE, IConfigOptionType.BOOLEAN, null, false, I18n.get("web.206"))); 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; } }); } /** * Process the pipeline document * * @return * * @throws PipelineException */ public PipelineStageResult[] process() throws PipelineException { try { // get the document PipelineDocument document = this.getDocument(); // get all the options String method = getOptions(HTTP_METHOD); String url = getOptions(HTTP_URL); NameValuePair[] queryString = getOptionList(new String[]{HTTP_QUERY_STRING}); boolean followRedirects = "true".equalsIgnoreCase(getOptions(HTTP_FOLLOW_REDIRECTS)); boolean oneDotOne = "true".equalsIgnoreCase(getOptions(HTTP_1_1)); boolean strictMode = "true".equalsIgnoreCase(getOptions(HTTP_STRICT_MODE)); NameValuePair[] headers = getOptionList(new String[]{HTTP_HEADERS}); NameValuePair[] postParameters = getOptionList(new String[]{HTTP_POST_PARAMETERS}); // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Choose the appropriate Http method HttpMethodBase httpMethod; if ("post".equalsIgnoreCase(method)) { httpMethod = new PostMethod(url); } else if ("get".equalsIgnoreCase(method)) { httpMethod = new GetMethod(url); } else if ("".equals(method) || (method == null)) { // default to GET if nothing is specified httpMethod = new GetMethod(url); } else { throw new PipelineException("Invalid method: " + method); } // Build the query string if applicable if ((queryString != null) && (queryString.length > 0)) { // set the query string from the parameters httpMethod.setQueryString(babeldocToHttpClient(queryString)); } // 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++) { httpMethod.setRequestHeader(headers[i].getName(), headers[i].getValue()); } } 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); } } // todo: do something with the status code - write it as an attribute, throw an exception // if its something other than OK, etc. int statusCode = client.executeMethod(httpMethod); // Read the response body. byte[] responseBody = httpMethod.getResponseBody(); // Release the connection. httpMethod.releaseConnection(); 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); } } /** * convert babeldoc NameValuePair[] to HttpClient NameValuePair **/ public static org.apache.commons.httpclient.NameValuePair[] babeldocToHttpClient( NameValuePair[] pairs ) { if (pairs == null) { return null; } org.apache.commons.httpclient.NameValuePair[] pairs2 = new org.apache.commons.httpclient.NameValuePair[pairs.length]; for (int i = 0; i < pairs.length; i++) { pairs2[i] = babeldocToHttpClient(pairs[i]); } return pairs2; } /** * convert babeldoc NameValuePair to HttpClient NameValuePair **/ public static org.apache.commons.httpclient.NameValuePair babeldocToHttpClient( NameValuePair pair ) { if (pair == null) { return null; } return new org.apache.commons.httpclient.NameValuePair(pair.getName(), pair.getValue()); } } |