Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3633 - Add more convenience methods to our core Builders #3634

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions core/api/src/main/java/cloud/piranha/core/api/WebApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ default Set<String> addFilterMapping(String filterName, boolean isMatchAfter, St

/**
* Destroy the web application.
*
* @return the web application.
*/
void destroy();
WebApplication destroy();

/**
* Get the default servlet.
Expand Down Expand Up @@ -197,8 +199,10 @@ default String getServletContextId() {

/**
* Initialize the web application.
*
* @return the web application.
*/
void initialize();
WebApplication initialize();

/**
* Marks the end of initializing declared (web.xml, annotations) artifacts
Expand Down Expand Up @@ -360,13 +364,17 @@ void service(ServletRequest request, ServletResponse response)

/**
* Start servicing.
*
* @return the web application.
*/
void start();
WebApplication start();

/**
* Stop servicing.
*
* @return the web application.
*/
void stop();
WebApplication stop();

/**
* Unlink the request and response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public void declareRoles(String... roles) {
}

@Override
public void destroy() {
public WebApplication destroy() {
verifyState(INITIALIZED, "Unable to destroy web application");

servletEnvironments.values().stream().forEach(servletEnv -> servletEnv.getServlet().destroy());
Expand All @@ -639,6 +639,7 @@ public void destroy() {
declaredContextListeners.stream().forEach(listener -> listener.contextDestroyed(new ServletContextEvent(this)));
declaredContextListeners.clear();
status = SETUP;
return this;
}

@Override
Expand Down Expand Up @@ -948,7 +949,7 @@ public String getVirtualServerName() {
}

@Override
public void initialize() {
public WebApplication initialize() {
LOGGER.log(DEBUG, "Initializing web application at {0}", contextPath);
verifyState(SETUP, "Unable to initialize web application");
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Expand All @@ -961,6 +962,7 @@ public void initialize() {
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
return this;
}

@Override
Expand Down Expand Up @@ -1343,19 +1345,21 @@ public void setWebApplicationRequestMapper(WebApplicationRequestMapper webApplic
}

@Override
public void start() {
public WebApplication start() {
LOGGER.log(DEBUG, "Starting web application at {0}", contextPath);
verifyState(INITIALIZED, "Unable to start servicing");
status = SERVICING;
LOGGER.log(DEBUG, "Started web application at {0}", contextPath);
return this;
}

@Override
public void stop() {
public WebApplication stop() {
LOGGER.log(DEBUG, "Stopping web application at {0}", contextPath);
verifyState(SERVICING, "Unable to stop servicing");
status = INITIALIZED;
LOGGER.log(DEBUG, "Stopped web application at {0}", contextPath);
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/
package cloud.piranha.core.impl;

import cloud.piranha.core.api.WebApplication;
import cloud.piranha.resource.api.Resource;
import cloud.piranha.resource.impl.DirectoryResource;
import jakarta.servlet.Filter;
Expand All @@ -40,7 +39,7 @@
import java.util.Map;

/**
* The default WebApplication builder.
* The DefaultWebApplication builder.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
Expand Down Expand Up @@ -81,35 +80,19 @@ public class DefaultWebApplicationBuilder {
*/
private final Map<String, Object> servlets = new HashMap<>();

/**
* Stores the web application.
*/
private WebApplication webApplication;

/**
* Constructor.
*/
public DefaultWebApplicationBuilder() {
}

/**
* Constructor.
*
* @param webApplication the web application.
*/
public DefaultWebApplicationBuilder(WebApplication webApplication) {
this.webApplication = webApplication;
}

/**
* Build the web application.
*
* @return the web application.
*/
public WebApplication build() {
if (webApplication == null) {
webApplication = new DefaultWebApplication();
}
public DefaultWebApplication build() {
DefaultWebApplication webApplication = new DefaultWebApplication();
for (Resource resource : resources) {
webApplication.addResource(resource);
}
Expand Down Expand Up @@ -312,15 +295,4 @@ public DefaultWebApplicationBuilder servletMapping(String servletName, String ma
servletMappings.put(servletName, mapping);
return this;
}

/**
* Set the web application.
*
* @param webApplication the web application.
* @return the web application builder.
*/
public DefaultWebApplicationBuilder webApplication(WebApplication webApplication) {
this.webApplication = webApplication;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package cloud.piranha.core.impl;

import cloud.piranha.core.api.WebApplication;
import java.io.OutputStream;

/**
* The DefaultWebApplicationResponseBuilder.
Expand All @@ -40,6 +41,11 @@ public class DefaultWebApplicationResponseBuilder {
* Stores the body only flag.
*/
private boolean bodyOnly = false;

/**
* Stores the underlying output stream.
*/
private OutputStream underlyingOutputStream;

/**
* Stores the web application.
Expand All @@ -65,10 +71,26 @@ public DefaultWebApplicationResponseBuilder bodyOnly(boolean bodyOnly) {
public DefaultWebApplicationResponse build() {
DefaultWebApplicationResponse response = new DefaultWebApplicationResponse();
response.setBodyOnly(bodyOnly);
response.setWebApplication(webApplication);
if (underlyingOutputStream != null) {
response.getWebApplicationOutputStream().setOutputStream(underlyingOutputStream);
}
if (webApplication != null) {
response.setWebApplication(webApplication);
}
return response;
}

/**
* Set the underlying output stream.
*
* @param underlyingOutputStream the underlying output stream.
* @return the builder.
*/
public DefaultWebApplicationResponseBuilder underlyingOutputStream(OutputStream underlyingOutputStream) {
this.underlyingOutputStream = underlyingOutputStream;
return this;
}

/**
* Set the web application.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ class DefaultWebApplicationBuilderTest {
void testBuild() {
assertNotNull(new DefaultWebApplicationBuilder().build());
}

/**
* Test constructor.
*/
@Test
void testConstructor() {
DefaultWebApplication webApplication = new DefaultWebApplication();
assertEquals(webApplication, new DefaultWebApplicationBuilder(webApplication).build());
}

/**
* Test directoryResource method.
Expand Down Expand Up @@ -200,16 +191,6 @@ void testServletMapping() {
webApplication.getServletRegistration("testServlet").getMappings().iterator().next());
}

/**
* Test webApplication method.
*/
@Test
void testWebApplication() {
DefaultWebApplication webApplication = new DefaultWebApplication();
assertEquals(webApplication,
new DefaultWebApplicationBuilder().webApplication(webApplication).build());
}

/**
* A test filter.
*/
Expand Down