package org.apache.camel.component.cxf.jaxrs;

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
import org.apache.camel.component.cxf.jaxrs.testbean.CustomerService;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean;
import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.message.MessageContentsList;
import org.junit.Test;

import javax.ws.rs.HttpMethod;

public class CxfRsProducerEndpointConfigurerTest extends CamelTestSupport {

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                CxfRsEndpoint endpoint = new CxfRsEndpoint();
                endpoint.setAddress("http://localhost:8000");
                endpoint.setCamelContext(context);
                endpoint.setResourceClasses(CustomerService.class);
                endpoint.setEndpointUriIfNotSpecified("cxfrs:simple");
                endpoint.setCxfRsEndpointConfigurer(new MyCxfRsEndpointConfigurer());

                from("direct:start")
                        .to(endpoint)
                        .to("mock:end");

                from("jetty:http://localhost:8000?matchOnUriPrefix=true")
                        .to("mock:result")
                        .process(exchange -> exchange.getIn().setBody(new Customer()));
            }
        };
    }

    @Test
    public void testCxfRsEndpoinConfigurerProxyApi() throws InterruptedException {
        template.send("direct:start", exchange -> {
            exchange.setPattern(ExchangePattern.InOut);
            Message inMessage = exchange.getIn();
            inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer");
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
            MessageContentsList messageContentsList = new MessageContentsList();
            messageContentsList.add("1");
            inMessage.setBody(messageContentsList);
        });
        getMockEndpoint("mock:result").expectedHeaderReceived("foo", "bar");
        assertMockEndpointsSatisfied();
    }

    @Test
    public void testCxfRsEndpointConfigurerHttpApi() throws InterruptedException {
        template.send("direct:start", exchange -> {
            exchange.setPattern(ExchangePattern.InOut);
            Message inMessage = exchange.getIn();
            inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/1");
            inMessage.setHeader(Exchange.HTTP_METHOD, HttpMethod.GET);
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
        });
        getMockEndpoint("mock:result").expectedHeaderReceived("foo", "bar");
        assertMockEndpointsSatisfied();
    }

    public static class MyCxfRsEndpointConfigurer implements CxfRsEndpointConfigurer {

        @Override
        public void configure(AbstractJAXRSFactoryBean factoryBean) {
        }

        @Override
        public void configureClient(Client client) {
            client.header("foo", "bar");
        }

        @Override
        public void configureServer(Server server) {
        }
    }

}

@@ -1,8 +1,812 @@
+/**%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 

fix CxfRsProducer doesn't configure CxfRsEndpointConfigurer while using Proxy API