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
e6f2db70
Commit
e6f2db70
authored
Jan 27, 2017
by
Eloi
Committed by
Spencer Gibb
Jan 27, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Maintain content type for data parts in multipart requests (#1614)
maintain multipart content type for data parts Fixes gh-1581
parent
65829c43
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
181 additions
and
98 deletions
+181
-98
FormBodyWrapperFilter.java
...cloud/netflix/zuul/filters/pre/FormBodyWrapperFilter.java
+3
-50
RequestContentDataExtractor.java
.../cloud/netflix/zuul/util/RequestContentDataExtractor.java
+97
-0
FormZuulProxyApplicationTests.java
...ork/cloud/netflix/zuul/FormZuulProxyApplicationTests.java
+81
-48
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/pre/FormBodyWrapperFilter.java
View file @
e6f2db70
...
...
@@ -30,21 +30,15 @@ import javax.servlet.ServletRequest;
import
javax.servlet.ServletRequestWrapper
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.cloud.netflix.zuul.util.RequestContentDataExtractor
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpOutputMessage
;
import
org.springframework.http.InvalidMediaTypeException
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter
;
import
org.springframework.util.Assert
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartRequest
;
import
org.springframework.web.servlet.DispatcherServlet
;
import
com.netflix.zuul.ZuulFilter
;
...
...
@@ -184,36 +178,9 @@ public class FormBodyWrapperFilter extends ZuulFilter {
private
synchronized
void
buildContentData
()
{
try
{
MultiValueMap
<
String
,
Object
>
builder
=
new
LinkedMultiValueMap
<
String
,
Object
>();
Set
<
String
>
queryParams
=
findQueryParams
();
for
(
Entry
<
String
,
String
[]>
entry
:
this
.
request
.
getParameterMap
()
.
entrySet
())
{
if
(!
queryParams
.
contains
(
entry
.
getKey
()))
{
for
(
String
value
:
entry
.
getValue
())
{
builder
.
add
(
entry
.
getKey
(),
value
);
}
}
}
if
(
this
.
request
instanceof
MultipartRequest
)
{
MultipartRequest
multi
=
(
MultipartRequest
)
this
.
request
;
for
(
Entry
<
String
,
List
<
MultipartFile
>>
parts
:
multi
.
getMultiFileMap
().
entrySet
())
{
for
(
MultipartFile
file
:
parts
.
getValue
())
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentDispositionFormData
(
file
.
getName
(),
file
.
getOriginalFilename
());
if
(
file
.
getContentType
()
!=
null
)
{
headers
.
setContentType
(
MediaType
.
valueOf
(
file
.
getContentType
()));
}
HttpEntity
<
Resource
>
entity
=
new
HttpEntity
<
Resource
>(
new
InputStreamResource
(
file
.
getInputStream
()),
headers
);
builder
.
add
(
parts
.
getKey
(),
entity
);
}
}
}
MultiValueMap
<
String
,
Object
>
builder
=
RequestContentDataExtractor
.
extract
(
this
.
request
);
FormHttpOutputMessage
data
=
new
FormHttpOutputMessage
();
this
.
contentType
=
MediaType
.
valueOf
(
this
.
request
.
getContentType
());
data
.
getHeaders
().
setContentType
(
this
.
contentType
);
this
.
converter
.
write
(
builder
,
this
.
contentType
,
data
);
...
...
@@ -227,20 +194,6 @@ public class FormBodyWrapperFilter extends ZuulFilter {
}
}
private
Set
<
String
>
findQueryParams
()
{
Set
<
String
>
result
=
new
HashSet
<>();
String
query
=
this
.
request
.
getQueryString
();
if
(
query
!=
null
)
{
for
(
String
value
:
StringUtils
.
tokenizeToStringArray
(
query
,
"&"
))
{
if
(
value
.
contains
(
"="
))
{
value
=
value
.
substring
(
0
,
value
.
indexOf
(
"="
));
}
result
.
add
(
value
);
}
}
return
result
;
}
private
class
FormHttpOutputMessage
implements
HttpOutputMessage
{
private
HttpHeaders
headers
=
new
HttpHeaders
();
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/util/RequestContentDataExtractor.java
0 → 100644
View file @
e6f2db70
package
org
.
springframework
.
cloud
.
netflix
.
zuul
.
util
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.IOException
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map.Entry
;
import
java.util.Set
;
public
class
RequestContentDataExtractor
{
public
static
MultiValueMap
<
String
,
Object
>
extract
(
HttpServletRequest
request
)
throws
IOException
{
return
(
request
instanceof
MultipartHttpServletRequest
)
?
extractFromMultipartRequest
((
MultipartHttpServletRequest
)
request
)
:
extractFromRequest
(
request
);
}
private
static
MultiValueMap
<
String
,
Object
>
extractFromRequest
(
HttpServletRequest
request
)
throws
IOException
{
MultiValueMap
<
String
,
Object
>
builder
=
new
LinkedMultiValueMap
<>();
Set
<
String
>
queryParams
=
findQueryParams
(
request
);
for
(
Entry
<
String
,
String
[]>
entry
:
request
.
getParameterMap
().
entrySet
())
{
String
key
=
entry
.
getKey
();
if
(!
queryParams
.
contains
(
key
))
{
for
(
String
value
:
entry
.
getValue
())
{
builder
.
add
(
key
,
value
);
}
}
}
return
builder
;
}
private
static
MultiValueMap
<
String
,
Object
>
extractFromMultipartRequest
(
MultipartHttpServletRequest
request
)
throws
IOException
{
MultiValueMap
<
String
,
Object
>
builder
=
new
LinkedMultiValueMap
<>();
Set
<
String
>
queryParams
=
findQueryParams
(
request
);
for
(
Entry
<
String
,
String
[]>
entry
:
request
.
getParameterMap
().
entrySet
())
{
String
key
=
entry
.
getKey
();
if
(!
queryParams
.
contains
(
key
))
{
for
(
String
value
:
entry
.
getValue
())
{
HttpHeaders
headers
=
new
HttpHeaders
();
String
type
=
request
.
getMultipartContentType
(
key
);
if
(
type
!=
null
)
{
headers
.
setContentType
(
MediaType
.
valueOf
(
type
));
}
builder
.
add
(
key
,
new
HttpEntity
<>(
value
,
headers
));
}
}
}
for
(
Entry
<
String
,
List
<
MultipartFile
>>
parts
:
request
.
getMultiFileMap
().
entrySet
())
{
for
(
MultipartFile
file
:
parts
.
getValue
())
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentDispositionFormData
(
file
.
getName
(),
file
.
getOriginalFilename
());
if
(
file
.
getContentType
()
!=
null
)
{
headers
.
setContentType
(
MediaType
.
valueOf
(
file
.
getContentType
()));
}
HttpEntity
entity
=
new
HttpEntity
<>(
new
InputStreamResource
(
file
.
getInputStream
()),
headers
);
builder
.
add
(
parts
.
getKey
(),
entity
);
}
}
return
builder
;
}
private
static
Set
<
String
>
findQueryParams
(
HttpServletRequest
request
)
{
Set
<
String
>
result
=
new
HashSet
<>();
String
query
=
request
.
getQueryString
();
if
(
query
!=
null
)
{
for
(
String
value
:
StringUtils
.
tokenizeToStringArray
(
query
,
"&"
))
{
if
(
value
.
contains
(
"="
))
{
value
=
value
.
substring
(
0
,
value
.
indexOf
(
"="
));
}
result
.
add
(
value
);
}
}
return
result
;
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulProxyApplicationTests.java
View file @
e6f2db70
This diff is collapsed.
Click to expand it.
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