cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA webscript, transforming content and writing to it

sihnu
Champ in-the-making
Champ in-the-making
Hello,

using Java I'm trying to implement a web script where I write to a content of an existing node and change the mimetype of the node. I've already implemented a web scrip where I send Base64 as a string, create a new node, write to the content of the node and store the node to Alfresco repository. So now I'm trying to implement update version of the web script. However, I get an error of channel being open after transformation (changing mimetype) when trying to write to the content of the node. Here is the exact error message:

<blockquote>
org.springframework.extensions.webscripts.WebScriptException: 03090001 Wrapped Exception (with status template): 03090028 A channel has already been opened
</blockquote>

Here is how I do the transformation:


options = new TransformationOptions(nodeRef, ContentModel.PROP_CONTENT, nodeRef, ContentModel.PROP_CONTENT);
this.contentService.transform(reader, writer, options);


Here I try to close the channel (just trial with errors…):


writer.getFileChannel(false).close();
writer.getContentOutputStream().flush();
writer.getContentOutputStream().close();


And here I try to write to the content:


ByteBuffer bf = ByteBuffer.wrap(Base64.decodeBase64(data.getBytes()));
FileChannel fileChannel = writer.getFileChannel(true);
fileChannel.write(bf);
fileChannel.force(false);


I've tried it other way around as well. First writing then transformation but the I'll get the same error when trying to do the transformation. So what is the proper way of implementing writing & transformation? I guess I'm just missing something simple here which is ruining my implementation but I just can't figure it out by myself. Any help would be greatly appreciated. I've already got so much help from here. I wish I could help ppl here as well. I'm going to try at some point.

Thanks in advance.
1 REPLY 1

raghav_bhardwaj
Champ on-the-rise
Champ on-the-rise
This is how i implemented this




              InputStream targetStream = FileUtils.openInputStream(finalupload);            
              String fileName = finalupload.getName()+".pdf";
              MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();                  
              String mimeType = "Adobe PDF Document";             
             
                          NodeRef newNode = createNewNode(parentNode,final_filename+".pdf");
             
             
             
             
        try {
         ContentWriter writer = contentService.getWriter(newNode, ContentModel.PROP_CONTENT, true);
         writer.setMimetype(MimetypeMap.MIMETYPE_PDF);
                  writer.putContent(targetStream);
                 
                  success = true;
        } catch (Exception e) {
         System.out.println(e);
        }
             
             
           }
             
              return success;

             
              }
       

       protected NodeRef createNewNode (NodeRef parentNode, String fileName) {
        try {
         QName contentQName = QName.createQName("{http://www.alfresco.org/model/content/1.0}content");
         FileInfo newNodeRef = fileFolderService.create(parentNode, fileName, contentQName);
        
         return newNodeRef.getNodeRef();
        } catch (Exception e) {
         System.err.println(e);
        
         return null;
        }
       
       }
   


Raghav Bhardwaj