diff options
author | mattinger <[email protected]> | 2006-08-17 05:38:38 +0000 |
---|---|---|
committer | mattinger <[email protected]> | 2006-08-17 05:38:38 +0000 |
commit | 871350dd27dfbb12c0fd6dd6b7424260309b318c (patch) | |
tree | e1a57bfeedee925e23fdfccaa623548feb41f7f1 | |
parent | b934ba90b56508c5868675495bfb7f1e9b9e4cb4 (diff) |
Adding httpclient tasks.
git-svn-id: file:///home/sven/projects/JOGL/temp/ant-contrib/svn/ant-contrib-code/trunk/ant-contrib@21 32d7a393-a5a9-423c-abd3-5d954feb1f2f
17 files changed, 977 insertions, 1 deletions
diff --git a/src/java/net/sf/antcontrib/antlib.xml b/src/java/net/sf/antcontrib/antlib.xml index 69cfa43..44f9fda 100644 --- a/src/java/net/sf/antcontrib/antlib.xml +++ b/src/java/net/sf/antcontrib/antlib.xml @@ -5,5 +5,39 @@ <taskdef name="for" classname="net.sf.antcontrib.logic.ForTask" - onerror="report"/> + onerror="report"/>
+
+ <typedef
+ name="http-state"
+ classname="net.sf.antcontrib.net.httpclient.HttpStateType"
+ onerror="report" /> + <typedef
+ name="http-client"
+ classname="net.sf.antcontrib.net.httpclient.HttpClientType"
+ onerror="report" />
+
+ <taskdef name="add-cookie"
+ classname="net.sf.antcontrib.net.httpclient.AddCookieTask"
+ onerror="report" />
+ <taskdef name="clear-cookies"
+ classname="net.sf.antcontrib.net.httpclient.ClearCookiesTask"
+ onerror="report" />
+ <taskdef name="add-credentials"
+ classname="net.sf.antcontrib.net.httpclient.AddCredentialsTask"
+ onerror="report" />
+ <taskdef name="clear-credentials"
+ classname="net.sf.antcontrib.net.httpclient.ClearCredentialsTask"
+ onerror="report" />
+ <taskdef name="purge-expired-cookies"
+ classname="net.sf.antcontrib.net.httpclient.PurgeExpiredCookiesTask"
+ onerror="report" />
+ <taskdef name="post-method"
+ classname="net.sf.antcontrib.net.httpclient.PostMethodTask"
+ onerror="report" />
+ <taskdef name="get-method"
+ classname="net.sf.antcontrib.net.httpclient.GetMethodTask"
+ onerror="report" />
+ <taskdef name="head-method"
+ classname="net.sf.antcontrib.net.httpclient.HeadMethodTask"
+ onerror="report" />
</antlib> diff --git a/src/java/net/sf/antcontrib/net/httpclient/AbstractHttpStateTypeTask.java b/src/java/net/sf/antcontrib/net/httpclient/AbstractHttpStateTypeTask.java new file mode 100644 index 0000000..6f4d963 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/AbstractHttpStateTypeTask.java @@ -0,0 +1,45 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+
+public abstract class AbstractHttpStateTypeTask
+ extends Task {
+
+ private String stateRefId;
+
+ public void setStateRefId(String stateRefId) {
+ this.stateRefId = stateRefId;
+ }
+
+ public Credentials createCredentials() {
+ return new Credentials();
+ }
+
+ static HttpStateType getStateType(Project project, String stateRefId) {
+ if (stateRefId == null) {
+ throw new BuildException("Missing 'stateRefId'.");
+ }
+
+ Object stateRef = project.getReference(stateRefId);
+ if (stateRef == null) {
+ throw new BuildException("Reference '" + stateRefId +
+ "' is not defined.");
+ }
+ if (! (stateRef instanceof HttpStateType)) {
+ throw new BuildException("Reference '" + stateRefId +
+ "' is not of the correct type.");
+ }
+
+ return (HttpStateType) stateRef;
+ }
+
+ public void execute()
+ throws BuildException {
+ execute(getStateType(getProject(), stateRefId));
+ }
+
+ protected abstract void execute(HttpStateType stateType)
+ throws BuildException;
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/AbstractMethodTask.java b/src/java/net/sf/antcontrib/net/httpclient/AbstractMethodTask.java new file mode 100644 index 0000000..e5c15ef --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/AbstractMethodTask.java @@ -0,0 +1,185 @@ +package net.sf.antcontrib.net.httpclient;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.URI;
+import org.apache.commons.httpclient.URIException;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.taskdefs.Property;
+import org.apache.tools.ant.util.FileUtils;
+
+public abstract class AbstractMethodTask
+ extends Task {
+
+ private HttpMethodBase method;
+ private String clientRefId;
+ private File responseDataFile;
+ private String responseDataProperty;
+ private String statusCodeProperty;
+ private List responseHeaders = new ArrayList();
+
+ public static class ResponseHeader {
+ private String name;
+ private String property;
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getProperty() {
+ return property;
+ }
+ public void setProperty(String property) {
+ this.property = property;
+ }
+ }
+
+ protected abstract HttpMethodBase createNewMethod();
+ protected void configureMethod(HttpMethodBase method) {
+ }
+ protected void cleanupResources(HttpMethodBase method) {
+ }
+
+ public void addConfiguredResponseHeader(ResponseHeader responseHeader) {
+ this.responseHeaders.add(responseHeader);
+ }
+
+ protected HttpMethodBase createMethodIfNecessary() {
+ if (method == null) {
+ method = createNewMethod();
+ }
+ return method;
+ }
+
+ public void setResponseDataFile(File responseDataFile) {
+ this.responseDataFile = responseDataFile;
+ }
+
+ public void setResponseDataProperty(String responseDataProperty) {
+ this.responseDataProperty = responseDataProperty;
+ }
+
+ public void setStatusCodeProperty(String statusCodeProperty) {
+ this.statusCodeProperty = statusCodeProperty;
+ }
+
+ public void setClientRefId(String clientRefId) {
+ this.clientRefId = clientRefId;
+ }
+
+ public void setDoAuthentication(boolean doAuthentication) {
+ createMethodIfNecessary().setDoAuthentication(doAuthentication);
+ }
+
+ public void setFollowRedirects(boolean doFollowRedirects) {
+ createMethodIfNecessary().setFollowRedirects(doFollowRedirects);
+ }
+
+ public void addConfiguredParams(MethodParams params) {
+ createMethodIfNecessary().setParams(params);
+ }
+
+ public void setPath(String path) {
+ createMethodIfNecessary().setPath(path);
+ }
+
+ public void setURL(String url) {
+ try {
+ createMethodIfNecessary().setURI(new URI(url, false));
+ }
+ catch (URIException e) {
+ throw new BuildException(e);
+ }
+ }
+
+ public void setQueryString(String queryString) {
+ createMethodIfNecessary().setQueryString(queryString);
+ }
+
+ public void addConfiguredHeader(Header header) {
+ createMethodIfNecessary().setRequestHeader(header);
+ }
+
+ public void execute() throws BuildException {
+ HttpClient client = null;
+ if (clientRefId != null) {
+ Object clientRef = getProject().getReference(clientRefId);
+ if (clientRef == null) {
+ throw new BuildException("Reference '" + clientRefId + "' does not exist.");
+ }
+ if (! (clientRef instanceof HttpClientType)) {
+ throw new BuildException("Reference '" + clientRefId + "' is of the wrong type.");
+ }
+ HttpClientType clientType = (HttpClientType) clientRef;
+ client = clientType.getClient();
+ }
+ else {
+ client = new HttpClient();
+ }
+
+ HttpMethodBase method = createMethodIfNecessary();
+ configureMethod(method);
+ try {
+ int statusCode = client.executeMethod(method);
+ if (statusCodeProperty != null) {
+ Property p = (Property)getProject().createTask("property");
+ p.setName(statusCodeProperty);
+ p.setValue(String.valueOf(statusCode));
+ p.perform();
+ }
+
+ Iterator it = responseHeaders.iterator();
+ while (it.hasNext()) {
+ ResponseHeader header = (ResponseHeader)it.next();
+ Property p = (Property)getProject().createTask("property");
+ p.setName(header.getProperty());
+ Header h = method.getResponseHeader(header.getName());
+ if (h != null && h.getValue() != null) {
+ p.setValue(h.getValue());
+ p.perform();
+ }
+
+ }
+ if (responseDataProperty != null) {
+ Property p = (Property)getProject().createTask("property");
+ p.setName(responseDataProperty);
+ p.setValue(method.getResponseBodyAsString());
+ p.perform();
+ }
+ else if (responseDataFile != null) {
+ FileOutputStream fos = null;
+ InputStream is = null;
+ try {
+ is = method.getResponseBodyAsStream();
+ fos = new FileOutputStream(responseDataFile);
+ byte buf[] = new byte[10*1024];
+ int read = 0;
+ while ((read = is.read(buf, 0, 10*1024)) != -1) {
+ fos.write(buf, 0, read);
+ }
+ }
+ finally {
+ FileUtils.close(fos);
+ FileUtils.close(is);
+ }
+ }
+ }
+ catch (IOException e) {
+ throw new BuildException(e);
+ }
+ finally {
+ cleanupResources(method);
+ }
+ }
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/AddCookieTask.java b/src/java/net/sf/antcontrib/net/httpclient/AddCookieTask.java new file mode 100644 index 0000000..b394c9a --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/AddCookieTask.java @@ -0,0 +1,32 @@ +package net.sf.antcontrib.net.httpclient;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.httpclient.Cookie;
+import org.apache.tools.ant.BuildException;
+
+public class AddCookieTask
+ extends AbstractHttpStateTypeTask {
+
+ private List cookies = new ArrayList();
+
+ public void addConfiguredCookie(Cookie cookie) {
+ this.cookies.add(cookie);
+ }
+
+ protected void execute(HttpStateType stateType) throws BuildException {
+ if (this.cookies.isEmpty()) {
+ throw new BuildException("At least one cookie must be specified.");
+ }
+
+ Iterator it = cookies.iterator();
+ while (it.hasNext()) {
+ Cookie c = (Cookie)it.next();
+ stateType.addConfiguredCookie(c);
+ }
+ }
+
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/AddCredentialsTask.java b/src/java/net/sf/antcontrib/net/httpclient/AddCredentialsTask.java new file mode 100644 index 0000000..513014f --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/AddCredentialsTask.java @@ -0,0 +1,41 @@ +package net.sf.antcontrib.net.httpclient;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.tools.ant.BuildException;
+
+public class AddCredentialsTask
+ extends AbstractHttpStateTypeTask {
+
+ private List credentials = new ArrayList();
+ private List proxyCredentials = new ArrayList();
+
+ public void addConfiguredCredentials(Credentials credentials) {
+ this.credentials.add(credentials);
+ }
+
+ public void addConfiguredProxyCredentials(Credentials credentials) {
+ this.proxyCredentials.add(credentials);
+ }
+
+ protected void execute(HttpStateType stateType) throws BuildException {
+ if (credentials.isEmpty() && proxyCredentials.isEmpty()) {
+ throw new BuildException("Either regular or proxy credentials" +
+ " must be supplied.");
+ }
+
+ Iterator it = credentials.iterator();
+ while (it.hasNext()) {
+ Credentials c = (Credentials)it.next();
+ stateType.addConfiguredCredentials(c);
+ }
+
+ it = proxyCredentials.iterator();
+ while (it.hasNext()) {
+ Credentials c = (Credentials)it.next();
+ stateType.addConfiguredProxyCredentials(c);
+ }
+ }
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/ClearCookiesTask.java b/src/java/net/sf/antcontrib/net/httpclient/ClearCookiesTask.java new file mode 100644 index 0000000..8dd4971 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/ClearCookiesTask.java @@ -0,0 +1,13 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.tools.ant.BuildException;
+
+public class ClearCookiesTask
+ extends AbstractHttpStateTypeTask {
+
+ protected void execute(HttpStateType stateType) throws BuildException {
+ stateType.getState().clearCookies();
+ }
+
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/ClearCredentialsTask.java b/src/java/net/sf/antcontrib/net/httpclient/ClearCredentialsTask.java new file mode 100644 index 0000000..63865c7 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/ClearCredentialsTask.java @@ -0,0 +1,22 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.tools.ant.BuildException;
+
+public class ClearCredentialsTask
+ extends AbstractHttpStateTypeTask {
+
+ private boolean proxy = false;
+
+ public void setProxy(boolean proxy) {
+ this.proxy = proxy;
+ }
+
+ protected void execute(HttpStateType stateType) throws BuildException {
+ if (proxy) {
+ stateType.getState().clearProxyCredentials();
+ }
+ else {
+ stateType.getState().clearCredentials();
+ }
+ }
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/ClientParams.java b/src/java/net/sf/antcontrib/net/httpclient/ClientParams.java new file mode 100644 index 0000000..785dba5 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/ClientParams.java @@ -0,0 +1,46 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.ProtocolException;
+import org.apache.commons.httpclient.params.HttpClientParams;
+import org.apache.tools.ant.BuildException;
+
+public class ClientParams
+ extends HttpClientParams {
+ private static final long serialVersionUID = -1;
+
+ public void setVersion(String version) {
+ try {
+ setVersion(HttpVersion.parse(version));
+ }
+ catch (ProtocolException e) {
+ throw new BuildException(e);
+ }
+ }
+
+ public void addConfiguredDouble(Params.DoubleParam param) {
+ setDoubleParameter(param.getName(), param.getValue());
+ }
+
+ public void addConfiguredInt(Params.IntParam param) {
+ setIntParameter(param.getName(), param.getValue());
+ }
+
+ public void addConfiguredLong(Params.LongParam param) {
+ setLongParameter(param.getName(), param.getValue());
+ }
+
+ public void addConfiguredString(Params.StringParam param) {
+ setParameter(param.getName(), param.getValue());
+ }
+
+ public void setStrict(boolean strict) {
+ if (strict) {
+ makeStrict();
+ }
+ else {
+ makeLenient();
+ }
+ }
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/Credentials.java b/src/java/net/sf/antcontrib/net/httpclient/Credentials.java new file mode 100644 index 0000000..a9f091f --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/Credentials.java @@ -0,0 +1,51 @@ +/**
+ *
+ */
+package net.sf.antcontrib.net.httpclient;
+
+public class Credentials {
+ private String host;
+ private int port = -1;
+ private String realm;
+ private String scheme;
+ private String username;
+ private String password;
+
+ public String getPassword() {
+ return password;
+ }
+ public void setPassword(String password) {
+ this.password = password;
+ }
+ public String getUsername() {
+ return username;
+ }
+ public void setUsername(String username) {
+ this.username = username;
+ }
+ public String getHost() {
+ return host;
+ }
+ public void setHost(String host) {
+ this.host = host;
+ }
+ public int getPort() {
+ return port;
+ }
+ public void setPort(int port) {
+ this.port = port;
+ }
+ public String getRealm() {
+ return realm;
+ }
+ public void setRealm(String realm) {
+ this.realm = realm;
+ }
+ public String getScheme() {
+ return scheme;
+ }
+ public void setScheme(String scheme) {
+ this.scheme = scheme;
+ }
+
+}
\ No newline at end of file diff --git a/src/java/net/sf/antcontrib/net/httpclient/GetMethodTask.java b/src/java/net/sf/antcontrib/net/httpclient/GetMethodTask.java new file mode 100644 index 0000000..be19a1d --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/GetMethodTask.java @@ -0,0 +1,14 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.methods.PostMethod;
+
+public class GetMethodTask
+ extends AbstractMethodTask {
+
+ protected HttpMethodBase createNewMethod() {
+ return new PostMethod();
+ }
+
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/HeadMethodTask.java b/src/java/net/sf/antcontrib/net/httpclient/HeadMethodTask.java new file mode 100644 index 0000000..27cce1c --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/HeadMethodTask.java @@ -0,0 +1,14 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.methods.PostMethod;
+
+public class HeadMethodTask
+ extends AbstractMethodTask {
+
+ protected HttpMethodBase createNewMethod() {
+ return new PostMethod();
+ }
+
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/HttpClientType.java b/src/java/net/sf/antcontrib/net/httpclient/HttpClientType.java new file mode 100644 index 0000000..ae171c4 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/HttpClientType.java @@ -0,0 +1,51 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.DataType;
+
+public class HttpClientType
+ extends DataType {
+
+ private HttpClient client;
+
+ public HttpClientType(Project p) {
+ super();
+ setProject(p);
+
+ client = new HttpClient();
+ }
+
+ public HttpClient getClient() {
+ if (isReference()) {
+ return getRef().getClient();
+ }
+ else {
+ return client;
+ }
+ }
+
+ public void setStateRefId(String stateRefId) {
+ if (isReference()) {
+ tooManyAttributes();
+ }
+ HttpStateType stateType = AbstractHttpStateTypeTask.getStateType(
+ getProject(),
+ stateRefId);
+ getClient().setState(stateType.getState());
+ }
+
+ protected HttpClientType getRef() {
+ return (HttpClientType) super.getCheckedRef(HttpClientType.class,
+ "http-client");
+ }
+
+ public ClientParams createClientParams() {
+ if (isReference()) {
+ tooManyAttributes();
+ }
+ ClientParams clientParams = new ClientParams();
+ client.setParams(clientParams);
+ return clientParams;
+ }
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/HttpStateType.java b/src/java/net/sf/antcontrib/net/httpclient/HttpStateType.java new file mode 100644 index 0000000..46d7a09 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/HttpStateType.java @@ -0,0 +1,77 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.HttpState;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.DataType;
+
+public class HttpStateType
+ extends DataType {
+
+ private HttpState state;
+
+ public HttpStateType(Project p) {
+ super();
+ setProject(p);
+
+ state = new HttpState();
+ }
+
+ public HttpState getState() {
+ if (isReference()) {
+ return getRef().getState();
+ }
+ else {
+ return state;
+ }
+ }
+
+ protected HttpStateType getRef() {
+ return (HttpStateType) super.getCheckedRef(HttpStateType.class,
+ "http-state");
+ }
+
+ public void addConfiguredCredentials(Credentials credentials) {
+ if (isReference()) {
+ tooManyAttributes();
+ }
+
+ AuthScope scope = new AuthScope(credentials.getHost(),
+ credentials.getPort(),
+ credentials.getRealm(),
+ credentials.getScheme());
+
+ UsernamePasswordCredentials c = new UsernamePasswordCredentials(
+ credentials.getUsername(),
+ credentials.getPassword());
+
+ state.setCredentials(scope, c);
+ }
+
+ public void addConfiguredProxyCredentials(Credentials credentials) {
+ if (isReference()) {
+ tooManyAttributes();
+ }
+
+ AuthScope scope = new AuthScope(credentials.getHost(),
+ credentials.getPort(),
+ credentials.getRealm(),
+ credentials.getScheme());
+
+ UsernamePasswordCredentials c = new UsernamePasswordCredentials(
+ credentials.getUsername(),
+ credentials.getPassword());
+
+ state.setProxyCredentials(scope, c);
+ }
+
+ public void addConfiguredCookie(Cookie cookie) {
+ if (isReference()) {
+ tooManyAttributes();
+ }
+
+ state.addCookie(cookie);
+ }
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/MethodParams.java b/src/java/net/sf/antcontrib/net/httpclient/MethodParams.java new file mode 100644 index 0000000..85185e7 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/MethodParams.java @@ -0,0 +1,46 @@ +package net.sf.antcontrib.net.httpclient;
+
+import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.ProtocolException;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.tools.ant.BuildException;
+
+public class MethodParams
+ extends HttpMethodParams {
+ private static final long serialVersionUID = -1;
+
+ public void setStrict(boolean strict) {
+ if (strict) {
+ makeStrict();
+ }
+ else {
+ makeLenient();
+ }
+ }
+
+ public void setVersion(String version) {
+ try {
+ setVersion(HttpVersion.parse(version));
+ }
+ catch (ProtocolException e) {
+ throw new BuildException(e);
+ }
+ }
+
+ public void addConfiguredDouble(Params.DoubleParam param) {
+ setDoubleParameter(param.getName(), param.getValue());
+ }
+
+ public void addConfiguredInt(Params.IntParam param) {
+ setIntParameter(param.getName(), param.getValue());
+ }
+
+ public void addConfiguredLong(Params.LongParam param) {
+ setLongParameter(param.getName(), param.getValue());
+ }
+
+ public void addConfiguredString(Params.StringParam param) {
+ setParameter(param.getName(), param.getValue());
+ }
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/Params.java b/src/java/net/sf/antcontrib/net/httpclient/Params.java new file mode 100644 index 0000000..ac1be93 --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/Params.java @@ -0,0 +1,68 @@ +package net.sf.antcontrib.net.httpclient;
+
+
+public class Params {
+ public static class Param {
+ private String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+
+ public static class DoubleParam extends Param{
+ private double value;
+
+ public double getValue() {
+ return value;
+ }
+
+ public void setValue(double value) {
+ this.value = value;
+ }
+
+ }
+
+ public static class IntParam extends Param{
+ private int value;
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ }
+
+ public static class LongParam extends Param{
+ private long value;
+
+ public long getValue() {
+ return value;
+ }
+
+ public void setValue(long value) {
+ this.value = value;
+ }
+
+ }
+
+ public static class StringParam extends Param{
+ private String value;
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ }
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/PostMethodTask.java b/src/java/net/sf/antcontrib/net/httpclient/PostMethodTask.java new file mode 100644 index 0000000..1106bcc --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/PostMethodTask.java @@ -0,0 +1,211 @@ +package net.sf.antcontrib.net.httpclient;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.FilePart;
+import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.Part;
+import org.apache.commons.httpclient.methods.multipart.StringPart;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.util.FileUtils;
+
+public class PostMethodTask
+ extends AbstractMethodTask {
+
+ private List parts = new ArrayList();
+ private boolean multipart;
+ private transient FileInputStream stream;
+
+
+ public static class FilePartType {
+ private File path;
+ private String contentType = FilePart.DEFAULT_CONTENT_TYPE;
+ private String charSet = FilePart.DEFAULT_CHARSET;
+
+ public File getPath() {
+ return path;
+ }
+
+ public void setPath(File path) {
+ this.path = path;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public void setContentType(String contentType) {
+ this.contentType = contentType;
+ }
+
+ public String getCharSet() {
+ return charSet;
+ }
+
+ public void setCharSet(String charSet) {
+ this.charSet = charSet;
+ }
+ }
+
+ public static class TextPartType {
+ private String name = "";
+ private String value = "";
+ private String charSet = StringPart.DEFAULT_CHARSET;
+ private String contentType = StringPart.DEFAULT_CONTENT_TYPE;
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getCharSet() {
+ return charSet;
+ }
+
+ public void setCharSet(String charSet) {
+ this.charSet = charSet;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public void setContentType(String contentType) {
+ this.contentType = contentType;
+ }
+ }
+
+ public void addConfiguredFile(FilePartType file) {
+ this.parts.add(file);
+ }
+
+ public void setMultipart(boolean multipart) {
+ this.multipart = multipart;
+ }
+
+ public void addConfiguredText(TextPartType text) {
+ this.parts.add(text);
+ }
+
+ public void setParameters(File parameters) {
+ PostMethod post = getPostMethod();
+ Properties p = new Properties();
+ Iterator it = p.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry entry = (Map.Entry) it.next();
+ post.addParameter(entry.getKey().toString(),
+ entry.getValue().toString());
+ }
+ }
+
+ protected HttpMethodBase createNewMethod() {
+ return new PostMethod();
+ }
+
+ private PostMethod getPostMethod() {
+ return ((PostMethod)createMethodIfNecessary());
+ }
+
+ public void addConfiguredParameter(NameValuePair pair) {
+ getPostMethod().setParameter(pair.getName(), pair.getValue());
+ }
+
+ public void setContentChunked(boolean contentChunked) {
+ getPostMethod().setContentChunked(contentChunked);
+ }
+
+ protected void configureMethod(HttpMethodBase method) {
+ PostMethod post = (PostMethod) method;
+
+ if (parts.size() == 1 && ! multipart) {
+ Object part = parts.get(0);
+ if (part instanceof FilePartType) {
+ FilePartType filePart = (FilePartType)part;
+ try {
+ stream = new FileInputStream(
+ filePart.getPath().getAbsolutePath());
+ post.setRequestEntity(
+ new InputStreamRequestEntity(stream,
+ filePart.getPath().length(),
+ filePart.getContentType()));
+ }
+ catch (IOException e) {
+ throw new BuildException(e);
+ }
+ }
+ else if (part instanceof TextPartType) {
+ TextPartType textPart = (TextPartType)part;
+ try {
+ post.setRequestEntity(
+ new StringRequestEntity(textPart.getValue(),
+ textPart.getContentType(),
+ textPart.getCharSet()));
+ }
+ catch (UnsupportedEncodingException e) {
+ throw new BuildException(e);
+ }
+ }
+ }
+ else if (! parts.isEmpty()){
+ Part partArray[] = new Part[parts.size()];
+ for (int i=0;i<parts.size();i++) {
+ Object part = parts.get(i);
+ if (part instanceof FilePartType) {
+ FilePartType filePart = (FilePartType)part;
+ try {
+ partArray[i] = new FilePart(filePart.getPath().getName(),
+ filePart.getPath().getName(),
+ filePart.getPath(),
+ filePart.getContentType(),
+ filePart.getCharSet());
+ }
+ catch (FileNotFoundException e) {
+ throw new BuildException(e);
+ }
+ }
+ else if (part instanceof TextPartType) {
+ TextPartType textPart = (TextPartType)part;
+ partArray[i] = new StringPart(textPart.getName(),
+ textPart.getValue(),
+ textPart.getCharSet());
+ ((StringPart)partArray[i]).setContentType(textPart.getContentType());
+ }
+ }
+ MultipartRequestEntity entity = new MultipartRequestEntity(
+ partArray,
+ post.getParams());
+ post.setRequestEntity(entity);
+ }
+ }
+
+ protected void cleanupResources(HttpMethodBase method) {
+ FileUtils.close(stream);
+ }
+
+
+}
diff --git a/src/java/net/sf/antcontrib/net/httpclient/PurgeExpiredCookiesTask.java b/src/java/net/sf/antcontrib/net/httpclient/PurgeExpiredCookiesTask.java new file mode 100644 index 0000000..7e9a47d --- /dev/null +++ b/src/java/net/sf/antcontrib/net/httpclient/PurgeExpiredCookiesTask.java @@ -0,0 +1,26 @@ +package net.sf.antcontrib.net.httpclient;
+
+import java.util.Date;
+
+import org.apache.tools.ant.BuildException;
+
+public class PurgeExpiredCookiesTask
+ extends AbstractHttpStateTypeTask {
+
+ private Date expiredDate;
+
+ public void setExpiredDate(Date expiredDate) {
+ this.expiredDate = expiredDate;
+ }
+
+ protected void execute(HttpStateType stateType) throws BuildException {
+ if (expiredDate != null) {
+ stateType.getState().purgeExpiredCookies(expiredDate);
+ }
+ else {
+ stateType.getState().purgeExpiredCookies();
+ }
+ }
+
+
+}
|