Skip to content

Commit

Permalink
Allow JspServlet's init parameters to be configured via the environment
Browse files Browse the repository at this point in the history
This commit adds support for configuring the JSP servlet’s init
parameters via the environment using server.jsp-servlet.init-parameters.*.
As part of this change the configuration of registerJspServlet and
jspServletClassName have been moved onto a new type, JspServlet, and the
existing setters on ConfigurableEmbeddedServletContainer have been 
deprecated. In addition to providing a model for configuring the JSP
servlet that’s consistent with the model for other configuration (SSL,
for example), this change also means that the class name and whether or
not the servlet is registered at all can now also be configured via the
environment.

Closes spring-projectsgh-2825
  • Loading branch information
wilkinsona committed May 12, 2015
1 parent 63a7b24 commit 18453c0
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.InitParameterConfiguringServletContextInitializer;
import org.springframework.boot.context.embedded.JspServlet;
import org.springframework.boot.context.embedded.Ssl;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
Expand Down Expand Up @@ -92,6 +93,9 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord

private final Undertow undertow = new Undertow();

@NestedConfigurationProperty
private JspServlet jspServlet;

/**
* ServletContext parameters.
*/
Expand All @@ -110,6 +114,10 @@ public Undertow getUndertow() {
return this.undertow;
}

public JspServlet jspServlet() {
return this.jspServlet;
}

public String getContextPath() {
return this.contextPath;
}
Expand Down Expand Up @@ -182,6 +190,14 @@ public void setSsl(Ssl ssl) {
this.ssl = ssl;
}

public JspServlet getJspServlet() {
return this.jspServlet;
}

public void setJspServlet(JspServlet jspServlet) {
this.jspServlet = jspServlet;
}

public Map<String, String> getContextParameters() {
return this.contextParameters;
}
Expand All @@ -207,6 +223,9 @@ public void customize(ConfigurableEmbeddedServletContainer container) {
if (getSsl() != null) {
container.setSsl(getSsl());
}
if (getJspServlet() != null) {
container.setJspServlet(getJspServlet());
}
if (container instanceof TomcatEmbeddedServletContainerFactory) {
getTomcat()
.customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
Expand Down Expand Up @@ -436,6 +455,13 @@ public void customize(Context context) {
}
});

factory.addContextCustomizers(new TomcatContextCustomizer() {
@Override
public void customize(Context context) {
context.setBackgroundProcessorDelay(Tomcat.this.backgroundProcessorDelay);
}
});

String remoteIpHeader = getRemoteIpHeader();
String protocolHeader = getProtocolHeader();
if (StringUtils.hasText(remoteIpHeader)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-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.
Expand All @@ -26,12 +26,14 @@
import java.util.concurrent.TimeUnit;

import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Abstract base class for {@link ConfigurableEmbeddedServletContainer} implementations.
*
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
* @see AbstractEmbeddedServletContainerFactory
*/
public abstract class AbstractConfigurableEmbeddedServletContainer implements
Expand All @@ -44,10 +46,6 @@ public abstract class AbstractConfigurableEmbeddedServletContainer implements

private boolean registerDefaultServlet = true;

private boolean registerJspServlet = true;

private String jspServletClassName = "org.apache.jasper.servlet.JspServlet";

private int port = 8080;

private List<ServletContextInitializer> initializers = new ArrayList<ServletContextInitializer>();
Expand All @@ -64,6 +62,8 @@ public abstract class AbstractConfigurableEmbeddedServletContainer implements

private Ssl ssl;

private JspServlet jspServlet = new JspServlet();

/**
* Create a new {@link AbstractConfigurableEmbeddedServletContainer} instance.
*/
Expand Down Expand Up @@ -228,18 +228,10 @@ public void setRegisterDefaultServlet(boolean registerDefaultServlet) {
this.registerDefaultServlet = registerDefaultServlet;
}

/**
* Flag to indicate that the JSP servlet should be registered if available on the
* classpath.
* @return true if the JSP servlet is to be registered
*/
public boolean isRegisterJspServlet() {
return this.registerJspServlet;
}

@Override
public void setRegisterJspServlet(boolean registerJspServlet) {
this.registerJspServlet = registerJspServlet;
Assert.notNull(this.jspServlet);
this.jspServlet.setRegistered(registerJspServlet);
}

/**
Expand All @@ -261,14 +253,17 @@ public Ssl getSsl() {

@Override
public void setJspServletClassName(String jspServletClassName) {
this.jspServletClassName = jspServletClassName;
Assert.notNull(this.jspServlet);
this.jspServlet.setClassName(jspServletClassName);
}

/**
* @return the JSP servlet class name
*/
protected String getJspServletClassName() {
return this.jspServletClassName;
@Override
public void setJspServlet(JspServlet jspServlet) {
this.jspServlet = jspServlet;
}

public JspServlet getJspServlet() {
return this.jspServlet;
}

/**
Expand All @@ -287,4 +282,16 @@ protected final ServletContextInitializer[] mergeInitializers(
.toArray(new ServletContextInitializer[mergedInitializers.size()]);
}

/**
* Returns whether or not he JSP servlet should be registered with the embedded
* container.
* @return {@code true} if the container should be registered, otherwise {@code false}
*/
protected boolean shouldRegisterJspServlet() {
return this.jspServlet != null
&& this.jspServlet.getRegistered()
&& ClassUtils.isPresent(this.jspServlet.getClassName(), getClass()
.getClassLoader());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* {@link EmbeddedServletContainerFactory}.
*
* @author Dave Syer
* @author Andy Wilkinson
* @see EmbeddedServletContainerFactory
* @see EmbeddedServletContainerCustomizer
*/
Expand Down Expand Up @@ -76,15 +77,23 @@ public interface ConfigurableEmbeddedServletContainer {
* Tomcat and Jetty use Jasper for their JSP implementation the default is
* <code>org.apache.jasper.servlet.JspServlet</code>.
* @param jspServletClassName the class name for the JSP servlet if used
* @deprecated in 1.3.0 in favor of {@link JspServlet#setClassName(String)}
* @see #setJspServlet
* @see JspServlet#setClassName(String)
*/
@Deprecated
void setJspServletClassName(String jspServletClassName);

/**
* Set if the JspServlet should be registered if it is on the classpath. Defaults to
* {@code true} so that files from the {@link #setDocumentRoot(File) document root}
* will be served.
* @param registerJspServlet if the JSP servlet should be registered
* @deprecated in 1.3.0 in favor of {@link JspServlet#setRegistered(boolean)}
* @see #setJspServlet
* @see JspServlet#setRegistered(boolean)
*/
@Deprecated
void setRegisterJspServlet(boolean registerJspServlet);

/**
Expand Down Expand Up @@ -146,4 +155,10 @@ public interface ConfigurableEmbeddedServletContainer {
*/
void setSsl(Ssl ssl);

/**
* Sets the configuration that will be applied to the container's JSP servlet
* @param jspServlet the JSP servlet configuration
*/
void setJspServlet(JspServlet jspServlet);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2012-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.boot.context.embedded;

import java.util.HashMap;
import java.util.Map;

/**
* Configuration for the container's JSP servlet.
*
* @author Andy Wilkinson
* @since 1.3.0
*/
public class JspServlet {

/**
* The class name of the servlet to use for JSPs. If registered is true and this class
* is on the classpath then it will be registered. Since both Tomcat and Jetty use
* Jasper for their JSP implementation the default is
* org.apache.jasper.servlet.JspServlet.
*/
private String className = "org.apache.jasper.servlet.JspServlet";

/**
* Init parameters use to configure the JSP servlet.
*/
private Map<String, String> initParameters = new HashMap<String, String>();

/**
* Whether or not the JSP servlet should be registered with the embedded servlet
* container.
*/
private boolean registered = true;

public String getClassName() {
return this.className;
}

public void setClassName(String className) {
this.className = className;
}

public Map<String, String> getInitParameters() {
return this.initParameters;
}

public void setInitParameters(Map<String, String> initParameters) {
this.initParameters = initParameters;
}

public boolean getRegistered() {
return this.registered;
}

public void setRegistered(boolean registered) {
this.registered = registered;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ protected final void configureWebAppContext(WebAppContext context,
if (isRegisterDefaultServlet()) {
addDefaultServlet(context);
}
if (isRegisterJspServlet()
&& ClassUtils.isPresent(getJspServletClassName(), getClass()
.getClassLoader())) {
if (shouldRegisterJspServlet()) {
addJspServlet(context);
}
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
Expand Down Expand Up @@ -297,8 +295,9 @@ protected final void addJspServlet(WebAppContext context) {
Assert.notNull(context, "Context must not be null");
ServletHolder holder = new ServletHolder();
holder.setName("jsp");
holder.setClassName(getJspServletClassName());
holder.setClassName(getJspServlet().getClassName());
holder.setInitParameter("fork", "false");
holder.setInitParameters(getJspServlet().getInitParameters());
holder.setInitOrder(3);
context.getServletHandler().addServlet(holder);
ServletMapping mapping = new ServletMapping();
Expand Down Expand Up @@ -378,7 +377,7 @@ protected void postProcessWebAppContext(WebAppContext webAppContext) {
}

/**
* Factory method called to create the {@link JettyEmbeddedServletContainer}.
* Factory method called to create the {@link JettyEmbeddedServletContainer} .
* Subclasses can override this method to return a different
* {@link JettyEmbeddedServletContainer} or apply additional processing to the Jetty
* server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -173,9 +174,7 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
if (isRegisterDefaultServlet()) {
addDefaultServlet(context);
}
if (isRegisterJspServlet()
&& ClassUtils.isPresent(getJspServletClassName(), getClass()
.getClassLoader())) {
if (shouldRegisterJspServlet()) {
addJspServlet(context);
addJasperInitializer(context);
context.addLifecycleListener(new StoreMergedWebXmlListener());
Expand All @@ -202,8 +201,12 @@ private void addDefaultServlet(Context context) {
private void addJspServlet(Context context) {
Wrapper jspServlet = context.createWrapper();
jspServlet.setName("jsp");
jspServlet.setServletClass(getJspServletClassName());
jspServlet.setServletClass(getJspServlet().getClassName());
jspServlet.addInitParameter("fork", "false");
for (Entry<String, String> initParameter : getJspServlet().getInitParameters()
.entrySet()) {
jspServlet.addInitParameter(initParameter.getKey(), initParameter.getValue());
}
jspServlet.setLoadOnStartup(3);
context.addChild(jspServlet);
context.addServletMapping("*.jsp", "jsp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class UndertowEmbeddedServletContainerFactory extends
*/
public UndertowEmbeddedServletContainerFactory() {
super();
setRegisterJspServlet(false);
getJspServlet().setRegistered(false);
}

/**
Expand All @@ -120,7 +120,7 @@ public UndertowEmbeddedServletContainerFactory() {
*/
public UndertowEmbeddedServletContainerFactory(int port) {
super(port);
setRegisterJspServlet(false);
getJspServlet().setRegistered(false);
}

/**
Expand All @@ -131,7 +131,7 @@ public UndertowEmbeddedServletContainerFactory(int port) {
*/
public UndertowEmbeddedServletContainerFactory(String contextPath, int port) {
super(contextPath, port);
setRegisterJspServlet(false);
getJspServlet().setRegistered(false);
}

/**
Expand Down Expand Up @@ -441,12 +441,6 @@ public void setDirectBuffers(Boolean directBuffers) {
this.directBuffers = directBuffers;
}

@Override
public void setRegisterJspServlet(boolean registerJspServlet) {
Assert.isTrue(!registerJspServlet, "Undertow does not support JSPs");
super.setRegisterJspServlet(registerJspServlet);
}

/**
* Undertow {@link ResourceManager} for JAR resources.
*/
Expand Down
Loading

0 comments on commit 18453c0

Please sign in to comment.