Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleHaUrlRewriteFunctionDescriptor
linenumberstrue
public class HaUrlRewriteFunctionDescriptor implements UrlRewriteFunctionDescriptor<HaUrlRewriteFunctionDescriptor> {
    public static final String FUNCTION_NAME = "ha-rewrite";
    private String configLocation;
    @Override
    public String name() {
        return FUNCTION_NAME;
    }
    public HaUrlRewriteFunctionDescriptor config( String configLocation ) {
        this.configLocation = configLocation;
        return this;
    }
    public String config() {
        return configLocation;
    }
    public String getConfig() {
        return config();
    }
    public void setConfig( String configLocation ) {
        config( configLocation );
    }
}
Code Block
languagejava
titleHaUrlRewriteFunctionDescriptor
linenumberstrue
public class HaUrlRewriteFunctionProcessor implements UrlRewriteFunctionProcessor<HaUrlRewriteFunctionDescriptor> {
    private HostMapperService hostMapperService;
    private HostMapper hostMapper = null;
    private String clusterName;

    @Override
    public String name() {
        return HaUrlRewriteFunctionDescriptor.FUNCTION_NAME;
    }
    @Override
    public void initialize(UrlRewriteEnvironment environment, HaUrlRewriteFunctionDescriptor descriptor) throws Exception {
        hostMapper = new HaBaseStrategyHostMapper();
        clusterName = environment.getAttribute(  GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE );
        GatewayServices services = environment.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE );
        if( clusterName != null && services != null ) {
            hostMapperService = services.getService( GatewayServices.HOST_MAPPING_SERVICE );
            if( hostMapperService != null ) {
                hostMapperService.registerHostMapperForCluster( clusterName, hostMapper );
            }
        }
    }
    @Override
    public void destroy() throws Exception {
        if( hostMapperService != null && clusterName != null ) {
            hostMapperService.removeHostMapperForCluster( clusterName );
        }
    }
    @Override
    public List<String> resolve(UrlRewriteContext context, List<String> parameters) throws Exception {
        List<String> result = null;
        if( parameters != null ) {
            result = new ArrayList<String>( parameters.size() );
            for( String parameter : parameters ) {
                switch( context.getDirection() ) {
                    case IN:
                        parameter = hostMapper.resolveInboundHostName( parameter );
                        break;
                    case OUT:
                        parameter = hostMapper.resolveOutboundHostName( parameter );
                        break;
                }
                result.add( parameter );
            }
        }
        return result;
    }
}