Author: stevel Date: Tue Jun 27 06:26:40 2006 New Revision: 417451 URL: http://svn.apache.org/viewvc?rev=417451&view=rev Log: Http tasks for ant
Added: ant/sandbox/antlibs/http/trunk/build.xml ant/sandbox/antlibs/http/trunk/docs/ ant/sandbox/antlibs/http/trunk/docs/http-antlib.html ant/sandbox/antlibs/http/trunk/src/ ant/sandbox/antlibs/http/trunk/src/etc/ ant/sandbox/antlibs/http/trunk/src/etc/testcases/ ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/ ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/base.xml ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/getTest.xml ant/sandbox/antlibs/http/trunk/src/main/ ant/sandbox/antlibs/http/trunk/src/main/org/ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Base64Encode.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/ContentGuesser.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Hostname.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpAuthenticationStrategy.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpBasicAuth.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpGet.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpHead.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpPost.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpRequestParameter.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpTask.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/NullOutputStream.java ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/antlib.xml ant/sandbox/antlibs/http/trunk/src/war/ ant/sandbox/antlibs/http/trunk/src/war/WEB-INF/ ant/sandbox/antlibs/http/trunk/src/war/WEB-INF/web.xml ant/sandbox/antlibs/http/trunk/src/war/resources/ ant/sandbox/antlibs/http/trunk/src/war/resources/error.jsp ant/sandbox/antlibs/http/trunk/src/war/resources/headers.jsp ant/sandbox/antlibs/http/trunk/src/war/resources/index.html Added: ant/sandbox/antlibs/http/trunk/build.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/build.xml?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/build.xml (added) +++ ant/sandbox/antlibs/http/trunk/build.xml Tue Jun 27 06:26:40 2006 @@ -0,0 +1,23 @@ +<?xml version="1.0"?> +<!-- + Copyright 2006 The Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project default="compile" name="http"> + + <!-- easy way to override properties --> + <property file="build.properties"/> + + <import file="common/build.xml"/> +</project> \ No newline at end of file Added: ant/sandbox/antlibs/http/trunk/docs/http-antlib.html URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/docs/http-antlib.html?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/docs/http-antlib.html (added) +++ ant/sandbox/antlibs/http/trunk/docs/http-antlib.html Tue Jun 27 06:26:40 2006 @@ -0,0 +1,364 @@ +<html> +<head> +<title>Http Antlib</title> +</head> +<body> +<h2>Http Antlib</h2> + +This antlib contains tasks to make the basic HTTP requests: +get, post, head, put, with Basicauthentication. + + +<h3>Core Functionality and Parameters</h3> + +<p>Common functionality to the core tasks tasks is: </p> + +<ol> +<li>Ability to name the remote URL which is the target of the request.</li> + +<li>Ability to name a local file as the local store of any returned +content.</li> + +<li>Ability to name a property as the local store of any returned +content.</li> + +<li>Ability to name a property to be set to "true" when a +request succeeds.</li> + +<li>The option to list a number of parameters, each with a name and a +value. Some methods (HttpGet, HttpHead) attach these parameters to the +stated url to generate the url to actually fetch. Others (HttpPost) send +the parameters up in the standard representation of form data.</li> + +<li>The option to state the authentication policy and then the username +and password. Currently only basic authentication is used, which is +utterly insecure except over an https link</li> + +<li>A 'verbose' option which provides extra information and progess +information during a download.</li> + +<li>Timestamp control, using the <i>usetimestamp</i> flag. When set the +timestamp of downloaded content is set to match that of the remote file +and the local timestamp of the destination +file (if it exists) used to set the if-modified-since header in the +request, which will trigger optional download only. </li> + +</ol> + +<h3>Parameters</h3> + +<p> The <i>url</i> parameter specifies the URL to access. The optional +<i>destFile</i> parameter specifies a destination to which the retrieved +page will be written. The optional <i>destProperty </i>parameter +specifies a name of a property to save the content to, instead of a +property. If neither <i>destFile</i> nor <i>destProperty</i> +specified, the contents of the specified URL are discarded (this is +useful when accessing the URL for the purpose of causing some action on +the remote server).</p> + +<p> When the <i>verbose</i> option is enabled, the task displays a '.' for every + 64 KB retrieved. If the <i>blocksize</i> parameter is adjusted then files are + uploaded or downloaded in a different size block from this, and progress markers + appear appropriately. </p> + +The <i>usetimestamp</i> option enables you to control downloads so that +the remote file is only fetched if newer than the local copy. If there +is no local copy, the download always takes place. When a file is +downloaded, the timestamp of the downloaded file is set to the remote +timestamp. NB: This timestamp facility +only works on downloads using the HTTP protocol. + +<p>The <i>authtype</i>, <i>username</i>, and <i>password</i> options enable support + for password protected pages. Currently only 'Basic' authentication is used, + which is notoriously insecure except over an encrypted https channel.</p> +<table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">authtype</td> + <td valign="top">the HTTP authentication protocol to use, <i>none</i> or <i>basic</i>.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">blocksize</td> + <td valign="top">size (in kilobytes) of the data block used for upload and + download. Default: 64.<br> + Keep this to a multiple of the hard disk sector size for file IO performance.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top"><b>destfile</b></td> + <td valign="top">the file where to store the retrieved file.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">destproperty</td> + <td valign="top">the name of a property to fill with the returned content. + Ignored if <i>destFile</i> is set</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">failonerror</td> + <td valign="top">stop the build if the request failed. default: true</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">password</td> + <td valign="top">the password for authentication.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">status</td> + <td valign="top">the name of a property to set to "true" if the + request succeeds.<br> + Set <i>failonerror</i> to false for this to be of use.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top"><b>url</b></td> + <td valign="top">the URL from which to retrieve a file.</td> + <td align="center" valign="top">Yes</td> + </tr> + <tr> + <td valign="top">usecaches</td> + <td valign="top">boolean to enable 'caching' of content during the fetch process. + default:false</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">useresponsecode</td> + <td valign="top">boolean to enable success/failure to be determined by result + of the received response code. HTTP only. default=true.</td> + <td align="center" valign="top"> </td> + </tr> + <tr> + <td valign="top">username</td> + <td valign="top">the user name for authentication.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">usetimestamp</td> + <td valign="top">boolean flag to conditionally download a file based on the + timestamp of the local copy. HTTP only</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">verbose</td> + <td valign="top"> boolean flag to control progress information display.</td> + <td align="center" valign="top">No</td> + </tr> +</table> +<p> The <i>status</i> attribute names a property which will be set to "true" if + the request was deemed successful. For any non-http protocol, success is defined + as the request completing. For http and https, success is defined as the request + completing, and the response code from the serving being one of the 'success' + values -any number between 200 and 299 inclusive. The usual HTTP_OK (200) is + therefore a success, as is HTTP_ACCEPTED (202). But failures such as BAD_REQUEST + (400) and the ubiquitous HTTP_NOT_FOUND (404) are most definately errors. So + an attempt to access a missing url may result 'failure',even though some content + was download (such as, perhaps, the 'file not found' text). If this is not what + you desire, then set <i>useresponsecode</i>="false" for the system + to interpret any data back as a success. +<h3>Parameters specified as nested elements</h3> + +<p><b>param</b></p> + +<p>Specifies an HTTP request parameter to send as part of the request. +For <i>get</i> and <i>head</i> request methods the parameters are +encoded as part of the URL. For <i>post</i> request methods, the +parameters are sent as the POST request data.</p> + +<table cellspacing="0" cellpadding="2" border="1"> + <tbody> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td valign="top" align="middle"><b>Required</b></td> + </tr> + <tr> + <td valign="top">name</td> + <td valign="top">the name of the request property to set.</td> + <td valign="top" align="middle">Yes</td> + </tr> + <tr> + <td valign="top">value</td> + <td valign="top">the value of the request property. You may alternatively + specify the value as text between the beginning and ending param tags.</td> + <td valign="center" align="middle">Yes</td> + </tr> + </tbody> +</table> + +<p><b>header</b></p> + +<p>Specifies an arbitrary HTTP request header that will be sent with the +request.</p> + +<table cellspacing="0" cellpadding="2" border="1"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td valign="top" align="middle"><b>Required</b></td> + </tr> + <tr> + <td valign="top">name</td> + <td valign="top">the name of the HTTP request header</td> + <td valign="top" align="middle">Yes</td> + </tr> + <tr> + <td valign="top">value</td> + <td valign="top">the value of the HTTP request header. You may alternatively + specify the value as text between the beginning and ending header tags.</td> + <td valign="center" align="middle">Yes</td> + </tr> +</table> +<h3>Quirky Limitations of java.net classes</h3> + +Multiple HTTP headers can with the same name can <b>not</b> be set, even +though the protocol permits it. Java 1.2 <i>may</i> permit multiple +cookies to be set, but this behaviour is explicitly not supported on Java1.3, +as someone went and change the code to stop this (Java bug ID #4242254). +You need to set multiple cookies in one go and hope the far end can handle it +<p> +Bug ID #4160499 covers another issue, to wit: some versions of Java throw +exceptions when an error code is greater than 400 and the destFile file isn't +one of a few simple file types, but Java 1.2 and 1.3 do not. So there +is no way to get the error text when a jsp page throws some exception. +<p> + +Also, on Java1.2 and 1.3,if you recieve a short response with less content than +the content-length header promises, the library seems to silently +reduce the content length header to match, which seems the wrong action. + + + +<h2><a name="httpget">HttpGet</a></h2> +<h3>Description</h3> + +<p>Accesses a URL to retrieve a file or to cause some action on the server.</p> + +<p>All the attributes of httptask may be used. Note that a quirk of the implementation + of the http client in java makes it impossible to reliably fetch the response + details from any unsuccessful request against a URL which doesn't end in '.htm,.html + or .txt'. This means that if the task is used to compile JSP pages by issuing + request against them, the text details of any errors will not be picked up. +<h3>Examples</h3> + +<pre> <httpget url="http://jakarta.apache.org/" destFile="help/index.html"/></pre> +<p>Fetches the index page of http://jakarta.apache.org/, and stores it in the + file <code>help/index.html</code>. </p> + +<pre> <httpget src="http://jakarta.apache.org/builds/tomcat/nightly/ant.zip" + destFile="optional.jar" + verbose="true" + usetimestamp="true" + > + <header name="Cookie" value="someid=43ff2b"/> + </httpget></pre> +<p> Retrieves the nightly ant build from the tomcat distribution, if the local + copy is missing or out of date. Uses the verbose option for progress information. + A cookie is supplied for the server's benefit.</p> +<pre> <httpget url="https://www.pizzaservices.com/prices.jsp" + destFile="pizza-prices.xml"> + <param name="zipcode">57340</param> + <param name="pizza">pepperoni</param> + </httpget></pre> +<p>Builds a URL by adding parameters ("?zipcode=57340&pizza=pepperoni") + to the base URL and then fetches the contents (fictional example)</p> +<h2><a name="httphead">HttpHead</a> </h2> +<p>The http HEAD request is similar to the normal GET request , except it, by + definition, returns no content, just a success code and http headers. Accordingly, + the destination properties of the base class -<i>destFile</i> and -, <i>destinationpropertyname</i>) + are not supported -any attempt to use them will result in a build failure. Note + also that the http and https protocols are the only protocols supported. </p> +<p> +HttpHead is useful for triggering server side actions, but note that many servers +interpret a HEAD very differently from a GET. An HttpGet request with the +return data discarded is often a more reliable approach. +</p> + +<p> Where head can be useful is in testing for the availability and reachability + of servers, such as in the following test for apache being reachable, which + sets a variable on success:- +<pre> + <httphead url="http://www.apache.org/" + failonerror="false" + successproperty="reachable.apache" + /> +</pre> + +<p>Note that sometimes a missing file on a mis-configured server still generates + a successful '200' response to a GET request -and returns a 'missing' file page, + but a HEAD request will reliably pick up the 'missing file' error. </p> +<h2><a name="httppost">HttpPost</a></h2> + +<p>This implements the POST request. Supplied parameter data is turned into form + data and sent as the body of the request, rather than appended to the URL. If + a file to upload is specified instead, using <i>uploadFile</i>, the parameter + values are ignored. Instead the content type of the file is sent in the header + -based on the <i>contentType</i> attribute or what the java runtime thinks the + content type is based on the file extension. The file is uploaded. </p> + +<p>Like HttpGet, this command can return a content which can downloaded to a file, + to a property, or just ignored.</p> + +<p>This task adds two new attributes to the base set. </p> +<table cellspacing="0" cellpadding="2" border="1"> + <tr> + <td valign="top" width="78"><b>Attribute</b></td> + <td valign="top" width="559"><b>Description</b></td> + <td valign="top" align="middle" width="62"><b>Required</b></td> + </tr> + <tr> + <td valign="top" width="78">uploadFile</td> + <td valign="top" width="559">a file to upload. when specified, all parameters + are ignored.</td> + <td valign="top" align="middle" width="62"> + <div align="center">no</div> + </td> + </tr> + <tr> + <td valign="top" width="78">contentType </td> + <td valign="top" width="559">the type of the content (text/html, text/xml, + application/binary, etc). Only of relevance when a file is being uploaded, + and still optional in that case. </td> + <td valign="center" align="middle" width="62"> + <div align="center">no</div> + </td> + </tr> +</table> +<h3></h3> +<pre> + <httppost url="http://www.example.com/servlet/docserver" + authtype="basic" username="joe" password="silly"> + <param name="action" value="getdoc"/> + <param name="ISBN">038550120X</param> + <param name="pages">19-20</param> + <header name="Accept-Language" value="en-us"/> + </httppost></pre> +<p>Accesses a server at www.foo.com, passing a request to some servlet asking it +to retrieve several pages from a stored book. An HTTP header specifying +acceptable languages for the returned contents is also sent. Basic +authentication is used with a user name of "joe" and a password of +"silly".</p> +<p> +<pre> <httppost url="https://www.pizzaservices.com" + uploadFile="pizza-order.xml" + contentType="text/xml"> +</pre> +<p>Sends a pre-prepared order for a pizza to a pizza vendor accepting orders using + xml-rpc requests. (NB: fictional example) </p> +<p align="center">Copyright © 2000-2006 Apache Software Foundation. All rights +Reserved.</p> + +</body> +</html> + + + + + + Added: ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/base.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/base.xml?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/base.xml (added) +++ ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/base.xml Tue Jun 27 06:26:40 2006 @@ -0,0 +1,49 @@ +<?xml version="1.0"?> + +<!-- + Copyright 2006 The Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<project name="http-base" + default="antunit" xmlns:http="antlib:org.apache.ant.http" + xmlns:au="antlib:org.apache.ant.antunit"> + + + <target name="fail"> + <fail>Only run via JUnit/Antunit</fail> + </target> + + <target name="setUp"> + <property name="port" value="8080"/> + <property name="host" value="127.0.0.1"/> + <property name="webapp" value="/testwar"/> + <property name="webapp.url" value="http://${host}:${port}/${webapp}"/> + <property name="index.html" + value="${webapp.url}/index.html"/> + <property name="error.jsp" + value="${webapp.url}/error.jsp" /> + <property name="headers.jsp" + value="${webapp.url}/headers.jsp"/> + + </target> + + <target name="antunit"> + <au:antunit> + <fileset file="${ant.file}"/> + <au:plainlistener/> + </au:antunit> + </target> + + </project> \ No newline at end of file Added: ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/getTest.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/getTest.xml?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/getTest.xml (added) +++ ant/sandbox/antlibs/http/trunk/src/etc/testcases/http/getTest.xml Tue Jun 27 06:26:40 2006 @@ -0,0 +1,81 @@ +<?xml version="1.0"?> + +<!-- + Copyright 2006 The Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<project name="http-get-test" + default="fail" xmlns:http="antlib:org.apache.ant.http" + xmlns:au="antlib:org.apache.ant.antunit"> + + + <import file="base.xml"/> + + <target name="testBadURL"> + <au:expectfailure> + <http:get url="more cheese, gromit?" /> + </au:expectfailure> + </target> + + <!-- test relies on example.org always being invalid. + Although the IETF require this, verisign, in their wisdom, have + been known to change things--> + <target name="testBadHost"> + <au:expectfailure > + <http:get url="http://invalid.example.org/ex1"/> + </au:expectfailure> + </target> + + <target name="test200"> + <http:get url="${error.jsp}" /> + </target> + + <target name="test200-status"> + <http:get url="${error.jsp}" status="status"/> + <au:assertPropertyEquals name="status" value="200" /> + </target> + + <target name="test200-text"> + <http:get url="${error.jsp}" + destProperty="result"/> + <au:assertPropertyContains name="result" value="error_code=200"/> + </target> + + <target name="testPaymentRequired"> + <http:get url="${error.jsp}" status="status" + failonerror="false"> + <param name="code" value="402" /> + </http:get> + <au:assertPropertyEquals name="status" value="402"/> + </target> + + <target name="testPaymentRequiredFailing"> + <au:expectfailure message="expected error code 402"> + <http:get url="${error.jsp}" + failonerror="false"> + <param name="code" value="402"/> + </http:get> + </au:expectfailure> + </target> + + <target name="testHeaders"> + <http:get url="${headers.jsp}" destProperty="result"> + <header name="X-custom-header" value="custom-header-value"/> + </http:get> + <au:assertPropertyContains name="result" value="X-custom-header"/> + <au:assertPropertyContains name="result" value="custom-header-value"/> + </target> + +</project> \ No newline at end of file Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Base64Encode.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Base64Encode.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Base64Encode.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Base64Encode.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,75 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + +/** + * This code handles Base64 encoding for basic authentication and the like. + * + * @created 02 April 2001 + */ +public class Base64Encode { + + /** + * the encode alphabet. + */ + private char[] alphabet = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" + .toCharArray(); + + + /** + * Encode a block of binary data as base64 as specified in RFC1521. + * + * @param data the binary data to encode. + * @return An array of characters that represent the data encoded as Base64 + */ + public char[] encodeBase64(byte[] data) { + char[] out = new char[((data.length + 2) / 3) * 4]; + + // + // 3 bytes encode to 4 chars. Output is always an even + // multiple of 4 characters. + // + for (int i = 0, index = 0; i < data.length; i += 3, index += 4) { + boolean quad = false; + boolean triple = false; + //convert to unsigned byte + int val = (0xFF & (int) data[i]); + val <<= 8; + if ((i + 1) < data.length) { + val |= (0xFF & (int) data[i + 1]); + triple = true; + } + val <<= 8; + if ((i + 2) < data.length) { + val |= (0xFF & (int) data[i + 2]); + quad = true; + } + out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)]; + val >>= 6; + out[index + 2] = alphabet[(triple ? (val & 0x3F) : 64)]; + val >>= 6; + out[index + 1] = alphabet[val & 0x3F]; + val >>= 6; + out[index + 0] = alphabet[val & 0x3F]; + } + return out; + } + +} + Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/ContentGuesser.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/ContentGuesser.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/ContentGuesser.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/ContentGuesser.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,63 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ant.http; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; + +/** + * this is a class to work around the fact a function (guessContentTypeFromName) + * is protected in java1.3 and below. It is public since then, but as Ant runs + * on older runtimes, this class exists to poke a hole in the security + * + * @created March 17, 2001 + */ +public class ContentGuesser extends URLConnection { + + /** + * stub Constructor for the ContentGuesser object. + * + * @param url Description of Parameter + */ + ContentGuesser(URL url) { + super(url); + } + + + /** + * this stub is needed for the build. + * + * @throws IOException Description of Exception + */ + public void connect() + throws IOException { + } + + + /** + * make a protected method public. This guesses file type from extension. + * It's ok for very well known types... + * + * @param filename file to guess type of + * @return what the system guessed + */ + public static String guessContentType(String filename) { + return guessContentTypeFromName(filename); + } +} + Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Hostname.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Hostname.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Hostname.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/Hostname.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,151 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * trivial task to get the hostname of a box; as IPaddr, hostname, or fullname. + * + * @created 07 January 2002 + */ + +public class Hostname extends Task { + + /** + * Property to set. + */ + private String property; + + /** + * Should we fail on an error? + */ + private boolean failonerror = true; + + /** + * The address to look up + */ + private boolean address = false; + + + /** + * Sets the FailOnError attribute. + * + * @param failonerror The new FailOnError value + */ + public void setFailOnError(boolean failonerror) { + this.failonerror = failonerror; + } + + + /** + * Sets the Address attribute. + * + * @param address The new Address value + */ + public void setAddress(boolean address) { + this.address = address; + } + + /** + * name the property to set to the result. + * + * @param property the property name + */ + public void setProperty(String property) { + this.property = property; + } + + /** + * Does the work. + * + * @throws BuildException Thrown in unrecoverable error. + */ + public void execute() + throws BuildException { + if (property == null) { + throw new BuildException("Property attribute must be defined"); + } + String result; + Throwable exception = null; + try { + if (address) { + result = getAddress(); + } else { + result = getHostname(); + } + project.setNewProperty(property, result); + } catch (UnknownHostException e) { + exception = e; + } catch (SecurityException e) { + exception = e; + } + if (exception != null) { + if (failonerror) { + throw new BuildException("resolving hostname", exception); + } else { + log("failed to resolve local hostname", Project.MSG_ERR); + } + } + } + + + /** + * Gets the Address attribute of the Hostname object + * + * @return The Address value + * @throws SecurityException Description of Exception + * @throws UnknownHostException Description of Exception + */ + public String getAddress() + throws SecurityException, UnknownHostException { + return getLocalHostAddress().getHostAddress(); + } + + + /** + * Gets the Hostname attribute of the Hostname object + * + * @return The Hostname value + * @throws SecurityException Description of Exception + * @throws UnknownHostException Description of Exception + */ + public String getHostname() + throws SecurityException, UnknownHostException { + return getLocalHostAddress().getHostName(); + } + + + /** + * Gets the LocalHostAddress attribute of the Hostname object + * + * @return The LocalHostAddress value + * @throws UnknownHostException Description of Exception + */ + public static InetAddress getLocalHostAddress() + throws UnknownHostException { + return InetAddress.getLocalHost(); + } + +} + Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpAuthenticationStrategy.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpAuthenticationStrategy.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpAuthenticationStrategy.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpAuthenticationStrategy.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,55 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package org.apache.ant.http; + +import org.apache.tools.ant.BuildException; + +import java.net.URLConnection; + +/** + * this interface is for use by classes which authenticate connections. + * + * @created 20 March 2001 + */ + +public interface HttpAuthenticationStrategy { + /** + * property used in the request. [EMAIL PROTECTED] + */ + String AUTH_PROPERTY = "Authorization"; + + + /** + * Sets the AuthenticationHeader. + * + * @param requestConnection The current request + * @param responseConnection any previous request, which can contain a + * challenge for the next round. Will often be + * null + * @param user the current user name + * @param password the current password + */ + public void setAuthenticationHeader(URLConnection requestConnection, + URLConnection responseConnection, + String user, String password) + throws BuildException; + + +} + Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpBasicAuth.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpBasicAuth.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpBasicAuth.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpBasicAuth.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,67 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + +import org.apache.tools.ant.BuildException; + +import java.net.URLConnection; + + +/** + * this class implements basic auth, the one that shouldn't be used except over + * an encrypted link or trusted network. + * + * @created 20 March 2001 + */ + +public class HttpBasicAuth implements HttpAuthenticationStrategy { + /** + * string used for basic auth [EMAIL PROTECTED] + */ + public static final String BASIC_AUTH = "BASIC "; + + + /** + * Sets the AuthenticationHeader attribute of the HttpAuthStrategy object + * + * @param requestConnection The current request + * @param responseConnection any previous request, which can contain a + * challenge for the next round. Will often be + * null + * @param username the current user name + * @param password the current password + */ + public void setAuthenticationHeader(URLConnection requestConnection, + URLConnection responseConnection, + String username, String password) { + + if (username != null) { + password = username == null ? "" : password; + String encodeStr = username + ":" + password; + char[] encodedPass; + String encodedPassStr; + + Base64Encode encoder = new Base64Encode(); + encodedPass = encoder.encodeBase64(encodeStr.getBytes()); + encodedPassStr = new String(encodedPass); + String authStr = BASIC_AUTH + encodedPassStr; + requestConnection.setRequestProperty(AUTH_PROPERTY, authStr); + } + } +} + Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpGet.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpGet.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpGet.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpGet.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,50 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + + +/** + * This class implements boring old HTTP1.0 get. It represents a refactoring of + * Get to HttpTask and then into a specific subclass. because almost everything + * is done by the parent, this class is almost completely empty + * + * @created March 17, 2001 + */ + +public class HttpGet extends HttpTask { + + /** + * override of the default; parameters are appended to the URL for a get. + * + * @return true always + */ + + protected boolean areParamsAddedToUrl() { + return true; + } + + /** + * this must be overridden by implementations to set the request method to + * GET, POST, whatever. + * + * @return GET, obviously + */ + public String getRequestMethod() { + return "GET"; + } +} Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpHead.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpHead.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpHead.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpHead.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,68 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + +import org.apache.tools.ant.BuildException; + +/** + * Head is a get with a different method and the notion of destination file + * missing. Why would anyone want to make a HEAD request? a) side effects on the + * server and b) polling for stuff + * + * @created March 17, 2001 + */ + +public class HttpHead extends HttpTask { + + /** + * this must be overridden by implementations to set the request method to + * GET, POST, whatever. + * + * @return HEAD always + */ + public String getRequestMethod() { + return "HEAD"; + } + + /** + * in HEAD requests, parameters go after the URL. + * + * @return true always + */ + + protected boolean areParamsAddedToUrl() { + return true; + } + + /** + * add a check for all the destination settings being null; nothing else + * makes sense for a HEAD. + * + * @throws BuildException only throw this when the failonerror flag is true + */ + + protected void verifyArguments() + throws BuildException { + if (getDestFile() != null || getDestinationProperty() != null) { + throw new BuildException( + "destination properties must not be defined for a HEAD request"); + } + super.verifyArguments(); + } + +} Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpPost.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpPost.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpPost.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpPost.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,195 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.net.URLConnection; +import java.util.Vector; + +/** + * this class does post of form content or raw files. you can have one or the + * other -as soon as a file is specified all the other properties are dropped on + * the floor. a file post will have content type determined from the extension, + * you can override it + * + * @created March 17, 2001 + */ + +public class HttpPost extends HttpTask { + + /** + * file to upload. Null is ok + */ + + protected File postFile = null; + + /** + * set the file to post. + */ + public void setUploadFile(File postFile) { + this.postFile = postFile; + } + + /** + * query the post file. + * + * @return the file or null for 'not defined' + */ + public File getUploadFile() { + return postFile; + } + + /** + * content type. ignored when the file is null, and even then we guess it if + * aint specified + */ + + private String contentType; + + /** + * set the content type. Recommended if a file is being uploaded + */ + public void setContentType(String contentType) { + this.contentType = contentType; + } + + /** + * query the content type. + * + * @return the content type or null for 'not defined' + */ + public String getContentType() { + return contentType; + } + + /** + * in a POST, params go in the payload, not in the URL. + * + * @return false always + */ + + protected boolean areParamsAddedToUrl() { + return false; + } + + /** + * this override of the base connect pumps up the parameter vector as form + * data. + * + * @param connection where to connect to + * @throws BuildException build trouble + * @throws IOException IO trouble + */ + protected URLConnection doConnect(URLConnection connection) + throws BuildException, IOException { + + if (postFile == null) { + return doConnectFormPost(connection); + } else { + return doConnectFilePost(connection); + } + } + + /** + * feed up the parameter vector as form data. + * + * @param connection where to connect to + * @throws BuildException build trouble + * @throws IOException IO trouble + */ + protected URLConnection doConnectFormPost(URLConnection connection) + throws BuildException, IOException { + + log("Posting data as a form", Project.MSG_VERBOSE); + // Create the output payload + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(256); + PrintWriter out = new PrintWriter(byteStream); + writePostData(out); + out.flush(); + out.close(); + byte[] data = byteStream.toByteArray(); + //send it + + return doConnectWithUpload(connection, + "application/x-www-form-urlencoded", + byteStream.size(), + new ByteArrayInputStream(data)); + } + + /** + * feed up the data file. + * + * @param connection where to connect to + * @throws BuildException build trouble + * @throws IOException IO trouble + */ + protected URLConnection doConnectFilePost(URLConnection connection) + throws BuildException, IOException { + int size = (int) postFile.length(); + log("Posting file " + postFile, Project.MSG_VERBOSE); + InputStream instream = new FileInputStream(postFile); + String type = contentType; + if (type == null) { + type = ContentGuesser.guessContentType(postFile.getName()); + } + return doConnectWithUpload(connection, + type, + size, + instream); + } + + + /** + * write out post data in form mode. + * + * @param out Description of Parameter + */ + protected void writePostData(PrintWriter out) { + HttpRequestParameter param; + Vector params = getRequestParameters(); + for (int i = 0; i < params.size(); i++) { + if (i > 0) { + out.print('&'); + } + param = (HttpRequestParameter) params.get(i); + out.print(param.toString()); + log("parameter : " + param.toString(), Project.MSG_DEBUG); + } + } + + /** + * this must be overridden by implementations to set the request method to + * GET, POST, whatever. + * + * @return the method string + */ + public String getRequestMethod() { + return "POST"; + } + + +} Added: ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpRequestParameter.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpRequestParameter.java?rev=417451&view=auto ============================================================================== --- ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpRequestParameter.java (added) +++ ant/sandbox/antlibs/http/trunk/src/main/org/apache/ant/http/HttpRequestParameter.java Tue Jun 27 06:26:40 2006 @@ -0,0 +1,106 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.http; + +import org.apache.tools.ant.ProjectComponent; + +import java.net.URLEncoder; + +/** + * This class is used to store name-value pairs for request parameters and + * headers. + * + * @created March 17, 2001 + */ + +public class HttpRequestParameter extends ProjectComponent { + + /** + * request name. + */ + private String name; + + /** + * request value. + */ + private String value; + + + /** + * Sets the Name of the request. + * + * @param name The new Name value + */ + public void setName(String name) { + this.name = name; + } + + /** + * What is the request name? + * + * @return The Name value + */ + public String getName() { + return name; + } + + + /** + * Sets the Value of the request. + * + * @param value The new Value value + */ + public void setValue(String value) { + this.value = value; + } + + + + /** + * What is the request value? + * + * @return The Value + */ + public String getValue() { + return value; + } + + + /** + * set the inline text, with property expansion. + * + * @param text Text to add to the request. + */ + public void addText(String text) { + this.value = getProject().replaceProperties(text); + } + + + /** + * simple stringifier returning name and value encoded for use in HTTP + * requests Although deprecated, the new version only came with Java1.4. + * + * @return a string for informational purposes + */ + public String toString() { + return URLEncoder.encode(getName()) + + '=' + URLEncoder.encode(getValue()); + } + +} + --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]