All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.gateway.handlers.http.MappedServices Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright 2005-2015 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you 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.handlers.http;

import io.fabric8.gateway.api.ServiceDetails;
import io.fabric8.gateway.api.handlers.http.IMappedServices;
import io.fabric8.gateway.api.handlers.http.ProxyMappingDetails;
import io.fabric8.gateway.handlers.http.policy.ReverseUriPolicy;
import io.fabric8.gateway.loadbalancer.LoadBalancer;

import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpClientResponse;
import org.vertx.java.core.http.HttpServerRequest;

/**
 * Represents the mapped services and the relevant mapping information so that a service implementation can be
 * chosen using a load balancer together with wrapping the client in whatever policies are required.
 */
public class MappedServices implements IMappedServices {
    private final ServiceDetails serviceDetails;
    private final LoadBalancer loadBalancer;
    private final boolean reverseHeaders;
    private Set serviceUrls = new CopyOnWriteArraySet();
    private ProxyMappingDetails proxyMappingDetails;

    public MappedServices(String service, ServiceDetails serviceDetails, LoadBalancer loadBalancer, boolean reverseHeaders) {
        this.serviceDetails = serviceDetails;
        this.loadBalancer = loadBalancer;
        this.reverseHeaders = reverseHeaders;
        serviceUrls.add(service);
    }

    @Override
    public String toString() {
        return "MappedServices{" +
                "serviceUrls=" + serviceUrls +
                '}';
    }

    /**
     * Chooses a request to use
     */
    public String chooseService(HttpServerRequest request) {
        return loadBalancer.choose(new ArrayList(serviceUrls), new HttpClientRequestFacade(request));
    }

    /**
     * Provides a hook so we can wrap a client response handler in a policy such
     * as to reverse the URIs {@link io.fabric8.gateway.handlers.http.policy.ReverseUriPolicy} or
     * add metering, limits, security or contract checks etc.
     */
    public Handler wrapResponseHandlerInPolicies(
    		HttpServerRequest request, 
    		Handler responseHandler, 
    		ProxyMappingDetails proxyMappingDetails) {
        if (reverseHeaders) {
            responseHandler = new ReverseUriPolicy(this, request, responseHandler, proxyMappingDetails);
        }
        return responseHandler;
    }

    /**
     * Rewrites the URI response from a request to a URI in the gateway namespace
     */
    public String rewriteUrl(String proxiedUrl) {
        return proxiedUrl;
    }
    public String getContainer() {
        return serviceDetails.getContainer();
    }

    public String getVersion() {
        return serviceDetails.getVersion();
    }

    public String getId() {
        return serviceDetails.getId();
    }

    public boolean isReverseHeaders() {
        return reverseHeaders;
    }

    public ServiceDetails getServiceDetails() {
        return serviceDetails;
    }

    public Set getServiceUrls() {
        return serviceUrls;
    }

	public ProxyMappingDetails getProxyMappingDetails() {
		return proxyMappingDetails;
	}

	public void setProxyMappingDetails(ProxyMappingDetails proxyMappingDetails) {
		this.proxyMappingDetails = proxyMappingDetails;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy