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

XML2JSON Data Format and empty requests

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.19.3
    • Fix Version/s: 2.19.4, 2.20.1
    • Component/s: camel-xmljson
    • Labels:
      None
    • Estimated Complexity:
      Unknown

      Description

      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

      	@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);
      	}
      

        Attachments

          Activity

            People

            • Assignee:
              onders Onder Sezgin
              Reporter:
              r.groote@beinformed.com Richard Groote
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: