<commit_before>/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.transport.file.connector.client.sender;

import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.VFS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.messaging.CarbonCallback;
import org.wso2.carbon.messaging.CarbonMessage;
import org.wso2.carbon.messaging.CarbonMessageProcessor;
import org.wso2.carbon.messaging.ClientConnector;
import org.wso2.carbon.messaging.exceptions.ClientConnectorException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

/**
 * FileClientConnector
 */
public class FileClientConnector implements ClientConnector {
    private static final Logger logger = LoggerFactory.getLogger(FileClientConnector.class);
    private String fileURI;
    private FileSystemManager fsManager;
    private FileSystemOptions opts = new FileSystemOptions();
    private CarbonMessageProcessor carbonMessageProcessor;

    @Override public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback)
            throws ClientConnectorException {
        return false;
    }

    @Override public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback, Map<String, String> map)
            throws ClientConnectorException {
        fileURI = map.get("uri");
        String action = map.get("action");
        FileType fileType;
        InputStream is = null;
        OutputStream os = null;
        try {
            fsManager = VFS.getManager();
            FileObject path = fsManager.resolveFile(fileURI, opts);
            fileType = path.getType();
            switch (action) {

                case "send":
                    if (fileType == FileType.IMAGINARY) {
                        path.createFile();
                        path.refresh();
                        fileType = path.getType();
                    }
                    if (carbonMessage != null && fileType == FileType.FILE) {
                        is = carbonMessage.getInputStream();
                        os = path.getContent().getOutputStream();
                        long bytesCopied = IOUtils.copy(is, os);
                        os.flush();
                        String bytes = Long.toString(bytesCopied);
                        logger.debug(bytes + " bytes sent");
                    }
                    // TODO: 3/20/17 Send response on error eg: not a file
                    break;
                case "create":
                    if (fileType == FileType.IMAGINARY) {
                        FileName name = path.getName();
                        boolean isFolder = name.getExtension().isEmpty();
                        if (isFolder) {
                            path.createFolder();
                        } else {
                            path.createFile();
                        }
                        path.refresh();
                        fileType = path.getType();
                    }
                    if (carbonMessage != null && fileType == FileType.FILE) {
                        is = carbonMessage.getInputStream();
                        os = path.getContent().getOutputStream();
                        long bytesCopied = IOUtils.copy(is, os);
                        os.flush();
                        String bytes = Long.toString(bytesCopied);
                        logger.debug(bytes + " bytes sent");
                    }
                    break;
                case "delete":
                    if (path.exists()) {
                        boolean isDeleted = path.delete();
                        if (isDeleted) {
                            logger.debug("File Successfully Deleted");
                        }
                    }
                    break;
                case "append":
                    if (!path.exists()) {
                        path.createFile();
                        path.refresh();
                        fileType = path.getType();
                    }
                    if (fileType == FileType.FILE) {
                        is = carbonMessage.getInputStream();
                        os = path.getContent().getOutputStream(true);
                        long bytesCopied = IOUtils.copy(is, os);
                        os.flush();

                        String bytes = Long.toString(bytesCopied);
                        logger.info(bytes + " bytes appended");
                    }
                    break;
                case "copy":
                    if (path.exists()) {
                        is = path.getContent().getInputStream();
                        String destination = map.get("destination");
                        path = fsManager.resolveFile(destination, opts);
                        if (!path.exists()) {
                            os = path.getContent().getOutputStream();
                            IOUtils.copy(is, os);
                            os.flush();
                        }
                    }
                    break;
                case "move":
                    if (path.exists()) {
                        is = path.getContent().getInputStream();
                        String destination = map.get("destination");
                        FileObject newPath = fsManager.resolveFile(destination, opts);
                        if (!newPath.exists()) {
                            path.moveTo(newPath);
                        }

                    }
                    break;
                case "archive":
                    if (path.exists()) {
                        String destination = map.get("destination");
                        FileObject newPath = fsManager.resolveFile(destination, opts);
                        if (!newPath.exists()) {
                            Utils.fileCompress(path, newPath);
                        }
                    }
                    break;
                default: return false;
            }
        } catch (IOException e) {
            throw new ClientConnectorException("Exception occurred while sending the message", e);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
        return true;<commit_msg>improve copy function implementation<commit_after>/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.transport.file.connector.client.sender;

import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.VFS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.messaging.CarbonCallback;
import org.wso2.carbon.messaging.CarbonMessage;
import org.wso2.carbon.messaging.CarbonMessageProcessor;
import org.wso2.carbon.messaging.ClientConnector;
import org.wso2.carbon.messaging.exceptions.ClientConnectorException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

/**
 * FileClientConnector
 */
public class FileClientConnector implements ClientConnector {
    private static final Logger logger = LoggerFactory.getLogger(FileClientConnector.class);
    private String fileURI;
    private FileSystemManager fsManager;
    private FileSystemOptions opts = new FileSystemOptions();
    private CarbonMessageProcessor carbonMessageProcessor;

    @Override public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback)
            throws ClientConnectorException {
        return false;
    }

    @Override public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback, Map<String, String> map)
            throws ClientConnectorException {
        fileURI = map.get("uri");
        String action = map.get("action");
        FileType fileType;
        InputStream is = null;
        OutputStream os = null;
        try {
            fsManager = VFS.getManager();
            FileObject path = fsManager.resolveFile(fileURI, opts);
            fileType = path.getType();
            switch (action) {

                case "send":
                    if (fileType == FileType.IMAGINARY) {
                        path.createFile();
                        path.refresh();
                        fileType = path.getType();
                    }
                    if (carbonMessage != null && fileType == FileType.FILE) {
                        is = carbonMessage.getInputStream();
                        os = path.getContent().getOutputStream();
                        long bytesCopied = IOUtils.copy(is, os);
                        os.flush();
                        String bytes = Long.toString(bytesCopied);
                        logger.debug(bytes + " bytes sent");
                    }
                    // TODO: 3/20/17 Send response on error eg: not a file
                    break;
                case "create":
                    if (fileType == FileType.IMAGINARY) {
                        FileName name = path.getName();
                        boolean isFolder = name.getExtension().isEmpty();
                        if (isFolder) {
                            path.createFolder();
                        } else {
                            path.createFile();
                        }
                        path.refresh();
                        fileType = path.getType();
                    }
                    if (carbonMessage != null && fileType == FileType.FILE) {
                        is = carbonMessage.getInputStream();
                        os = path.getContent().getOutputStream();
                        long bytesCopied = IOUtils.copy(is, os);
                        os.flush();
                        String bytes = Long.toString(bytesCopied);
                        logger.debug(bytes + " bytes sent");
                    }
                    break;
                case "delete":
                    if (path.exists()) {
                        boolean isDeleted = path.delete();
                        if (isDeleted) {
                            logger.debug("File Successfully Deleted");
                        }
                    }
                    break;
                case "append":
                    if (!path.exists()) {
                        path.createFile();
                        path.refresh();
                        fileType = path.getType();
                    }
                    if (fileType == FileType.FILE) {
                        is = carbonMessage.getInputStream();
                        os = path.getContent().getOutputStream(true);
                        long bytesCopied = IOUtils.copy(is, os);
                        os.flush();

                        String bytes = Long.toString(bytesCopied);
                        logger.info(bytes + " bytes appended");
                    }
                    break;
                case "copy":
                    if (path.exists()) {
                        String destination = map.get("destination");
                        FileObject dest = fsManager.resolveFile(destination, opts);
                        dest.copyFrom(path, Selectors.SELECT_ALL);
                    }
                    break;
                case "move":
                    if (path.exists()) {
                        is = path.getContent().getInputStream();
                        String destination = map.get("destination");
                        FileObject newPath = fsManager.resolveFile(destination, opts);
                        if (!newPath.exists()) {
                            path.moveTo(newPath);
                        }

                    }
                    break;
                case "archive":
                    if (path.exists()) {
                        String destination = map.get("destination");
                        FileObject newPath = fsManager.resolveFile(destination, opts);
                        if (!newPath.exists()) {
                            Utils.fileCompress(path, newPath);
                        }
                    }
                    break;
                default: return false;
            }
        } catch (IOException e) {
            throw new ClientConnectorException("Exception occurred while sending the message", e);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
        return true;
{'fraction_non_alphanumeric': 0.0897272855729829, 'fraction_numerical': 0.004309735763741699, 'mean_word_length': 1.4011874469889738, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5273402', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3161, 'n_words': 794}