Skip to content

Commit

Permalink
[PAXWEB-352] Add org.ops4j.pax.web.server.showStacks property to cont…
Browse files Browse the repository at this point in the history
…rol display of stack trace in case of error
  • Loading branch information
jbonofre committed Oct 31, 2019
1 parent 18e1351 commit 2a33ee5
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public interface WebContainerConstants {

String PROPERTY_IDLE_TIMEOUT = "org.ops4j.pax.web.server.idleTimeout";
String PROPERTY_CONNECTOR_IDLE_TIMEOUT = "org.ops4j.pax.web.server.connector.idleTimeout";
String PROPERTY_SHOW_STACKS = "org.ops4j.pax.web.server.showStacks";

/**
* Servlet context attribute containing the bundle context of the bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class HttpServiceContext extends ServletContextHandler {
final WebContainerContext httpContext,
final AccessControlContext accessControllerContext,
final Map<ServletContainerInitializer, Set<Class<?>>> containerInitializers,
URL jettyWebXmlUrl, List<String> virtualHosts) {
URL jettyWebXmlUrl, List<String> virtualHosts, Boolean showStacks) {
super(parent, "/" + contextName, SESSIONS | SECURITY);
LOG.info("registering context {}, with context-name: {}", httpContext,
contextName);
Expand All @@ -128,7 +128,11 @@ class HttpServiceContext extends ServletContextHandler {
_scontext.setAttribute("org.eclipse.jetty.server.session.timer", executorScheduler);

setServletHandler(new HttpServiceServletHandler(httpContext));
setErrorHandler(new ErrorPageErrorHandler());
ErrorPageErrorHandler errorPageErrorHandler = new ErrorPageErrorHandler();
if (showStacks != null) {
errorPageErrorHandler.setShowStacks(showStacks);
}
setErrorHandler(errorPageErrorHandler);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void configureContext(Map<String, Object> attributes,
String sessionPath, String sessionUrl, Boolean sessionCookieHttpOnly,
Boolean sessionCookieSecure, String sessionWorkerName,
Boolean lazyLoad, String storeDirectory,
Integer maxAge);
Integer maxAge, Boolean showStacks);

void removeContext(HttpContext httpContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ public void configureContext(final Map<String, Object> attributes,
final String sessionUrl, final Boolean sessionCookieHttpOnly,
final Boolean sessionCookieSecure, final String workerName,
final Boolean lazyLoad, final String storeDirectory,
final Integer maxAge) {
final Integer maxAge, final Boolean showStacks) {
server.configureContext(attributes, sessionTimeout, sessionCookie,
sessionDomain, sessionPath, sessionUrl, sessionCookieHttpOnly,
sessionCookieSecure, workerName, lazyLoad, storeDirectory, maxAge);
sessionCookieSecure, workerName, lazyLoad, storeDirectory, maxAge, showStacks);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public HttpServiceContext getHandler() {
private String sessionWorkerName;
private Boolean lazyLoad;
private String storeDirectory;
private Boolean showStacks;

private File serverConfigDir;

Expand Down Expand Up @@ -165,7 +166,7 @@ public HandlerCollection getRootHandlerCollection() {
public void configureContext(final Map<String, Object> attributes, final Integer timeout, final String cookie,
final String domain, final String path, final String url, final Boolean cookieHttpOnly,
final Boolean sessionCookieSecure, final String workerName, final Boolean lazy, final String directory,
Integer maxAge) {
Integer maxAge, final Boolean showStacks) {
this.contextAttributes = attributes;
this.sessionTimeout = timeout;
this.sessionCookie = cookie;
Expand All @@ -178,6 +179,7 @@ public void configureContext(final Map<String, Object> attributes, final Integer
lazyLoad = lazy;
this.storeDirectory = directory;
this.sessionCookieMaxAge = maxAge;
this.showStacks = showStacks;
}

HttpServiceContext getContext(final HttpContext httpContext) {
Expand Down Expand Up @@ -294,7 +296,7 @@ private HttpServiceContext addContext(final ContextModel model) {
HttpServiceContext context = new HttpServiceContext(rootCollections, model.getContextParams(),
getContextAttributes(bundleContext), model.getContextName(), model.getHttpContext(),
model.getAccessControllerContext(), model.getContainerInitializers(), model.getJettyWebXmlURL(),
model.getVirtualHosts());
model.getVirtualHosts(), model.isShowStacks());
context.setClassLoader(model.getClassLoader());
Integer modelSessionTimeout = model.getSessionTimeout();
if (modelSessionTimeout == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ public void start() {
configuration.getWorkerName(),
configuration.getSessionLazyLoad(),
configuration.getSessionStoreDirectory(),
configuration.getSessionCookieMaxAge());
configuration.getSessionCookieMaxAge(),
configuration.isShowStacks());

// Configure NCSA RequestLogHandler
if (configuration.isLogNCSAFormatEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public File getDataFile(String filename) {
{
Servlet servlet = new DefaultServlet();
ContextModel contextModel = new ContextModel(httpContext,
testBundle, getClass().getClassLoader());
testBundle, getClass().getClassLoader(), null);
ServletModel servletModel = new ServletModel(contextModel, servlet,
"/", null, null, null);
server.addServlet(servletModel);
Expand All @@ -421,7 +421,7 @@ public File getDataFile(String filename) {

Servlet servlet = new DefaultServlet();
ContextModel contextModel = new ContextModel(httpContext,
testBundle, getClass().getClassLoader());
testBundle, getClass().getClassLoader(), null);
ServletModel servletModel = new ServletModel(contextModel, servlet,
"/", null, null, null);
server.addServlet(servletModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void registrationAndUnregistrationOfTwoServletsThereShouldBeNoContexts()
try {
Bundle testBundle = mock(Bundle.class);
ContextModel contextModel = new ContextModel(httpContextMock,
testBundle, getClass().getClassLoader());
testBundle, getClass().getClassLoader(), null);
ServletModel servletModel1 = new ServletModel(contextModel, new DefaultServlet(),
"/s1", null, null, null);
ServletModel servletModel2 = new ServletModel(contextModel, new DefaultServlet(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,7 @@
import static org.ops4j.pax.web.jsp.JspWebdefaults.PROPERTY_JSP_PRECOMPILATION;
import static org.ops4j.pax.web.jsp.JspWebdefaults.PROPERTY_JSP_SCRATCH_DIR;
import static org.ops4j.pax.web.jsp.JspWebdefaults.PROPERTY_JSP_TAGPOOL_MAX_SIZE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_CONNECTOR_NAME;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_ENABLED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_PORT;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_SECURE_CONNECTOR_NAME;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_SECURE_ENABLED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_SECURE_PORT;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_HTTP_USE_NIO;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LISTENING_ADDRESSES;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LOG_NCSA_APPEND;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LOG_NCSA_DISPATCH;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LOG_NCSA_EXTENDED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LOG_NCSA_FORMAT;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LOG_NCSA_LOGTIMEZONE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_LOG_NCSA_RETAINDAYS;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SERVER_CONFIGURATION_FILE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SERVER_CONFIGURATION_URL;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SESSION_COOKIE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SESSION_DOMAIN;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SESSION_PATH;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SESSION_COOKIE_SECURE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SESSION_TIMEOUT;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SESSION_URL;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_CLIENT_AUTH_NEEDED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_CLIENT_AUTH_WANTED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_KEYPASSWORD;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_KEYSTORE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_KEYSTORE_TYPE;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_PASSWORD;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_CIPHERSUITE_INCLUDED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_CIPHERSUITE_EXCLUDED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_TEMP_DIR;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_WORKER_NAME;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_SSL_RENEGOTIATION_ALLOWED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_CRL_PATH;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENABLE_CRLDP;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_VALIDATE_CERTS;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_VALIDATE_PEER_CERTS;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENABLE_OCSP;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_OCSP_RESPONDER_URL;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENC_MASTERPASSWORD;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENC_ALGORITHM;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENC_ENABLED;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENC_PREFIX;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_ENC_SUFFIX;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_DEFAULT_AUTHMETHOD;
import static org.ops4j.pax.web.service.WebContainerConstants.PROPERTY_DEFAULT_REALMNAME;
import static org.ops4j.pax.web.service.WebContainerConstants.*;

import java.io.File;
import java.util.Dictionary;
Expand Down Expand Up @@ -430,7 +385,7 @@ protected void updateController(Dictionary<String, ?> dictionary,
HttpService createService(final Bundle bundle) {
return new HttpServiceProxy(new HttpServiceStarted(
bundle, serverController, serverModel,
servletEventDispatcher));
servletEventDispatcher, configuration.get(PROPERTY_SHOW_STACKS)));
}
}, props);

Expand Down Expand Up @@ -525,6 +480,8 @@ private Dictionary<String, Object> determineServiceProperties(
configuration.getDefaultAuthMethod());
setProperty(toPropagate, PROPERTY_DEFAULT_REALMNAME,
configuration.getDefaultRealmName());
setProperty(toPropagate, PROPERTY_SHOW_STACKS,
configuration.isShowStacks());

// then replace ports
setProperty(toPropagate, PROPERTY_HTTP_PORT, httpPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public Integer getConnectorIdleTimeout() {
return getResolvedIntegerProperty(PROPERTY_CONNECTOR_IDLE_TIMEOUT);
}

@Override
public Boolean isShowStacks() {
return getResolvedBooleanProperty(PROPERTY_SHOW_STACKS);
}

/**
* @see Configuration#getHttpConnectorName()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class HttpServiceStarted implements StoppableHttpService {
private final ServiceModel serviceModel;
private final ServerListener serverListener;
private final ServletListener eventDispatcher;
private final Boolean showStacks;

private final Object lock = new Object();

Expand All @@ -119,7 +120,8 @@ class HttpServiceStarted implements StoppableHttpService {

HttpServiceStarted(final Bundle bundle,
final ServerController srvController,
final ServerModel serverModel, final ServletListener eventDispatcher) {
final ServerModel serverModel, final ServletListener eventDispatcher,
final Boolean showStacks) {
LOG.debug("Creating http service for: " + bundle);

NullArgumentException.validateNotNull(bundle, "Bundle");
Expand All @@ -138,6 +140,7 @@ class HttpServiceStarted implements StoppableHttpService {
this.serverController = srvController;
this.serverModel = serverModel;
this.eventDispatcher = eventDispatcher;
this.showStacks = showStacks;
this.serviceModel = new ServiceModel();
this.serverListener = new ServerListener() {
@Override
Expand Down Expand Up @@ -1218,7 +1221,7 @@ private ContextModel getOrCreateContext(final HttpContext httpContext) {
ContextModel contextModel = serviceModel.getContextModel(context);
if (contextModel == null) {
contextModel = new ContextModel(context, serviceBundle,
bundleClassLoader);
bundleClassLoader, showStacks);
contextModel.setVirtualHosts(serverController.getConfiguration()
.getVirtualHosts());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ limitations under the License.
<AD name="Suffix for Encrypted password" id="org.ops4j.pax.web.enc.suffix" type="String" default=")" />
<AD name="Default Auth" id="org.ops4j.pax.web.default.authmethod" required="false" type="String" default=""/>
<AD name="Default Realm" id="org.ops4j.pax.web.default.realmname" required="false" type="String" default=""/>
<AD name="Display stack trace when error occurs" id="org.ops4j.pax.web.server.showStacks" required="false" type="Boolean" default="true"/>
</OCD>
<Designate pid="org.ops4j.pax.web">
<Object ocdref="org.ops4j.pax.web"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface Configuration {

Integer getConnectorIdleTimeout();

Boolean isShowStacks();

Integer getHttpSecurePort();

String getHttpSecureConnectorName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class ContextModel extends Identity {
* Bundle that used the http context to register an web element.
*/
private final Bundle bundle;
private final Boolean showStacks;
/**
* The realm name to use with the http context.
*/
Expand Down Expand Up @@ -165,7 +166,7 @@ public class ContextModel extends Identity {
* @param classLoader
*/
public ContextModel(final WebContainerContext httpContext, final Bundle bundle,
final ClassLoader classLoader) {
final ClassLoader classLoader, final Boolean showStacks) {
this.bundle = bundle;
NullArgumentException.validateNotNull(httpContext, "Http context");
NullArgumentException.validateNotNull(classLoader, "Class loader");
Expand All @@ -181,6 +182,7 @@ public ContextModel(final WebContainerContext httpContext, final Bundle bundle,
this.virtualHosts = new ArrayList<>();
this.connectors = new ArrayList<>();
this.webBundle = false;
this.showStacks = showStacks;
}

public WebContainerContext getHttpContext() {
Expand Down Expand Up @@ -429,6 +431,10 @@ public Bundle getBundle() {
return bundle;
}

public Boolean isShowStacks() {
return showStacks;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class FilterModelTest {
public void registerWithNoDispatcherInitParams() {
FilterModel fm = new FilterModel(new ContextModel(
createMock(WebContainerContext.class), null, getClass()
.getClassLoader()), createMock(Filter.class),
.getClassLoader(), null), createMock(Filter.class),
new String[]{"/*"}, null, new Hashtable<>(), false);

System.out.println(Arrays.asList(fm.getDispatcher()));
Expand All @@ -50,7 +50,7 @@ public void registerWithNoDispatcherInitParams() {
public void registerWithCorrectSubsetOfDispatcherInitParams() {
FilterModel fm = new FilterModel(new ContextModel(
createMock(WebContainerContext.class), null, getClass()
.getClassLoader()), createMock(Filter.class),
.getClassLoader(), null), createMock(Filter.class),
new String[]{"/*"}, null, new Hashtable<String, String>() {
/**
*
Expand All @@ -70,7 +70,7 @@ public void registerWithCorrectSubsetOfDispatcherInitParams() {
public void registerWithFullComplimentOfDispatcherInitParams() {
FilterModel fm = new FilterModel(new ContextModel(
createMock(WebContainerContext.class), null, getClass()
.getClassLoader()), createMock(Filter.class),
.getClassLoader(), null), createMock(Filter.class),
new String[]{"/*"}, null, new Hashtable<String, String>() {
/**
*
Expand All @@ -89,7 +89,7 @@ public void registerWithFullComplimentOfDispatcherInitParams() {
@Test(expected = IllegalArgumentException.class)
public void registerWithErrorInDispatcherInitParams() {
new FilterModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Filter.class),
getClass().getClassLoader(), null), createMock(Filter.class),
new String[]{"/*"}, null, new Hashtable<String, String>() {
/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,49 @@ public class ServletModelTest {
@Test(expected = IllegalArgumentException.class)
public void registerServletWithNullAlias() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Servlet.class), null,
getClass().getClassLoader(), null), createMock(Servlet.class), null,
new Hashtable<>(), null, null);
}

@Test(expected = IllegalArgumentException.class)
public void registerServletWithNullServlet() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), null, "/test",
getClass().getClassLoader(), null), null, "/test",
new Hashtable<>(), null, null);
}

@Test
public void registerServletWithNullInitParams() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Servlet.class),
getClass().getClassLoader(), null), createMock(Servlet.class),
"/test", null, null, null);
}

@Test
public void registerServletWithOnlySlashInAlias() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Servlet.class), "/",
getClass().getClassLoader(), null), createMock(Servlet.class), "/",
new Hashtable<>(), null, null);
}

@Test(expected = IllegalArgumentException.class)
public void registerServletWithEndSlashInAlias() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Servlet.class),
getClass().getClassLoader(), null), createMock(Servlet.class),
"/test/", new Hashtable<>(), null, null);
}

@Test(expected = IllegalArgumentException.class)
public void registerServletWithoutStartingSlashInAlias() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Servlet.class),
getClass().getClassLoader(), null), createMock(Servlet.class),
"test", new Hashtable<>(), null, null);
}

@Test(expected = IllegalArgumentException.class)
public void registerServletWithoutStartingSlashAndWithEndingSlashInAlias() {
new ServletModel(new ContextModel(createMock(WebContainerContext.class), null,
getClass().getClassLoader()), createMock(Servlet.class),
getClass().getClassLoader(), null), createMock(Servlet.class),
"test/", new Hashtable<>(), null, null);
}
}

0 comments on commit 2a33ee5

Please sign in to comment.