Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
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
openSource
spring-cloud-netflix
Commits
3bb1562b
Commit
3bb1562b
authored
May 15, 2015
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix RibbonClientHttpRequestFactory to add headers and accept a body
fixes gh-350
parent
68b0c91f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
201 additions
and
14 deletions
+201
-14
RibbonClientHttpRequestFactory.java
.../cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java
+41
-14
RibbonClientHttpRequestFactoryTests.java
...d/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java
+160
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java
View file @
3bb1562b
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
;
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
...
@@ -25,7 +26,6 @@ import java.util.Map;
...
@@ -25,7 +26,6 @@ import java.util.Map;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerClient
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerClient
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerRequest
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
...
@@ -67,48 +67,58 @@ public class RibbonClientHttpRequestFactory implements ClientHttpRequestFactory
...
@@ -67,48 +67,58 @@ public class RibbonClientHttpRequestFactory implements ClientHttpRequestFactory
//@formatter:off
//@formatter:off
IClientConfig
clientConfig
=
clientFactory
.
getClientConfig
(
instance
.
getServiceId
());
IClientConfig
clientConfig
=
clientFactory
.
getClientConfig
(
instance
.
getServiceId
());
RestClient
client
=
clientFactory
.
getClient
(
instance
.
getServiceId
(),
RestClient
.
class
);
RestClient
client
=
clientFactory
.
getClient
(
instance
.
getServiceId
(),
RestClient
.
class
);
HttpRequest
request
=
HttpRequest
.
newBuilder
()
HttpRequest
.
Verb
verb
=
HttpRequest
.
Verb
.
valueOf
(
httpMethod
.
name
());
.
uri
(
uri
)
.
verb
(
HttpRequest
.
Verb
.
valueOf
(
httpMethod
.
name
()))
.
build
();
//@formatter:on
//@formatter:on
return
new
RibbonHttpRequest
(
request
,
client
,
clientConfig
);
return
new
RibbonHttpRequest
(
uri
,
verb
,
client
,
clientConfig
);
}
}
public
class
RibbonHttpRequest
extends
AbstractClientHttpRequest
{
public
class
RibbonHttpRequest
extends
AbstractClientHttpRequest
{
private
HttpRequest
request
;
private
HttpRequest
.
Builder
builder
;
private
URI
uri
;
private
HttpRequest
.
Verb
verb
;
private
RestClient
client
;
private
RestClient
client
;
private
IClientConfig
config
;
private
IClientConfig
config
;
private
ByteArrayOutputStream
outputStream
=
null
;
@SuppressWarnings
(
"deprecation"
)
public
RibbonHttpRequest
(
URI
uri
,
HttpRequest
.
Verb
verb
,
RestClient
client
,
public
RibbonHttpRequest
(
HttpRequest
request
,
RestClient
client
,
IClientConfig
config
)
{
IClientConfig
config
)
{
this
.
request
=
request
;
this
.
uri
=
uri
;
this
.
verb
=
verb
;
this
.
client
=
client
;
this
.
client
=
client
;
this
.
config
=
config
;
this
.
config
=
config
;
request
.
getHeaders
().
putAll
(
getHeaders
()
);
this
.
builder
=
HttpRequest
.
newBuilder
().
uri
(
uri
).
verb
(
verb
);
}
}
@Override
@Override
public
HttpMethod
getMethod
()
{
public
HttpMethod
getMethod
()
{
return
HttpMethod
.
valueOf
(
request
.
getVerb
()
.
name
());
return
HttpMethod
.
valueOf
(
verb
.
name
());
}
}
@Override
@Override
public
URI
getURI
()
{
public
URI
getURI
()
{
return
request
.
getUri
()
;
return
uri
;
}
}
@Override
@Override
protected
OutputStream
getBodyInternal
(
HttpHeaders
headers
)
throws
IOException
{
protected
OutputStream
getBodyInternal
(
HttpHeaders
headers
)
throws
IOException
{
throw
new
RuntimeException
(
"Not implemented"
);
if
(
outputStream
==
null
)
{
outputStream
=
new
ByteArrayOutputStream
();
}
return
outputStream
;
}
}
@Override
@Override
@SuppressWarnings
(
"deprecation"
)
protected
ClientHttpResponse
executeInternal
(
HttpHeaders
headers
)
protected
ClientHttpResponse
executeInternal
(
HttpHeaders
headers
)
throws
IOException
{
throws
IOException
{
try
{
try
{
addHeaders
(
headers
);
if
(
outputStream
!=
null
)
{
outputStream
.
close
();
builder
.
entity
(
outputStream
.
toByteArray
());
}
HttpRequest
request
=
builder
.
build
();
HttpResponse
response
=
client
.
execute
(
request
,
config
);
HttpResponse
response
=
client
.
execute
(
request
,
config
);
return
new
RibbonHttpResponse
(
response
);
return
new
RibbonHttpResponse
(
response
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -123,6 +133,23 @@ public class RibbonClientHttpRequestFactory implements ClientHttpRequestFactory
...
@@ -123,6 +133,23 @@ public class RibbonClientHttpRequestFactory implements ClientHttpRequestFactory
}
}
});*/
});*/
}
}
private
void
addHeaders
(
HttpHeaders
headers
)
{
for
(
String
name
:
headers
.
keySet
())
{
// apache http RequestContent pukes if there is a body and
// the dynamic headers are already present
if
(!
isDynamic
(
name
)
||
outputStream
==
null
)
{
List
<
String
>
values
=
headers
.
get
(
name
);
for
(
String
value
:
values
)
{
builder
.
header
(
name
,
value
);
}
}
}
}
private
boolean
isDynamic
(
String
name
)
{
return
name
.
equals
(
"Content-Length"
)
||
name
.
equals
(
"Transfer-Encoding"
);
}
}
}
public
class
RibbonHttpResponse
extends
AbstractClientHttpResponse
{
public
class
RibbonHttpResponse
extends
AbstractClientHttpResponse
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java
0 → 100644
View file @
3bb1562b
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.net.URI
;
import
java.util.Arrays
;
import
lombok.SneakyThrows
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.WebIntegrationTest
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.RequestEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.client.RestTemplate
;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
/**
* @author Spencer Gibb
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
RibbonClientHttpRequestFactoryTests
.
App
.
class
)
@WebIntegrationTest
(
value
=
{
"spring.application.name=ribbonclienttest"
,
"spring.jmx.enabled=true"
},
randomPort
=
true
)
@DirtiesContext
public
class
RibbonClientHttpRequestFactoryTests
{
@Autowired
private
RestTemplate
restTemplate
;
@Test
public
void
requestFactoryIsRibbon
()
{
assertTrue
(
"wrong RequestFactory type"
,
restTemplate
.
getRequestFactory
()
instanceof
RibbonClientHttpRequestFactory
);
}
@Test
public
void
vanillaRequestWorks
()
{
ResponseEntity
<
String
>
response
=
restTemplate
.
getForEntity
(
"http://simple/"
,
String
.
class
);
assertEquals
(
"wrong response code"
,
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
"wrong response body"
,
"hello"
,
response
.
getBody
());
}
@Test
public
void
requestWithPathParamWorks
()
{
ResponseEntity
<
String
>
response
=
restTemplate
.
getForEntity
(
"http://simple/path/{param}"
,
String
.
class
,
"world"
);
assertEquals
(
"wrong response code"
,
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
"wrong response body"
,
"hello world"
,
response
.
getBody
());
}
@Test
public
void
requestWithRequestParamWorks
()
{
ResponseEntity
<
String
>
response
=
restTemplate
.
getForEntity
(
"http://simple/request?param={param}"
,
String
.
class
,
"world"
);
assertEquals
(
"wrong response code"
,
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
"wrong response body"
,
"hello world"
,
response
.
getBody
());
}
@Test
public
void
requestWithPostWorks
()
{
ResponseEntity
<
String
>
response
=
restTemplate
.
postForEntity
(
"http://simple/post"
,
"world"
,
String
.
class
);
assertEquals
(
"wrong response code"
,
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
"wrong response body"
,
"hello world"
,
response
.
getBody
());
}
@Test
@SneakyThrows
public
void
requestWithHeaderWorks
()
{
RequestEntity
<
Void
>
entity
=
RequestEntity
.
get
(
new
URI
(
"http://simple/header"
))
.
header
(
"X-Param"
,
"world"
)
.
build
();
ResponseEntity
<
String
>
response
=
restTemplate
.
exchange
(
entity
,
String
.
class
);
assertEquals
(
"wrong response code"
,
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
"wrong response body"
,
"hello world"
,
response
.
getBody
());
}
@Configuration
@EnableAutoConfiguration
@RestController
@RibbonClient
(
value
=
"simple"
,
configuration
=
SimpleRibbonClientConfiguration
.
class
)
protected
static
class
App
{
@RequestMapping
(
"/"
)
public
String
hi
()
{
return
"hello"
;
}
@RequestMapping
(
"/path/{param}"
)
public
String
hiParam
(
@PathVariable
(
"param"
)
String
param
)
{
return
"hello "
+
param
;
}
@RequestMapping
(
"/request"
)
public
String
hiRequest
(
@RequestParam
(
"param"
)
String
param
)
{
return
"hello "
+
param
;
}
@RequestMapping
(
value
=
"/post"
,
method
=
RequestMethod
.
POST
)
public
String
hiPost
(
@RequestBody
String
param
)
{
return
"hello "
+
param
;
}
@RequestMapping
(
"/header"
)
public
String
hiHeader
(
@RequestHeader
(
"X-Param"
)
String
param
)
{
return
"hello "
+
param
;
}
}
}
@Configuration
class
SimpleRibbonClientConfiguration
{
@Value
(
"${local.server.port}"
)
private
int
port
=
0
;
@Bean
public
ILoadBalancer
ribbonLoadBalancer
()
{
BaseLoadBalancer
balancer
=
new
BaseLoadBalancer
();
balancer
.
setServersList
(
Arrays
.
asList
(
new
Server
(
"localhost"
,
port
)));
return
balancer
;
}
}
\ No newline at end of file
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