Search in sources :

Example 1 with UnknownSchemeException

use of com.linkedin.r2.netty.common.UnknownSchemeException in project rest.li by linkedin.

the class AbstractNettyClient method writeRequest.

/**
 * This method calls the user defined method {@link AbstractNettyClient#doWriteRequest(Request, RequestContext, SocketAddress, Map, TimeoutTransportCallback, long)}
 * after having checked that the client is still running and resolved the DNS
 */
private void writeRequest(Req request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<Res> callback) {
    // Decorates callback
    TransportCallback<Res> executionCallback = getExecutionCallback(callback);
    TransportCallback<Res> shutdownAwareCallback = getShutdownAwareCallback(executionCallback);
    // Resolves request timeout
    long requestTimeout = HttpNettyClient.resolveRequestTimeout(requestContext, _requestTimeout);
    // By wrapping the callback in a Timeout callback before passing it along, we deny the rest
    // of the code access to the unwrapped callback.  This ensures two things:
    // 1. The user callback will always be invoked, since the Timeout will eventually expire
    // 2. The user callback is never invoked more than once
    TimeoutTransportCallback<Res> timeoutCallback = new TimeoutTransportCallback<>(_scheduler, requestTimeout, TimeUnit.MILLISECONDS, shutdownAwareCallback, "Exceeded request timeout of " + requestTimeout + "ms");
    // check lifecycle
    NettyClientState state = _state.get();
    if (state != NettyClientState.RUNNING) {
        errorResponse(callback, new IllegalStateException("Client is " + state));
        return;
    }
    // resolve address
    final SocketAddress address;
    try {
        TimingContextUtil.markTiming(requestContext, TIMING_KEY);
        address = HttpNettyClient.resolveAddress(request, requestContext);
        TimingContextUtil.markTiming(requestContext, TIMING_KEY);
    } catch (UnknownHostException | UnknownSchemeException e) {
        errorResponse(callback, e);
        return;
    }
    doWriteRequest(request, requestContext, address, wireAttrs, timeoutCallback, requestTimeout);
}
Also used : UnknownHostException(java.net.UnknownHostException) UnknownSchemeException(com.linkedin.r2.netty.common.UnknownSchemeException) TimeoutTransportCallback(com.linkedin.r2.transport.http.client.TimeoutTransportCallback) NettyClientState(com.linkedin.r2.netty.common.NettyClientState) SocketAddress(java.net.SocketAddress)

Example 2 with UnknownSchemeException

use of com.linkedin.r2.netty.common.UnknownSchemeException in project rest.li by linkedin.

the class HttpNettyClient method resolveAddress.

/**
 * Resolves the IP Address from the URI host
 *
 * @param request Request object
 * @param requestContext Request's context
 * @return SocketAddress resolved from the URI host
 */
public static SocketAddress resolveAddress(Request request, RequestContext requestContext) throws UnknownHostException, UnknownSchemeException {
    final URI uri = request.getURI();
    final String scheme = uri.getScheme();
    if (!HTTP_SCHEME.equalsIgnoreCase(scheme) && !HTTPS_SCHEME.equalsIgnoreCase(scheme)) {
        throw new UnknownSchemeException("Unknown scheme: " + scheme + " (only http/https is supported)");
    }
    final String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        port = HTTP_SCHEME.equalsIgnoreCase(scheme) ? HTTP_DEFAULT_PORT : HTTPS_DEFAULT_PORT;
    }
    // TODO investigate DNS resolution and timing
    final InetAddress inetAddress = InetAddress.getByName(host);
    final SocketAddress address = new InetSocketAddress(inetAddress, port);
    requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_ADDR, inetAddress.getHostAddress());
    requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_PORT, port);
    return address;
}
Also used : UnknownSchemeException(com.linkedin.r2.netty.common.UnknownSchemeException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) InetAddress(java.net.InetAddress)

Aggregations

UnknownSchemeException (com.linkedin.r2.netty.common.UnknownSchemeException)2 SocketAddress (java.net.SocketAddress)2 NettyClientState (com.linkedin.r2.netty.common.NettyClientState)1 TimeoutTransportCallback (com.linkedin.r2.transport.http.client.TimeoutTransportCallback)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1