Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
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
DEMO
spring-boot
Commits
bc460392
Commit
bc460392
authored
Jul 22, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Configure Jetty to compress responses to requests other than GET
Closes gh-8184
parent
6df1be3f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
49 deletions
+36
-49
JettyServletWebServerFactory.java
...boot/web/embedded/jetty/JettyServletWebServerFactory.java
+6
-2
JettyServletWebServerFactoryTests.java
...web/embedded/jetty/JettyServletWebServerFactoryTests.java
+0
-41
AbstractServletWebServerFactoryTests.java
.../servlet/server/AbstractServletWebServerFactoryTests.java
+30
-6
No files found.
spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java
View file @
bc460392
...
...
@@ -32,6 +32,7 @@ import javax.servlet.ServletException;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.eclipse.jetty.http.HttpVersion
;
import
org.eclipse.jetty.http.MimeTypes
;
import
org.eclipse.jetty.server.AbstractConnector
;
...
...
@@ -229,6 +230,9 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
Compression
compression
=
getCompression
();
handler
.
setMinGzipSize
(
compression
.
getMinResponseSize
());
handler
.
setIncludedMimeTypes
(
compression
.
getMimeTypes
());
for
(
HttpMethod
httpMethod
:
HttpMethod
.
values
())
{
handler
.
addIncludedMethods
(
httpMethod
.
name
());
}
if
(
compression
.
getExcludedUserAgents
()
!=
null
)
{
handler
.
setExcludedAgentPatterns
(
compression
.
getExcludedUserAgents
());
}
...
...
@@ -581,8 +585,8 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
}
/**
* Returns a mutable collection of Jetty {@link JettyServerCustomizer}s that will be
applied
* to the {@link Server} before the it is created.
* Returns a mutable collection of Jetty {@link JettyServerCustomizer}s that will be
*
applied
to the {@link Server} before the it is created.
* @return the {@link JettyServerCustomizer}s
*/
public
Collection
<
JettyServerCustomizer
>
getServerCustomizers
()
{
...
...
spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java
View file @
bc460392
...
...
@@ -16,18 +16,12 @@
package
org
.
springframework
.
boot
.
web
.
embedded
.
jetty
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.jasper.servlet.JspServlet
;
import
org.eclipse.jetty.server.Handler
;
import
org.eclipse.jetty.server.Server
;
...
...
@@ -43,13 +37,10 @@ import org.eclipse.jetty.webapp.WebAppContext;
import
org.junit.Test
;
import
org.mockito.InOrder
;
import
org.springframework.boot.web.server.Compression
;
import
org.springframework.boot.web.server.PortInUseException
;
import
org.springframework.boot.web.server.Ssl
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory
;
import
org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests
;
import
org.springframework.http.HttpHeaders
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
hamcrest
.
CoreMatchers
.
isA
;
...
...
@@ -296,38 +287,6 @@ public class JettyServletWebServerFactoryTests
factory
.
getWebServer
().
start
();
}
@Override
@SuppressWarnings
(
"serial"
)
// Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646
protected
String
setUpFactoryForCompression
(
final
int
contentSize
,
String
[]
mimeTypes
,
String
[]
excludedUserAgents
)
throws
Exception
{
char
[]
chars
=
new
char
[
contentSize
];
Arrays
.
fill
(
chars
,
'F'
);
final
String
testContent
=
new
String
(
chars
);
AbstractServletWebServerFactory
factory
=
getFactory
();
Compression
compression
=
new
Compression
();
compression
.
setEnabled
(
true
);
if
(
mimeTypes
!=
null
)
{
compression
.
setMimeTypes
(
mimeTypes
);
}
if
(
excludedUserAgents
!=
null
)
{
compression
.
setExcludedUserAgents
(
excludedUserAgents
);
}
factory
.
setCompression
(
compression
);
this
.
webServer
=
factory
.
getWebServer
(
new
ServletRegistrationBean
<
HttpServlet
>(
new
HttpServlet
()
{
@Override
protected
void
doGet
(
HttpServletRequest
req
,
HttpServletResponse
resp
)
throws
ServletException
,
IOException
{
resp
.
setContentLength
(
contentSize
);
resp
.
setHeader
(
HttpHeaders
.
CONTENT_TYPE
,
"text/plain"
);
resp
.
getWriter
().
print
(
testContent
);
}
},
"/test.txt"
));
this
.
webServer
.
start
();
return
testContent
;
}
@Override
protected
JspServlet
getJspServlet
()
throws
Exception
{
WebAppContext
context
=
(
WebAppContext
)
((
JettyWebServer
)
this
.
webServer
)
...
...
spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java
View file @
bc460392
...
...
@@ -58,7 +58,9 @@ import javax.servlet.ServletContext;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
org.apache.http.client.HttpClient
;
...
...
@@ -775,10 +777,15 @@ public abstract class AbstractServletWebServerFactoryTests {
}
@Test
public
void
compression
()
throws
Exception
{
public
void
compression
OfResposeToGetRequest
()
throws
Exception
{
assertThat
(
doTestCompression
(
10000
,
null
,
null
)).
isTrue
();
}
@Test
public
void
compressionOfResposeToPostRequest
()
throws
Exception
{
assertThat
(
doTestCompression
(
10000
,
null
,
null
,
HttpMethod
.
POST
)).
isTrue
();
}
@Test
public
void
noCompressionForSmallResponse
()
throws
Exception
{
assertThat
(
doTestCompression
(
100
,
null
,
null
)).
isFalse
();
...
...
@@ -991,12 +998,18 @@ public abstract class AbstractServletWebServerFactoryTests {
private
boolean
doTestCompression
(
int
contentSize
,
String
[]
mimeTypes
,
String
[]
excludedUserAgents
)
throws
Exception
{
return
doTestCompression
(
contentSize
,
mimeTypes
,
excludedUserAgents
,
HttpMethod
.
GET
);
}
private
boolean
doTestCompression
(
int
contentSize
,
String
[]
mimeTypes
,
String
[]
excludedUserAgents
,
HttpMethod
method
)
throws
Exception
{
String
testContent
=
setUpFactoryForCompression
(
contentSize
,
mimeTypes
,
excludedUserAgents
);
TestGzipInputStreamFactory
inputStreamFactory
=
new
TestGzipInputStreamFactory
();
Map
<
String
,
InputStreamFactory
>
contentDecoderMap
=
Collections
.
singletonMap
(
"gzip"
,
(
InputStreamFactory
)
inputStreamFactory
);
String
response
=
getResponse
(
getLocalUrl
(
"/test.txt"
),
String
response
=
getResponse
(
getLocalUrl
(
"/test.txt"
),
method
,
new
HttpComponentsClientHttpRequestFactory
(
HttpClientBuilder
.
create
().
setUserAgent
(
"testUserAgent"
)
.
setContentDecoderRegistry
(
contentDecoderMap
).
build
()));
...
...
@@ -1004,15 +1017,12 @@ public abstract class AbstractServletWebServerFactoryTests {
return
inputStreamFactory
.
wasCompressionUsed
();
}
pr
otected
String
setUpFactoryForCompression
(
int
contentSize
,
String
[]
mimeTypes
,
pr
ivate
String
setUpFactoryForCompression
(
int
contentSize
,
String
[]
mimeTypes
,
String
[]
excludedUserAgents
)
throws
Exception
{
char
[]
chars
=
new
char
[
contentSize
];
Arrays
.
fill
(
chars
,
'F'
);
String
testContent
=
new
String
(
chars
);
AbstractServletWebServerFactory
factory
=
getFactory
();
FileCopyUtils
.
copy
(
testContent
,
new
FileWriter
(
this
.
temporaryFolder
.
newFile
(
"test.txt"
)));
factory
.
setDocumentRoot
(
this
.
temporaryFolder
.
getRoot
());
Compression
compression
=
new
Compression
();
compression
.
setEnabled
(
true
);
if
(
mimeTypes
!=
null
)
{
...
...
@@ -1022,6 +1032,20 @@ public abstract class AbstractServletWebServerFactoryTests {
compression
.
setExcludedUserAgents
(
excludedUserAgents
);
}
factory
.
setCompression
(
compression
);
factory
.
addInitializers
(
new
ServletRegistrationBean
<
HttpServlet
>(
new
HttpServlet
()
{
@Override
protected
void
service
(
HttpServletRequest
req
,
HttpServletResponse
resp
)
throws
ServletException
,
IOException
{
resp
.
setContentType
(
"text/plain"
);
resp
.
setContentLength
(
testContent
.
length
());
resp
.
getWriter
().
write
(
testContent
);
resp
.
getWriter
().
flush
();
}
},
"/test.txt"
));
this
.
webServer
=
factory
.
getWebServer
();
this
.
webServer
.
start
();
return
testContent
;
...
...
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