Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
05bdc2cf
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,体验更适合开发者的 AI 搜索 >>
提交
05bdc2cf
编写于
11月 11, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistent declaration and use of UTF-8 Charset constants, plus related polishing
上级
53eec48f
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
99 addition
and
100 deletion
+99
-100
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java
...rg/springframework/messaging/simp/stomp/StompDecoder.java
+17
-29
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java
...rg/springframework/messaging/simp/stomp/StompEncoder.java
+6
-7
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
...ingframework/http/converter/FormHttpMessageConverter.java
+3
-1
spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
...gframework/http/converter/StringHttpMessageConverter.java
+13
-7
spring-websocket/src/main/java/org/springframework/web/socket/TextMessage.java
...main/java/org/springframework/web/socket/TextMessage.java
+3
-3
spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java
...rk/web/socket/server/support/DefaultHandshakeHandler.java
+6
-2
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java
...work/web/socket/sockjs/support/AbstractSockJsService.java
+2
-2
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpReceivingTransportHandler.java
...nsport/handler/AbstractHttpReceivingTransportHandler.java
+1
-1
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
...ransport/handler/AbstractHttpSendingTransportHandler.java
+6
-4
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/EventSourceTransportHandler.java
...sockjs/transport/handler/EventSourceTransportHandler.java
+5
-5
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.java
...et/sockjs/transport/handler/HtmlFileTransportHandler.java
+17
-18
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpPollingTransportHandler.java
...ockjs/transport/handler/JsonpPollingTransportHandler.java
+6
-6
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpReceivingTransportHandler.java
...kjs/transport/handler/JsonpReceivingTransportHandler.java
+1
-1
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java
...t/sockjs/transport/handler/WebSocketTransportHandler.java
+3
-5
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrPollingTransportHandler.java
.../sockjs/transport/handler/XhrPollingTransportHandler.java
+3
-3
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java
...ockjs/transport/handler/XhrStreamingTransportHandler.java
+7
-6
未找到文件。
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java
浏览文件 @
05bdc2cf
...
...
@@ -67,21 +67,20 @@ public class StompDecoder {
}
/**
*
@return the configured {@code MessageHeaderInitializer}
if any.
*
Return the configured {@code MessageHeaderInitializer},
if any.
*/
public
MessageHeaderInitializer
getHeaderInitializer
()
{
return
this
.
headerInitializer
;
}
/**
* Decodes one or more STOMP frames from the given {@code ByteBuffer} into a
* list of {@link Message}s. If the input buffer contains any incplcontains partial STOMP frame content, or additional
* content with a partial STOMP frame, the buffer is reset and {@code null} is
* returned.
*
* list of {@link Message}s. If the input buffer contains partial STOMP frame
* content, or additional content with a partial STOMP frame, the buffer is
* reset and {@code null} is returned.
* @param buffer The buffer to decode the STOMP frame from
*
* @return the decoded messages or an empty list
* @return the decoded messages, or an empty list if none
*/
public
List
<
Message
<
byte
[]>>
decode
(
ByteBuffer
buffer
)
{
return
decode
(
buffer
,
null
);
...
...
@@ -90,33 +89,28 @@ public class StompDecoder {
/**
* Decodes one or more STOMP frames from the given {@code buffer} and returns
* a list of {@link Message}s.
*
* <p>If the given ByteBuffer contains only partial STOMP frame content and no
* complete STOMP frames, an empty list is returned, and the buffer is reset to
* to where it was.
*
* <p>If the buffer contains one ore more STOMP frames, those are returned and
* the buffer reset to point to the beginning of the unused partial content.
*
* <p>The output partialMessageHeaders map is used to store successfully parsed
* headers in case of partial content. The caller can then check if a
* "content-length" header was read, which helps to determine how much more
* content is needed before the next attempt to decode.
*
* @param buffer The buffer to decode the STOMP frame from
* @param partialMessageHeaders an empty output map that will store the last
* successfully parsed partialMessageHeaders in case of partial message content
* in cases where the partial buffer ended with a partial STOMP frame
*
* @return decoded messages or an empty list
* @throws StompConversionException raised in case of decoding issues
*/
public
List
<
Message
<
byte
[]>>
decode
(
ByteBuffer
buffer
,
MultiValueMap
<
String
,
String
>
partialMessageHeaders
)
{
List
<
Message
<
byte
[]>>
messages
=
new
ArrayList
<
Message
<
byte
[]>>();
while
(
buffer
.
hasRemaining
())
{
Message
<
byte
[]>
m
=
decodeMessage
(
buffer
,
partialMessageHeaders
);
if
(
m
!=
null
)
{
messages
.
add
(
m
);
Message
<
byte
[]>
m
essage
=
decodeMessage
(
buffer
,
partialMessageHeaders
);
if
(
m
essage
!=
null
)
{
messages
.
add
(
m
essage
);
}
else
{
break
;
...
...
@@ -129,28 +123,23 @@ public class StompDecoder {
* Decode a single STOMP frame from the given {@code buffer} into a {@link Message}.
*/
private
Message
<
byte
[]>
decodeMessage
(
ByteBuffer
buffer
,
MultiValueMap
<
String
,
String
>
headers
)
{
Message
<
byte
[]>
decodedMessage
=
null
;
skipLeadingEol
(
buffer
);
buffer
.
mark
();
String
command
=
readCommand
(
buffer
);
if
(
command
.
length
()
>
0
)
{
StompHeaderAccessor
headerAccessor
=
null
;
byte
[]
payload
=
null
;
if
(
buffer
.
remaining
()
>
0
)
{
StompCommand
stompCommand
=
StompCommand
.
valueOf
(
command
);
headerAccessor
=
StompHeaderAccessor
.
create
(
stompCommand
);
initHeaders
(
headerAccessor
);
readHeaders
(
buffer
,
headerAccessor
);
payload
=
readPayload
(
buffer
,
headerAccessor
);
}
if
(
payload
!=
null
)
{
if
(
(
payload
.
length
>
0
)
&&
(!
headerAccessor
.
getCommand
().
isBodyAllowed
()
))
{
if
(
payload
.
length
>
0
&&
!
headerAccessor
.
getCommand
().
isBodyAllowed
(
))
{
throw
new
StompConversionException
(
headerAccessor
.
getCommand
()
+
" shouldn't have a payload: length="
+
payload
.
length
+
", headers="
+
headers
);
}
...
...
@@ -185,12 +174,14 @@ public class StompDecoder {
logger
.
trace
(
"Decoded "
+
headerAccessor
.
getDetailedLogMessage
(
null
));
}
}
return
decodedMessage
;
}
private
void
initHeaders
(
StompHeaderAccessor
headerAccessor
)
{
if
(
getHeaderInitializer
()
!=
null
)
{
getHeaderInitializer
().
initHeaders
(
headerAccessor
);
MessageHeaderInitializer
initializer
=
getHeaderInitializer
();
if
(
initializer
!=
null
)
{
initializer
.
initHeaders
(
headerAccessor
);
}
}
...
...
@@ -253,14 +244,13 @@ public class StompDecoder {
* <a href="http://stomp.github.io/stomp-specification-1.2.html#Value_Encoding">"Value Encoding"</a>.
*/
private
String
unescape
(
String
inString
)
{
StringBuilder
sb
=
new
StringBuilder
();
int
pos
=
0
;
// position in the old string
StringBuilder
sb
=
new
StringBuilder
(
inString
.
length
());
int
pos
=
0
;
// position in the old string
int
index
=
inString
.
indexOf
(
"\\"
);
while
(
index
>=
0
)
{
sb
.
append
(
inString
.
substring
(
pos
,
index
));
if
((
index
+
1
)
>=
inString
.
length
())
{
if
(
index
+
1
>=
inString
.
length
())
{
throw
new
StompConversionException
(
"Illegal escape sequence at index "
+
index
+
": "
+
inString
);
}
Character
c
=
inString
.
charAt
(
index
+
1
);
...
...
@@ -289,7 +279,6 @@ public class StompDecoder {
}
private
byte
[]
readPayload
(
ByteBuffer
buffer
,
StompHeaderAccessor
headerAccessor
)
{
Integer
contentLength
;
try
{
contentLength
=
headerAccessor
.
getContentLength
();
...
...
@@ -329,7 +318,6 @@ public class StompDecoder {
/**
* Try to read an EOL incrementing the buffer position if successful.
*
* @return whether an EOL was consumed
*/
private
boolean
tryConsumeEndOfLine
(
ByteBuffer
buffer
)
{
...
...
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java
浏览文件 @
05bdc2cf
...
...
@@ -51,7 +51,6 @@ public final class StompEncoder {
/**
* Encodes the given STOMP {@code message} into a {@code byte[]}
*
* @param message the message to encode
* @return the encoded message
*/
...
...
@@ -61,7 +60,6 @@ public final class StompEncoder {
/**
* Encodes the given payload and headers into a {@code byte[]}.
*
* @param headers the headers
* @param payload the payload
* @return the encoded message
...
...
@@ -69,6 +67,7 @@ public final class StompEncoder {
public
byte
[]
encode
(
Map
<
String
,
Object
>
headers
,
byte
[]
payload
)
{
Assert
.
notNull
(
headers
,
"'headers' is required"
);
Assert
.
notNull
(
payload
,
"'payload' is required"
);
try
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
128
+
payload
.
length
);
DataOutputStream
output
=
new
DataOutputStream
(
baos
);
...
...
@@ -89,8 +88,8 @@ public final class StompEncoder {
return
baos
.
toByteArray
();
}
catch
(
IOException
e
)
{
throw
new
StompConversionException
(
"Failed to encode STOMP frame, headers="
+
headers
+
"."
,
e
);
catch
(
IOException
e
x
)
{
throw
new
StompConversionException
(
"Failed to encode STOMP frame, headers="
+
headers
,
ex
);
}
}
...
...
@@ -102,7 +101,7 @@ public final class StompEncoder {
(
Map
<
String
,
List
<
String
>>)
headers
.
get
(
NativeMessageHeaderAccessor
.
NATIVE_HEADERS
);
if
(
logger
.
isTraceEnabled
())
{
logger
.
trace
(
"Encoding STOMP "
+
command
+
", headers="
+
nativeHeaders
+
"."
);
logger
.
trace
(
"Encoding STOMP "
+
command
+
", headers="
+
nativeHeaders
);
}
if
(
nativeHeaders
==
null
)
{
...
...
@@ -137,8 +136,8 @@ public final class StompEncoder {
}
private
byte
[]
encodeHeaderString
(
String
input
,
boolean
escape
)
{
input
=
escape
?
escape
(
input
)
:
input
;
return
input
.
getBytes
(
StompDecoder
.
UTF8_CHARSET
);
String
inputToUse
=
(
escape
?
escape
(
input
)
:
input
)
;
return
input
ToUse
.
getBytes
(
StompDecoder
.
UTF8_CHARSET
);
}
/**
...
...
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
浏览文件 @
05bdc2cf
...
...
@@ -86,6 +86,8 @@ import org.springframework.util.StringUtils;
*/
public
class
FormHttpMessageConverter
implements
HttpMessageConverter
<
MultiValueMap
<
String
,
?>>
{
public
static
final
Charset
DEFAULT_CHARSET
=
Charset
.
forName
(
"UTF-8"
);
private
static
final
byte
[]
BOUNDARY_CHARS
=
new
byte
[]
{
'-'
,
'_'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'0'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
,
'g'
,
'h'
,
'i'
,
'j'
,
'k'
,
'l'
,
'm'
,
'n'
,
'o'
,
'p'
,
'q'
,
'r'
,
's'
,
't'
,
'u'
,
'v'
,
'w'
,
'x'
,
'y'
,
'z'
,
'A'
,
...
...
@@ -93,7 +95,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
'V'
,
'W'
,
'X'
,
'Y'
,
'Z'
};
private
Charset
charset
=
Charset
.
forName
(
"UTF-8"
)
;
private
Charset
charset
=
DEFAULT_CHARSET
;
private
Charset
multipartCharset
;
...
...
spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
浏览文件 @
05bdc2cf
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -41,6 +41,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
public
static
final
Charset
DEFAULT_CHARSET
=
Charset
.
forName
(
"ISO-8859-1"
);
private
final
Charset
defaultCharset
;
private
final
List
<
Charset
>
availableCharsets
;
...
...
@@ -66,6 +67,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
this
.
availableCharsets
=
new
ArrayList
<
Charset
>(
Charset
.
availableCharsets
().
values
());
}
/**
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
* <p>Default is {@code true}.
...
...
@@ -74,6 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
this
.
writeAcceptCharset
=
writeAcceptCharset
;
}
@Override
public
boolean
supports
(
Class
<?>
clazz
)
{
return
String
.
class
.
equals
(
clazz
);
...
...
@@ -86,10 +89,10 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
}
@Override
protected
Long
getContentLength
(
String
s
,
MediaType
contentType
)
{
protected
Long
getContentLength
(
String
s
tr
,
MediaType
contentType
)
{
Charset
charset
=
getContentTypeCharset
(
contentType
);
try
{
return
(
long
)
s
.
getBytes
(
charset
.
name
()).
length
;
return
(
long
)
s
tr
.
getBytes
(
charset
.
name
()).
length
;
}
catch
(
UnsupportedEncodingException
ex
)
{
// should not occur
...
...
@@ -98,17 +101,19 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
}
@Override
protected
void
writeInternal
(
String
s
,
HttpOutputMessage
outputMessage
)
throws
IOException
{
protected
void
writeInternal
(
String
s
tr
,
HttpOutputMessage
outputMessage
)
throws
IOException
{
if
(
this
.
writeAcceptCharset
)
{
outputMessage
.
getHeaders
().
setAcceptCharset
(
getAcceptedCharsets
());
}
Charset
charset
=
getContentTypeCharset
(
outputMessage
.
getHeaders
().
getContentType
());
StreamUtils
.
copy
(
s
,
charset
,
outputMessage
.
getBody
());
StreamUtils
.
copy
(
s
tr
,
charset
,
outputMessage
.
getBody
());
}
/**
* Return the list of supported {@link Charset}.
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
* Return the list of supported {@link Charset}s.
* <p>By default, returns {@link Charset#availableCharsets()}.
* Can be overridden in subclasses.
* @return the list of accepted charsets
*/
protected
List
<
Charset
>
getAcceptedCharsets
()
{
...
...
@@ -123,4 +128,5 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
return
this
.
defaultCharset
;
}
}
}
spring-websocket/src/main/java/org/springframework/web/socket/TextMessage.java
浏览文件 @
05bdc2cf
...
...
@@ -26,7 +26,7 @@ import java.nio.charset.Charset;
*/
public
final
class
TextMessage
extends
AbstractWebSocketMessage
<
String
>
{
private
static
final
Charset
UTF
_8
=
Charset
.
forName
(
"UTF-8"
);
private
static
final
Charset
UTF
8_CHARSET
=
Charset
.
forName
(
"UTF-8"
);
private
final
byte
[]
bytes
;
...
...
@@ -46,7 +46,7 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
* @param payload the non-null payload
*/
public
TextMessage
(
byte
[]
payload
)
{
super
(
new
String
(
payload
,
UTF
_8
));
super
(
new
String
(
payload
,
UTF
8_CHARSET
));
this
.
bytes
=
payload
;
}
...
...
@@ -70,7 +70,7 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
}
public
byte
[]
asBytes
()
{
return
(
this
.
bytes
!=
null
?
this
.
bytes
:
getPayload
().
getBytes
(
UTF
_8
));
return
(
this
.
bytes
!=
null
?
this
.
bytes
:
getPayload
().
getBytes
(
UTF
8_CHARSET
));
}
@Override
...
...
spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java
浏览文件 @
05bdc2cf
...
...
@@ -17,6 +17,7 @@
package
org.springframework.web.socket.server.support
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
java.security.Principal
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -60,6 +61,9 @@ import org.springframework.web.socket.server.RequestUpgradeStrategy;
*/
public
class
DefaultHandshakeHandler
implements
HandshakeHandler
{
private
static
final
Charset
UTF8_CHARSET
=
Charset
.
forName
(
"UTF-8"
);
private
static
final
boolean
jettyWsPresent
=
ClassUtils
.
isPresent
(
"org.eclipse.jetty.websocket.server.WebSocketServerFactory"
,
DefaultHandshakeHandler
.
class
.
getClassLoader
());
...
...
@@ -223,7 +227,7 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
logger
.
error
(
"Handshake failed due to invalid Upgrade header: "
+
request
.
getHeaders
().
getUpgrade
());
}
response
.
setStatusCode
(
HttpStatus
.
BAD_REQUEST
);
response
.
getBody
().
write
(
"Can \"Upgrade\" only to \"WebSocket\"."
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
"Can \"Upgrade\" only to \"WebSocket\"."
.
getBytes
(
UTF8_CHARSET
));
}
protected
void
handleInvalidConnectHeader
(
ServerHttpRequest
request
,
ServerHttpResponse
response
)
throws
IOException
{
...
...
@@ -231,7 +235,7 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
logger
.
error
(
"Handshake failed due to invalid Connection header "
+
request
.
getHeaders
().
getConnection
());
}
response
.
setStatusCode
(
HttpStatus
.
BAD_REQUEST
);
response
.
getBody
().
write
(
"\"Connection\" must be \"upgrade\"."
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
"\"Connection\" must be \"upgrade\"."
.
getBytes
(
UTF8_CHARSET
));
}
protected
boolean
isWebSocketVersionSupported
(
WebSocketHttpHeaders
httpHeaders
)
{
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java
浏览文件 @
05bdc2cf
...
...
@@ -337,8 +337,8 @@ public abstract class AbstractSockJsService implements SockJsService {
try
{
if
(
sockJsPath
.
equals
(
""
)
||
sockJsPath
.
equals
(
"/"
))
{
logger
.
debug
(
requestInfo
);
response
.
getHeaders
().
setContentType
(
new
MediaType
(
"text"
,
"plain"
,
Charset
.
forName
(
"UTF-8"
)
));
response
.
getBody
().
write
(
"Welcome to SockJS!\n"
.
getBytes
(
"UTF-8"
));
response
.
getHeaders
().
setContentType
(
new
MediaType
(
"text"
,
"plain"
,
UTF8_CHARSET
));
response
.
getBody
().
write
(
"Welcome to SockJS!\n"
.
getBytes
(
UTF8_CHARSET
));
}
else
if
(
sockJsPath
.
equals
(
"/info"
))
{
logger
.
debug
(
requestInfo
);
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpReceivingTransportHandler.java
浏览文件 @
05bdc2cf
...
...
@@ -87,7 +87,7 @@ public abstract class AbstractHttpReceivingTransportHandler extends AbstractTran
private
void
handleReadError
(
ServerHttpResponse
response
,
String
error
,
String
sessionId
)
{
try
{
response
.
setStatusCode
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
response
.
getBody
().
write
(
error
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
error
.
getBytes
(
UTF8_CHARSET
));
}
catch
(
IOException
ex
)
{
throw
new
SockJsException
(
"Failed to send error: "
+
error
,
sessionId
,
ex
);
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
浏览文件 @
05bdc2cf
...
...
@@ -49,7 +49,7 @@ public abstract class AbstractHttpSendingTransportHandler extends AbstractTransp
AbstractHttpSockJsSession
sockJsSession
=
(
AbstractHttpSockJsSession
)
wsSession
;
String
protocol
=
null
;
// https://github.com/sockjs/sockjs-client/issues/130
String
protocol
=
null
;
// https://github.com/sockjs/sockjs-client/issues/130
sockJsSession
.
setAcceptedProtocol
(
protocol
);
// Set content type before writing
...
...
@@ -99,20 +99,22 @@ public abstract class AbstractHttpSendingTransportHandler extends AbstractTransp
}
}
protected
abstract
MediaType
getContentType
();
protected
abstract
SockJsFrameFormat
getFrameFormat
(
ServerHttpRequest
request
);
protected
final
String
getCallbackParam
(
ServerHttpRequest
request
)
{
String
query
=
request
.
getURI
().
getQuery
();
MultiValueMap
<
String
,
String
>
params
=
UriComponentsBuilder
.
newInstance
().
query
(
query
).
build
().
getQueryParams
();
String
value
=
params
.
getFirst
(
"c"
);
try
{
return
StringUtils
.
isEmpty
(
value
)
?
null
:
UriUtils
.
decode
(
value
,
"UTF-8"
);
return
(!
StringUtils
.
isEmpty
(
value
)
?
UriUtils
.
decode
(
value
,
"UTF-8"
)
:
null
);
}
catch
(
UnsupportedEncodingException
e
)
{
catch
(
UnsupportedEncodingException
e
x
)
{
// should never happen
throw
new
SockJsException
(
"Unable to decode callback query parameter"
,
null
,
e
);
throw
new
SockJsException
(
"Unable to decode callback query parameter"
,
null
,
e
x
);
}
}
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/EventSourceTransportHandler.java
浏览文件 @
05bdc2cf
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -49,8 +49,8 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan
}
@Override
public
StreamingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
public
StreamingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
EventSourceStreamingSockJsSession
(
sessionId
,
getServiceConfig
(),
handler
,
attributes
);
}
...
...
@@ -61,9 +61,9 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan
}
private
final
class
EventSourceStreamingSockJsSession
extends
StreamingSockJsSession
{
private
class
EventSourceStreamingSockJsSession
extends
StreamingSockJsSession
{
p
rivate
EventSourceStreamingSockJsSession
(
String
sessionId
,
SockJsServiceConfig
config
,
p
ublic
EventSourceStreamingSockJsSession
(
String
sessionId
,
SockJsServiceConfig
config
,
WebSocketHandler
wsHandler
,
Map
<
String
,
Object
>
attributes
)
{
super
(
sessionId
,
config
,
wsHandler
,
attributes
);
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.java
浏览文件 @
05bdc2cf
...
...
@@ -26,6 +26,7 @@ import org.springframework.http.server.ServerHttpResponse;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.socket.CloseStatus
;
import
org.springframework.web.socket.WebSocketHandler
;
import
org.springframework.web.socket.sockjs.SockJsException
;
import
org.springframework.web.socket.sockjs.SockJsTransportFailureException
;
import
org.springframework.web.socket.sockjs.frame.DefaultSockJsFrameFormat
;
import
org.springframework.web.socket.sockjs.frame.SockJsFrameFormat
;
...
...
@@ -52,6 +53,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
// http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors
private
static
final
int
MINIMUM_PARTIAL_HTML_CONTENT_LENGTH
=
1024
;
static
{
StringBuilder
sb
=
new
StringBuilder
(
"<!doctype html>\n"
+
...
...
@@ -71,7 +73,6 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
while
(
sb
.
length
()
<
MINIMUM_PARTIAL_HTML_CONTENT_LENGTH
)
{
sb
.
append
(
" "
);
}
PARTIAL_HTML_CONTENT
=
sb
.
toString
();
}
...
...
@@ -87,25 +88,25 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
}
@Override
public
StreamingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
public
StreamingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
HtmlFileStreamingSockJsSession
(
sessionId
,
getServiceConfig
(),
handler
,
attributes
);
}
@Override
public
void
handleRequestInternal
(
ServerHttpRequest
request
,
ServerHttpResponse
response
,
AbstractHttpSockJsSession
sockJsSession
)
{
AbstractHttpSockJsSession
sockJsSession
)
throws
SockJsException
{
String
callback
=
getCallbackParam
(
request
);
if
(!
StringUtils
.
hasText
(
callback
))
{
if
(!
StringUtils
.
hasText
(
callback
))
{
response
.
setStatusCode
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
try
{
response
.
getBody
().
write
(
"\"callback\" parameter required"
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
"\"callback\" parameter required"
.
getBytes
(
UTF8_CHARSET
));
}
catch
(
IOException
t
)
{
sockJsSession
.
tryCloseWithSockJsTransportError
(
t
,
CloseStatus
.
SERVER_ERROR
);
throw
new
SockJsTransportFailureException
(
"Failed to write to response"
,
sockJsSession
.
getId
(),
t
);
catch
(
IOException
ex
)
{
sockJsSession
.
tryCloseWithSockJsTransportError
(
ex
,
CloseStatus
.
SERVER_ERROR
);
throw
new
SockJsTransportFailureException
(
"Failed to write to response"
,
sockJsSession
.
getId
(),
ex
);
}
return
;
}
...
...
@@ -124,9 +125,9 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
}
private
final
class
HtmlFileStreamingSockJsSession
extends
StreamingSockJsSession
{
private
class
HtmlFileStreamingSockJsSession
extends
StreamingSockJsSession
{
p
rivate
HtmlFileStreamingSockJsSession
(
String
sessionId
,
SockJsServiceConfig
config
,
p
ublic
HtmlFileStreamingSockJsSession
(
String
sessionId
,
SockJsServiceConfig
config
,
WebSocketHandler
wsHandler
,
Map
<
String
,
Object
>
attributes
)
{
super
(
sessionId
,
config
,
wsHandler
,
attributes
);
...
...
@@ -134,18 +135,16 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
@Override
protected
void
writePrelude
(
ServerHttpRequest
request
,
ServerHttpResponse
response
)
{
//
we already validated the parameter above
..
//
We already validated the parameter above.
..
String
callback
=
getCallbackParam
(
request
);
String
html
=
String
.
format
(
PARTIAL_HTML_CONTENT
,
callback
);
try
{
response
.
getBody
().
write
(
html
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
html
.
getBytes
(
UTF8_CHARSET
));
response
.
flush
();
}
catch
(
IOException
e
)
{
tryCloseWithSockJsTransportError
(
e
,
CloseStatus
.
SERVER_ERROR
);
throw
new
SockJsTransportFailureException
(
"Failed to write HTML content"
,
getId
(),
e
);
catch
(
IOException
e
x
)
{
tryCloseWithSockJsTransportError
(
e
x
,
CloseStatus
.
SERVER_ERROR
);
throw
new
SockJsTransportFailureException
(
"Failed to write HTML content"
,
getId
(),
e
x
);
}
}
}
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpPollingTransportHandler.java
浏览文件 @
05bdc2cf
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -53,8 +53,8 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
}
@Override
public
PollingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
public
PollingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
PollingSockJsSession
(
sessionId
,
getServiceConfig
(),
handler
,
attributes
);
}
...
...
@@ -65,9 +65,9 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
try
{
String
callback
=
getCallbackParam
(
request
);
if
(!
StringUtils
.
hasText
(
callback
))
{
if
(!
StringUtils
.
hasText
(
callback
))
{
response
.
setStatusCode
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
response
.
getBody
().
write
(
"\"callback\" parameter required"
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
"\"callback\" parameter required"
.
getBytes
(
UTF8_CHARSET
));
return
;
}
}
...
...
@@ -81,7 +81,7 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
@Override
protected
SockJsFrameFormat
getFrameFormat
(
ServerHttpRequest
request
)
{
//
w
e already validated the parameter above...
//
W
e already validated the parameter above...
String
callback
=
getCallbackParam
(
request
);
return
new
DefaultSockJsFrameFormat
(
callback
+
"(\"%s\");\r\n"
)
{
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpReceivingTransportHandler.java
浏览文件 @
05bdc2cf
...
...
@@ -53,7 +53,7 @@ public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTranspo
super
.
handleRequestInternal
(
request
,
response
,
wsHandler
,
sockJsSession
);
try
{
response
.
getBody
().
write
(
"ok"
.
getBytes
(
"UTF-8"
));
response
.
getBody
().
write
(
"ok"
.
getBytes
(
UTF8_CHARSET
));
}
catch
(
IOException
ex
)
{
throw
new
SockJsException
(
"Failed to write to the response body"
,
sockJsSession
.
getId
(),
ex
);
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java
浏览文件 @
05bdc2cf
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -67,8 +67,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
}
@Override
public
AbstractSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
public
AbstractSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
WebSocketServerSockJsSession
(
sessionId
,
getServiceConfig
(),
handler
,
attributes
);
}
...
...
@@ -88,8 +88,6 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
}
}
// HandshakeHandler methods
@Override
public
boolean
doHandshake
(
ServerHttpRequest
request
,
ServerHttpResponse
response
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
throws
HandshakeFailureException
{
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrPollingTransportHandler.java
浏览文件 @
05bdc2cf
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -51,8 +51,8 @@ public class XhrPollingTransportHandler extends AbstractHttpSendingTransportHand
}
@Override
public
PollingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
public
PollingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
PollingSockJsSession
(
sessionId
,
getServiceConfig
(),
handler
,
attributes
);
}
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java
浏览文件 @
05bdc2cf
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -49,8 +49,8 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
}
@Override
public
StreamingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
public
StreamingSockJsSession
createSession
(
String
sessionId
,
WebSocketHandler
handler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
XhrStreamingSockJsSession
(
sessionId
,
getServiceConfig
(),
handler
,
attributes
);
}
...
...
@@ -61,9 +61,9 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
}
private
final
class
XhrStreamingSockJsSession
extends
StreamingSockJsSession
{
private
class
XhrStreamingSockJsSession
extends
StreamingSockJsSession
{
p
rivate
XhrStreamingSockJsSession
(
String
sessionId
,
SockJsServiceConfig
config
,
p
ublic
XhrStreamingSockJsSession
(
String
sessionId
,
SockJsServiceConfig
config
,
WebSocketHandler
wsHandler
,
Map
<
String
,
Object
>
attributes
)
{
super
(
sessionId
,
config
,
wsHandler
,
attributes
);
...
...
@@ -71,11 +71,12 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
@Override
protected
void
writePrelude
(
ServerHttpRequest
request
,
ServerHttpResponse
response
)
throws
IOException
{
for
(
int
i
=
0
;
i
<
2048
;
i
++)
{
for
(
int
i
=
0
;
i
<
2048
;
i
++)
{
response
.
getBody
().
write
(
'h'
);
}
response
.
getBody
().
write
(
'\n'
);
response
.
flush
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录