-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25107 from OndroMih/ondromih-gh24712-webSocket-de…
…fault-web-module Fix for a ClassCastException when a filter over a WebSocket endpoint wraps the request
- Loading branch information
Showing
9 changed files
with
501 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
appserver/itest-tools/src/main/java/org/glassfish/main/itest/tools/HttpListenerType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* 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.main.itest.tools; | ||
|
||
/** | ||
* | ||
* @author Ondro Mihalyi | ||
*/ | ||
public enum HttpListenerType { | ||
ADMIN("AS_ADMIN_PORT", 4848), HTTP("AS_HTTP_PORT", 8080), HTTPS("AS_HTTPS_PORT", 8181); | ||
protected String variableName; | ||
protected int defaultPort; | ||
|
||
private HttpListenerType(String variableName, int defaultPort) { | ||
this.variableName = variableName; | ||
this.defaultPort = defaultPort; | ||
} | ||
|
||
public String getVariableName() { | ||
return variableName; | ||
} | ||
|
||
public int getDefaultPort() { | ||
return defaultPort; | ||
} | ||
|
||
public int getPort() { | ||
return Integer.parseInt(System.getenv() | ||
.getOrDefault(getVariableName(), String.valueOf(getDefaultPort())) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...c/main/java/org/glassfish/main/test/app/websocket/CustomRequestResponseServletFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* 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.main.test.app.websocket; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.annotation.WebFilter; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpServletResponseWrapper; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
import static java.util.logging.Level.SEVERE; | ||
|
||
/** | ||
* | ||
* @author Ondro Mihalyi | ||
*/ | ||
@WebFilter("/hello") | ||
public class CustomRequestResponseServletFilter implements Filter { | ||
|
||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
ServletRequest wrappedRequest = new HttpServletRequestWrapper((HttpServletRequest) request) { | ||
}; | ||
ServletResponse wrappedResponse = new HttpServletResponseWrapper((HttpServletResponse) response) { | ||
}; | ||
try { | ||
chain.doFilter(wrappedRequest, wrappedResponse); | ||
} catch (Exception e) { | ||
Logger.getLogger(this.getClass().getName()).log(SEVERE, e.getMessage(), e); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...plication/src/main/java/org/glassfish/main/test/app/websocket/HelloWebSocketEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* 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.main.test.app.websocket; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.websocket.OnMessage; | ||
import jakarta.websocket.Session; | ||
import jakarta.websocket.server.ServerEndpoint; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
* @author Ondro Mihalyi | ||
*/ | ||
@ServerEndpoint("/hello") | ||
@ApplicationScoped | ||
public class HelloWebSocketEndpoint { | ||
|
||
@OnMessage | ||
public void processGreeting(String message, Session session) throws IOException { | ||
session.getBasicRemote().sendText("World"); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...test/java/org/glassfish/main/test/app/websocket/CustomRequestResponseOnWebSocketTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation. | ||
* | ||
* 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.main.test.app.websocket; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.glassfish.main.itest.tools.GlassFishTestEnvironment; | ||
import org.glassfish.main.itest.tools.asadmin.Asadmin; | ||
import org.glassfish.main.itest.tools.asadmin.AsadminResult; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.exporter.ZipExporter; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import static java.lang.System.Logger.Level.INFO; | ||
import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.webSocketUri; | ||
import static org.glassfish.main.itest.tools.HttpListenerType.HTTP; | ||
import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
/* | ||
* Tests for https://github.com/eclipse-ee4j/glassfish/issues/24712 | ||
*/ | ||
public class CustomRequestResponseOnWebSocketTest { | ||
|
||
private static final System.Logger LOG = System.getLogger(CustomRequestResponseOnWebSocketTest.class.getName()); | ||
|
||
private static final int HTTP_PORT = GlassFishTestEnvironment.getPort(HTTP); | ||
|
||
private static final String WEBAPP_FILE_NAME = "webapp.war"; | ||
|
||
private static final String WEBAPP_NAME = "webapp"; | ||
|
||
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(); | ||
|
||
@TempDir | ||
private static File webAppDir; | ||
|
||
@BeforeAll | ||
public static void deployAll() throws IOException { | ||
File webApp = createWebApp(); | ||
|
||
AsadminResult result = ASADMIN.exec("deploy", | ||
"--contextroot", "/" + WEBAPP_NAME, | ||
"--name", WEBAPP_NAME, | ||
webApp.getAbsolutePath()); | ||
assertThat(result, asadminOK()); | ||
} | ||
|
||
@AfterAll | ||
public static void undeployAll() { | ||
assertAll( | ||
() -> assertThat(ASADMIN.exec("undeploy", WEBAPP_NAME), asadminOK()) | ||
); | ||
} | ||
|
||
@Test | ||
public void testCustomRequest() throws IOException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException { | ||
final WebSocketClient webSocketClient = new WebSocketClient(webSocketUri(HTTP_PORT, "/" + WEBAPP_NAME + "/hello")); | ||
webSocketClient.sendMessage("Hello"); | ||
CompletableFuture<String> waitForMessage = new CompletableFuture<>(); | ||
webSocketClient.addMessageHandler(msg -> waitForMessage.complete(msg)); | ||
|
||
final String message = waitForMessage.get(10, TimeUnit.SECONDS); | ||
|
||
assertThat(message, equalTo("World")); | ||
} | ||
|
||
private static File createWebApp() throws IOException { | ||
WebArchive webArchive = ShrinkWrap.create(WebArchive.class) | ||
.addClass(CustomRequestResponseServletFilter.class) | ||
.addClass(HelloWebSocketEndpoint.class); | ||
|
||
LOG.log(INFO, webArchive.toString(true)); | ||
|
||
File webApp = new File(webAppDir, WEBAPP_FILE_NAME); | ||
webArchive.as(ZipExporter.class).exportTo(webApp, true); | ||
return webApp; | ||
} | ||
} |
Oops, something went wrong.