doc work, new example for expanding shortened urls
This commit is contained in:
parent
1f5cf7151e
commit
1157dc6b58
|
@ -6,3 +6,4 @@
|
||||||
### Relevant Articles:
|
### 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)
|
- [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 Timeout](http://www.baeldung.com/httpclient-timeout)
|
||||||
|
- [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl)
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</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">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
<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/java"/>
|
||||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
|
<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="context-root" value="spring-security-rest-custom"/>
|
||||||
<property name="java-output-path" value="/spring-security-rest-custom/target/classes"/>
|
<property name="java-output-path" value="/spring-security-rest-custom/target/classes"/>
|
||||||
</wb-module>
|
</wb-module>
|
||||||
|
|
|
@ -1,242 +1,267 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
||||||
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>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<groupId>org.baeldung</groupId>
|
||||||
<groupId>org.baeldung</groupId>
|
<artifactId>spring-security-rest-custom</artifactId>
|
||||||
<artifactId>spring-security-rest-custom</artifactId>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<version>0.1-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<name>spring-security-rest-custom</name>
|
<name>spring-security-rest-custom</name>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!-- Spring Security -->
|
<!-- Spring Security -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-web</artifactId>
|
<artifactId>spring-security-web</artifactId>
|
||||||
<version>${org.springframework.security.version}</version>
|
<version>${org.springframework.security.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-config</artifactId>
|
<artifactId>spring-security-config</artifactId>
|
||||||
<version>${org.springframework.security.version}</version>
|
<version>${org.springframework.security.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-core</artifactId>
|
<artifactId>spring-core</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-jdbc</artifactId>
|
<artifactId>spring-jdbc</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-beans</artifactId>
|
<artifactId>spring-beans</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-aop</artifactId>
|
<artifactId>spring-aop</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-tx</artifactId>
|
<artifactId>spring-tx</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-expression</artifactId>
|
<artifactId>spring-expression</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-web</artifactId>
|
<artifactId>spring-web</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-oxm</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- marshalling -->
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-oxm</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- marshalling -->
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-databind</artifactId>
|
|
||||||
<version>2.2.2</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- web -->
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- web -->
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
|
||||||
<version>3.0.1</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>jstl</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
<version>1.2</version>
|
<version>3.0.1</version>
|
||||||
<scope>runtime</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- util -->
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>jstl</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- http -->
|
||||||
<groupId>com.google.guava</groupId>
|
|
||||||
<artifactId>guava</artifactId>
|
|
||||||
<version>${guava.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
<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>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>junit-dep</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>${httpclient.version}</version>
|
||||||
<scope>test</scope>
|
<exclusions>
|
||||||
</dependency>
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- util -->
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-core</artifactId>
|
|
||||||
<version>${org.hamcrest.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-library</artifactId>
|
|
||||||
<version>${org.hamcrest.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>${mockito.version}</version>
|
<version>${guava.version}</version>
|
||||||
<scope>test</scope>
|
</dependency>
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
<!-- test scoped -->
|
||||||
|
|
||||||
<build>
|
<dependency>
|
||||||
<finalName>spring-security-rest-custom</finalName>
|
<groupId>junit</groupId>
|
||||||
<resources>
|
<artifactId>junit-dep</artifactId>
|
||||||
<resource>
|
<version>${junit.version}</version>
|
||||||
<directory>src/main/resources</directory>
|
<scope>test</scope>
|
||||||
<filtering>true</filtering>
|
</dependency>
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<plugins>
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<plugin>
|
<dependency>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<version>3.1</version>
|
<version>${mockito.version}</version>
|
||||||
<configuration>
|
<scope>test</scope>
|
||||||
<source>1.7</source>
|
</dependency>
|
||||||
<target>1.7</target>
|
|
||||||
<debug>true</debug>
|
|
||||||
<debuglevel>source</debuglevel>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
</dependencies>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
|
||||||
</excludes>
|
|
||||||
<systemPropertyVariables>
|
|
||||||
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
|
||||||
</systemPropertyVariables>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
<build>
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
<finalName>spring-security-rest-custom</finalName>
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
<resources>
|
||||||
<version>${cargo-maven2-plugin.version}</version>
|
<resource>
|
||||||
<configuration>
|
<directory>src/main/resources</directory>
|
||||||
<wait>true</wait>
|
<filtering>true</filtering>
|
||||||
<container>
|
</resource>
|
||||||
<containerId>jetty8x</containerId>
|
</resources>
|
||||||
<type>embedded</type>
|
|
||||||
<systemProperties>
|
|
||||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
|
||||||
</systemProperties>
|
|
||||||
</container>
|
|
||||||
<configuration>
|
|
||||||
<properties>
|
|
||||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
|
||||||
</properties>
|
|
||||||
</configuration>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
</plugins>
|
<plugins>
|
||||||
|
|
||||||
</build>
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
<debug>true</debug>
|
||||||
|
<debuglevel>source</debuglevel>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<properties>
|
<plugin>
|
||||||
<!-- Spring -->
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
||||||
|
</excludes>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<!-- persistence -->
|
<plugin>
|
||||||
<hibernate.version>4.2.3.Final</hibernate.version>
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
<mysql-connector-java.version>5.1.26</mysql-connector-java.version>
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
|
<version>${cargo-maven2-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<wait>true</wait>
|
||||||
|
<container>
|
||||||
|
<containerId>jetty8x</containerId>
|
||||||
|
<type>embedded</type>
|
||||||
|
<systemProperties>
|
||||||
|
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
||||||
|
</systemProperties>
|
||||||
|
</container>
|
||||||
|
<configuration>
|
||||||
|
<properties>
|
||||||
|
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||||
|
</properties>
|
||||||
|
</configuration>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<!-- logging -->
|
</plugins>
|
||||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
|
||||||
<logback.version>1.0.11</logback.version>
|
|
||||||
|
|
||||||
<!-- various -->
|
</build>
|
||||||
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
|
|
||||||
|
|
||||||
<!-- util -->
|
<properties>
|
||||||
<guava.version>14.0.1</guava.version>
|
<!-- Spring -->
|
||||||
<commons-lang3.version>3.1</commons-lang3.version>
|
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
|
||||||
|
<org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
|
||||||
|
|
||||||
<!-- testing -->
|
<!-- persistence -->
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<hibernate.version>4.2.3.Final</hibernate.version>
|
||||||
<junit.version>4.11</junit.version>
|
<mysql-connector-java.version>5.1.26</mysql-connector-java.version>
|
||||||
<mockito.version>1.9.5</mockito.version>
|
|
||||||
|
|
||||||
<httpcore.version>4.2.4</httpcore.version>
|
<!-- logging -->
|
||||||
<httpclient.version>4.2.5</httpclient.version>
|
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||||
|
<logback.version>1.0.11</logback.version>
|
||||||
|
|
||||||
<rest-assured.version>1.8.1</rest-assured.version>
|
<!-- various -->
|
||||||
<groovy.version>1.8.9</groovy.version>
|
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
|
||||||
|
|
||||||
<!-- Maven plugins -->
|
<!-- util -->
|
||||||
<cargo-maven2-plugin.version>1.4.3</cargo-maven2-plugin.version>
|
<guava.version>14.0.1</guava.version>
|
||||||
<maven-surefire-plugin.version>2.15</maven-surefire-plugin.version>
|
<commons-lang3.version>3.1</commons-lang3.version>
|
||||||
</properties>
|
|
||||||
|
<!-- testing -->
|
||||||
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
<junit.version>4.11</junit.version>
|
||||||
|
<mockito.version>1.9.5</mockito.version>
|
||||||
|
|
||||||
|
<httpcore.version>4.2.4</httpcore.version>
|
||||||
|
<httpclient.version>4.2.5</httpclient.version>
|
||||||
|
|
||||||
|
<rest-assured.version>1.8.1</rest-assured.version>
|
||||||
|
<groovy.version>1.8.9</groovy.version>
|
||||||
|
|
||||||
|
<!-- Maven plugins -->
|
||||||
|
<cargo-maven2-plugin.version>1.4.3</cargo-maven2-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>2.15</maven-surefire-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -93,18 +93,6 @@
|
||||||
|
|
||||||
<!-- http -->
|
<!-- 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>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpcore</artifactId>
|
<artifactId>httpcore</artifactId>
|
||||||
|
|
Loading…
Reference in New Issue