Search in sources :

Example 1 with VFSOutTransportInfo

use of org.apache.synapse.commons.vfs.VFSOutTransportInfo in project wso2-synapse by wso2.

the class VFSTransportListener method processFile.

/**
 * Process a single file through Axis2
 * @param entry the PollTableEntry for the file (or its parent directory or archive)
 * @param file the file that contains the actual message pumped into Axis2
 * @throws AxisFault on error
 */
private void processFile(PollTableEntry entry, FileObject file) throws AxisFault {
    try {
        FileContent content = file.getContent();
        String fileName = file.getName().getBaseName();
        String filePath = file.getName().getPath();
        String fileURI = file.getName().getURI();
        metrics.incrementBytesReceived(content.getSize());
        Map<String, Object> transportHeaders = new HashMap<String, Object>();
        transportHeaders.put(VFSConstants.FILE_PATH, filePath);
        transportHeaders.put(VFSConstants.FILE_NAME, fileName);
        transportHeaders.put(VFSConstants.FILE_URI, fileURI);
        try {
            transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
            transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
        } catch (FileSystemException ignore) {
        }
        MessageContext msgContext = entry.createMessageContext();
        String contentType = entry.getContentType();
        if (BaseUtils.isBlank(contentType)) {
            if (file.getName().getExtension().toLowerCase().endsWith(".xml")) {
                contentType = "text/xml";
            } else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) {
                contentType = "text/plain";
            }
        } else {
            // Extract the charset encoding from the configured content type and
            // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
            String charSetEnc = null;
            try {
                if (contentType != null) {
                    charSetEnc = new ContentType(contentType).getParameter("charset");
                }
            } catch (ParseException ex) {
            // ignore
            }
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        }
        // if the content type was not found, but the service defined it.. use it
        if (contentType == null) {
            if (entry.getContentType() != null) {
                contentType = entry.getContentType();
            } else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) {
                contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE);
            }
        }
        // does the service specify a default reply file URI ?
        String replyFileURI = entry.getReplyFileURI();
        if (replyFileURI != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled()));
        }
        // Determine the message builder to use
        Builder builder;
        if (contentType == null) {
            if (log.isDebugEnabled()) {
                log.debug("No content type specified. Using SOAP builder.");
            }
            builder = new SOAPBuilder();
        } else {
            int index = contentType.indexOf(';');
            String type = index > 0 ? contentType.substring(0, index) : contentType;
            builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
            if (builder == null) {
                if (log.isDebugEnabled()) {
                    log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
                }
                builder = new SOAPBuilder();
            }
        }
        // set the message payload to the message context
        InputStream in;
        ManagedDataSource dataSource;
        if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) {
            in = null;
            dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType));
        } else {
            in = new AutoCloseInputStream(content.getInputStream());
            dataSource = null;
        }
        try {
            OMElement documentElement;
            if (in != null) {
                documentElement = builder.processDocument(in, contentType, msgContext);
            } else {
                documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType, msgContext);
            }
            msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
            handleIncomingMessage(msgContext, transportHeaders, // * SOAP Action - not applicable *//
            null, contentType);
        } finally {
            if (dataSource != null) {
                dataSource.destroy();
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Processed file : " + VFSUtils.maskURLPassword(file.toString()) + " of Content-type : " + contentType);
        }
    } catch (FileSystemException e) {
        handleException("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);
    } finally {
        try {
            if (file != null) {
                if (fsManager != null && file.getName() != null && file.getName().getScheme() != null && file.getName().getScheme().startsWith("file")) {
                    fsManager.closeFileSystem(file.getParent().getFileSystem());
                }
                file.close();
            }
        } catch (FileSystemException warn) {
        // ignore the warning,  since we handed over the stream close job to
        // AutocloseInputstream..
        }
    }
}
Also used : ContentType(javax.mail.internet.ContentType) HashMap(java.util.HashMap) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) InputStream(java.io.InputStream) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder) Builder(org.apache.axis2.builder.Builder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) OMElement(org.apache.axiom.om.OMElement) FileContent(org.apache.commons.vfs2.FileContent) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) FileObjectDataSource(org.apache.synapse.commons.vfs.FileObjectDataSource) MessageContext(org.apache.axis2.context.MessageContext) ParseException(javax.mail.internet.ParseException) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) ManagedDataSource(org.apache.axis2.format.ManagedDataSource) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder)

Example 2 with VFSOutTransportInfo

use of org.apache.synapse.commons.vfs.VFSOutTransportInfo in project wso2-synapse by wso2.

the class VFSTransportSenderTest method testReplyFileCreation.

/**
 * Test creation of reply file in target directory
 * @throws AxisFault
 */
public void testReplyFileCreation() throws AxisFault {
    String replyFileName = "testFile.txt";
    // Clear file holder
    MockFileHolder.getInstance().clear();
    VFSTransportSender vfsTransportSender = new VFSTransportSender();
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    TransportOutDescription transportOutDescription = new TransportOutDescription("Test");
    vfsTransportSender.init(configurationContext, transportOutDescription);
    // Create message context
    org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
    populateMessageContext(mc);
    // Add transport.vfs.ReplyFileName axis service parameter
    mc.getAxisService().addParameter(new Parameter(VFSConstants.REPLY_FILE_NAME, replyFileName));
    String filePath = "test1:///foo/bar/test-" + System.currentTimeMillis() + "/DIR/";
    String parameters = "?transport.vfs.CreateFolder=true";
    String fURI = filePath + parameters;
    OutTransportInfo outTransportInfo = new VFSOutTransportInfo(fURI, true);
    vfsTransportSender.sendMessage(mc, fURI, outTransportInfo);
    Assert.assertNotNull(MockFileHolder.getInstance().getFile(filePath + replyFileName));
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) Parameter(org.apache.axis2.description.Parameter) MessageContext(org.apache.axis2.context.MessageContext) MessageContext(org.apache.axis2.context.MessageContext) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

Example 3 with VFSOutTransportInfo

use of org.apache.synapse.commons.vfs.VFSOutTransportInfo in project wso2-synapse by wso2.

the class VFSTransportSenderTest method testMemoryLeakWhileLockCreation.

public void testMemoryLeakWhileLockCreation() throws AxisFault, NoSuchFieldException, IllegalAccessException {
    // Clear file holder
    MockFileHolder.getInstance().clear();
    VFSTransportSender vfsTransportSender = new VFSTransportSender();
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    TransportOutDescription transportOutDescription = new TransportOutDescription("Test");
    vfsTransportSender.init(configurationContext, transportOutDescription);
    org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
    populateMessageContext(mc);
    for (int i = 0; i < FILE_SEND_ITERATIONS; i++) {
        String fName = "test1://foo/bar/test" + i + "-" + System.currentTimeMillis() + ".ack?transport.vfs.MaxRetryCount=0&transport.vfs.ReconnectTimeout=1";
        OutTransportInfo outTransportInfo = new VFSOutTransportInfo(fName, true);
        try {
            vfsTransportSender.sendMessage(mc, fName, outTransportInfo);
        } catch (AxisFault fse) {
        // Ignore
        }
    }
    // Perform the GC
    System.gc();
    Map<?, ?> refReverseMap = getSoftReferenceMap(vfsTransportSender);
    assertEquals("If there is no memory leak, soft reference map size should be zero.", 0, refReverseMap.size());
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) MessageContext(org.apache.axis2.context.MessageContext) MessageContext(org.apache.axis2.context.MessageContext) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

Example 4 with VFSOutTransportInfo

use of org.apache.synapse.commons.vfs.VFSOutTransportInfo in project wso2-synapse by wso2.

the class VFSTransportSenderTest method testTransportSenderInitAndSend.

/**
 * Test Transport sender initialization and sending a file
 * @throws AxisFault
 */
public void testTransportSenderInitAndSend() throws AxisFault, XMLStreamException {
    // Clear file holder
    MockFileHolder.getInstance().clear();
    VFSTransportSender vfsTransportSender = new VFSTransportSender();
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    TransportOutDescription transportOutDescription = new TransportOutDescription("Test");
    // Enable Autolock release
    transportOutDescription.addParameter(new Parameter(VFSConstants.TRANSPORT_AUTO_LOCK_RELEASE, true));
    // Set Autolock release interval with default value
    transportOutDescription.addParameter(new Parameter(VFSConstants.TRANSPORT_AUTO_LOCK_RELEASE_INTERVAL, 20000));
    transportOutDescription.addParameter(new Parameter(VFSConstants.TRANSPORT_AUTO_LOCK_RELEASE_SAME_NODE, true));
    vfsTransportSender.init(configurationContext, transportOutDescription);
    // Create message context
    org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
    populateMessageContext(mc);
    String filePath = "test1:///foo/bar/test-" + System.currentTimeMillis() + ".ack";
    String parameters = "?transport.vfs.MaxRetryCount=0&transport.vfs.ReconnectTimeout=1&" + "transport.vfs.SendFileSynchronously=true&transport.vfs.CreateFolder=true";
    String fURI = filePath + parameters;
    OutTransportInfo outTransportInfo = new VFSOutTransportInfo(fURI, true);
    vfsTransportSender.sendMessage(mc, fURI, outTransportInfo);
    MockFileHolder fileHolder = MockFileHolder.getInstance();
    Assert.assertNotNull("File creation failed", fileHolder.getFile(filePath));
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) MockFileHolder(org.wso2.carbon.inbound.endpoint.protocol.file.MockFileHolder) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) Parameter(org.apache.axis2.description.Parameter) MessageContext(org.apache.axis2.context.MessageContext) MessageContext(org.apache.axis2.context.MessageContext) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

Example 5 with VFSOutTransportInfo

use of org.apache.synapse.commons.vfs.VFSOutTransportInfo in project wso2-synapse by wso2.

the class VFSTransportSender method sendMessage.

/**
 * Send the given message over the VFS transport
 *
 * @param msgCtx the axis2 message context
 * @throws AxisFault on error
 */
public void sendMessage(MessageContext msgCtx, String targetAddress, OutTransportInfo outTransportInfo) throws AxisFault {
    if (waitForSynchronousResponse(msgCtx)) {
        throw new AxisFault("The VFS transport doesn't support synchronous responses. " + "Please use the appropriate (out only) message exchange pattern.");
    }
    VFSOutTransportInfo vfsOutInfo = null;
    if (targetAddress != null) {
        vfsOutInfo = new VFSOutTransportInfo(targetAddress, globalFileLockingFlag);
    } else if (outTransportInfo != null && outTransportInfo instanceof VFSOutTransportInfo) {
        vfsOutInfo = (VFSOutTransportInfo) outTransportInfo;
    }
    WriteLockObject lockObject = null;
    String baseUri = null;
    if (vfsOutInfo != null) {
        if (vfsOutInfo.getSendFileSynchronously()) {
            baseUri = getBaseUri(targetAddress);
            if (baseUri != null) {
                if (!lockingObjects.containsKey(baseUri)) {
                    lockingObjects.putIfAbsent(baseUri, new WriteLockObject());
                    log.debug("New locking object created for Synchronous write|MapSize:" + lockingObjects.size());
                }
                lockObject = lockingObjects.get(baseUri);
                lockObject.incrementUsers();
            }
        }
    }
    try {
        if (lockObject == null) {
            writeFile(msgCtx, vfsOutInfo);
        } else {
            synchronized (lockObject) {
                writeFile(msgCtx, vfsOutInfo);
            }
        }
    } finally {
        if (lockObject != null && (lockObject.decrementAndGetUsers() == 0)) {
            lockingObjects.remove(baseUri);
            if (log.isDebugEnabled()) {
                log.debug("locking object removed for after Synchronous write|MapSize:" + lockingObjects.size());
            }
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo)

Aggregations

VFSOutTransportInfo (org.apache.synapse.commons.vfs.VFSOutTransportInfo)5 MessageContext (org.apache.axis2.context.MessageContext)4 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)3 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)3 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)3 OutTransportInfo (org.apache.axis2.transport.OutTransportInfo)3 AxisFault (org.apache.axis2.AxisFault)2 Parameter (org.apache.axis2.description.Parameter)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ContentType (javax.mail.internet.ContentType)1 ParseException (javax.mail.internet.ParseException)1 OMElement (org.apache.axiom.om.OMElement)1 Builder (org.apache.axis2.builder.Builder)1 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)1 DataSourceMessageBuilder (org.apache.axis2.format.DataSourceMessageBuilder)1 ManagedDataSource (org.apache.axis2.format.ManagedDataSource)1 AutoCloseInputStream (org.apache.commons.io.input.AutoCloseInputStream)1 FileContent (org.apache.commons.vfs2.FileContent)1 FileObject (org.apache.commons.vfs2.FileObject)1