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
018231d2
Commit
018231d2
authored
Jun 17, 2013
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
7bf3e352
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
68 deletions
+77
-68
MultipartAutoConfiguration.java
...otstrap/autoconfigure/web/MultipartAutoConfiguration.java
+8
-8
EmbeddedWebApplicationContext.java
...strap/context/embedded/EmbeddedWebApplicationContext.java
+13
-17
ServletRegistrationBean.java
...k/bootstrap/context/embedded/ServletRegistrationBean.java
+21
-10
MultipartAutoConfigurationTests.java
...ap/autoconfigure/web/MultipartAutoConfigurationTests.java
+35
-33
No files found.
spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/web/MultipartAutoConfiguration.java
View file @
018231d2
...
@@ -13,31 +13,31 @@
...
@@ -13,31 +13,31 @@
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
*/
package
org
.
springframework
.
bootstrap
.
autoconfigure
.
web
;
package
org
.
springframework
.
bootstrap
.
autoconfigure
.
web
;
import
javax.servlet.MultipartConfigElement
;
import
javax.servlet.MultipartConfigElement
;
import
org.springframework.bootstrap.context.annotation.ConditionalOnBean
;
import
org.springframework.bootstrap.context.annotation.ConditionalOnBean
;
import
org.springframework.bootstrap.context.annotation.EnableAutoConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.multipart.support.StandardServletMultipartResolver
;
import
org.springframework.web.multipart.support.StandardServletMultipartResolver
;
/**
/**
*
Autoconfiguration for multipart uploads. It detects the existence of a
*
{@link EnableAutoConfiguration Auto-configuration} for multi-part uploads. It detects
*
{@link MultipartConfigElement} in the app context and then adds critical bean
s
*
the existence of a {@link MultipartConfigElement} in the app context and then add
s
* while also autowiring it into the Jetty/Tomcat embedded containers.
*
critical beans
while also autowiring it into the Jetty/Tomcat embedded containers.
*
*
* @author Greg Turnquist
* @author Greg Turnquist
*
*/
*/
@Configuration
@Configuration
public
class
MultipartAutoConfiguration
{
public
class
MultipartAutoConfiguration
{
@ConditionalOnBean
(
MultipartConfigElement
.
class
)
@Bean
@Bean
@ConditionalOnBean
(
MultipartConfigElement
.
class
)
public
StandardServletMultipartResolver
multipartResolver
()
{
public
StandardServletMultipartResolver
multipartResolver
()
{
System
.
out
.
println
(
"Loading up a MultipartResolver!!!"
);
return
new
StandardServletMultipartResolver
();
return
new
StandardServletMultipartResolver
();
}
}
}
}
spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java
View file @
018231d2
...
@@ -23,7 +23,6 @@ import java.util.Comparator;
...
@@ -23,7 +23,6 @@ import java.util.Comparator;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.LinkedHashSet
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -210,13 +209,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
...
@@ -210,13 +209,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
}
}
initializers
.
add
(
initializer
);
initializers
.
add
(
initializer
);
}
}
Map
<
String
,
MultipartConfigElement
>
multipartConfigBeans
;
MultipartConfigElement
multipartConfigElement
=
null
;
multipartConfigBeans
=
getBeanFactory
().
getBeansOfType
(
MultipartConfigElement
.
class
);
for
(
MultipartConfigElement
bean
:
multipartConfigBeans
.
values
())
{
multipartConfigElement
=
bean
;
}
List
<
Entry
<
String
,
Servlet
>>
servletBeans
=
getOrderedBeansOfType
(
Servlet
.
class
);
List
<
Entry
<
String
,
Servlet
>>
servletBeans
=
getOrderedBeansOfType
(
Servlet
.
class
);
for
(
Entry
<
String
,
Servlet
>
servletBean
:
servletBeans
)
{
for
(
Entry
<
String
,
Servlet
>
servletBean
:
servletBeans
)
{
...
@@ -229,15 +221,11 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
...
@@ -229,15 +221,11 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
if
(
name
.
equals
(
DISPATCHER_SERVLET_NAME
))
{
if
(
name
.
equals
(
DISPATCHER_SERVLET_NAME
))
{
url
=
"/"
;
// always map the main dispatcherServlet to "/"
url
=
"/"
;
// always map the main dispatcherServlet to "/"
}
}
if
(
multipartConfigElement
!=
null
)
{
ServletRegistrationBean
registration
=
new
ServletRegistrationBean
(
servlet
,
initializers
.
add
(
new
ServletRegistrationBean
(
servlet
,
multipartConfigElement
,
url
)
{{
url
);
setName
(
name
);
registration
.
setName
(
name
);
}});
registration
.
setMultipartConfig
(
getMultipartConfig
());
}
else
{
initializers
.
add
(
registration
);
initializers
.
add
(
new
ServletRegistrationBean
(
servlet
,
url
)
{{
setName
(
name
);
}});
}
}
}
for
(
Entry
<
String
,
Filter
>
filterBean
:
getOrderedBeansOfType
(
Filter
.
class
))
{
for
(
Entry
<
String
,
Filter
>
filterBean
:
getOrderedBeansOfType
(
Filter
.
class
))
{
...
@@ -254,6 +242,14 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
...
@@ -254,6 +242,14 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
return
initializers
;
return
initializers
;
}
}
private
MultipartConfigElement
getMultipartConfig
()
{
List
<
Entry
<
String
,
MultipartConfigElement
>>
beans
=
getOrderedBeansOfType
(
MultipartConfigElement
.
class
);
if
(
beans
.
isEmpty
())
{
return
null
;
}
return
beans
.
get
(
0
).
getValue
();
}
/**
/**
* Prepare the {@link WebApplicationContext} with the given fully loaded
* Prepare the {@link WebApplicationContext} with the given fully loaded
* {@link ServletContext}. This method is usually called from
* {@link ServletContext}. This method is usually called from
...
...
spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/ServletRegistrationBean.java
View file @
018231d2
...
@@ -56,8 +56,8 @@ public class ServletRegistrationBean extends RegistrationBean {
...
@@ -56,8 +56,8 @@ public class ServletRegistrationBean extends RegistrationBean {
private
int
loadOnStartup
=
1
;
private
int
loadOnStartup
=
1
;
private
Set
<
Filter
>
filters
=
new
LinkedHashSet
<
Filter
>();
private
Set
<
Filter
>
filters
=
new
LinkedHashSet
<
Filter
>();
private
MultipartConfigElement
multipartConfig
Element
=
null
;
private
MultipartConfigElement
multipartConfig
;
/**
/**
* Create a new {@link ServletRegistrationBean} instance.
* Create a new {@link ServletRegistrationBean} instance.
...
@@ -67,7 +67,7 @@ public class ServletRegistrationBean extends RegistrationBean {
...
@@ -67,7 +67,7 @@ public class ServletRegistrationBean extends RegistrationBean {
/**
/**
* Create a new {@link ServletRegistrationBean} instance with the specified
* Create a new {@link ServletRegistrationBean} instance with the specified
* {@link Servlet} and URL mapping.
* {@link Servlet} and URL mapping
s
.
* @param servlet the servlet being mapped
* @param servlet the servlet being mapped
* @param urlMappings the URLs being mapped
* @param urlMappings the URLs being mapped
*/
*/
...
@@ -75,11 +75,6 @@ public class ServletRegistrationBean extends RegistrationBean {
...
@@ -75,11 +75,6 @@ public class ServletRegistrationBean extends RegistrationBean {
setServlet
(
servlet
);
setServlet
(
servlet
);
addUrlMappings
(
urlMappings
);
addUrlMappings
(
urlMappings
);
}
}
public
ServletRegistrationBean
(
Servlet
servlet
,
MultipartConfigElement
multipartConfigElement
,
String
...
urlMappings
)
{
this
(
servlet
,
urlMappings
);
this
.
multipartConfigElement
=
multipartConfigElement
;
}
/**
/**
* Sets the servlet to be registered.
* Sets the servlet to be registered.
...
@@ -152,6 +147,22 @@ public class ServletRegistrationBean extends RegistrationBean {
...
@@ -152,6 +147,22 @@ public class ServletRegistrationBean extends RegistrationBean {
this
.
filters
.
addAll
(
Arrays
.
asList
(
filters
));
this
.
filters
.
addAll
(
Arrays
.
asList
(
filters
));
}
}
/**
* Set the the {@link MultipartConfigElement multi-part configuration}.
* @param multipartConfig the muti-part configuration to set or {@code null}
*/
public
void
setMultipartConfig
(
MultipartConfigElement
multipartConfig
)
{
this
.
multipartConfig
=
multipartConfig
;
}
/**
* Returns the {@link MultipartConfigElement multi-part configuration} to be applied
* or {@code null}.
*/
public
MultipartConfigElement
getMultipartConfig
()
{
return
this
.
multipartConfig
;
}
/**
/**
* Returns the servlet name that will be registered.
* Returns the servlet name that will be registered.
*/
*/
...
@@ -189,8 +200,8 @@ public class ServletRegistrationBean extends RegistrationBean {
...
@@ -189,8 +200,8 @@ public class ServletRegistrationBean extends RegistrationBean {
}
}
registration
.
addMapping
(
urlMapping
);
registration
.
addMapping
(
urlMapping
);
registration
.
setLoadOnStartup
(
this
.
loadOnStartup
);
registration
.
setLoadOnStartup
(
this
.
loadOnStartup
);
if
(
multipartConfigElement
!=
null
)
{
if
(
this
.
multipartConfig
!=
null
)
{
registration
.
setMultipartConfig
(
multipartConfigElement
);
registration
.
setMultipartConfig
(
this
.
multipartConfig
);
}
}
}
}
}
}
spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/MultipartAutoConfigurationTests.java
View file @
018231d2
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
*/
package
org
.
springframework
.
bootstrap
.
autoconfigure
.
web
;
package
org
.
springframework
.
bootstrap
.
autoconfigure
.
web
;
import
javax.servlet.MultipartConfigElement
;
import
javax.servlet.MultipartConfigElement
;
...
@@ -40,8 +41,8 @@ import static org.junit.Assert.assertNull;
...
@@ -40,8 +41,8 @@ import static org.junit.Assert.assertNull;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertSame
;
/**
/**
*
A series of embedded unit tests, based on an empty configuration, no multipart
*
Tests for {@link MultipartAutoConfiguration}. Tests an empty configuration, no
*
configuration, and a multipart configuration, with both Jetty and Tomcat
.
*
multipart configuration, and a multipart configuration (with both Jetty and Tomcat)
.
*
*
* @author Greg Turnquist
* @author Greg Turnquist
* @author Dave Syer
* @author Dave Syer
...
@@ -51,7 +52,7 @@ public class MultipartAutoConfigurationTests {
...
@@ -51,7 +52,7 @@ public class MultipartAutoConfigurationTests {
private
AnnotationConfigEmbeddedWebApplicationContext
context
;
private
AnnotationConfigEmbeddedWebApplicationContext
context
;
@Rule
@Rule
public
ExpectedException
exceptio
n
=
ExpectedException
.
none
();
public
ExpectedException
throw
n
=
ExpectedException
.
none
();
@After
@After
public
void
close
()
{
public
void
close
()
{
...
@@ -121,8 +122,39 @@ public class MultipartAutoConfigurationTests {
...
@@ -121,8 +122,39 @@ public class MultipartAutoConfigurationTests {
verifyServletWorks
();
verifyServletWorks
();
}
}
@Test
public
void
containerWithAutomatedMultipartJettyConfiguration
()
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
ContainerWithEverythingJetty
.
class
,
EmbeddedServletContainerAutoConfiguration
.
class
,
MultipartAutoConfiguration
.
class
);
this
.
context
.
getBean
(
MultipartConfigElement
.
class
);
assertSame
(
this
.
context
.
getBean
(
DispatcherServlet
.
class
).
getMultipartResolver
(),
this
.
context
.
getBean
(
StandardServletMultipartResolver
.
class
));
verifyServletWorks
();
}
@Test
public
void
containerWithAutomatedMultipartTomcatConfiguration
()
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
ContainerWithEverythingTomcat
.
class
,
EmbeddedServletContainerAutoConfiguration
.
class
,
MultipartAutoConfiguration
.
class
);
this
.
context
.
getBean
(
MultipartConfigElement
.
class
);
assertSame
(
this
.
context
.
getBean
(
DispatcherServlet
.
class
).
getMultipartResolver
(),
this
.
context
.
getBean
(
StandardServletMultipartResolver
.
class
));
verifyServletWorks
();
}
private
void
verifyServletWorks
()
{
RestTemplate
restTemplate
=
new
RestTemplate
();
assertEquals
(
restTemplate
.
getForObject
(
"http://localhost:8080/"
,
String
.
class
),
"Hello"
);
}
@Configuration
@Configuration
public
static
class
ContainerWithNoMultipartTomcat
{
public
static
class
ContainerWithNoMultipartTomcat
{
@Bean
@Bean
TomcatEmbeddedServletContainerFactory
containerFactory
()
{
TomcatEmbeddedServletContainerFactory
containerFactory
()
{
return
new
TomcatEmbeddedServletContainerFactory
();
return
new
TomcatEmbeddedServletContainerFactory
();
...
@@ -134,18 +166,6 @@ public class MultipartAutoConfigurationTests {
...
@@ -134,18 +166,6 @@ public class MultipartAutoConfigurationTests {
}
}
}
}
@Test
public
void
containerWithAutomatedMultipartJettyConfiguration
()
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
ContainerWithEverythingJetty
.
class
,
EmbeddedServletContainerAutoConfiguration
.
class
,
MultipartAutoConfiguration
.
class
);
this
.
context
.
getBean
(
MultipartConfigElement
.
class
);
assertSame
(
this
.
context
.
getBean
(
DispatcherServlet
.
class
).
getMultipartResolver
(),
this
.
context
.
getBean
(
StandardServletMultipartResolver
.
class
));
verifyServletWorks
();
}
@Configuration
@Configuration
public
static
class
ContainerWithEverythingJetty
{
public
static
class
ContainerWithEverythingJetty
{
@Bean
@Bean
...
@@ -164,18 +184,6 @@ public class MultipartAutoConfigurationTests {
...
@@ -164,18 +184,6 @@ public class MultipartAutoConfigurationTests {
}
}
}
}
@Test
public
void
containerWithAutomatedMultipartTomcatConfiguration
()
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
(
ContainerWithEverythingTomcat
.
class
,
EmbeddedServletContainerAutoConfiguration
.
class
,
MultipartAutoConfiguration
.
class
);
this
.
context
.
getBean
(
MultipartConfigElement
.
class
);
assertSame
(
this
.
context
.
getBean
(
DispatcherServlet
.
class
).
getMultipartResolver
(),
this
.
context
.
getBean
(
StandardServletMultipartResolver
.
class
));
verifyServletWorks
();
}
@Configuration
@Configuration
@EnableWebMvc
@EnableWebMvc
public
static
class
ContainerWithEverythingTomcat
{
public
static
class
ContainerWithEverythingTomcat
{
...
@@ -204,10 +212,4 @@ public class MultipartAutoConfigurationTests {
...
@@ -204,10 +212,4 @@ public class MultipartAutoConfigurationTests {
}
}
}
}
private
void
verifyServletWorks
()
{
RestTemplate
restTemplate
=
new
RestTemplate
();
assertEquals
(
restTemplate
.
getForObject
(
"http://localhost:8080/"
,
String
.
class
),
"Hello"
);
}
}
}
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