Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
b08ad442
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b08ad442
编写于
3月 03, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-5536: RestTemplate does not do HTTP GET if it should
上级
66310928
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
74 addition
and
16 deletion
+74
-16
org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java
...springframework/http/client/CommonsClientHttpRequest.java
+1
-3
org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java
.../springframework/http/client/SimpleClientHttpRequest.java
+5
-3
org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java
...framework/http/client/SimpleClientHttpRequestFactory.java
+6
-2
org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
...ework/http/client/AbstractHttpRequestFactoryTestCase.java
+62
-8
未找到文件。
org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java
浏览文件 @
b08ad442
...
...
@@ -28,8 +28,6 @@ import org.apache.commons.httpclient.methods.RequestEntity;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.client.AbstractClientHttpRequest
;
import
org.springframework.http.client.ClientHttpResponse
;
/**
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
...
...
@@ -48,7 +46,7 @@ final class CommonsClientHttpRequest extends AbstractClientHttpRequest {
private
final
HttpMethodBase
httpMethod
;
public
CommonsClientHttpRequest
(
HttpClient
httpClient
,
HttpMethodBase
httpMethod
)
{
CommonsClientHttpRequest
(
HttpClient
httpClient
,
HttpMethodBase
httpMethod
)
{
this
.
httpClient
=
httpClient
;
this
.
httpMethod
=
httpMethod
;
}
...
...
org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java
浏览文件 @
b08ad442
...
...
@@ -21,9 +21,9 @@ import java.net.HttpURLConnection;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.util.FileCopyUtils
;
/**
* {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute requests.
...
...
@@ -38,7 +38,7 @@ final class SimpleClientHttpRequest extends AbstractClientHttpRequest {
private
final
HttpURLConnection
connection
;
public
SimpleClientHttpRequest
(
HttpURLConnection
connection
)
{
SimpleClientHttpRequest
(
HttpURLConnection
connection
)
{
this
.
connection
=
connection
;
}
...
...
@@ -56,7 +56,9 @@ final class SimpleClientHttpRequest extends AbstractClientHttpRequest {
}
}
this
.
connection
.
connect
();
FileCopyUtils
.
copy
(
bufferedOutput
,
this
.
connection
.
getOutputStream
());
if
(
bufferedOutput
.
length
>
0
)
{
FileCopyUtils
.
copy
(
bufferedOutput
,
this
.
connection
.
getOutputStream
());
}
return
new
SimpleClientHttpResponse
(
this
.
connection
);
}
...
...
org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java
浏览文件 @
b08ad442
...
...
@@ -21,8 +21,8 @@ import java.net.HttpURLConnection;
import
java.net.URI
;
import
java.net.URLConnection
;
import
org.springframework.util.Assert
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.util.Assert
;
/**
* {@link ClientHttpRequestFactory} implementation that uses standard J2SE facilities.
...
...
@@ -51,7 +51,11 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
*/
protected
void
prepareConnection
(
HttpURLConnection
connection
,
String
httpMethod
)
throws
IOException
{
connection
.
setDoInput
(
true
);
connection
.
setDoOutput
(
true
);
if
(
"PUT"
.
equals
(
httpMethod
)
||
"POST"
.
equals
(
httpMethod
))
{
connection
.
setDoOutput
(
true
);
}
else
{
connection
.
setDoOutput
(
false
);
}
connection
.
setRequestMethod
(
httpMethod
);
}
...
...
org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
浏览文件 @
b08ad442
...
...
@@ -20,6 +20,7 @@ import java.io.IOException;
import
java.net.URI
;
import
java.util.Arrays
;
import
java.util.Enumeration
;
import
java.util.Locale
;
import
javax.servlet.GenericServlet
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
...
...
@@ -37,9 +38,9 @@ import org.mortbay.jetty.Server;
import
org.mortbay.jetty.servlet.Context
;
import
org.mortbay.jetty.servlet.ServletHolder
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.FileCopyUtils
;
public
abstract
class
AbstractHttpRequestFactoryTestCase
{
...
...
@@ -53,6 +54,12 @@ public abstract class AbstractHttpRequestFactoryTestCase {
Context
jettyContext
=
new
Context
(
jettyServer
,
"/"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
EchoServlet
()),
"/echo"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
ErrorServlet
(
404
)),
"/errors/notfound"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"DELETE"
)),
"/methods/delete"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"GET"
)),
"/methods/get"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"HEAD"
)),
"/methods/head"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"OPTIONS"
)),
"/methods/options"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"POST"
)),
"/methods/post"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"PUT"
)),
"/methods/put"
);
jettyServer
.
start
();
}
...
...
@@ -104,8 +111,13 @@ public abstract class AbstractHttpRequestFactoryTestCase {
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889/echo"
),
HttpMethod
.
POST
);
byte
[]
body
=
"Hello World"
.
getBytes
(
"UTF-8"
);
FileCopyUtils
.
copy
(
body
,
request
.
getBody
());
request
.
execute
();
FileCopyUtils
.
copy
(
body
,
request
.
getBody
());
ClientHttpResponse
response
=
request
.
execute
();
try
{
FileCopyUtils
.
copy
(
body
,
request
.
getBody
());
}
finally
{
response
.
close
();
}
}
@Test
(
expected
=
IllegalStateException
.
class
)
...
...
@@ -114,13 +126,40 @@ public abstract class AbstractHttpRequestFactoryTestCase {
request
.
getHeaders
().
add
(
"MyHeader"
,
"value"
);
byte
[]
body
=
"Hello World"
.
getBytes
(
"UTF-8"
);
FileCopyUtils
.
copy
(
body
,
request
.
getBody
());
request
.
execute
();
request
.
getHeaders
().
add
(
"MyHeader"
,
"value"
);
ClientHttpResponse
response
=
request
.
execute
();
try
{
request
.
getHeaders
().
add
(
"MyHeader"
,
"value"
);
}
finally
{
response
.
close
();
}
}
@Test
public
void
httpMethods
()
throws
Exception
{
assertHttpMethod
(
"get"
,
HttpMethod
.
GET
);
assertHttpMethod
(
"head"
,
HttpMethod
.
HEAD
);
assertHttpMethod
(
"post"
,
HttpMethod
.
POST
);
assertHttpMethod
(
"put"
,
HttpMethod
.
PUT
);
assertHttpMethod
(
"options"
,
HttpMethod
.
OPTIONS
);
assertHttpMethod
(
"delete"
,
HttpMethod
.
DELETE
);
}
/**
* Servlet that returns and error message for a given status code.
*/
private
void
assertHttpMethod
(
String
path
,
HttpMethod
method
)
throws
Exception
{
ClientHttpResponse
response
=
null
;
try
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889/methods/"
+
path
),
method
);
response
=
request
.
execute
();
assertEquals
(
"Invalid method"
,
path
.
toUpperCase
(
Locale
.
ENGLISH
),
request
.
getMethod
().
name
());
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
}
/** Servlet that returns and error message for a given status code. */
private
static
class
ErrorServlet
extends
GenericServlet
{
private
final
int
sc
;
...
...
@@ -135,6 +174,21 @@ public abstract class AbstractHttpRequestFactoryTestCase {
}
}
private
static
class
MethodServlet
extends
GenericServlet
{
private
final
String
method
;
private
MethodServlet
(
String
method
)
{
this
.
method
=
method
;
}
@Override
public
void
service
(
ServletRequest
req
,
ServletResponse
res
)
throws
ServletException
,
IOException
{
HttpServletRequest
httpReq
=
(
HttpServletRequest
)
req
;
assertEquals
(
"Invalid HTTP method"
,
method
,
httpReq
.
getMethod
());
}
}
private
static
class
EchoServlet
extends
HttpServlet
{
@Override
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录