io.fabric8.gateway.loadbalancer.ClientRequestFacadeFactory Maven / Gradle / Ivy
/**
* Copyright (C) FuseSource, Inc.
* http://fusesource.com
*
* 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.fabric8.gateway.loadbalancer;
import io.fabric8.gateway.SocketWrapper;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* A factory that creates ClientRequestFacade instances which extract
* values from a NetSocket and ConnectionParameters based on
* the factory is configured.
*/
public class ClientRequestFacadeFactory {
private final ArrayList keyExtractors;
interface KeyExtractor {
String extract(SocketWrapper socket, ConnectionParameters connectionParameters);
}
static enum KeyExtractors implements KeyExtractor {
REMOTE_ADDRESS {
@Override
public String extract(SocketWrapper socket, ConnectionParameters connectionParameters) {
if( socket!=null ) {
InetSocketAddress address = socket.remoteAddress();
if( address!=null ) {
return address.toString();
}
}
return null;
}
},
LOCAL_ADDRESS {
@Override
public String extract(SocketWrapper socket, ConnectionParameters connectionParameters) {
if( socket!=null ) {
InetSocketAddress address = socket.localAddress();
if( address!=null ) {
return address.toString();
}
}
return null;
}
},
PROTOCOL_VIRTUAL_HOST {
@Override
public String extract(SocketWrapper socket, ConnectionParameters connectionParameters) {
if( connectionParameters!=null ) {
return connectionParameters.protocolVirtualHost;
}
return null;
}
},
PROTOCOL_USER {
@Override
public String extract(SocketWrapper socket, ConnectionParameters connectionParameters) {
if( connectionParameters!=null ) {
return connectionParameters.protocolUser;
}
return null;
}
},
PROTOCOL_CLIENT_ID {
@Override
public String extract(SocketWrapper socket, ConnectionParameters connectionParameters) {
if( connectionParameters!=null ) {
return connectionParameters.protocolClientId;
}
return null;
}
},
PROTOCOL_SESSION_ID {
@Override
public String extract(SocketWrapper socket, ConnectionParameters connectionParameters) {
if( connectionParameters!=null ) {
return connectionParameters.protocolSessionId;
}
return null;
}
};
}
public ClientRequestFacadeFactory(String extractors) {
this(toExtractors(extractors));
}
private static ArrayList toExtractors(String extractors) {
String[] parts = extractors.split("[\\s,]+");
ArrayList rc = new ArrayList(parts.length);
for (String part : parts) {
rc.add(KeyExtractors.valueOf(part.toUpperCase()));
}
return rc;
}
public ClientRequestFacadeFactory(KeyExtractor...extractors) {
this(Arrays.asList(extractors));
}
public ClientRequestFacadeFactory(Collection extractors) {
this.keyExtractors = new ArrayList(extractors);
}
public ClientRequestFacade create(final SocketWrapper socket, final ConnectionParameters params) {
return new ClientRequestFacade() {
@Override
public String getClientRequestKey() {
for (KeyExtractor extractor : keyExtractors) {
String value = extractor.extract(socket, params);
if( value!=null ) {
return value;
}
}
return null;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy