Merge pull request #79 from Doha2012/master

syntax upgrade httpClient
This commit is contained in:
Eugen 2014-11-25 15:22:24 +02:00
commit 3899aa3a92
2 changed files with 179 additions and 184 deletions

View File

@ -15,9 +15,7 @@ import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;
@ -27,135 +25,132 @@ import com.google.common.collect.Lists;
public class HttpClientUnshortenLiveTest {
private CloseableHttpClient client;
private CloseableHttpClient client;
// fixtures
// fixtures
@Before
public final void before() {
final HttpParams httpParameters = new BasicHttpParams();
httpParameters.setParameter("http.protocol.handle-redirects", false);
@Before
public final void before() {
client = HttpClientBuilder.create().disableRedirectHandling().build();
}
client = new DefaultHttpClient(httpParameters);
}
// tests
// tests
@Test
public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
final String expectedResult = "http://www.baeldung.com/rest-versioning";
final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1");
assertThat(actualResult, equalTo(expectedResult));
}
@Test
public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
final String expectedResult = "http://www.baeldung.com/rest-versioning";
final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1");
assertThat(actualResult, equalTo(expectedResult));
}
@Test
public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
final String expectedResult = "http://www.baeldung.com/rest-versioning";
final String actualResult = expand("http://t.co/e4rDDbnzmk");
assertThat(actualResult, equalTo(expectedResult));
}
@Test
public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
final String expectedResult = "http://www.baeldung.com/rest-versioning";
final String actualResult = expand("http://t.co/e4rDDbnzmk");
assertThat(actualResult, equalTo(expectedResult));
}
// API
// API
final String expand(final String urlArg) throws IOException {
String originalUrl = urlArg;
String newUrl = expandSingleLevel(originalUrl);
while (!originalUrl.equals(newUrl)) {
originalUrl = newUrl;
newUrl = expandSingleLevel(originalUrl);
}
final String expand(final String urlArg) throws IOException {
String originalUrl = urlArg;
String newUrl = expandSingleLevel(originalUrl);
while (!originalUrl.equals(newUrl)) {
originalUrl = newUrl;
newUrl = expandSingleLevel(originalUrl);
}
return newUrl;
}
return newUrl;
}
final String expandSafe(final String urlArg) throws IOException {
String originalUrl = urlArg;
String newUrl = expandSingleLevelSafe(originalUrl).getRight();
final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
while (!originalUrl.equals(newUrl)) {
originalUrl = newUrl;
final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl);
newUrl = statusAndUrl.getRight();
final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302;
if (isRedirect && alreadyVisited.contains(newUrl)) {
throw new IllegalStateException("Likely a redirect loop");
}
alreadyVisited.add(newUrl);
}
final String expandSafe(final String urlArg) throws IOException {
String originalUrl = urlArg;
String newUrl = expandSingleLevelSafe(originalUrl).getRight();
final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
while (!originalUrl.equals(newUrl)) {
originalUrl = newUrl;
final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl);
newUrl = statusAndUrl.getRight();
final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302;
if (isRedirect && alreadyVisited.contains(newUrl)) {
throw new IllegalStateException("Likely a redirect loop");
}
alreadyVisited.add(newUrl);
}
return newUrl;
}
return newUrl;
}
final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException {
HttpGet request = null;
HttpEntity httpEntity = null;
InputStream entityContentStream = null;
final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException {
HttpGet request = null;
HttpEntity httpEntity = null;
InputStream entityContentStream = null;
try {
request = new HttpGet(url);
final HttpResponse httpResponse = client.execute(request);
try {
request = new HttpGet(url);
final HttpResponse httpResponse = client.execute(request);
httpEntity = httpResponse.getEntity();
entityContentStream = httpEntity.getContent();
httpEntity = httpResponse.getEntity();
entityContentStream = httpEntity.getContent();
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != 301 && statusCode != 302) {
return new ImmutablePair<Integer, String>(statusCode, url);
}
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
Preconditions.checkState(headers.length == 1);
final String newUrl = headers[0].getValue();
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != 301 && statusCode != 302) {
return new ImmutablePair<Integer, String>(statusCode, url);
}
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
Preconditions.checkState(headers.length == 1);
final String newUrl = headers[0].getValue();
return new ImmutablePair<Integer, String>(statusCode, newUrl);
} catch (final IllegalArgumentException uriEx) {
return new ImmutablePair<Integer, String>(500, url);
} finally {
if (request != null) {
request.releaseConnection();
}
if (entityContentStream != null) {
entityContentStream.close();
}
if (httpEntity != null) {
EntityUtils.consume(httpEntity);
}
}
}
return new ImmutablePair<Integer, String>(statusCode, newUrl);
} catch (final IllegalArgumentException uriEx) {
return new ImmutablePair<Integer, String>(500, url);
} finally {
if (request != null) {
request.releaseConnection();
}
if (entityContentStream != null) {
entityContentStream.close();
}
if (httpEntity != null) {
EntityUtils.consume(httpEntity);
}
}
}
final String expandSingleLevel(final String url) throws IOException {
HttpGet request = null;
HttpEntity httpEntity = null;
InputStream entityContentStream = null;
final String expandSingleLevel(final String url) throws IOException {
HttpGet request = null;
HttpEntity httpEntity = null;
InputStream entityContentStream = null;
try {
request = new HttpGet(url);
final HttpResponse httpResponse = client.execute(request);
try {
request = new HttpGet(url);
final HttpResponse httpResponse = client.execute(request);
httpEntity = httpResponse.getEntity();
entityContentStream = httpEntity.getContent();
httpEntity = httpResponse.getEntity();
entityContentStream = httpEntity.getContent();
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != 301 && statusCode != 302) {
return url;
}
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
Preconditions.checkState(headers.length == 1);
final String newUrl = headers[0].getValue();
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != 301 && statusCode != 302) {
return url;
}
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
Preconditions.checkState(headers.length == 1);
final String newUrl = headers[0].getValue();
return newUrl;
} catch (final IllegalArgumentException uriEx) {
return url;
} finally {
if (request != null) {
request.releaseConnection();
}
if (entityContentStream != null) {
entityContentStream.close();
}
if (httpEntity != null) {
EntityUtils.consume(httpEntity);
}
}
}
return newUrl;
} catch (final IllegalArgumentException uriEx) {
return url;
} finally {
if (request != null) {
request.releaseConnection();
}
if (entityContentStream != null) {
entityContentStream.close();
}
if (httpEntity != null) {
EntityUtils.consume(httpEntity);
}
}
}
}

View File

@ -8,12 +8,12 @@ import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
@ -24,95 +24,95 @@ import org.junit.Test;
public class HttpClientCookieLiveTest {
private CloseableHttpClient instance;
private CloseableHttpClient instance;
private CloseableHttpResponse response;
private CloseableHttpResponse response;
@Before
public final void before() {
instance = HttpClientBuilder.create().build();
}
@Before
public final void before() {
instance = HttpClientBuilder.create().build();
}
@After
public final void after() throws IllegalStateException, IOException {
if (response == null) {
return;
}
@After
public final void after() throws IllegalStateException, IOException {
if (response == null) {
return;
}
try {
final HttpEntity entity = response.getEntity();
if (entity != null) {
final InputStream instream = entity.getContent();
instream.close();
}
} finally {
response.close();
}
}
try {
final HttpEntity entity = response.getEntity();
if (entity != null) {
final InputStream instream = entity.getContent();
instream.close();
}
} finally {
response.close();
}
}
// tests
// tests
@Test
public final void whenSettingCookiesOnARequest_thenCorrect() throws ClientProtocolException, IOException {
instance = HttpClientBuilder.create().build();
final HttpGet request = new HttpGet("http://www.github.com");
request.setHeader("Cookie", "JSESSIONID=1234");
@Test
public final void whenSettingCookiesOnARequest_thenCorrect() throws ClientProtocolException, IOException {
instance = HttpClientBuilder.create().build();
final HttpGet request = new HttpGet("http://www.github.com");
request.setHeader("Cookie", "JSESSIONID=1234");
response = instance.execute(request);
response = instance.execute(request);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
@Test
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
final DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(cookieStore);
@Test
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
final HttpClient client = HttpClientBuilder.create()
.setDefaultCookieStore(cookieStore).build();
final HttpGet request = new HttpGet("http://www.github.com");
final HttpGet request = new HttpGet("http://www.github.com");
response = client.execute(request);
response = (CloseableHttpResponse) client.execute(request);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
@Test
public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
@Test
public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
final HttpGet request = new HttpGet("http://www.github.com");
final HttpGet request = new HttpGet("http://www.github.com");
response = instance.execute(request);
response = instance.execute(request);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
@Test
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().build();
@Test
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().build();
final HttpGet request = new HttpGet("http://www.github.com");
final HttpGet request = new HttpGet("http://www.github.com");
final HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
response = instance.execute(request, localContext);
final HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
response = instance.execute(request, localContext);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
}