@Override public void beforeRequest(Map<String, List<String>> headers) { if (transportMessage.getSessionId() != null) headers.put("Cookie", Collections.singletonList("JSESSIONID=" + transportMessage.getSessionId())); headers.put("connectId", Collections.singletonList(transportMessage.getId())); headers.put("GDSClientType", Collections.singletonList(transportMessage.getClientType().toString())); String clientId = transportMessage.getClientId() != null ? transportMessage.getClientId() : channel.getClientId(); if (clientId != null) headers.put("GDSClientId", Collections.singletonList(clientId)); }
@Override public void onOpen(Connection connection) { synchronized (channel) { connectionFuture = null; reconnectAttempts = 0; ((TransportData)channel.getTransportData()).connection = connection; send(channel, null); } }
@Override public void connect(final Channel channel, final TransportMessage transportMessage) { try { log.info("Connecting to websocket %s sessionId %s", channel.getUri(), transportMessage.getSessionId()); webSocketContainer.connectToServer(new GravityWebSocketEndpoint(channel), new GravityWebSocketEndpointConfig(channel, transportMessage), channel.getUri()); } catch (Exception e) { log.error(e, "Could not connect to uri %s", channel.getUri()); getStatusHandler().handleException(new TransportException("Could not connect to uri " + channel.getUri(), e)); } }
public void cancelled() { if (!message.isConnect()) getStatusHandler().handleIO(false); if (message.isDisconnect()) { channel.onDisconnect(); return; } channel.onCancelled(message); } });
protected void onClose(Channel channel, int closeCode, String message) { log.info("Websocket connection closed %d %s channel %s", closeCode, message, channel.getClientId()); boolean waitBeforeReconnect = !(closeCode == CLOSE_NORMAL && message != null && message.startsWith("Idle")); if (channel.getTransportData() != null) { ((TransportData<?>)channel.getTransportData()).disconnect(); channel.setTransportData(null); connected = false; else if (closeCode != CLOSE_SHUTDOWN && channel.getClientId() == null) { log.debug("Websocket connection could not connect"); getStatusHandler().handleException(new TransportException("Transport could not connect code: " + closeCode + " " + message)); channel.onDisconnect(); disconnecting = false; connected = false; channel.onError(connectMessage, new RuntimeException(message + " (code=" + closeCode + ")")); getStatusHandler().handleException(new TransportException("Transport disconnected")); return;
@Override public void connect(final Channel channel, final TransportMessage transportMessage) { try { ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setRequestURI(channel.getUri()); String protocol = "org.granite.gravity." + transportMessage.getContentType().substring("application/x-".length()); request.setSubProtocols(protocol); if (transportMessage.getSessionId() != null) webSocketClient.getCookieStore().add(channel.getUri(), new HttpCookie("JSESSIONID", transportMessage.getSessionId())); request.setCookiesFrom(webSocketClient.getCookieStore()); request.setHeader("connectId", transportMessage.getId()); request.setHeader("GDSClientType", transportMessage.getClientType().toString()); String clientId = transportMessage.getClientId() != null ? transportMessage.getClientId() : channel.getClientId(); if (clientId != null) request.setHeader("GDSClientId", clientId); log.info("Connecting to websocket %s protocol %s sessionId %s clientId %s", channel.getUri(), protocol, transportMessage.getSessionId(), clientId); webSocketClient.connect(new WebSocketHandler(channel), channel.getUri(), request); } catch (Exception e) { log.error(e, "Could not connect to uri %s", channel.getUri()); getStatusHandler().handleException(new TransportException("Could not connect to uri " + channel.getUri(), e)); } }
((TransportData)channel.getTransportData()).connection = null; connectionFuture = null; if (channel.getClientId() == null) { getStatusHandler().handleException(new TransportException("Transport could not connect code: " + closeCode + " " + message)); return; stop(); channel.onError(transportMessage, new RuntimeException(message + " (code=" + closeCode + ")")); getStatusHandler().handleException(new TransportException("Transport disconnected")); return;
channel.onDisconnect(); return; channel.onError(message, new TransportHttpStatusException( response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()) try { is = response.getEntity().getContent(); channel.onMessage(message, is);
TransportData transportData = channel.getTransportData(); if (transportData == null) { transportData = new TransportData(); channel.setTransportData(transportData);
public void completed(HttpResponse response) { if (!message.isConnect()) getStatusHandler().handleIO(false); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { channel.onError(message, new TransportHttpStatusException( response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()) ); return; } InputStream is = null; try { is = response.getEntity().getContent(); channel.onMessage(is); } catch (Exception e) { getStatusHandler().handleException(new TransportIOException(message, "Could not deserialize message", e)); } finally { if (is != null) try { is.close(); } catch (Exception e) { } } }
public void failed(Exception e) { if (!message.isConnect()) getStatusHandler().handleIO(false); if (message.isDisconnect()) { channel.onDisconnect(); return; } channel.onError(message, e); getStatusHandler().handleException(new TransportIOException(message, "Request failed", e)); }
protected void onError(Channel channel, Throwable throwable) { log.error(throwable, "Websocket connection error"); channel.onError(connectMessage, new RuntimeException("Websocket connection error", throwable)); getStatusHandler().handleException(new TransportException("Websocket connection error: " + throwable.getMessage())); } }
@Override public ResponseMessageFuture invoke() { request.setTimeToLive(timeToLive); return remoteService.channel.send(request, listeners.toArray(new ResponseListener[listeners.size()])); } }
@Override public void onMessage(byte[] data, int offset, int length) { channel.onMessage(new ByteArrayInputStream(data, offset, length)); }
public void cancelled() { if (!message.isConnect()) getStatusHandler().handleIO(false); channel.onCancelled(message); } });
protected void onClose(Channel channel, int closeCode, String message) { log.info("Websocket connection closed %d %s channel %s", closeCode, message, channel.getClientId()); boolean waitBeforeReconnect = !(closeCode == CLOSE_NORMAL && message != null && message.startsWith("Idle")); if (channel.getTransportData() != null) { ((TransportData<?>)channel.getTransportData()).disconnect(); channel.setTransportData(null); connected = false; else if (closeCode != CLOSE_SHUTDOWN && channel.getClientId() == null) { log.debug("Websocket connection could not connect"); getStatusHandler().handleException(new TransportException("Transport could not connect code: " + closeCode + " " + message)); channel.onDisconnect(); disconnecting = false; connected = false; channel.onError(connectMessage, new RuntimeException(message + " (code=" + closeCode + ")")); getStatusHandler().handleException(new TransportException("Transport disconnected")); return;
@Override public void connect(final Channel channel, final TransportMessage transportMessage) { try { ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setRequestURI(channel.getUri()); String protocol = "org.granite.gravity." + transportMessage.getContentType().substring("application/x-".length()); request.setSubProtocols(protocol); if (transportMessage.getSessionId() != null) webSocketClient.getCookieStore().add(channel.getUri(), new HttpCookie("JSESSIONID", transportMessage.getSessionId())); request.setCookiesFrom(webSocketClient.getCookieStore()); request.setHeader("connectId", transportMessage.getId()); request.setHeader("GDSClientType", transportMessage.getClientType().toString()); String clientId = transportMessage.getClientId() != null ? transportMessage.getClientId() : channel.getClientId(); if (clientId != null) request.setHeader("GDSClientId", clientId); log.info("Connecting to websocket %s protocol %s sessionId %s clientId %s", channel.getUri(), protocol, transportMessage.getSessionId(), clientId); webSocketClient.connect(new WebSocketHandler(channel), channel.getUri(), request); } catch (Exception e) { log.error(e, "Could not connect to uri %s", channel.getUri()); getStatusHandler().handleException(new TransportException("Could not connect to uri " + channel.getUri(), e)); } }
channel.onDisconnect(); return; channel.onError(message, new TransportHttpStatusException( response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()) try { is = response.getEntity().getContent(); channel.onMessage(message, is);
transportData = channel.getTransportData(); if (transportData == null) { transportData = newTransportData(); channel.setTransportData(transportData);
public void cancelled() { if (!message.isConnect()) getStatusHandler().handleIO(false); if (message.isDisconnect()) { channel.onDisconnect(); return; } channel.onCancelled(message); } });