Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package io.undertow.server.handlers.proxy;
 
 
 import java.net.URI;
Simple proxy client provider. This provider simply proxies to another server, using a a one to one connection strategy.

Author(s):
Stuart Douglas
 
 public class SimpleProxyClientProvider implements ProxyClient {
 
     private final URI uri;
     private final AttachmentKey<ClientConnectionclientAttachmentKey = AttachmentKey.create(ClientConnection.class);
     private final UndertowClient client;
 
     private static final ProxyTarget TARGET = new ProxyTarget() {};
 
     public SimpleProxyClientProvider(URI uri) {
         this. = uri;
          = UndertowClient.getInstance();
     }
 
     @Override
     public ProxyTarget findTarget(HttpServerExchange exchange) {
         return ;
     }
 
     @Override
     public void getConnection(ProxyTarget targetHttpServerExchange exchangeProxyCallback<ProxyConnectioncallbacklong timeoutTimeUnit timeUnit) {
         ClientConnection existing = exchange.getConnection().getAttachment();
         if (existing != null) {
             if (existing.isOpen()) {
                 //this connection already has a client, re-use it
                 callback.completed(exchangenew ProxyConnection(existing.getPath() == null ? "/" : .getPath()));
                 return;
             } else {
                 exchange.getConnection().removeAttachment();
             }
         }
         .connect(new ConnectNotifier(callbackexchange), exchange.getIoThread(), exchange.getConnection().getBufferPool(), .);
     }
 
     private final class ConnectNotifier implements ClientCallback<ClientConnection> {
         private final ProxyCallback<ProxyConnectioncallback;
         private final HttpServerExchange exchange;
 
         private ConnectNotifier(ProxyCallback<ProxyConnectioncallbackHttpServerExchange exchange) {
             this. = callback;
             this. = exchange;
         }
 
         @Override
         public void completed(final ClientConnection connection) {
             final ServerConnection serverConnection = .getConnection();
             //we attach to the connection so it can be re-used
             serverConnection.putAttachment(connection);
             serverConnection.addCloseListener(new ServerConnection.CloseListener() {
                 @Override
                 public void closed(ServerConnection serverConnection) {
                     IoUtils.safeClose(connection);
                 }
             });
             connection.getCloseSetter().set(new ChannelListener<Channel>() {
                 @Override
                 public void handleEvent(Channel channel) {
                     serverConnection.removeAttachment();
                 }
            });
            .completed(new ProxyConnection(connection.getPath() == null ? "/" : .getPath()));
        }
        @Override
        public void failed(IOException e) {
            .failed();
        }
    }
New to GrepCode? Check out our FAQ X