doc work, new example for expanding shortened urls

This commit is contained in:
eugenp 2013-08-11 01:27:27 +03:00
parent 1f5cf7151e
commit 1157dc6b58
6 changed files with 335 additions and 217 deletions

View File

@ -6,3 +6,4 @@
### Relevant Articles:
- [RestTemplate with Basic Authentication in Spring](http://www.baeldung.com/2012/04/16/how-to-use-resttemplate-with-basic-authentication-in-spring-3-1)
- [HttpClient Timeout](http://www.baeldung.com/httpclient-timeout)
- [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl)

View File

@ -16,6 +16,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>

View File

@ -4,6 +4,7 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<property name="context-root" value="spring-security-rest-custom"/>
<property name="java-output-path" value="/spring-security-rest-custom/target/classes"/>
</wb-module>

View File

@ -1,5 +1,4 @@
<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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-security-rest-custom</artifactId>
@ -102,6 +101,32 @@
<scope>runtime</scope>
</dependency>
<!-- http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- util -->
<dependency>

View File

@ -0,0 +1,102 @@
package org.baeldung.live;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;
import com.google.common.base.Preconditions;
public class HttpLiveServiceTemp {
private DefaultHttpClient client;
// fixtures
@Before
public final void before() {
final HttpParams httpParameters = new BasicHttpParams();
httpParameters.setParameter("http.protocol.handle-redirects", false);
client = new DefaultHttpClient(httpParameters);
}
// 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 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
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;
}
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);
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();
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

@ -93,18 +93,6 @@
<!-- http -->
<!-- <dependency> -->
<!-- <groupId>org.apache.httpcomponents</groupId> -->
<!-- <artifactId>fluent-hc</artifactId> -->
<!-- <version>4.2.5</version> -->
<!-- <exclusions> -->
<!-- <exclusion> -->
<!-- <artifactId>commons-logging</artifactId> -->
<!-- <groupId>commons-logging</groupId> -->
<!-- </exclusion> -->
<!-- </exclusions> -->
<!-- </dependency> -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>