Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: http inbound-gateway and returning PDF stream

  1. #1
    Join Date
    Apr 2005
    Posts
    112

    Default http inbound-gateway and returning PDF stream

    What is the best strategy to return PDF binary stream as the response to an HTTP request. I used ByteArrayHttpMessageConverter to send back as octet-stream. But this did not work as I am getting error while extracting the data. I am using RestTemplate to consume the PDF binary stream and write the length of the contents in the log.

    Error:
    Could not extract response: no suitable HttpMessageConverter found for response type [[Ljava.lang.Byte;] and content type [application/octet-stream]

    The DocumentService given in the example below return a reference to the PDF File Object as the input to the File to Bytes Trasformer which further gets send as the response to the http request.

    Do I have to write a special http message converter to send back PDF stream as the response? Can someone throw some pointers.

    My servlet-config.xml file is given below.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
    		http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-2.0.xsd
    		http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.0.xsd"
    	xmlns:int="http://www.springframework.org/schema/integration"
    	xmlns:int-file="http://www.springframework.org/schema/integration/file"
    	xmlns:int-http="http://www.springframework.org/schema/integration/http">
    
    	
    	<int-http:inbound-gateway id="inboundDocGateway" name="/retrieveDococument.htm" 
    								supported-methods="GET, POST"
    									request-channel="docReqChannel"
    									reply-channel="docResChannel" 
    									convert-exceptions="true" 	
    									message-converters="byteArrayMessageConverter" 
    									/>
    																	  
    									  
    	
    	
    	
    	<int:channel id="docReqChannel"/>
    	<int:channel id="docResChannel"/>
    	
    	<int:channel id="fileIn"/>
    		
    	
    	<int:service-activator input-channel="docReqChannel" output-channel="fileIn" ref="docService" method="getFile" requires-reply="true" />
    	
    	<bean id="docService" class="gov.dc.dmv.destiny.iis.DocumentService"/>
    		
    	<int-file:file-to-bytes-transformer input-channel="fileIn" output-channel="docResChannel" delete-files="false"/>
    	 
    	<bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
    	/bean>
    		
    </beans>
    Last edited by vbose; Feb 23rd, 2011 at 03:25 PM.

  2. #2
    Join Date
    Apr 2005
    Posts
    112

    Default

    I figured it out as to why I have been getting error in my client.

    I changed the ResponseType from Byte[].class to byte[].class in the exchange method and now I am getting the expected result.

    Bad Code:
    Code:
    ResponseEntity<Byte[]> rByteEntity = template.exchange(docUri, HttpMethod.GET, requestEntity, Byte[].class);
    Correct Code:
    Code:
    ResponseEntity<byte[]> rByteEntity = template.exchange(docUri, HttpMethod.GET, requestEntity, byte[].class);
    Last edited by vbose; Feb 23rd, 2011 at 05:26 PM.

  3. #3
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    So, everything is working ok now?

    Thanks for posting the update.

  4. #4
    Join Date
    Apr 2005
    Posts
    112

    Default

    Yes. it is working now and I hope our client will be able to convert the octet-stream/byte[] into a proper PDF for viewing. Our client is a PowerBuilder Desktop application. Thanks for checking the status Mark!. You guys rock.

  5. #5
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    Have you considered setting the 'supportedMediaTypes' property on your ByteArrayHttpMessageConverter instance to "application/pdf"?

  6. #6
    Join Date
    Apr 2005
    Posts
    112

    Default

    Mark,

    I have two URLs that my client can try. One with application/pdf as the media type and the other one with the default application/octet-stream. But, I am not sure whether PowerBuilder client has any native support to convert it to the right document format from the HTTP response binary stream. So we are trying many options.

    Thanks,
    Vigil

  7. #7
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    Okay. Please keep us posted on your progress. It sounds very interesting!

    Cheers,
    Mark

  8. #8
    Join Date
    Apr 2005
    Posts
    112

    Default

    Sure. I will keep you posted. The entire project is very interesting. I plan on writing an article based on my experience.

    Thanks,
    Vigil

  9. #9
    Join Date
    Apr 2005
    Posts
    112

    Default

    Just a quick update on the progress...Our PowerBuilder client application is now able to retrieve and display PDF using Microsoft XmlHttp Object and OLE control. With this, we are now able to integrate Spring Integration and Spring MVC powered RESTFUL service with one of our desktop based clients. The client sets the accept header as application/pdf and the response media type is set accordingly.
    Last edited by vbose; Feb 28th, 2011 at 10:07 AM.

  10. #10
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    Glad to hear that! Thanks for the update. Let us know if you have any other feature requests or improvements to suggest.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •