From f4104354b4c1493eb52e723452a75c66b9adf036 Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 22 May 2014 20:57:29 +0300 Subject: [PATCH] cleanup work for httpclient --- httpclient/.classpath | 6 - httpclient/pom.xml | 295 ++++++++++-------- httpclient/src/main/resources/temp.txt | 1 + .../httpclient/HttpClientMultipartTest.java | 182 ++++++----- 4 files changed, 246 insertions(+), 238 deletions(-) create mode 100644 httpclient/src/main/resources/temp.txt diff --git a/httpclient/.classpath b/httpclient/.classpath index 5e95764558..0720e4851b 100644 --- a/httpclient/.classpath +++ b/httpclient/.classpath @@ -32,11 +32,5 @@ - - - - - - diff --git a/httpclient/pom.xml b/httpclient/pom.xml index e4eb326560..58d052af33 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -1,170 +1,189 @@ - - 4.0.0 - org.baeldung - httpclient - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + httpclient + 0.1-SNAPSHOT - httpclient + httpclient - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - commons-logging - commons-logging - - - - - org.apache.httpcomponents - httpcore - ${httpcore.version} - + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + commons-logging + commons-logging + + + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + - + + org.apache.httpcomponents + fluent-hc + 4.3.3 + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - + + org.apache.httpcomponents + httpmime + 4.3.3 + - + + commons-codec + commons-codec + 1.9 + - - junit - junit-dep - ${junit.version} - test - + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - - org.mockito - mockito-core - ${mockito.version} - test - + - + + junit + junit-dep + ${junit.version} + test + - - httpclient - - - src/main/resources - true - - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - + + org.mockito + mockito-core + ${mockito.version} + test + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + httpclient + + + src/main/resources + true + + - + - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + - - - 4.0.4.RELEASE - 3.2.3.RELEASE + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - - 4.3.5.Final - 5.1.30 + - - 1.7.6 - 1.1.1 + - - 5.1.1.Final + + + 4.0.4.RELEASE + 3.2.3.RELEASE - - 17.0 - 3.3.2 + + 4.3.5.Final + 5.1.30 - - 1.3 - 4.11 - 1.9.5 + + 1.7.6 + 1.1.1 - 4.3.2 - 4.3.3 + + 5.1.1.Final - 2.3.1 + + 17.0 + 3.3.2 - - 3.1 - 2.4 - 2.17 - 2.6 - 1.4.8 + + 1.3 + 4.11 + 1.9.5 - + 4.3.2 + 4.3.3 + + 2.3.1 + + + 3.1 + 2.4 + 2.17 + 2.6 + 1.4.8 + + \ No newline at end of file diff --git a/httpclient/src/main/resources/temp.txt b/httpclient/src/main/resources/temp.txt new file mode 100644 index 0000000000..f0eec86f61 --- /dev/null +++ b/httpclient/src/main/resources/temp.txt @@ -0,0 +1 @@ +some content \ No newline at end of file diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java index 4248a6818b..ef11c76187 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java @@ -2,11 +2,15 @@ package org.baeldung.httpclient; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.fluent.Request; 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.mime.HttpMultipartMode; 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.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; public class HttpClientMultipartTest { @@ -40,107 +46,98 @@ public class HttpClientMultipartTest { textFileName = ".\temp.txt"; imageFileName = "image.jpg"; zipFileName = "zipFile.zip"; - } - @Before - public void setUp() throws Exception { + @Test + @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 - public final void whenUploadWithAddPart_thenNoExceptions() throws IOException { + @Test + @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(); - - 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)); - Header[] headers = response.getAllHeaders(); + for (final Header thisHeader : headers) { + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); + } + } - for (Header thisHeader : headers) { - System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); - } - } */ - /*@Test - public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { - - final File file = new File(textFileName); - 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.addTextBody("note", message, ContentType.TEXT_PLAIN); - final HttpEntity entity = builder.build(); - post.setEntity(entity); - HttpResponse response = client.execute(post); - System.out.println(getContent(response)); - Header[] headers = response.getAllHeaders(); + @Test + @Ignore + public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { + final File file = new File(imageFileName); + 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.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)); + final Header[] headers = response.getAllHeaders(); - for (Header thisHeader : headers) { - System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); - } + for (final Header thisHeader : headers) { + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); + } + } - }*/ + @Test + @Ignore + 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); - /* @Test - public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { + System.out.println(getContent(response)); + final Header[] headers = response.getAllHeaders(); - 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()); - } - - - }*/ + for (final Header thisHeader : headers) { + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); + } + } // BUG - @Test - public final void whenFluentRequestWithBody_ThenNoException() throws IOException{ - + + @Test + public final void whenFluentRequestWithBody_ThenNoException() throws IOException { final String fileName = ".\temp.txt"; final File fileBin = new File(fileName); final String message = "This is a multipart post"; @@ -149,12 +146,10 @@ public class HttpClientMultipartTest { builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName); builder.addTextBody("note", message, ContentType.TEXT_PLAIN); final HttpEntity entity = builder.build(); - final Response response = Request.Post(SERVER) - .body(entity).execute(); + final Response response = Request.Post(SERVER).body(entity).execute(); } public static String getContent(final HttpResponse response) throws IOException { - final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String body = ""; String content = ""; @@ -164,6 +159,5 @@ public class HttpClientMultipartTest { } return content.trim(); } - - + }