/** * Create a descriptive string for the type of connection that this session has been created on. This is generally just used * in the Command Center. The following is the same as the insides of ResourceAddress.getProtocol(), which is private. NOTE: * we're using BridgeSession.LOCAL_ADDRESS.get(session) to retrieve the address to send to management. */ public static String getSessionTypeName(ResourceAddress address) { String externalURI = address.getExternalURI(); return URIUtils.getScheme(externalURI); }
public String toString() { Iterator<Entry<ResourceAddress,HttpBinding>> i = entrySet().iterator(); if (! i.hasNext()) return "{}"; StringBuilder sb = new StringBuilder(); sb.append("{\n"); for (;;) { Entry<ResourceAddress, HttpBinding> e = i.next(); ResourceAddress key = e.getKey(); HttpBinding value = e.getValue(); String location = key.getExternalURI(); String nextProtocol = key.getOption(ResourceAddress.NEXT_PROTOCOL); String nextProtocolStr = nextProtocol == null ? "" : " " + nextProtocol; sb.append(" ").append('[').append(URIUtils.resolve(location, "/")).append(nextProtocolStr).append(']'); sb.append('='); sb.append(value); if (! i.hasNext()) { return sb.append("\n}").toString(); } sb.append(',').append('\n'); } }
@Override public String toString() { String externalRemoteURI = getRemoteAddress().getExternalURI(); String externalLocalURI = getLocalAddress().getExternalURI(); if (getService() instanceof BridgeAcceptor) { StringBuilder sb = new StringBuilder(); sb.append('(').append(getIdAsString()).append(": ").append(getServiceName()); sb.append(", server, ").append(externalRemoteURI).append(" => "); sb.append(externalLocalURI).append(')'); String result = sb.toString(); return result; } else { return "(" + getIdAsString() + ": " + getServiceName() + ", client, " + externalLocalURI + " => " + externalRemoteURI + ')'; } }
@Override protected UnbindFuture unbindInternal(ResourceAddress address, IoHandler handler, BridgeSessionInitializer<? extends IoFuture> initializer) { if (logger.isTraceEnabled()) { logger.trace(format("unbinding: '%s' %s", address.getExternalURI(), address.getOption(NEXT_PROTOCOL))); } ResourceAddress transport = address.getTransport(); BridgeAcceptor acceptor = bridgeServiceFactory.newBridgeAcceptor(transport); return acceptor.unbind(transport); }
@Override public void bind(ResourceAddress address, IoHandler handler, BridgeSessionInitializer<? extends IoFuture> initializer) { // bind only address with matching scheme URI location = address.getResource(); String schemeName = location.getScheme(); if (!canBind(schemeName)) { throw new IllegalArgumentException(format("Unexpected scheme \"%s\" for URI: %s", schemeName, location)); } // note: ignore BIND_ALTERNATE (used by delegate acceptors) do { BridgeAcceptor bridgeAcceptor = selectWsAcceptor(address); if ( bridgeAcceptor != null ) { bridgeAcceptor.bind(address, handler, initializer); } else { if (logger.isDebugEnabled()) { String format = "No bridge acceptor found for address '%s'. Is the %s transport enabled?"; logger.warn(format(format, address.getExternalURI(), URIUtils.getScheme(address.getExternalURI()))); } } address = address.getOption(ALTERNATE); } while (address != null); }
@Override public UnbindFuture unbind(ResourceAddress address) { UnbindFuture future = DefaultUnbindFuture.succeededFuture(); // note: ignore BIND_ALTERNATE (used by delegate acceptors) do { BridgeAcceptor bridgeAcceptor = selectWsAcceptor(address); if ( bridgeAcceptor == null ) { if (logger.isTraceEnabled()) { logger.trace(format("The acceptor for %s is not enabled.", address.getExternalURI())); } } else { future = combineFutures(future, bridgeAcceptor.unbind(address)); } address = address.getOption(ALTERNATE); } while (address != null); return future; }
protected final ResourceAddress resolve(String oldPath, String newPath) { URI addressURI = getResource(); String externalURI = getExternalURI(); boolean newPathDiffersFromOld = !oldPath.equals(newPath); if ( !newPathDiffersFromOld ) { return this; } boolean shouldResolveNewPath = oldPath.equals(addressURI.getPath()); if (!shouldResolveNewPath) { return this; } URI newResourceURI = addressURI.resolve(newPath); String newExternalURI = URIUtils.resolve(externalURI, newPath); ResourceOptions newOptions = FACTORY.newResourceOptions(this); resolve(oldPath, newPath, newOptions); String externalUriToString = newExternalURI; String newResourceUriToString = URIUtils.uriToString(newResourceURI); return factory.newResourceAddress0(externalUriToString, newResourceUriToString, newOptions); }
private List<ResourceAddress> asResourceAddressList(ResourceAddress addr) { List<ResourceAddress> result = new ArrayList<>(); List<ResourceAddress> topLevelAddresses = new ArrayList<>(); ResourceAddress cursor = addr; do { topLevelAddresses.add(cursor); cursor = cursor.getOption(ResourceAddress.ALTERNATE); } while (cursor != null); //recurse for ( ResourceAddress resourceAddress: topLevelAddresses ) { ResourceAddress transport = resourceAddress.getTransport(); if ( transport != null ) { for (ResourceAddress transportAddress: asResourceAddressList(transport)) { ResourceOptions newResultOptions = ResourceOptions.FACTORY.newResourceOptions(resourceAddress); newResultOptions.setOption(TRANSPORT, transportAddress); ResourceAddress newResult = addressFactory.newResourceAddress(resourceAddress.getExternalURI(), newResultOptions); result.add(newResult); } } else { result.add(resourceAddress); } } return result; } }
private ResourceAddress createApiHttpAddress(ResourceAddress httpTransport) { String path = URIUtils.getPath(appendURI(ensureTrailingSlash(httpTransport.getExternalURI()), HttpProtocolCompatibilityFilter.API_PATH)); String httpLocation = modifyURIPath(httpTransport.getExternalURI(), path); ResourceOptions httpOptions = ResourceOptions.FACTORY.newResourceOptions(httpTransport); httpOptions.setOption(NEXT_PROTOCOL, null); // terminal endpoint, so next protocol null httpOptions.setOption(ALTERNATE, null); return resourceAddressFactory.newResourceAddress(httpLocation, httpOptions); }
private ResourceAddress createApiHttpxeAddress(ResourceAddress httpxeTransport) { String path = URIUtils.getPath(appendURI(ensureTrailingSlash(httpxeTransport.getExternalURI()), HttpProtocolCompatibilityFilter.API_PATH)); httpxeTransport = httpxeTransport.resolve(path); String httpxeLocation = modifyURIPath(httpxeTransport.getExternalURI(), path); ResourceOptions httpxeOptions = ResourceOptions.FACTORY.newResourceOptions(httpxeTransport); httpxeOptions.setOption(NEXT_PROTOCOL, null); // terminal endpoint, so next protocol null return resourceAddressFactory.newResourceAddress(httpxeLocation, httpxeOptions); }
@Override protected <T extends IoFuture> void bindInternal(final ResourceAddress address, IoHandler handler, final BridgeSessionInitializer<T> initializer) { if (logger.isTraceEnabled()) { logger.trace(format("binding: '%s' %s", address.getExternalURI(), address.getOption(NEXT_PROTOCOL))); } // // Bind the transport of the address. // final ResourceAddress transportAddress = address.getTransport(); final URI transportURI = transportAddress.getResource(); final Protocol transportProtocol = bridgeServiceFactory.getTransportFactory().getProtocol(transportURI.getScheme()); final BridgeSessionInitializer<T> sessionInitializer = (initializer != null) ? initializer.getParentInitializer(transportProtocol) : null; BridgeAcceptor acceptor = bridgeServiceFactory.newBridgeAcceptor(transportAddress); acceptor.bind(transportAddress, bridgeHandler, sessionInitializer); }
private ResourceAddress createWsrRtmpAddress(ResourceAddress address) { // wsr[wsr/1.0] | tcp or wsr[wsr/1.0] | ssl // so find http[http/1.1] layer's transport either way and you'll get ssl or tcp. ResourceAddress wsrRtmpTransportAddress; wsrRtmpTransportAddress = address.findTransport("http[http/1.1]").getTransport(); ResourceOptions wsrRtmpTransportAddressOptions = ResourceOptions.FACTORY.newResourceOptions(wsrRtmpTransportAddress); wsrRtmpTransportAddressOptions.setOption(NEXT_PROTOCOL, "rtmp/1.0"); wsrRtmpTransportAddressOptions.setOption(ResourceAddress.ALTERNATE, null); wsrRtmpTransportAddress = resourceAddressFactory.newResourceAddress(wsrRtmpTransportAddress.getExternalURI(), wsrRtmpTransportAddressOptions); ResourceOptions wsrRtmpAddressOptions = ResourceOptions.FACTORY.newResourceOptions(address); wsrRtmpAddressOptions.setOption(TRANSPORT, wsrRtmpTransportAddress); return resourceAddressFactory.newResourceAddress(address.getExternalURI(), wsrRtmpAddressOptions); }
@Override public ConnectFuture connect(ResourceAddress address, final IoHandler connectHandler, final IoSessionInitializer<ConnectFuture> connectSessionInitializer) { String uriScheme = getScheme(address.getExternalURI()); Transport transport = transportFactory.getTransportForScheme(uriScheme); BridgeConnector connector = transport.getConnector(address); return connector.connect(address, connectHandler, new IoSessionInitializer<ConnectFuture>() { @Override public void initializeSession(IoSession session, ConnectFuture future) { sessionInitializer.initializeSession(session, future); if (connectSessionInitializer != null) { connectSessionInitializer.initializeSession(session, future); } } }); }
private ResourceAddress httpAddressNoSecurity(ResourceAddress httpAddress, ResourceAddress httpxeAddressNoSecurity, IdentityResolver resolver) { ResourceOptions noSecurityOptions = new NoSecurityResourceOptions(httpAddress); noSecurityOptions.setOption(ALTERNATE, httpxeAddressNoSecurity); noSecurityOptions.setOption(ResourceAddress.IDENTITY_RESOLVER, resolver); noSecurityOptions.setOption(HttpResourceAddress.REALMS, new HttpRealmInfo[0]); return resourceAddressFactory.newResourceAddress(httpAddress.getExternalURI(), noSecurityOptions, httpAddress.getOption(ResourceAddress.QUALIFIER)); }
@Override public void bind(final ResourceAddress address, IoHandler handler, BridgeSessionInitializer<? extends IoFuture> initializer) throws NioBindException { boolean useMCP = false; try { String uri = address.getExternalURI(); InetAddress inet = InetAddress.getByName(URIUtils.getHost(uri)); if (inet.isMulticastAddress()) { useMCP = true; } } catch (Exception e) { // do nothing } if (useMCP) { MulticastAcceptor acceptor = new MulticastAcceptor(); acceptor.setConfiguration(new Properties()); acceptor.setResourceAddressFactory(resourceAddressFactory); acceptor.setBridgeServiceFactory(bridgeServiceFactory); acceptor.bind(address, handler, initializer); } else { super.bind(address, handler, initializer); } }
@Override public ConnectFuture connect(ResourceAddress address, IoHandler handler, IoSessionInitializer<? extends ConnectFuture> initializer) { boolean useMCP = false; try { String uri = address.getExternalURI(); InetAddress inet = InetAddress.getByName(URIUtils.getHost(uri)); if (inet.isMulticastAddress()) { useMCP = true; } } catch (Exception e) { // do nothing } if (useMCP) { MulticastConnector connector = new MulticastConnector(); connector.setConfiguration(getProperties()); connector.setResourceAddressFactory(resourceAddressFactory); connector.setBridgeServiceFactory(bridgeServiceFactory); return connector.connect(address, handler, initializer); } else { return super.connect(address, handler, initializer); } }
private ResourceAddress getSslSessionLocalAddress(IoSession session) { // note: bound address is unified in SSL options during bind to avoid conflicts like different cipher suites ResourceAddress boundAddress = SslAcceptor.SSL_RESOURCE_ADDRESS.remove(session); assert (boundAddress != null); // construct the candidate address with observed transport and next protocol String candidateURI = boundAddress.getExternalURI(); ResourceOptions candidateOptions = ResourceOptions.FACTORY.newResourceOptions(boundAddress); candidateOptions.setOption(NEXT_PROTOCOL, NEXT_PROTOCOL_KEY.get(session)); candidateOptions.setOption(TRANSPORT, LOCAL_ADDRESS.get(session)); ResourceAddress candidateAddress = resourceAddressFactory.newResourceAddress(candidateURI, candidateOptions); // lookup the binding for this candidate address Binding binding = bindings.getBinding(candidateAddress); return (binding != null) ? binding.bindAddress() : null; }
private ResourceAddress createApiAddress(ResourceAddress address) { ResourceAddress transport = address.getTransport(); ResourceOptions apiAddressOptions = ResourceOptions.FACTORY.newResourceOptions(transport); // /;api/operation is a terminal endpoint so next protocol should be null apiAddressOptions.setOption(NEXT_PROTOCOL, null); // even if the address has an alternate, do not bind the alternate apiAddressOptions.setOption(BIND_ALTERNATE, Boolean.FALSE); String path = URIUtils.getPath(appendURI(ensureTrailingSlash(address.getExternalURI()), HttpProtocolCompatibilityFilter.API_PATH)); String apiLocation = URIUtils.modifyURIPath(URIUtils.uriToString(transport.getResource()), path); return resourceAddressFactory.newResourceAddress(apiLocation, apiAddressOptions); }
private ResourceAddress httpxeAddressNoSecurity(ResourceAddress httpxeAddress, IdentityResolver resolver) { // Remove REALM_NAME option at http layer (upstream and downstream requests shouldn't have to // go through authentication/authorization) ResourceAddress httpAddress = httpxeAddress.getTransport(); ResourceOptions noSecurityOptions = new NoSecurityResourceOptions(httpAddress); noSecurityOptions.setOption(ResourceAddress.IDENTITY_RESOLVER, resolver); ResourceAddress httpAddressNoSecurity = resourceAddressFactory.newResourceAddress( httpAddress.getExternalURI(), noSecurityOptions, httpAddress.getOption(ResourceAddress.QUALIFIER)); // Remove REALM_NAME option at httpxe layer but preserve all other options like // ORIGIN_SECURITY etc. Otherwise, upstream and downstream requests will be subjected // to different origin security constraints. Then finally add http as transport to httpxe ResourceOptions httpxeOptions = ResourceOptions.FACTORY.newResourceOptions(httpxeAddress); httpxeOptions.setOption(TRANSPORT, httpAddressNoSecurity); httpxeOptions.setOption(ResourceAddress.IDENTITY_RESOLVER, resolver); httpxeOptions = new NoSecurityResourceOptions(httpxeOptions); return resourceAddressFactory.newResourceAddress(URIUtils.uriToString(httpxeAddress.getResource()), httpxeOptions); }
private ResourceAddress getResourcesAddress(HttpBinding newHttpBinding) { ResourceAddress bindAddress = newHttpBinding.bindAddress(); String location = bindAddress.getExternalURI(); String resourcesURI = URIUtils.resolve(location, "/;resource"); ResourceOptions options = ResourceOptions.FACTORY.newResourceOptions(); options.setOption(TRANSPORT_URI, bindAddress.getOption(TRANSPORT_URI)); options.setOption(TRANSPORT, bindAddress.getOption(TRANSPORT)); options.setOption(TEMP_DIRECTORY, bindAddress.getOption(TEMP_DIRECTORY)); options.setOption(NEXT_PROTOCOL, bindAddress.getOption(NEXT_PROTOCOL)); options.setOption(ORIGIN_SECURITY, bindAddress.getOption(ORIGIN_SECURITY)); options.setOption(GATEWAY_ORIGIN_SECURITY, bindAddress.getOption(GATEWAY_ORIGIN_SECURITY)); options.setOption(BALANCE_ORIGINS, bindAddress.getOption(BALANCE_ORIGINS)); return addressFactory.newResourceAddress(resourcesURI, options); }