1 22 package org.objectweb.petals.binding.filetransferbc.listeners; 23 24 import java.io.File ; 25 import java.util.Set ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import javax.jbi.messaging.NormalizedMessage; 30 31 import org.objectweb.petals.binding.filetransferbc.FileTransferBCException; 32 import org.objectweb.petals.binding.filetransferbc.IOUtils; 33 import org.objectweb.petals.component.common.HandlingException; 34 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 35 import org.objectweb.petals.component.common.bc.JBIListener; 36 import org.objectweb.petals.component.common.util.MessageExchangeWrapper; 37 import org.objectweb.petals.component.common.util.SourceHelper; 38 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions; 39 import org.objectweb.petals.tools.jbicommon.util.StringHelper; 40 41 50 public class FileTransferBCJBIListener implements JBIListener { 51 52 private final Logger logger; 53 54 private String rootPath; 55 56 64 public FileTransferBCJBIListener(String rootPath, Logger logger) { 65 this.logger = logger; 66 this.rootPath = rootPath; 67 } 68 69 76 public boolean onJBIMessage(String address, 77 MessageExchangeWrapper exchange, Extensions extensions) 78 throws HandlingException { 79 boolean result = false; 80 81 String content = null; 82 NormalizedMessage nm = exchange.getInMessage(); 83 84 try { 86 content = SourceHelper.createString(nm.getContent()); 87 } catch (PEtALSComponentSDKException e) { 88 throw new HandlingException(e.getMessage()); 89 } 90 91 File tmpDir = new File (address); 95 File parentDir = null; 96 if (!tmpDir.isAbsolute()) { 97 parentDir = new File (rootPath, address); 98 } else { 99 parentDir = new File (address); 100 } 101 if (!parentDir.exists()) 102 parentDir.mkdirs(); 103 104 String destDir = exchange.getEndpoint().getEndpointName(); 105 File outputDir = new File (parentDir, destDir); 106 107 logger.log(Level.INFO, "Writing data to directory : " 108 + outputDir.getAbsolutePath()); 109 110 if (StringHelper.isNullOrEmpty(content)) { 111 logger.log(Level.INFO, "content is null"); 112 113 } else { 114 try { 115 String fileName = null; 116 if (exchange.getOperation() != null) { 117 fileName = exchange.getOperation().getLocalPart(); 118 } else { 119 fileName = "content"; 120 } 121 122 IOUtils.writeStringToFile(content, outputDir, fileName); 123 } catch (FileTransferBCException e) { 124 throw new HandlingException(e); 125 } 126 } 127 128 Set attachIds = nm.getAttachmentNames(); 130 if (!attachIds.isEmpty()) { 131 try { 132 IOUtils.writeAttachmentsToFiles(nm, outputDir); 133 } catch (FileTransferBCException e) { 134 throw new HandlingException(e); 135 } 136 } 137 138 return result; 139 } 140 } 141 | Popular Tags |