camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Richard Groote (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CAMEL-11891) XML2JSON Data Format and empty requests
Date Wed, 11 Oct 2017 06:05:01 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-11891?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16199842#comment-16199842
] 

Richard Groote commented on CAMEL-11891:
----------------------------------------

Below is the spring configuration we are using. When a POST is done (through swagger client)
with nothing in the body or when a get is done it will fail.
Because in both cases the body is an empty stream.

{noformat}
  <!-- New bean which checks if the data is null -->
  <bean class="com.acme.extensions.XML2JSONDataFormat" id="xml2json">
    <property name="typeHints" value="YES"/>
    <property name="expandableProperties" value="objects"/>
    <property name="forceTopLevelObject" value="true"/>
    <property name="trimSpaces" value="true"/>
    <property name="rootName" value="request"/>
    <property name="skipNamespaces" value="true"/>
    <property name="removeNamespacePrefixes" value="true"/>
  </bean>   
   
    <camel:route>
      <camel:from uri="servlet:///ui?matchOnUriPrefix=true&amp;httpMethodRestrict=GET,POST"/>
      
      <!--
          Below was working in 2.14.0. 
          Below does not work anymore in 2.19.3 because the servlet component or http binding
add the stream cache to the body and that's correct
          It would be nice if you could check the body some how.
       -->
      <!--
      <camel:choice>
        <camel:when>
          <camel:simple>${body} != null</camel:simple>
          <camel:unmarshal ref="xml2json"/>
        </camel:when>
      </camel:choice>
      -->
      
      
      <!-- Below refers to the new bean which checks if the data is null or empty-->
      <camel:unmarshal ref="xml2json"/>
      
    </camel:route>  
{noformat}



> XML2JSON Data Format and empty requests
> ---------------------------------------
>
>                 Key: CAMEL-11891
>                 URL: https://issues.apache.org/jira/browse/CAMEL-11891
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-xmljson
>    Affects Versions: 2.19.3
>            Reporter: Richard Groote
>
> It would be nice if the XmlJsonDataFormat can handle empty body.
> In our project we upgraded from 2.14.0 to 2.19.3. 
> The below code no longer works in 2.19.3 when a GET request is made to a servlet component.
In our situation the body contains a stream cache and the xml2json will fail.
> A possible solution is to check the http method before doing the unmarshalling.
>       <camel:choice>
>         <camel:when>
>           <camel:simple>${body} != null</camel:simple>
>           <camel:unmarshal ref="xml2json"/>
>         </camel:when>
>       </camel:choice>
> The current solution in our project is a extension of the XmlJsonDataFormat.
> In the extension the method unmarshall is overriden
> {code:java}
> 	@Override
> 	public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
>         Object inBody = exchange.getIn().getBody();
>         
>         if(inBody == null) {
>         		return null;
>         }
>         
>         if(inBody instanceof StreamCache) {
>         		long length = ((StreamCache) inBody).length();
>         		if(length <= 0) {
>         			return null;
>         		}
>         } else {
> 	        if(!(inBody instanceof JSON)) {
> 	        	 	String jsonString = exchange.getContext().getTypeConverter().convertTo(String.class,
exchange, inBody);
> 	        	 	if(StringUtils.isEmpty(jsonString)) {
> 	        	 		return null;
> 	        	 	}
> 	        }
>         }
>         
>         
>         
>         return super.unmarshal(exchange, stream);
> 	}
> {code}
> 	



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message