Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-7457

[camel-cxf] cxfrs: SimpleConsumer does not honur Accept header

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.12.3
    • 2.12.4, 2.13.2, 2.14.0
    • camel-cxf
    • None
    • Unknown

    Description

      Content negotiation does not work correctly.

      I create service class and spring xml as follows.

      CustomerService.java
      @Path("/customer")
      public class CustomerService {
          
          @GET
          @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
          public Customers getCustomers() {
              return null;
          }
      }
      
          <cxf:rsServer xmlns:cxf="http://camel.apache.org/schema/cxf"
                        id="rsServer"
                        address="http://localhost:9999/rsServer"
                        loggingFeatureEnabled="true"
                        loggingSizeLimit="200"> 
              <cxf:serviceBeans>
                  <ref bean="CustomerService" />
              </cxf:serviceBeans>
              <cxf:providers>
                  <ref bean="jettisonProvider" />
              </cxf:providers>
          </cxf:rsServer>
      
              
          <!-- provider -->
          <bean id="jettisonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider" />
                  
          <!-- interface -->    
          <bean id="CustomerService" class="com.buildria.camel.cxf.negotiation.CustomerService" />
          <!-- Concrete class -->
          <bean id="CustomerServiceImpl" class="com.buildria.camel.cxf.negotiation.CustomerServiceImpl" />
          
          <camelContext xmlns="http://camel.apache.org/schema/spring">
       
              <route> 
                  <!-- (1) SimpleConsumer BindingStyle -->
                  <from uri="cxfrs://bean://rsServer?bindingStyle=SimpleConsumer" />
                  <!-- (2) Default BindingStyle -->
                  <!-- <from uri="cxfrs://bean://rsServer" />-->
                  
                  <choice>
                      <when>
                          <simple>$simple{headers.operationName} == 'getCustomers'</simple>
                          <bean ref="CustomerServiceImpl" method="getCustomers" />
                      </when>
                      <otherwise>
                          <setHeader headerName="CamelHttpResponseCode">
                              <constant>404</constant>
                          </setHeader>
                      </otherwise>
                  </choice>
              </route>
          </camelContext>
      

      A) I tested it with curl command like this.

      $ curl  -H "Accept: application/json"  http://localhost:9999/rsServer/customer/ 
      {"customers":{"customers":{"address":"Japan","name":"Ken"}}}sogabe@emilia ~/src/camel/camel-rest-cxfrs [2.10.6] 2014/05/22 午後 23:15:56 
      $ LANG=C curl  -H "Accept: application/json"  http://localhost:9999/rsServer/customer/ -v
      * Hostname was NOT found in DNS cache
      *   Trying ::1...
      * connect to ::1 port 9999 failed: Connection refused
      *   Trying 127.0.0.1...
      * Connected to localhost (127.0.0.1) port 9999 (#0)
      > GET /rsServer/customer/ HTTP/1.1
      > User-Agent: curl/7.36.0
      > Host: localhost:9999
      > Accept: application/json
      > 
      < HTTP/1.1 200 OK
      < Accept: application/json
      < breadcrumbId: ID-emilia-46343-1400768103395-0-7
      < Content-Type: application/json
      < Date: Thu, 22 May 2014 14:16:12 GMT
      < Host: localhost:9999
      < User-Agent: curl/7.36.0
      < Transfer-Encoding: chunked
      * Server Jetty(8.1.14.v20131031) is not blacklisted
      < Server: Jetty(8.1.14.v20131031)
      < 
      * Connection #0 to host localhost left intact
      {"customers":{"customers":{"address":"Japan","name":"Ken"}}}
      

      I got a reponse as a JSON format. It's OK!

      B) But if "Content-Type: application/xml" specified,

      $ LANG=C curl  -H "Accept: application/json"  -H "Content-Type: application/xml" http://localhost:9999/rsServer/customer/ -v
      * Hostname was NOT found in DNS cache
      *   Trying ::1...
      * connect to ::1 port 9999 failed: Connection refused
      *   Trying 127.0.0.1...
      * Connected to localhost (127.0.0.1) port 9999 (#0)
      > GET /rsServer/customer/ HTTP/1.1
      > User-Agent: curl/7.36.0
      > Host: localhost:9999
      > Accept: application/json
      > Content-Type: application/xml
      > 
      < HTTP/1.1 200 OK
      < Accept: application/json
      < breadcrumbId: ID-emilia-46343-1400768103395-0-9
      < Content-Type: application/xml
      < Date: Thu, 22 May 2014 14:19:17 GMT
      < Host: localhost:9999
      < User-Agent: curl/7.36.0
      < Transfer-Encoding: chunked
      * Server Jetty(8.1.14.v20131031) is not blacklisted
      < Server: Jetty(8.1.14.v20131031)
      < 
      * Connection #0 to host localhost left intact
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?><customers><customers><address>Japan</address><name>Ken</name></customers></customers>
      

      I got a response as a xml format. It should be JSON format.

      C) replace SimpleConsumer with Default style.
      ( (2) in spring.xml)

       $ LANG=C curl  -H "Accept: application/json"  -H "Content-Type: application/xml" http://localhost:9999/rsServer/customer/ -v
      * Hostname was NOT found in DNS cache
      *   Trying ::1...
      * connect to ::1 port 9999 failed: Connection refused
      *   Trying 127.0.0.1...
      * Connected to localhost (127.0.0.1) port 9999 (#0)
      > GET /rsServer/customer/ HTTP/1.1
      > User-Agent: curl/7.36.0
      > Host: localhost:9999
      > Accept: application/json
      > Content-Type: application/xml
      > 
      < HTTP/1.1 200 OK
      < Content-Type: application/json
      < Date: Thu, 22 May 2014 14:27:19 GMT
      < Transfer-Encoding: chunked
      * Server Jetty(8.1.14.v20131031) is not blacklisted
      < Server: Jetty(8.1.14.v20131031)
      < 
      * Connection #0 to host localhost left intact
      {"customers":{"customers":{"address":"Japan","name":"Ken"}}}
      
      

      It's OK without bindingStyle option.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ssogabe Seiji Sogabe
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: