// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/FeignReqInterceptor.java
// public class FeignReqInterceptor implements RequestInterceptor {
// 
//   @Override
//   public void apply(RequestTemplate requestTemplate) {
//     ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
//         .getRequestAttributes();
//     if (attributes == null) {
//       return;
//     }
//     HttpServletRequest request = attributes.getRequest();
//     if (request == null) {
//       return;
//     }
//     Enumeration<String> headerNames = request.getHeaderNames();
//     if (headerNames != null) {
//       while (headerNames.hasMoreElements()) {
//         String name = headerNames.nextElement();
//         String values = request.getHeader(name);
//         requestTemplate.header(name, values);
//       }
//     }
//   }
// }
// 
// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/ResponseHttpMessageConverter.java
// public class ResponseHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
// 
//     @Override
//     protected boolean supports(Class<?> clazz) {
//         return true;
//     }
// 
//     @Override
//     protected boolean canRead(MediaType mediaType) {
//         return true;
//     }
// 
//     @Override
//     protected boolean canWrite(MediaType mediaType) {
//         return false;
//     }
// 
//     @Override
//     protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
// 
//         if (clazz.isAssignableFrom(byte[].class)||clazz.isAssignableFrom(Byte[].class)) {
//             return Util.toByteArray(inputMessage.getBody());
//         }
//         String body = Util.toString(new InputStreamReader(inputMessage.getBody()));
//         
// //        JSONObject obj = JSON.parseObject(body);
// //        Integer code = obj.getInteger("code");
// //        if (code != null && code != RespCode.SUCCESS) {
// //            throw new BusinessException(code);
// //        }
// 
//         return JSON.parseObject(body, clazz);
// 
//     }
// 
//     @Override
//     protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// 
// 
//     }
// 
// 
// 
// 
// }
// 
// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/CustomErrorDecoder.java
// public class CustomErrorDecoder implements ErrorDecoder {
// 
//   @Override
//   public Exception decode(String s, Response response) {
//     try {
//       if (response.body() != null) {
//         String body = Util.toString(response.body().asReader());
//         Resp req = JSON.parseObject(body, Resp.class);
//         if (req.getCode() != RespCode.SUCCESS) {
//           return new BusinessException(req.getCode(), req.getMsg(), null);
//         }
//       }
//     } catch (IOException e) {
//       return e;
//     }
//     return new Exception("UNKOWN ERROR");
//   }
// 
// 
// }


import com.jayqqaa12.jbase.spring.feign.FeignReqInterceptor;
import com.jayqqaa12.jbase.spring.feign.ResponseHttpMessageConverter;
import com.jayqqaa12.jbase.spring.feign.CustomErrorDecoder;
import feign.*;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
package com.jayqqaa12.jbase.spring.feign;



/**
 * Created by 12 on 2017/12/2.
 */
@Configuration
@ConditionalOnClass({Feign.class})
public class FiegnConfig {

  @Bean
  public ErrorDecoder errorDecoder() {
    return new CustomErrorDecoder();
  }
  @Bean
  public Decoder feignDecoder() {
    return new ResponseEntityDecoder(
// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/FeignReqInterceptor.java
// public class FeignReqInterceptor implements RequestInterceptor {
// 
//   @Override
//   public void apply(RequestTemplate requestTemplate) {
//     ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
//         .getRequestAttributes();
//     if (attributes == null) {
//       return;
//     }
//     HttpServletRequest request = attributes.getRequest();
//     if (request == null) {
//       return;
//     }
//     Enumeration<String> headerNames = request.getHeaderNames();
//     if (headerNames != null) {
//       while (headerNames.hasMoreElements()) {
//         String name = headerNames.nextElement();
//         String values = request.getHeader(name);
//         requestTemplate.header(name, values);
//       }
//     }
//   }
// }
// 
// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/ResponseHttpMessageConverter.java
// public class ResponseHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
// 
//     @Override
//     protected boolean supports(Class<?> clazz) {
//         return true;
//     }
// 
//     @Override
//     protected boolean canRead(MediaType mediaType) {
//         return true;
//     }
// 
//     @Override
//     protected boolean canWrite(MediaType mediaType) {
//         return false;
//     }
// 
//     @Override
//     protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
// 
//         if (clazz.isAssignableFrom(byte[].class)||clazz.isAssignableFrom(Byte[].class)) {
//             return Util.toByteArray(inputMessage.getBody());
//         }
//         String body = Util.toString(new InputStreamReader(inputMessage.getBody()));
//         
// //        JSONObject obj = JSON.parseObject(body);
// //        Integer code = obj.getInteger("code");
// //        if (code != null && code != RespCode.SUCCESS) {
// //            throw new BusinessException(code);
// //        }
// 
//         return JSON.parseObject(body, clazz);
// 
//     }
// 
//     @Override
//     protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// 
// 
//     }
// 
// 
// 
// 
// }
// 
// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/CustomErrorDecoder.java
// public class CustomErrorDecoder implements ErrorDecoder {
// 
//   @Override
//   public Exception decode(String s, Response response) {
//     try {
//       if (response.body() != null) {
//         String body = Util.toString(response.body().asReader());
//         Resp req = JSON.parseObject(body, Resp.class);
//         if (req.getCode() != RespCode.SUCCESS) {
//           return new BusinessException(req.getCode(), req.getMsg(), null);
//         }
//       }
//     } catch (IOException e) {
//       return e;
//     }
//     return new Exception("UNKOWN ERROR");
//   }
// 
// 
// }


// Path: jbase-spring/src/main/java/com/jayqqaa12/jbase/spring/feign/FiegnConfig.java
import com.jayqqaa12.jbase.spring.feign.FeignReqInterceptor;
import com.jayqqaa12.jbase.spring.feign.ResponseHttpMessageConverter;
import com.jayqqaa12.jbase.spring.feign.CustomErrorDecoder;
import feign.*;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

package com.jayqqaa12.jbase.spring.feign;



/**
 * Created by 12 on 2017/12/2.
 */
@Configuration
@ConditionalOnClass({Feign.class})
public class FiegnConfig {

  @Bean
  public ErrorDecoder errorDecoder() {
    return new CustomErrorDecoder();
  }
  @Bean
  public Decoder feignDecoder() {
    return new ResponseEntityDecoder(
        new SpringDecoder(() -> new HttpMessageConverters(new ResponseHttpMessageConverter())));