package com.lutao.cloud.auth.sso.client;

import com.lutao.cloud.auth.sso.domain.ServiceResponse;
import feign.codec.Decoder;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.List;


@FeignClient(name="sso", url = "${sso.url}", configuration = SSOHttpClient.FeignConfiguration.class)
public interface SSOHttpClient {

    @GetMapping(value = "/p3/serviceValidate")
    @ResponseBody
    ServiceResponse serviceValidate(@RequestParam("service") String loginUrl, @RequestParam("ticket") String ticket);

    class FeignConfiguration {
        @Bean
        public Decoder textPlainDecoder() {
            return new SpringDecoder(() -> new HttpMessageConverters(new CustomJaxb2RootElementHttpMessageConverter()));
        }
    }

    class CustomJaxb2RootElementHttpMessageConverter extends Jaxb2RootElementHttpMessageConverter {
        @Override
        public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
            super.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML));
        }
    }
}
