public O get() throws IOException, CancellationException { return type.cast(parent.get()); }
/** * {@inheritDoc} */ public void notify(final IoFuture<? extends T> future, A attachment) { switch (future.getStatus()) { case CANCELLED: handleCancelled(attachment); break; case DONE: try { handleDone(future.get(), attachment); } catch (IOException e) { // not possible throw new IllegalStateException(); } break; case FAILED: handleFailed(future.getException(), attachment); break; default: // not possible throw new IllegalStateException(); } }
private static Channel getChannel ( ConnectionProviderContextImpl context, ConnectionHandler ch, OptionMap options ) throws IOException { Channel c; FutureResult<Channel> chResult = new FutureResult<Channel>(context.getExecutor()); ch.open("jmx", chResult, options); IoFuture<Channel> cFuture = chResult.getIoFuture(); Status s2 = cFuture.await(); if ( s2 == Status.FAILED ) { System.err.println("Cannot connect"); if ( cFuture.getException() != null ) { throw new IOException("Connect failed", cFuture.getException()); } } else if ( s2 != Status.DONE ) { cFuture.cancel(); throw new IOException("Connect timeout"); } c = cFuture.get(); return c; }
protected boolean isConnected(final URI uri) { final IoFuture<ConnectionPeerIdentity> future = Endpoint.getCurrent().getConnectedIdentityIfExists(uri, "ejb", "jboss", AuthenticationContext.captureCurrent()); try { return future != null && future.getStatus() == IoFuture.Status.DONE && future.get().getConnection().isOpen(); } catch (IOException e) { // impossible throw Assert.unreachableCode(); } }
static TransactionClientChannel forConnection(final Connection connection) throws IOException { return CLIENT_SERVICE_HANDLE.getClientService(connection, OptionMap.EMPTY).get(); }
IoFuture.Status result = futureChannel.await(getTimeoutValue(Timeout.CHANNEL, combinedEnvironment), TimeUnit.SECONDS); if (result == IoFuture.Status.DONE) { channel = futureChannel.get(); } else if (result == IoFuture.Status.FAILED) { throw futureChannel.getException();
public T get() throws IOException { return convert(delegate.get()); }
void start() throws IOException { sendVersionHeader(); IoFuture<String> futureConnectionId = ConnectionIdReceiver.getConnectionId(channel); IoFuture.Status result = futureConnectionId.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: connectionId = futureConnectionId.get(); mbeanServerConnection = new TheConnection(); localNotificationManager = new LocalNotificationManager(); channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureConnectionId.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } }
/** * Get the connection peer identity. If the connection is not configured as {@code immediate}, then the connection * will not actually be established until this method is called. The resultant connection should be closed and * discarded in the event of an error, in order to facilitate automatic reconnection. * * @param location a location from {@link ProviderEnvironment#getProviderUris()} (must not be {@code null}) * @return the connection peer identity (not {@code null}) * @throws AuthenticationException if authenticating or re-authenticating the peer failed * @throws IOException if connecting the peer failed */ public ConnectionPeerIdentity getPeerIdentity(final URI location) throws IOException { Assert.checkNotNullParam("location", location); return getFuturePeerIdentity(location).get(); }
InitialHeader header = futureHeader.get();
this.httpClient.connect(url, this.worker, this.bufferPool, this.optionMap).get(); try { ClientRequest request = new ClientRequest().setMethod(method).setPath(url.getPath());
ConnectionPeerIdentity getPeerIdentity() throws IOException { SSLContext finalSslContext; if (sslContext == null) { try { finalSslContext = CLIENT.getSSLContext(location, AuthenticationContext.captureCurrent(), "jta", "jboss"); } catch (GeneralSecurityException e) { throw new IOException(e); } } else { finalSslContext = sslContext; } AuthenticationConfiguration finalAuthenticationConfiguration; if (authenticationConfiguration == null) { finalAuthenticationConfiguration = CLIENT.getAuthenticationConfiguration(location, AuthenticationContext.captureCurrent(), -1, "jta", "jboss"); } else { finalAuthenticationConfiguration = authenticationConfiguration; } return endpoint.getConnectedIdentity(location, finalSslContext, finalAuthenticationConfiguration).get(); }
@Override public void checkFail(String serviceId, String token) { if(logger.isDebugEnabled()) logger.debug("checkFail serviceId = " + serviceId); String path = "/v1/agent/check/fail/" + "service:" + serviceId; final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { if(connection == null || !connection.isOpen() || reqCounter >= maxReqPerConn) { if(logger.isDebugEnabled()) logger.debug("connection is closed with counter " + reqCounter + ", reconnecting..."); connection = client.connect(uri, Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, optionMap).get(); reqCounter = 0; } ClientRequest request = new ClientRequest().setMethod(Methods.PUT).setPath(path); request.getRequestHeaders().put(Headers.HOST, "localhost"); if(token != null) request.getRequestHeaders().put(HttpStringConstants.CONSUL_TOKEN, token); connection.sendRequest(request, client.createClientCallback(reference, latch)); latch.await(); reqCounter++; int statusCode = reference.get().getResponseCode(); if(statusCode >= 300){ logger.error("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY)); throw new Exception("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY)); } } catch (Exception e) { logger.error("CheckPass request exception", e); } }
@Override public void checkPass(String serviceId, String token) { if(logger.isDebugEnabled()) logger.debug("checkPass serviceId = " + serviceId); String path = "/v1/agent/check/pass/" + "service:" + serviceId; final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { if(connection == null || !connection.isOpen() || reqCounter >= maxReqPerConn) { if(logger.isDebugEnabled()) logger.debug("connection is closed with counter " + reqCounter + ", reconnecting..."); connection = client.connect(uri, Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, optionMap).get(); reqCounter = 0; } ClientRequest request = new ClientRequest().setMethod(Methods.PUT).setPath(path); request.getRequestHeaders().put(Headers.HOST, "localhost"); if(token != null) request.getRequestHeaders().put(HttpStringConstants.CONSUL_TOKEN, token); connection.sendRequest(request, client.createClientCallback(reference, latch)); latch.await(); reqCounter++; int statusCode = reference.get().getResponseCode(); if(statusCode >= 300){ logger.error("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY)); throw new Exception("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY)); } } catch (Exception e) { logger.error("CheckPass request exception", e); } }
@Override public void unregisterService(String serviceId, String token) { String path = "/v1/agent/service/deregister/" + serviceId; final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { if(connection == null || !connection.isOpen() || reqCounter >= maxReqPerConn) { if(logger.isDebugEnabled()) logger.debug("connection is closed with counter " + reqCounter + ", reconnecting..."); connection = client.connect(uri, Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, optionMap).get(); reqCounter = 0; } ClientRequest request = new ClientRequest().setMethod(Methods.PUT).setPath(path); request.getRequestHeaders().put(Headers.HOST, "localhost"); if(token != null) request.getRequestHeaders().put(HttpStringConstants.CONSUL_TOKEN, token); connection.sendRequest(request, client.createClientCallback(reference, latch)); latch.await(); reqCounter++; int statusCode = reference.get().getResponseCode(); if(statusCode >= 300){ System.out.println("body = " + reference.get().getAttachment(Http2Client.RESPONSE_BODY)); throw new Exception("Failed to unregister on Consul: " + statusCode); } } catch (Exception e) { logger.error("Exception:", e); } }
final ClientConnection connection; try { connection = client.connect(this.url.toURI(), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get(); } catch (Exception e) { throw new ClientException(e);
@Override public void registerService(ConsulService service, String token) { String json = service.toString(); String path = "/v1/agent/service/register"; final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { if(connection == null || !connection.isOpen() || reqCounter >= maxReqPerConn) { if(logger.isDebugEnabled()) logger.debug("connection is closed with counter " + reqCounter + ", reconnecting..."); connection = client.connect(uri, Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, optionMap).get(); reqCounter = 0; } ClientRequest request = new ClientRequest().setMethod(Methods.PUT).setPath(path); if(token != null) request.getRequestHeaders().put(HttpStringConstants.CONSUL_TOKEN, token); request.getRequestHeaders().put(Headers.HOST, "localhost"); request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked"); connection.sendRequest(request, client.createClientCallback(reference, latch, json)); latch.await(); reqCounter++; int statusCode = reference.get().getResponseCode(); if(statusCode >= 300){ throw new Exception("Failed to register on Consul: " + statusCode); } } catch (Exception e) { logger.error("Exception:", e); } }
return ioFuture.get(); } catch (AuthenticationException e) { throw e;
public static String derefToken(DerefRequest derefRequest) throws ClientException { final Http2Client client = Http2Client.getInstance(); final CountDownLatch latch = new CountDownLatch(1); final ClientConnection connection; try { connection = client.connect(new URI(derefRequest.getServerUrl()), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, derefRequest.enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true): OptionMap.EMPTY).get(); } catch (Exception e) { throw new ClientException(e); } final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { ClientRequest request = new ClientRequest().setPath(derefRequest.getUri()).setMethod(Methods.GET); request.getRequestHeaders().put(Headers.AUTHORIZATION, getBasicAuthHeader(derefRequest.getClientId(), derefRequest.getClientSecret())); request.getRequestHeaders().put(Headers.HOST, "localhost"); connection.sendRequest(request, client.createClientCallback(reference, latch)); latch.await(); } catch (Exception e) { logger.error("Exception: ", e); throw new ClientException(e); } finally { IoUtils.safeClose(connection); } return reference.get().getAttachment(Http2Client.RESPONSE_BODY); }
public static String getKey(KeyRequest keyRequest) throws ClientException { final Http2Client client = Http2Client.getInstance(); final CountDownLatch latch = new CountDownLatch(1); final ClientConnection connection; try { connection = client.connect(new URI(keyRequest.getServerUrl()), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, keyRequest.enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true): OptionMap.EMPTY).get(); } catch (Exception e) { throw new ClientException(e); } final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { ClientRequest request = new ClientRequest().setPath(keyRequest.getUri()).setMethod(Methods.GET); if (keyRequest.getClientId()!=null) { request.getRequestHeaders().put(Headers.AUTHORIZATION, getBasicAuthHeader(keyRequest.getClientId(), keyRequest.getClientSecret())); } request.getRequestHeaders().put(Headers.HOST, "localhost"); connection.sendRequest(request, client.createClientCallback(reference, latch)); latch.await(); } catch (Exception e) { logger.error("Exception: ", e); throw new ClientException(e); } finally { IoUtils.safeClose(connection); } return reference.get().getAttachment(Http2Client.RESPONSE_BODY); }