Skip to content

Commit

Permalink
Post Processing Servlet Filter test
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Apr 24, 2024
1 parent 7f2f21f commit 9286d63
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,15 @@ public void process(final Throwable throwable) {

if (!processResponseError(responseError)) {
// Pass the exception to the container.
LOGGER.log(Level.WARNING, LocalizationMessages.ERROR_EXCEPTION_MAPPING_THROWN_TO_CONTAINER(), responseError);

try {
request.getResponseWriter().failure(responseError);
} finally {
if (runtime.configSetStatusOverSendError) {
completionCallbackRunner.onComplete(responseError);
} else {
LOGGER.log(Level.WARNING,
LocalizationMessages.ERROR_EXCEPTION_MAPPING_THROWN_TO_CONTAINER(),
responseError);
defaultMapperResponse = processResponseWithDefaultExceptionMapper(responseError, request);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/servlet-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-external</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.servlettests;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;

import java.io.IOException;

import static org.glassfish.jersey.tests.integration.servlettests.PostProcessingErrorResource.ERROR_MESSAGE;

public class PostProcessingErrorFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException {

try {
chain.doFilter(request, response);
} catch (ServletException ex) {
//post-processing attempt
final Throwable orig = ex.getRootCause();
if (orig.getMessage().equalsIgnoreCase(ERROR_MESSAGE)) {
response.getWriter().print(ERROR_MESSAGE);
response.getWriter().flush();
}
}
}

@Override
public void destroy() {
Filter.super.destroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.servlettests;

import jakarta.servlet.http.HttpServlet;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.core.Response;

@Path("postprocessing")
public class PostProcessingErrorResource extends HttpServlet {

static final String ERROR_MESSAGE = "Must be post processed";
@GET
public Response getException() {
throw new ProcessingException(ERROR_MESSAGE);
}

}
27 changes: 27 additions & 0 deletions tests/integration/servlet-tests/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,31 @@
<filter-name>custom404</filter-name>
<url-pattern>/custom404/*</url-pattern>
</filter-mapping>

<!-- post process errors (40*, 50*) -->
<filter>
<filter-name>PostProcessFilter</filter-name>
<filter-class>org.glassfish.jersey.tests.integration.servlettests.PostProcessingErrorFilter</filter-class>
</filter>
<servlet>
<servlet-name>PostProcessServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.tests.integration.servlettests.PostProcessingErrorResource</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.response.setStatusOverSendError</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PostProcessServlet</servlet-name>
<url-pattern>/postProcess/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>PostProcessFilter</filter-name>
<url-pattern>/postProcess/*</url-pattern>
</filter-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.servlettests;

import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.external.ExternalTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.jupiter.api.Test;

import java.util.Locale;

import static org.glassfish.jersey.tests.integration.servlettests.PostProcessingErrorResource.ERROR_MESSAGE;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PostProcesingITCase extends JerseyTest {

@Override
protected Application configure() {
// dummy resource config
return new ResourceConfig();
}

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new ExternalTestContainerFactory();
}

@Test
public void testPostProcessingLocale() {
final Response response = target()
.path("postProcess/postprocessing")
.request().get();
assertEquals(ERROR_MESSAGE, response.readEntity(String.class));
}

}

0 comments on commit 9286d63

Please sign in to comment.