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

Freeman Fang commented on CXF-4772:
-----------------------------------

It's not a issue of CXF, it's how stream works by design.

Also I think you misread that blog, CacheStream is re-readable.

Generally the Stream could be read only once, this is for a better performance, 
and this is really what "stream" means.

However, in some cases if you wanna the steam re-readable, you need cache that 
stream, cache a stream means it will use more memory or space on your disk, cxf 
CachedOutputStream can do this job for you, some code like
            InputStream is = ....
            CachedOutputStream bos = new CachedOutputStream();
            try {
                IOUtils.copy(is, bos);
                bos.flush();
                is.close();
                bos.close();
                // bos.getInputStream() is re-readable
            } catch (IOException e) {
            }

I believe that blog exactly mentioned if you wanna cache a stream, what you 
should do. The "How to reuse the attachment’s stream" part of that blog, which 
indicate you can use AttachmentDataSource.hold() method.

If you look into the AttachmentDataSource.hold() method, you can find it just 
cache the InputStream.

Hope this helps

Freeman
                
> AssessmentDataStore do not cache the last attachment binary stream, so the 
> DataHandler.writeTo() will fail if it is called for the second time
> ----------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CXF-4772
>                 URL: https://issues.apache.org/jira/browse/CXF-4772
>             Project: CXF
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.6.2
>            Reporter: Yan Min Sheng
>            Priority: Critical
>              Labels: patch
>         Attachments: CXF4772.patch
>
>
> When trying test MTOM through JAX-WS, we found that the AssessmentDataStore 
> can only be written once. It will fail for the second time. After 
> investigation, it seems that the binary stream is not cached by the 
> AssessmentDataStore, so each time a DelegatingStream will be created to 
> handle the writeTo request. While the DelegatingStream is closed, it can not 
> handle the writeTo request again.
> The suggestion is to cache the newly created attachement after it is created 
> in AttachmentDeserializer
> The interface is:
> /**
>  * This class was generated by the JAXWS SI.
>  * JAX-WS RI 2.0_01-b15-fcs
>  * Generated source version: 2.0
>  * 
>  */
> @WebService(name = "MTOMInterface", targetNamespace = 
> "http://shengym.com/wssvt/acme/InsBusiness/";)
> public interface MTOMInterface {
>     /**
>      * 
>      * @param input
>      * @param params
>      * @return
>      *     returns com.ibm.wssvt.acme.insbusiness.ImageDepot
>      * @throws InsFaultException
>      */
>     @WebMethod(action = "http://shengym.com/wssvt/acme/sendImage";)
>     @WebResult(name = "output", targetNamespace = "")
>     @RequestWrapper(localName = "sendImage", targetNamespace = 
> "http://shengym.com/wssvt/acme/InsBusiness/";, className = 
> "com.shengym.wssvt.acme.insbusiness.SendImage")
>     @ResponseWrapper(localName = "sendImageResponse", targetNamespace = 
> "http://shengym.com/wssvt/acme/InsBusiness/";, className = 
> "com.shengym.wssvt.acme.insbusiness.SendImageResponse")
>     public ImageDepot sendImage(
>         @WebParam(name = "input", targetNamespace = "")
>         ImageDepot input,
>         @WebParam(name = "params", targetNamespace = "")
>         ParamsType params)
>         throws InsFaultException
>     ;
> }
> ImageDepot.java
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "ImageDepot", propOrder = {
>     "image"
> })
> public class ImageDepot {
>     @XmlElement(required = true)
>     @XmlMimeType("multipart/*")
>     protected List<DataHandler> image;
>     /**
>      * Gets the value of the image property.
>      * 
>      * <p>
>      * This accessor method returns a reference to the live list,
>      * not a snapshot. Therefore any modification you make to the
>      * returned list will be present inside the JAXB object.
>      * This is why there is not a <CODE>set</CODE> method for the image 
> property.
>      * 
>      * <p>
>      * For example, to add a new item, do as follows:
>      * <pre>
>      *    getImage().add(newItem);
>      * </pre>
>      * 
>      * 
>      * <p>
>      * Objects of the following type(s) are allowed in the list
>      * {@link DataHandler }
>      * 
>      * 
>      */
>     public List<DataHandler> getImage() {
>         if (image == null) {
>             image = new ArrayList<DataHandler>();
>         }
>         return this.image;
>     }
> }
> The client code will look like:
>        void setOutputImage(ImageDepot imgin, ImageDepot imgout) throws 
> Exception {
>               List<DataHandler> imglist = imgin.getImage();
>               try {
>                       DataHandler img = null;
>                       // traverse each of the image
>                       for (DataHandler img : imglist) {
>                               if (img != null) {
>                                       File f = new File(msgID + 
> "_server_image"+ imglist.indexOf(img));
>                                       if (f.exists()) {
>                                               f.delete();
>                                       }
>                                       FileOutputStream fos = new 
> FileOutputStream(f);
>                                       img.writeTo(fos);
>                                       imgout.getImage().add(img);
>                                       fos.close();
>                               } 
>                       }                       
>               } catch (Exception e) {
>                       System.out.println("Exception caught in MTOMService 
> sendImage ...");
>                       e.printStackTrace(System.out);
>                       throw e;
>               }
>       }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to