Skip to content

Commit

Permalink
Allow to set the UpgradeResponse status and headers
Browse files Browse the repository at this point in the history
Invoke ClientEndpointConfig.Configurator#afterResponse when error status

Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Nov 13, 2023
1 parent e83c07e commit 40b6ab9
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
<inherited>true</inherited>
<configuration>
<source>${java.version}</source>
Expand Down
3 changes: 2 additions & 1 deletion bundles/samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/examples-assembly.xml</descriptor>
Expand All @@ -124,7 +125,7 @@
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
<goal>single</goal>
</goals>
</execution>
</executions>
Expand Down
4 changes: 2 additions & 2 deletions bundles/websocket-ri-archive/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2013, 2023 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
Expand Down Expand Up @@ -169,7 +169,7 @@
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>attached</goal>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
Expand Down
6 changes: 3 additions & 3 deletions bundles/websocket-risrc-licensee/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2013, 2023 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
Expand All @@ -23,7 +23,7 @@
<parent>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-bundles</artifactId>
<version>1.2-SNAPSHOT</version>
<version>1.99-SNAPSHOT</version>
</parent>

<artifactId>websocket-risrc-licensee</artifactId>
Expand Down Expand Up @@ -103,7 +103,7 @@
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>attached</goal>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023 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
Expand Down Expand Up @@ -294,6 +294,8 @@ public ClientUpgradeInfo processResponse(final UpgradeResponse upgradeResponse,
LocalizationMessages.HANDSHAKE_HTTP_RETRY_AFTER_MESSAGE(), delay));
return UPGRADE_INFO_FAILED;
default:
((ClientEndpointConfig) endpointWrapper.getEndpointConfig()).getConfigurator().afterResponse(upgradeResponse);

clientEngineState = TyrusClientEngineState.FAILED;
HandshakeException e = new HandshakeException(
upgradeResponse.getStatus(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 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
Expand Down Expand Up @@ -229,7 +229,7 @@ public boolean isUserInRole(String role) {
final WebSocketEngine.UpgradeInfo upgradeInfo = engine.upgrade(requestContext, tyrusUpgradeResponse);
switch (upgradeInfo.getStatus()) {
case HANDSHAKE_FAILED:
appendTraceHeaders(httpServletResponse, tyrusUpgradeResponse);
appendAllHeaders(httpServletResponse, tyrusUpgradeResponse);
httpServletResponse.sendError(tyrusUpgradeResponse.getStatus());
break;
case NOT_APPLICABLE:
Expand All @@ -253,9 +253,7 @@ public boolean isUserInRole(String role) {
}

httpServletResponse.setStatus(tyrusUpgradeResponse.getStatus());
for (Map.Entry<String, List<String>> entry : tyrusUpgradeResponse.getHeaders().entrySet()) {
httpServletResponse.addHeader(entry.getKey(), Utils.getHeaderFromList(entry.getValue()));
}
appendAllHeaders(httpServletResponse, tyrusUpgradeResponse);

response.flushBuffer();
LOGGER.fine("Handshake Complete");
Expand Down Expand Up @@ -283,7 +281,7 @@ public boolean isUserInRole(String role) {
}
}

private void appendTraceHeaders(HttpServletResponse httpServletResponse, TyrusUpgradeResponse
private static void appendTraceHeaders(HttpServletResponse httpServletResponse, TyrusUpgradeResponse
tyrusUpgradeResponse) {
for (Map.Entry<String, List<String>> entry : tyrusUpgradeResponse.getHeaders().entrySet()) {
if (entry.getKey().contains(UpgradeResponse.TRACING_HEADER_PREFIX)) {
Expand All @@ -292,6 +290,13 @@ private void appendTraceHeaders(HttpServletResponse httpServletResponse, TyrusUp
}
}

private static void appendAllHeaders(HttpServletResponse httpServletResponse, TyrusUpgradeResponse
tyrusUpgradeResponse) {
for (Map.Entry<String, List<String>> entry : tyrusUpgradeResponse.getHeaders().entrySet()) {
httpServletResponse.addHeader(entry.getKey(), Utils.getHeaderFromList(entry.getValue()));
}
}

private synchronized JAXBContext getWsadlJaxbContext() throws JAXBException {
if (wsadlJaxbContext == null) {
wsadlJaxbContext = JAXBContext.newInstance(Application.class.getPackage().getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 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
Expand Down Expand Up @@ -401,8 +401,21 @@ public Map<String, Object> getProperties() {
}

response.getHeaders().putAll(debugContext.getTracingHeaders());
return new SuccessfulUpgradeInfo(endpointWrapper, protocolHandler, incomingBufferSize, request, response,
extensionContext, debugContext);
switch (response.getStatus()) {
case 101:
case 300:
case 301:
case 302:
case 303:
case 307:
case 308:
case 401:
case 503:
return new SuccessfulUpgradeInfo(endpointWrapper, protocolHandler, incomingBufferSize, request, response,
extensionContext, debugContext);
default:
return new NoConnectionUpgradeInfo(UpgradeStatus.HANDSHAKE_FAILED);
}
}

response.setStatus(500);
Expand Down
8 changes: 4 additions & 4 deletions docs/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2013, 2023 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
Expand Down Expand Up @@ -44,7 +44,7 @@
<executions>
<execution>
<configuration>
<tasks>
<target>
<echo message="Preparing char entities" />
<mkdir dir="${src.dir}" />
<copy todir="${src.dir}">
Expand All @@ -53,7 +53,7 @@
<replace file="${src.dir}/tyrus.ent" token="$version" value="${project.version}" />
<replace file="${src.dir}/tyrus.ent" token="$repository" value="${javanet.repository.id}" />
<replace file="${src.dir}/tyrus.ent" token="$websocket-api-version" value="${websocket-api.version}" />
</tasks>
</target>
</configuration>
<id>process-entities</id>
<phase>package</phase>
Expand Down Expand Up @@ -137,7 +137,7 @@
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
<goal>single</goal>
</goals>
</execution>
</executions>
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@

<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.glassfish.copyright</groupId>
<artifactId>glassfish-copyright-maven-plugin</artifactId>
Expand Down Expand Up @@ -317,7 +325,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<version>3.4.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
Expand Down
4 changes: 2 additions & 2 deletions samples/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2011, 2017 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2011, 2023 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -70,7 +70,7 @@
<id>assemble-src-zip</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 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
Expand All @@ -18,20 +18,26 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.websocket.ClientEndpointConfig;
import javax.websocket.DeploymentException;
import javax.websocket.EndpointConfig;
import javax.websocket.HandshakeResponse;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;

import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.server.Server;
import org.glassfish.tyrus.spi.UpgradeResponse;
import org.glassfish.tyrus.test.standard_config.bean.TestEndpoint;
import org.glassfish.tyrus.test.tools.TestContainer;

Expand Down Expand Up @@ -118,4 +124,72 @@ public void onOpen(Session session) {
stopServer(server);
}
}


static final int STATUS = 499;
static final String HEADER = "TEST_HEADER";

public static class StatusSetterConfiguration extends ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
((UpgradeResponse) response).setStatus(STATUS);
response.getHeaders().put(HEADER, Collections.singletonList(HEADER));
}
}

@Test
public void customResponseTest() throws DeploymentException, IOException {
@ServerEndpoint(value = "/status", configurator = StatusSetterConfiguration.class)
class StatusSetterEndpoint {
@OnMessage
public void onMessage(String message) {
throw new IllegalStateException("ON MESSAGE");
}
}

final AtomicReference<Integer> status = new AtomicReference<>();
final AtomicReference<String> header = new AtomicReference<>();

Server server = startServer(StatusSetterEndpoint.class);

ClientEndpointConfig.Configurator cecc = new ClientEndpointConfig.Configurator() {
@Override
public void afterResponse(HandshakeResponse hr) {
status.set(((UpgradeResponse) hr).getStatus());
header.set(((UpgradeResponse) hr).getFirstHeaderValue(HEADER));
}
};

ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().configurator(cecc).build();

try {
ClientManager client = createClient();
Session session = client.connectToServer(new TestEndpointAdapter() {
@Override
public void onMessage(String message) {
}

@Override
public void onOpen(Session session) {
try {
session.getBasicRemote().sendText("This should never be sent");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public EndpointConfig getEndpointConfig() {
return cec;
}
}, cec, getURI(StatusSetterEndpoint.class));

throw new IllegalStateException("DeploymentException was not thrown");
} catch (DeploymentException de) {
Assert.assertEquals(STATUS, status.get().intValue());
Assert.assertEquals(HEADER, header.get());
} finally {
server.stop();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023 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
Expand Down Expand Up @@ -68,8 +68,8 @@ private void twoClientsChat(SeleniumToolkit.Browser alice, SeleniumToolkit.Brows
}

private void chatScalabitlity(SeleniumToolkit.Browser browser) throws InterruptedException, Exception {
logger.log(Level.INFO, "=============testScalabilityWith" + TestScenarios.MAX_CHAT_CLIENTS +
"Users===============================");
logger.log(Level.INFO, "=============testScalabilityWith" + TestScenarios.MAX_CHAT_CLIENTS
+ "Users===============================");
List<SeleniumToolkit> toolkits = new ArrayList<SeleniumToolkit>();
// Launch 100 browsers
for (int idx = 0; idx < TestScenarios.MAX_CHAT_CLIENTS; idx++) {
Expand Down
Loading

0 comments on commit 40b6ab9

Please sign in to comment.