From f09c32085507db5a72e2ff4df7b182e846788971 Mon Sep 17 00:00:00 2001 From: jansupol Date: Fri, 10 Nov 2023 12:46:03 +0100 Subject: [PATCH] Allow to set the UpgradeResponse status and headers Invoke ClientEndpointConfig.Configurator#afterResponse when error status Signed-off-by: jansupol --- .../resources/archetype-resources/pom.xml | 1 + bundles/samples/pom.xml | 3 +- bundles/websocket-ri-archive/pom.xml | 4 +- bundles/websocket-risrc-licensee/pom.xml | 6 +- .../tyrus/client/TyrusClientEngine.java | 4 +- .../tyrus/servlet/TyrusServletFilter.java | 17 +++-- .../tyrus/core/TyrusWebSocketEngine.java | 19 ++++- docs/pom.xml | 8 +- pom.xml | 2 +- samples/pom.xml | 4 +- .../test/standard_config/HandshakeTest.java | 76 ++++++++++++++++++- .../glassfish/tyrus/tests/qa/BrowserTest.java | 6 +- .../tyrus/tests/qa/SeleniumToolkit.java | 19 +++-- 13 files changed, 134 insertions(+), 35 deletions(-) diff --git a/archetypes/echo/src/main/resources/archetype-resources/pom.xml b/archetypes/echo/src/main/resources/archetype-resources/pom.xml index 682032ae..19dd4862 100644 --- a/archetypes/echo/src/main/resources/archetype-resources/pom.xml +++ b/archetypes/echo/src/main/resources/archetype-resources/pom.xml @@ -23,6 +23,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.9.0 true ${java.version} diff --git a/bundles/samples/pom.xml b/bundles/samples/pom.xml index 68271662..e40cb0ed 100644 --- a/bundles/samples/pom.xml +++ b/bundles/samples/pom.xml @@ -114,6 +114,7 @@ maven-assembly-plugin + 3.6.0 src/main/assembly/examples-assembly.xml @@ -124,7 +125,7 @@ make-assembly package - attached + single diff --git a/bundles/websocket-ri-archive/pom.xml b/bundles/websocket-ri-archive/pom.xml index 8e14fdb4..3ffd6913 100755 --- a/bundles/websocket-ri-archive/pom.xml +++ b/bundles/websocket-ri-archive/pom.xml @@ -1,7 +1,7 @@ - attached + single diff --git a/bundles/websocket-risrc-licensee/pom.xml b/bundles/websocket-risrc-licensee/pom.xml index 68a4302b..7e83c334 100755 --- a/bundles/websocket-risrc-licensee/pom.xml +++ b/bundles/websocket-risrc-licensee/pom.xml @@ -1,7 +1,7 @@ - attached + single diff --git a/client/src/main/java/org/glassfish/tyrus/client/TyrusClientEngine.java b/client/src/main/java/org/glassfish/tyrus/client/TyrusClientEngine.java index 0a5bae98..1625a535 100644 --- a/client/src/main/java/org/glassfish/tyrus/client/TyrusClientEngine.java +++ b/client/src/main/java/org/glassfish/tyrus/client/TyrusClientEngine.java @@ -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 @@ -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(), diff --git a/containers/servlet/src/main/java/org/glassfish/tyrus/servlet/TyrusServletFilter.java b/containers/servlet/src/main/java/org/glassfish/tyrus/servlet/TyrusServletFilter.java index 9fa26bdc..eb96fda1 100644 --- a/containers/servlet/src/main/java/org/glassfish/tyrus/servlet/TyrusServletFilter.java +++ b/containers/servlet/src/main/java/org/glassfish/tyrus/servlet/TyrusServletFilter.java @@ -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 @@ -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: @@ -253,9 +253,7 @@ public boolean isUserInRole(String role) { } httpServletResponse.setStatus(tyrusUpgradeResponse.getStatus()); - for (Map.Entry> entry : tyrusUpgradeResponse.getHeaders().entrySet()) { - httpServletResponse.addHeader(entry.getKey(), Utils.getHeaderFromList(entry.getValue())); - } + appendAllHeaders(httpServletResponse, tyrusUpgradeResponse); response.flushBuffer(); LOGGER.fine("Handshake Complete"); @@ -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> entry : tyrusUpgradeResponse.getHeaders().entrySet()) { if (entry.getKey().contains(UpgradeResponse.TRACING_HEADER_PREFIX)) { @@ -292,6 +290,13 @@ private void appendTraceHeaders(HttpServletResponse httpServletResponse, TyrusUp } } + private static void appendAllHeaders(HttpServletResponse httpServletResponse, TyrusUpgradeResponse + tyrusUpgradeResponse) { + for (Map.Entry> 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()); diff --git a/core/src/main/java/org/glassfish/tyrus/core/TyrusWebSocketEngine.java b/core/src/main/java/org/glassfish/tyrus/core/TyrusWebSocketEngine.java index 7533735b..52751a70 100644 --- a/core/src/main/java/org/glassfish/tyrus/core/TyrusWebSocketEngine.java +++ b/core/src/main/java/org/glassfish/tyrus/core/TyrusWebSocketEngine.java @@ -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 @@ -401,8 +401,21 @@ public Map 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); diff --git a/docs/pom.xml b/docs/pom.xml index 4a258ee3..76252f87 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -1,7 +1,7 @@