mirror of
				https://github.com/gyurix1968/guacamole-client.git
				synced 2025-10-31 00:53:21 +00:00 
			
		
		
		
	GUACAMOLE-504: Merge changes adding support for overriding HTTP/WebSocket status codes via custom exceptions.
This commit is contained in:
		| @@ -173,7 +173,7 @@ public class RESTExceptionWrapper implements MethodInterceptor { | ||||
|         // Translate GuacamoleException subclasses to HTTP error codes | ||||
|         catch (GuacamoleException e) { | ||||
|             throw new APIException( | ||||
|                 Response.Status.fromStatusCode(e.getStatus().getHttpStatusCode()), | ||||
|                 Response.Status.fromStatusCode(e.getHttpStatusCode()), | ||||
|                 e | ||||
|             ); | ||||
|         } | ||||
|   | ||||
| @@ -53,17 +53,42 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|     private static final int BUFFER_SIZE = 8192; | ||||
|  | ||||
|     /** | ||||
|      * Sends the given status on the given WebSocket connection and closes the | ||||
|      * Sends the given numeric Guacamole and WebSocket status | ||||
|      * on the given WebSocket connection and closes the | ||||
|      * connection. | ||||
|      * | ||||
|      * @param connection The WebSocket connection to close. | ||||
|      * @param guac_status The status to send. | ||||
|      * @param connection | ||||
|      *     The WebSocket connection to close. | ||||
|      * | ||||
|      * @param guacamoleStatusCode | ||||
|      *     The numeric Guacamole Status code to send. | ||||
|      * | ||||
|      * @param webSocketCode | ||||
|      *     The numeric WebSocket status code to send. | ||||
|      */ | ||||
|     public static void closeConnection(Connection connection, | ||||
|             GuacamoleStatus guac_status) { | ||||
|     private static void closeConnection(Connection connection, | ||||
|             int guacamoleStatusCode, int webSocketCode) { | ||||
|  | ||||
|         connection.close(guac_status.getWebSocketCode(), | ||||
|                 Integer.toString(guac_status.getGuacamoleStatusCode())); | ||||
|         connection.close(webSocketCode, | ||||
|                 Integer.toString(guacamoleStatusCode)); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Sends the given status on the given WebSocket connection | ||||
|      * and closes the connection. | ||||
|      * | ||||
|      * @param connection | ||||
|      *     The WebSocket connection to close. | ||||
|      * | ||||
|      * @param guacStatus | ||||
|      *     The status to send. | ||||
|      */ | ||||
|     private static void closeConnection(Connection connection, | ||||
|             GuacamoleStatus guacStatus) { | ||||
|  | ||||
|         closeConnection(connection, guacStatus.getGuacamoleStatusCode(), | ||||
|                 guacStatus.getWebSocketCode()); | ||||
|  | ||||
|     } | ||||
|  | ||||
| @@ -114,7 +139,8 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|                 catch (GuacamoleException e) { | ||||
|                     logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage()); | ||||
|                     logger.debug("Error connecting WebSocket tunnel.", e); | ||||
|                     closeConnection(connection, e.getStatus()); | ||||
|                     closeConnection(connection, e.getStatus().getGuacamoleStatusCode(), | ||||
|                             e.getWebSocketCode()); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
| @@ -168,7 +194,8 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|                             catch (GuacamoleClientException e) { | ||||
|                                 logger.info("WebSocket connection terminated: {}", e.getMessage()); | ||||
|                                 logger.debug("WebSocket connection terminated due to client error.", e); | ||||
|                                 closeConnection(connection, e.getStatus()); | ||||
|                                 closeConnection(connection, e.getStatus().getGuacamoleStatusCode(), | ||||
|                                         e.getWebSocketCode()); | ||||
|                             } | ||||
|                             catch (GuacamoleConnectionClosedException e) { | ||||
|                                 logger.debug("Connection to guacd closed.", e); | ||||
| @@ -177,7 +204,8 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|                             catch (GuacamoleException e) { | ||||
|                                 logger.error("Connection to guacd terminated abnormally: {}", e.getMessage()); | ||||
|                                 logger.debug("Internal error during connection to guacd.", e); | ||||
|                                 closeConnection(connection, e.getStatus()); | ||||
|                                 closeConnection(connection, e.getStatus().getGuacamoleStatusCode(), | ||||
|                                         e.getWebSocketCode()); | ||||
|                             } | ||||
|  | ||||
|                         } | ||||
|   | ||||
| @@ -57,18 +57,25 @@ public abstract class GuacamoleWebSocketTunnelListener implements WebSocketListe | ||||
|     private GuacamoleTunnel tunnel; | ||||
|   | ||||
|     /** | ||||
|      * Sends the given status on the given WebSocket connection and closes the | ||||
|      * Sends the given numeric Guacamole and WebSocket status | ||||
|      * codes on the given WebSocket connection and closes the | ||||
|      * connection. | ||||
|      * | ||||
|      * @param session The outbound WebSocket connection to close. | ||||
|      * @param guac_status The status to send. | ||||
|      * @param session | ||||
|      *     The outbound WebSocket connection to close. | ||||
|      * | ||||
|      * @param guacamoleStatusCode | ||||
|      *     The numeric Guacamole status code to send. | ||||
|      * | ||||
|      * @param webSocketCode | ||||
|      *     The numeric WebSocket status code to send. | ||||
|      */ | ||||
|     private void closeConnection(Session session, GuacamoleStatus guac_status) { | ||||
|     private void closeConnection(Session session, int guacamoleStatusCode, | ||||
|             int webSocketCode) { | ||||
|  | ||||
|         try { | ||||
|             int code = guac_status.getWebSocketCode(); | ||||
|             String message = Integer.toString(guac_status.getGuacamoleStatusCode()); | ||||
|             session.close(new CloseStatus(code, message)); | ||||
|             String message = Integer.toString(guacamoleStatusCode); | ||||
|             session.close(new CloseStatus(webSocketCode, message)); | ||||
|         } | ||||
|         catch (IOException e) { | ||||
|             logger.debug("Unable to close WebSocket connection.", e); | ||||
| @@ -76,6 +83,24 @@ public abstract class GuacamoleWebSocketTunnelListener implements WebSocketListe | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Sends the given status on the given WebSocket connection | ||||
|      * and closes the connection. | ||||
|      * | ||||
|      * @param session | ||||
|      *     The outbound WebSocket connection to close. | ||||
|      * | ||||
|      * @param guacStatus | ||||
|      *     The status to send. | ||||
|      */ | ||||
|     private void closeConnection(Session session, | ||||
|             GuacamoleStatus guacStatus) { | ||||
|  | ||||
|         closeConnection(session, guacStatus.getGuacamoleStatusCode(), | ||||
|                 guacStatus.getWebSocketCode()); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Returns a new tunnel for the given session. How this tunnel is created | ||||
|      * or retrieved is implementation-dependent. | ||||
| @@ -105,7 +130,7 @@ public abstract class GuacamoleWebSocketTunnelListener implements WebSocketListe | ||||
|         catch (GuacamoleException e) { | ||||
|             logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage()); | ||||
|             logger.debug("Error connecting WebSocket tunnel.", e); | ||||
|             closeConnection(session, e.getStatus()); | ||||
|             closeConnection(session, e.getStatus().getGuacamoleStatusCode(), e.getWebSocketCode()); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
| @@ -159,7 +184,8 @@ public abstract class GuacamoleWebSocketTunnelListener implements WebSocketListe | ||||
|                     catch (GuacamoleClientException e) { | ||||
|                         logger.info("WebSocket connection terminated: {}", e.getMessage()); | ||||
|                         logger.debug("WebSocket connection terminated due to client error.", e); | ||||
|                         closeConnection(session, e.getStatus()); | ||||
|                         closeConnection(session, e.getStatus().getGuacamoleStatusCode(), | ||||
|                                 e.getWebSocketCode()); | ||||
|                     } | ||||
|                     catch (GuacamoleConnectionClosedException e) { | ||||
|                         logger.debug("Connection to guacd closed.", e); | ||||
| @@ -168,7 +194,8 @@ public abstract class GuacamoleWebSocketTunnelListener implements WebSocketListe | ||||
|                     catch (GuacamoleException e) { | ||||
|                         logger.error("Connection to guacd terminated abnormally: {}", e.getMessage()); | ||||
|                         logger.debug("Internal error during connection to guacd.", e); | ||||
|                         closeConnection(session, e.getStatus()); | ||||
|                         closeConnection(session, e.getStatus().getGuacamoleStatusCode(), | ||||
|                                 e.getWebSocketCode()); | ||||
|                     } | ||||
|  | ||||
|                 } | ||||
|   | ||||
| @@ -58,17 +58,25 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|     private final Logger logger = LoggerFactory.getLogger(GuacamoleWebSocketTunnelServlet.class); | ||||
|  | ||||
|     /** | ||||
|      * Sends the given status on the given WebSocket connection and closes the | ||||
|      * Sends the given Guacamole and WebSocket numeric status | ||||
|      * on the given WebSocket connection and closes the | ||||
|      * connection. | ||||
|      * | ||||
|      * @param outbound The outbound WebSocket connection to close. | ||||
|      * @param guac_status The status to send. | ||||
|      * @param outbound | ||||
|      *     The outbound WebSocket connection to close. | ||||
|      * | ||||
|      * @param guacamoleStatusCode | ||||
|      *     The status to send. | ||||
|      * | ||||
|      * @param webSocketCode | ||||
|      *     The numeric WebSocket status code to send. | ||||
|      */ | ||||
|     public void closeConnection(WsOutbound outbound, GuacamoleStatus guac_status) { | ||||
|     private void closeConnection(WsOutbound outbound, int guacamoleStatusCode, | ||||
|             int webSocketCode) { | ||||
|  | ||||
|         try { | ||||
|             byte[] message = Integer.toString(guac_status.getGuacamoleStatusCode()).getBytes("UTF-8"); | ||||
|             outbound.close(guac_status.getWebSocketCode(), ByteBuffer.wrap(message)); | ||||
|             byte[] message = Integer.toString(guacamoleStatusCode).getBytes("UTF-8"); | ||||
|             outbound.close(webSocketCode, ByteBuffer.wrap(message)); | ||||
|         } | ||||
|         catch (IOException e) { | ||||
|             logger.debug("Unable to close WebSocket tunnel.", e); | ||||
| @@ -76,6 +84,24 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Sends the given status on the given WebSocket connection | ||||
|      * and closes the connection. | ||||
|      * | ||||
|      * @param outbound | ||||
|      *     The outbound WebSocket connection to close. | ||||
|      * | ||||
|      * @param guacStatus | ||||
|      *     The status to send. | ||||
|      */ | ||||
|     private void closeConnection(WsOutbound outbound, | ||||
|             GuacamoleStatus guacStatus) { | ||||
|  | ||||
|         closeConnection(outbound, guacStatus.getGuacamoleStatusCode(), | ||||
|                 guacStatus.getWebSocketCode()); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     protected String selectSubProtocol(List<String> subProtocols) { | ||||
|  | ||||
| @@ -142,7 +168,8 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|                 catch (GuacamoleException e) { | ||||
|                     logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage()); | ||||
|                     logger.debug("Error connecting WebSocket tunnel.", e); | ||||
|                     closeConnection(outbound, e.getStatus()); | ||||
|                     closeConnection(outbound, e.getStatus().getGuacamoleStatusCode(), | ||||
|                             e.getWebSocketCode()); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
| @@ -196,7 +223,8 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|                             catch (GuacamoleClientException e) { | ||||
|                                 logger.info("WebSocket connection terminated: {}", e.getMessage()); | ||||
|                                 logger.debug("WebSocket connection terminated due to client error.", e); | ||||
|                                 closeConnection(outbound, e.getStatus()); | ||||
|                                 closeConnection(outbound, e.getStatus().getGuacamoleStatusCode(), | ||||
|                                         e.getWebSocketCode()); | ||||
|                             } | ||||
|                             catch (GuacamoleConnectionClosedException e) { | ||||
|                                 logger.debug("Connection to guacd closed.", e); | ||||
| @@ -205,7 +233,8 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet { | ||||
|                             catch (GuacamoleException e) { | ||||
|                                 logger.error("Connection to guacd terminated abnormally: {}", e.getMessage()); | ||||
|                                 logger.debug("Internal error during connection to guacd.", e); | ||||
|                                 closeConnection(outbound, e.getStatus()); | ||||
|                                 closeConnection(outbound, e.getStatus().getGuacamoleStatusCode(), | ||||
|                                         e.getWebSocketCode()); | ||||
|                             } | ||||
|  | ||||
|                         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user