cleanup work for httpclient

This commit is contained in:
eugenp 2014-05-22 20:57:29 +03:00
parent f7bd1ca219
commit f4104354b4
4 changed files with 246 additions and 238 deletions

View File

@ -32,11 +32,5 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/httpclient-4.3.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/fluent-hc-4.3.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/httpmime-4.3.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/httpcore-4.3.2.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/commons-logging-1.1.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/commons-codec-1.6.jar"/>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -1,170 +1,189 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.baeldung</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>httpclient</artifactId> <groupId>org.baeldung</groupId>
<version>0.1-SNAPSHOT</version> <artifactId>httpclient</artifactId>
<version>0.1-SNAPSHOT</version>
<name>httpclient</name> <name>httpclient</name>
<dependencies> <dependencies>
<!-- utils --> <!-- utils -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency> </dependency>
<!-- http client --> <!-- http client -->
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>${httpclient.version}</version> <version>${httpclient.version}</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId> <groupId>commons-logging</groupId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId> <artifactId>httpcore</artifactId>
<version>${httpcore.version}</version> <version>${httpcore.version}</version>
</dependency> </dependency>
<!-- logging --> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.3.3</version>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>httpmime</artifactId>
<version>${org.slf4j.version}</version> <version>4.3.3</version>
</dependency> </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<!-- test scoped --> <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency> <!-- logging -->
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.slf4j</groupId>
<artifactId>hamcrest-core</artifactId> <artifactId>slf4j-api</artifactId>
<version>${org.hamcrest.version}</version> <version>${org.slf4j.version}</version>
<scope>test</scope> </dependency>
</dependency> <dependency>
<dependency> <groupId>ch.qos.logback</groupId>
<groupId>org.hamcrest</groupId> <artifactId>logback-classic</artifactId>
<artifactId>hamcrest-library</artifactId> <version>${logback.version}</version>
<version>${org.hamcrest.version}</version> <!-- <scope>runtime</scope> -->
<scope>test</scope> </dependency>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency> <!-- test scoped -->
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<build> <dependency>
<finalName>httpclient</finalName> <groupId>org.hamcrest</groupId>
<resources> <artifactId>hamcrest-core</artifactId>
<resource> <version>${org.hamcrest.version}</version>
<directory>src/main/resources</directory> <scope>test</scope>
<filtering>true</filtering> </dependency>
</resource> <dependency>
</resources> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<plugins> <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<plugin> </dependencies>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin> <build>
<groupId>org.apache.maven.plugins</groupId> <finalName>httpclient</finalName>
<artifactId>maven-surefire-plugin</artifactId> <resources>
<version>${maven-surefire-plugin.version}</version> <resource>
</plugin> <directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</plugins> <plugins>
</build> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<properties> <plugin>
<!-- Spring --> <groupId>org.apache.maven.plugins</groupId>
<org.springframework.version>4.0.4.RELEASE</org.springframework.version> <artifactId>maven-surefire-plugin</artifactId>
<org.springframework.security.version>3.2.3.RELEASE</org.springframework.security.version> <version>${maven-surefire-plugin.version}</version>
</plugin>
<!-- persistence --> </plugins>
<hibernate.version>4.3.5.Final</hibernate.version>
<mysql-connector-java.version>5.1.30</mysql-connector-java.version>
<!-- logging --> </build>
<org.slf4j.version>1.7.6</org.slf4j.version>
<logback.version>1.1.1</logback.version>
<!-- various --> <properties>
<hibernate-validator.version>5.1.1.Final</hibernate-validator.version> <!-- Spring -->
<org.springframework.version>4.0.4.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.3.RELEASE</org.springframework.security.version>
<!-- util --> <!-- persistence -->
<guava.version>17.0</guava.version> <hibernate.version>4.3.5.Final</hibernate.version>
<commons-lang3.version>3.3.2</commons-lang3.version> <mysql-connector-java.version>5.1.30</mysql-connector-java.version>
<!-- testing --> <!-- logging -->
<org.hamcrest.version>1.3</org.hamcrest.version> <org.slf4j.version>1.7.6</org.slf4j.version>
<junit.version>4.11</junit.version> <logback.version>1.1.1</logback.version>
<mockito.version>1.9.5</mockito.version>
<httpcore.version>4.3.2</httpcore.version> <!-- various -->
<httpclient.version>4.3.3</httpclient.version> <hibernate-validator.version>5.1.1.Final</hibernate-validator.version>
<rest-assured.version>2.3.1</rest-assured.version> <!-- util -->
<guava.version>17.0</guava.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<!-- maven plugins --> <!-- testing -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version> <org.hamcrest.version>1.3</org.hamcrest.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version> <junit.version>4.11</junit.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version> <mockito.version>1.9.5</mockito.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
</properties> <httpcore.version>4.3.2</httpcore.version>
<httpclient.version>4.3.3</httpclient.version>
<rest-assured.version>2.3.1</rest-assured.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
</properties>
</project> </project>

View File

@ -0,0 +1 @@
some content

View File

@ -2,11 +2,15 @@ package org.baeldung.httpclient;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response; import org.apache.http.client.fluent.Response;
@ -14,9 +18,11 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class HttpClientMultipartTest { public class HttpClientMultipartTest {
@ -40,107 +46,98 @@ public class HttpClientMultipartTest {
textFileName = ".\temp.txt"; textFileName = ".\temp.txt";
imageFileName = "image.jpg"; imageFileName = "image.jpg";
zipFileName = "zipFile.zip"; zipFileName = "zipFile.zip";
} }
@Before @Test
public void setUp() throws Exception { @Ignore
public final void whenUploadWithAddPart_thenNoExceptions() throws IOException {
final File file = new File(textFileName);
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA);
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("submitted", fileBody);
builder.addPart("note", stringBody1);
builder.addPart("note2", stringBody2);
final HttpEntity entity = builder.build();
post.setEntity(entity);
final HttpResponse response = client.execute(post);
System.out.println(getContent(response));
final Header[] headers = response.getAllHeaders();
for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
}
} }
/* @Test @Test
public final void whenUploadWithAddPart_thenNoExceptions() throws IOException { @Ignore
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final File file = new File(textFileName);
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
post.setEntity(entity);
final HttpResponse response = client.execute(post);
System.out.println(getContent(response));
final Header[] headers = response.getAllHeaders();
for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
}
}
final File file = new File(textFileName); @Test
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); @Ignore
final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA); public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA); final File file = new File(imageFileName);
final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final String message = "This is a multipart post";
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("submitted", fileBody); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("note", stringBody1); builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
builder.addPart("note2", stringBody2); // builder.addBinaryBody("upfile", fileBin);
final HttpEntity entity = builder.build(); builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
post.setEntity(entity); final HttpEntity entity = builder.build();
final HttpResponse response = client.execute(post); post.setEntity(entity);
System.out.println(getContent(response)); final HttpResponse response = client.execute(post);
Header[] headers = response.getAllHeaders(); System.out.println(getContent(response));
final Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
} */ }
/*@Test
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final File file = new File(textFileName); @Test
String message = "This is a multipart post"; @Ignore
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); final InputStream inputStream = new FileInputStream(zipFileName);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); final String message = "This is a multipart post";
builder.addTextBody("note", message, ContentType.TEXT_PLAIN); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
final HttpEntity entity = builder.build(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
post.setEntity(entity); // builder.addBinaryBody("submitted", inputStream, ContentType.create("application/zip"), "zipFileName");
HttpResponse response = client.execute(post); builder.addBinaryBody("upfile", inputStream, ContentType.create("application/zip"), "zipFileName");
System.out.println(getContent(response)); builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
Header[] headers = response.getAllHeaders(); final HttpEntity entity = builder.build();
post.setEntity(entity);
final HttpResponse response = client.execute(post);
for (Header thisHeader : headers) { System.out.println(getContent(response));
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); final Header[] headers = response.getAllHeaders();
}
}*/ for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
/* @Test }
public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { }
final File file = new File(imageFileName);
final String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
//builder.addBinaryBody("upfile", fileBin);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
post.setEntity(entity);
final HttpResponse response = client.execute(post);
System.out.println(getContent(response));
Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
}
}*/
/* @Test
public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException{
final InputStream inputStream = new FileInputStream(zipFileName);
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// builder.addBinaryBody("submitted", inputStream, ContentType.create("application/zip"), "zipFileName");
builder.addBinaryBody("upfile", inputStream, ContentType.create("application/zip"), "zipFileName");
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
post.setEntity(entity);
final HttpResponse response = client.execute(post);
System.out.println(getContent(response));
Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
}
}*/
// BUG // BUG
@Test
public final void whenFluentRequestWithBody_ThenNoException() throws IOException{
@Test
public final void whenFluentRequestWithBody_ThenNoException() throws IOException {
final String fileName = ".\temp.txt"; final String fileName = ".\temp.txt";
final File fileBin = new File(fileName); final File fileBin = new File(fileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
@ -149,12 +146,10 @@ public class HttpClientMultipartTest {
builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName); builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN); builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
final Response response = Request.Post(SERVER) final Response response = Request.Post(SERVER).body(entity).execute();
.body(entity).execute();
} }
public static String getContent(final HttpResponse response) throws IOException { public static String getContent(final HttpResponse response) throws IOException {
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = ""; String body = "";
String content = ""; String content = "";
@ -165,5 +160,4 @@ public class HttpClientMultipartTest {
return content.trim(); return content.trim();
} }
} }