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

org.cattleframework.cloud.discovery.enhancement.feign.FeignClient Maven / Gradle / Ivy

There is a newer version: 1.0.1-M5
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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 org.cattleframework.cloud.discovery.enhancement.feign;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;

import org.cattleframework.cloud.discovery.enhancement.EnhancementConstants;
import org.cattleframework.cloud.discovery.enhancement.context.DataContextHolder;
import org.cattleframework.cloud.discovery.enhancement.plugin.PluginContext;
import org.cattleframework.cloud.discovery.enhancement.plugin.PluginRunner;
import org.cattleframework.cloud.discovery.enhancement.plugin.PluginType;
import org.cattleframework.cloud.discovery.enhancement.plugin.RequestContext;
import org.cattleframework.cloud.discovery.enhancement.plugin.ResponseContext;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;

import feign.Client;
import feign.Request;
import feign.Request.Options;
import feign.Response;
import feign.Util;

/**
 * Feign客户端
 * 
 * @author orange
 *
 */
public class FeignClient implements Client {

    private final Client delegate;

    private final PluginRunner pluginRunner;

    public FeignClient(Client target, PluginRunner pluginRunner) {
	this.delegate = Util.checkNotNull(target, "target");
	this.pluginRunner = pluginRunner;
    }

    @Override
    public Response execute(Request request, Options options) throws IOException {
	PluginContext pluginContext = new PluginContext();
	HttpHeaders requestHeaders = new HttpHeaders();
	request.headers().forEach((s, strings) -> requestHeaders.addAll(s, new ArrayList(strings)));
	URI url = URI.create(request.url());
	RequestContext requestContext = RequestContext.builder().httpHeaders(requestHeaders)
		.httpMethod(HttpMethod.valueOf(request.httpMethod().name())).url(url).build();
	pluginContext.setRequest(requestContext);
	pluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
	pluginContext.setTargetServiceInstance((ServiceInstance) DataContextHolder.get().getLoadbalancerDataMap()
		.get(EnhancementConstants.LOAD_BALANCER_SERVICE_INSTANCE), url);
	pluginRunner.run(PluginType.Client.PRE, pluginContext);
	long startMillis = System.currentTimeMillis();
	try {
	    Response response = delegate.execute(request, options);
	    pluginContext.setDelay(System.currentTimeMillis() - startMillis);
	    HttpHeaders responseHeaders = new HttpHeaders();
	    response.headers().forEach((s, strings) -> responseHeaders.addAll(s, new ArrayList(strings)));
	    ResponseContext responseContext = ResponseContext.builder().httpStatus(response.status())
		    .httpHeaders(responseHeaders).build();
	    pluginContext.setResponse(responseContext);
	    pluginRunner.run(PluginType.Client.POST, pluginContext);
	    return response;
	} catch (IOException e) {
	    pluginContext.setDelay(System.currentTimeMillis() - startMillis);
	    pluginContext.setThrowable(e);
	    pluginRunner.run(PluginType.Client.EXCEPTION, pluginContext);
	    throw e;
	} finally {
	    pluginRunner.run(PluginType.Client.FINALLY, pluginContext);
	}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy