|
From: <tr...@us...> - 2003-07-15 22:46:59
|
Update of /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/pipeline/stage In directory sc8-pr-cvs1:/tmp/cvs-serv24833/web/src/com/babeldoc/web/pipeline/stage Modified Files: HttpClientPipelineStage.java Log Message: added the header and some small reformattings. Index: HttpClientPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/pipeline/stage/HttpClientPipelineStage.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** HttpClientPipelineStage.java 14 Jul 2003 20:49:47 -0000 1.1 --- HttpClientPipelineStage.java 15 Jul 2003 22:46:56 -0000 1.2 *************** *** 1,2 **** --- 1,67 ---- + /* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" must + * not be used to endorse or promote products derived from this + * software without prior written permission. For written + * permission, please contact ap...@ap.... + * + * 5. Products derived from this software may not be called "Apache", + * nor may "Apache" appear in their name, without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + * Portions of this software are based upon public domain software + * originally written at the National Center for Supercomputing Applications, + * University of Illinois, Urbana-Champaign. + * ==================================================================== + * + * Babeldoc: The Universal Document Processor + * + * $Header$ + * $DateTime$ + * $Author$ + * + */ package com.babeldoc.web.pipeline.stage; *************** *** 5,29 **** // 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 --- 70,90 ---- // TODO: handle all http methods import com.babeldoc.core.I18n; + import com.babeldoc.core.NameValuePair; + import com.babeldoc.core.option.ComplexConfigOptionType; import com.babeldoc.core.option.ConfigOption; 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; /** ! * Pipeline stage for http/https client * * @author Jonathan Leech *************** *** 47,56 **** /* 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"; --- 108,117 ---- /* 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"; *************** *** 65,69 **** // 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"))); --- 126,130 ---- // 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"))); *************** *** 86,201 **** * @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()); } } --- 147,254 ---- * @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()); } } |