package com.docus.server;

import com.docus.server.collection.webservice.IDeptServer;
import com.docus.server.collection.webservice.IUserServer;
import com.docus.server.collection.webservice.ReceiveServer;
import com.docus.server.collection.webservice.BasicService;
import org.apache.cxf.jaxws.EndpointImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.transport.servlet.CXFServlet;

import javax.xml.ws.Endpoint;

@Configuration
@RequiredArgsConstructor
public class CxfConfig {
    private final IUserServer userServer;

    private final IDeptServer deptServer;

    private final BasicService tBasicService;
    private final ReceiveServer receiveServer;
    /**
     * 注入Servlet，注意beanName不能为dispatcherServlet
     * @author Fang Ruichuan
     * @date 2022/11/14 19:16
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), receiveServer);
        endpoint.publish("/api");
        return endpoint;
    }

    @Bean
    @Qualifier("userEndPoint")
    public Endpoint userEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userServer);
        endpoint.publish("/api/user");
        return endpoint;
    }

    @Bean
    @Qualifier("deptEndPoint")
    public Endpoint deptEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), deptServer);
        endpoint.publish("/api/dept");
        return endpoint;
    }

    @Bean
    public Endpoint endpoint2() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), tBasicService);
        endpoint.publish("/api/basic");
        return endpoint;
    }
}
