Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
4c14f6f6
Commit
4c14f6f6
authored
Mar 11, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for Apache HttpClient if available
parent
7a285cf6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
7 deletions
+39
-7
pom.xml
spring-boot/pom.xml
+5
-5
RestTemplates.java
...ain/java/org/springframework/boot/test/RestTemplates.java
+34
-2
No files found.
spring-boot/pom.xml
View file @
4c14f6f6
...
@@ -48,6 +48,11 @@
...
@@ -48,6 +48,11 @@
<artifactId>
log4j
</artifactId>
<artifactId>
log4j
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.apache.tomcat.embed
</groupId>
<groupId>
org.apache.tomcat.embed
</groupId>
<artifactId>
tomcat-embed-core
</artifactId>
<artifactId>
tomcat-embed-core
</artifactId>
...
@@ -119,11 +124,6 @@
...
@@ -119,11 +124,6 @@
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<!-- Test -->
<!-- Test -->
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpasyncclient
</artifactId>
<artifactId>
httpasyncclient
</artifactId>
...
...
spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java
View file @
4c14f6f6
...
@@ -17,22 +17,33 @@
...
@@ -17,22 +17,33 @@
package
org
.
springframework
.
boot
.
test
;
package
org
.
springframework
.
boot
.
test
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
org.apache.http.client.config.CookieSpecs
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.config.RequestConfig.Builder
;
import
org.apache.http.client.protocol.HttpClientContext
;
import
org.apache.http.protocol.HttpContext
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.http.client.HttpComponentsClientHttpRequestFactory
;
import
org.springframework.http.client.InterceptingClientHttpRequestFactory
;
import
org.springframework.http.client.InterceptingClientHttpRequestFactory
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.web.client.DefaultResponseErrorHandler
;
import
org.springframework.web.client.DefaultResponseErrorHandler
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
/**
/**
* Convenient static factory for {@link RestTemplate} instances that are suitable for
* Convenient static factory for {@link RestTemplate} instances that are suitable for
* integration tests. They are fault tolerant, ignore cookies, and optionally can carry
* integration tests. They are fault tolerant, and optionally can carry Basic
* Basic authentication headers.
* authentication headers. If Apache Http Client 4.3.2 or better is available
* (recommended) it will be used as the client, and configured to ignore cookies and
* redirects.
*
*
* @author Dave Syer
* @author Dave Syer
*/
*/
...
@@ -80,6 +91,9 @@ public class RestTemplates {
...
@@ -80,6 +91,9 @@ public class RestTemplates {
RestTemplate
restTemplate
=
new
RestTemplate
(
RestTemplate
restTemplate
=
new
RestTemplate
(
new
InterceptingClientHttpRequestFactory
(
new
InterceptingClientHttpRequestFactory
(
new
SimpleClientHttpRequestFactory
(),
interceptors
));
new
SimpleClientHttpRequestFactory
(),
interceptors
));
if
(
ClassUtils
.
isPresent
(
"org.apache.http.client.config.RequestConfig"
,
null
))
{
new
HttpComponentsCustomizer
().
customize
(
restTemplate
);
}
restTemplate
.
setErrorHandler
(
new
DefaultResponseErrorHandler
()
{
restTemplate
.
setErrorHandler
(
new
DefaultResponseErrorHandler
()
{
@Override
@Override
public
void
handleError
(
ClientHttpResponse
response
)
throws
IOException
{
public
void
handleError
(
ClientHttpResponse
response
)
throws
IOException
{
...
@@ -89,4 +103,22 @@ public class RestTemplates {
...
@@ -89,4 +103,22 @@ public class RestTemplates {
}
}
private
static
class
HttpComponentsCustomizer
{
public
void
customize
(
RestTemplate
restTemplate
)
{
restTemplate
.
setRequestFactory
(
new
HttpComponentsClientHttpRequestFactory
()
{
@Override
protected
HttpContext
createHttpContext
(
HttpMethod
httpMethod
,
URI
uri
)
{
HttpClientContext
context
=
HttpClientContext
.
create
();
Builder
builder
=
RequestConfig
.
custom
()
.
setCookieSpec
(
CookieSpecs
.
IGNORE_COOKIES
)
.
setAuthenticationEnabled
(
false
).
setRedirectsEnabled
(
false
);
context
.
setRequestConfig
(
builder
.
build
());
return
context
;
}
});
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment