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
b6fef047
Commit
b6fef047
authored
Feb 26, 2016
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Preserve upstream character encoding. Only default to UTF-8 if not set.
fixes gh-530
parent
b1bcf483
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
2 deletions
+107
-2
SendResponseFilter.java
...k/cloud/netflix/zuul/filters/post/SendResponseFilter.java
+4
-2
SendResponseFilterTests.java
...ud/netflix/zuul/filters/post/SendResponseFilterTests.java
+103
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/post/SendResponseFilter.java
View file @
b6fef047
...
@@ -92,13 +92,15 @@ public class SendResponseFilter extends ZuulFilter {
...
@@ -92,13 +92,15 @@ public class SendResponseFilter extends ZuulFilter {
return
;
return
;
}
}
HttpServletResponse
servletResponse
=
context
.
getResponse
();
HttpServletResponse
servletResponse
=
context
.
getResponse
();
servletResponse
.
setCharacterEncoding
(
"UTF-8"
);
if
(
servletResponse
.
getCharacterEncoding
()
==
null
)
{
// only set if not set
servletResponse
.
setCharacterEncoding
(
"UTF-8"
);
}
OutputStream
outStream
=
servletResponse
.
getOutputStream
();
OutputStream
outStream
=
servletResponse
.
getOutputStream
();
InputStream
is
=
null
;
InputStream
is
=
null
;
try
{
try
{
if
(
RequestContext
.
getCurrentContext
().
getResponseBody
()
!=
null
)
{
if
(
RequestContext
.
getCurrentContext
().
getResponseBody
()
!=
null
)
{
String
body
=
RequestContext
.
getCurrentContext
().
getResponseBody
();
String
body
=
RequestContext
.
getCurrentContext
().
getResponseBody
();
writeResponse
(
new
ByteArrayInputStream
(
body
.
getBytes
()),
outStream
);
writeResponse
(
new
ByteArrayInputStream
(
body
.
getBytes
(
servletResponse
.
getCharacterEncoding
()
)),
outStream
);
return
;
return
;
}
}
boolean
isGzipRequested
=
false
;
boolean
isGzipRequested
=
false
;
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/SendResponseFilterTests.java
0 → 100644
View file @
b6fef047
/*
* 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
.
zuul
.
filters
.
post
;
import
java.io.ByteArrayInputStream
;
import
javax.servlet.http.HttpServletRequest
;
import
com.netflix.zuul.context.RequestContext
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.web.util.WebUtils
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* @author Spencer Gibb
*/
public
class
SendResponseFilterTests
{
@Before
public
void
setTestRequestcontext
()
{
RequestContext
context
=
new
RequestContext
();
RequestContext
.
testSetCurrentContext
(
context
);
}
@After
public
void
reset
()
{
RequestContext
.
getCurrentContext
().
clear
();
}
@Test
public
void
runsNormally
()
throws
Exception
{
String
characterEncoding
=
null
;
String
content
=
"hello"
;
runFilter
(
characterEncoding
,
content
,
false
);
}
@Test
public
void
characterEncodingNotOverridden
()
throws
Exception
{
String
characterEncoding
=
"UTF-16"
;
String
content
=
"\u00a5"
;
runFilter
(
characterEncoding
,
content
,
true
);
}
private
void
runFilter
(
String
characterEncoding
,
String
content
,
boolean
streamContent
)
throws
Exception
{
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
SendResponseFilter
filter
=
createFilter
(
content
,
characterEncoding
,
response
,
streamContent
);
assertTrue
(
"shouldFilter returned false"
,
filter
.
shouldFilter
());
filter
.
run
();
String
encoding
=
RequestContext
.
getCurrentContext
().
getResponse
().
getCharacterEncoding
();
String
expectedEncoding
=
characterEncoding
!=
null
?
characterEncoding
:
WebUtils
.
DEFAULT_CHARACTER_ENCODING
;
assertThat
(
"wrong character encoding"
,
encoding
,
equalTo
(
expectedEncoding
));
assertThat
(
"wrong content"
,
response
.
getContentAsString
(),
equalTo
(
content
));
}
private
SendResponseFilter
createFilter
(
String
content
,
String
characterEncoding
,
MockHttpServletResponse
response
,
boolean
streamContent
)
throws
Exception
{
HttpServletRequest
request
=
new
MockHttpServletRequest
();
RequestContext
context
=
new
RequestContext
();
context
.
setRequest
(
request
);
context
.
setResponse
(
response
);
if
(
characterEncoding
!=
null
)
{
response
.
setCharacterEncoding
(
characterEncoding
);
}
if
(
streamContent
)
{
context
.
setResponseDataStream
(
new
ByteArrayInputStream
(
content
.
getBytes
(
characterEncoding
)));
}
else
{
context
.
setResponseBody
(
content
);
}
context
.
addZuulResponseHeader
(
HttpHeaders
.
CONTENT_LENGTH
,
String
.
valueOf
(
content
.
length
()));
context
.
set
(
"error.status_code"
,
HttpStatus
.
NOT_FOUND
.
value
());
RequestContext
.
testSetCurrentContext
(
context
);
SendResponseFilter
filter
=
new
SendResponseFilter
();
return
filter
;
}
}
\ 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