Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>http-builder</artifactId>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<version>0.7.3-SNAPSHOT</version>
<version>0.7.3.1-SNAPSHOT</version>
<name>HTTP client framework for Groovy</name>
<url>http://groovy.codehaus.org/modules/http-builder/</url>
<inceptionYear>2008</inceptionYear>
Expand All @@ -27,9 +27,24 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
<version>4.3.6</version>
</dependency>
<dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<!-- Only needed for JSON parsing -->
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
Expand Down Expand Up @@ -230,7 +245,7 @@
</systemProperties>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
Expand All @@ -247,13 +262,13 @@
</configuration>
<executions>
<!--<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
--><execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
--><execution>
<id>defaults</id>
<goals>
<goal>clean</goal>
Expand Down Expand Up @@ -417,27 +432,23 @@
</pluginRepositories>

<scm>
<developerConnection>scm:git:git@github.com:jgritman/httpbuilder.git</developerConnection>
<url>http://github.com/jgritman/httpbuilder</url>
<developerConnection>scm:git:git@github.com:adcarabajal/httpbuilder.git</developerConnection>
<url>https://github.com/adcarabajal/httpbuilder</url>
<tag>HEAD</tag>
</scm>

<distributionManagement>
<repository>
<id>Groovy-Contrib</id>
<name>Codehaus Groovy Repo</name>
<url>dav:https://dav.codehaus.org/repository/gmod</url>
<id>mule-ee-releases</id>
<name>MuleSoft Release Repository</name>
<url>https://repository-master.mulesoft.org/nexus/content/repositories/ci-releases/</url>
</repository>
<snapshotRepository>
<id>Groovy-Contrib</id>
<name>Codehaus Groovy Snapshots Repo</name>
<url>dav:https://dav.codehaus.org/snapshots.repository/gmod</url>
<id>mule-ee-snapshots</id>
<name>MuleSoft Snapshot Repository</name>
<url>https://repository-master.mulesoft.org/nexus/content/repositories/ci-snapshots/</url>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
<site>
<id>Groovy-Contrib</id>
<name>Codehaus Groovy WebDAV</name>
<url>dav:https://dav.codehaus.org/groovy/modules/http-builder</url>
</site>
</distributionManagement>

<mailingLists>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/groovyx/net/http/ContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public enum ContentType {
/** <code>application/x-www-form-urlencoded</code> */
URLENC("application/x-www-form-urlencoded"),
/** <code>application/octet-stream</code> */
BINARY("application/octet-stream");
BINARY("application/octet-stream"),
/** <code>multipart/form-data</code> */
MULTIPART("multipart/form-data");

private final String[] ctStrings;
public String[] getContentTypeStrings() { return ctStrings; }
Expand Down
40 changes: 31 additions & 9 deletions src/main/java/groovyx/net/http/EncoderRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@
import groovy.xml.StreamingMarkupBuilder;
import groovyx.net.http.HTTPBuilder.RequestConfigDelegate;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -55,6 +47,7 @@
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.MethodClosure;
Expand Down Expand Up @@ -318,6 +311,34 @@ else if ( model instanceof String || model instanceof GString )
return this.createEntity( contentType, json.toString() );
}

/**
* Set the request body as a multipart list of parameters.
* Binary parts should be included as java.io.File or an InputStream
* @param params
* @return an {@link HttpEntity} encapsulating this request data
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unchecked")
public HttpEntity encodeFormMultipart(Map<String,?> params) throws UnsupportedEncodingException {
MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create();

for ( String key : params.keySet() ) {
Object val = params.get( key );
if ( val instanceof String){

multipartBuilder.addTextBody(key, (String) val);
}
if ( val instanceof File){
multipartBuilder.addBinaryBody(key, (File) val);
}
if ( val instanceof InputStream){
multipartBuilder.addBinaryBody(key, (InputStream) val, org.apache.http.entity.ContentType.DEFAULT_BINARY, key);
}
}

return multipartBuilder.build();
}

/**
* Helper method used by encoder methods to create an {@link HttpEntity}
* instance that encapsulates the request data. This may be used by any
Expand Down Expand Up @@ -349,6 +370,7 @@ protected Map<String,Closure> buildDefaultEncoderMap() {
encoders.put( ContentType.BINARY.toString(), new MethodClosure(this,"encodeStream") );
encoders.put( ContentType.TEXT.toString(), new MethodClosure( this, "encodeText" ) );
encoders.put( ContentType.URLENC.toString(), new MethodClosure( this, "encodeForm" ) );
encoders.put( ContentType.MULTIPART.toString(), new MethodClosure( this, "encodeFormMultipart" ) );

Closure encClosure = new MethodClosure(this,"encodeXML");
for ( String ct : ContentType.XML.getContentTypeStrings() )
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/groovyx/net/http/HttpDeleteWithBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package groovyx.net.http;

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;

@NotThreadSafe
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() { return METHOD_NAME; }

public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() { super(); }
}
2 changes: 1 addition & 1 deletion src/main/java/groovyx/net/http/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum Method {
GET( HttpGet.class ),
PUT( HttpPut.class ),
POST( HttpPost.class ),
DELETE( HttpDelete.class ),
DELETE( HttpDeleteWithBody.class ),
HEAD( HttpHead.class ),
PATCH( HttpPatch.class );

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/groovyx/net/http/RESTClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public Object head( Map<String,?> args ) throws URISyntaxException,
*/
public Object delete( Map<String,?> args ) throws URISyntaxException,
ClientProtocolException, IOException {
return this.doRequest( new RequestConfigDelegate( args, new HttpDelete(), null ) );
return this.doRequest( new RequestConfigDelegate( args, new HttpDeleteWithBody(), null ) );
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/groovyx/net/http/thirdparty/GAEClientConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public GAEClientConnection(ClientConnectionManager cm, HttpRoute route, Object s
}

// From interface ManagedClientConnection
public String getId() {
return null;
}

public void bind(Socket socket) throws IOException {
throw new IOException("not supported");
}

public Socket getSocket() {
return null;
}

public boolean isSecure() {
return route.isSecure();
Expand Down
18 changes: 16 additions & 2 deletions src/test/groovy/groovyx/net/http/RegistryTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package groovyx.net.http
import org.apache.http.ProtocolVersion
import org.apache.http.entity.StringEntity
import org.apache.http.message.BasicHttpResponse
import org.junit.Testimport java.io.StringReaderimport java.io.ByteArrayInputStream
import static groovyx.net.http.ContentType.*
import org.junit.Test
import java.io.StringReader
import java.io.ByteArrayInputStream
import static groovyx.net.http.ContentType.*

/**
* @author tnichols
*/
Expand Down Expand Up @@ -189,4 +192,15 @@ public class RegistryTest {
assert map.p1 == 'goober'
assert map.p2 == 'something else'
}

@Test public void testMultipartEncoder() {
def enc = new EncoderRegistry()

def param1 = "p1"
def entity = enc.encodeFormMultipart( [param1:'one', p2: new File(getClass().getResource("/log4j.xml").getPath())] )

assert entity.contentType.elements[0].name == 'multipart/form-data'
assert entity.multipart.parts[0].header.toString() == "[Content-Disposition: form-data; name=\"param1\", Content-Type: text/plain; charset=ISO-8859-1, Content-Transfer-Encoding: 8bit]"
assert entity.multipart.parts[1].header.toString() == "[Content-Disposition: form-data; name=\"p2\"; filename=\"log4j.xml\", Content-Type: application/octet-stream, Content-Transfer-Encoding: binary]"
}
}