Search in sources :

Example 16 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class StorageClient method do_upload_file.

/**
 * upload file to storage server
 *
 * @param cmd             the command code
 * @param group_name      the group name to upload file to, can be empty
 * @param master_filename the master file name to generate the slave file
 * @param prefix_name     the prefix name to generate the slave file
 * @param file_ext_name   file ext name, do not include dot(.)
 * @param file_size       the file size
 * @param callback        the write data callback object
 * @param meta_list       meta info array
 * @return 2 elements string array if success:<br>
 * <ul><li> results[0]: the group name to store the file</li></ul>
 * <ul><li> results[1]: the new created filename</li></ul>
 * return null if fail
 */
protected String[] do_upload_file(byte cmd, String group_name, String master_filename, String prefix_name, String file_ext_name, long file_size, UploadCallback callback, NameValuePair[] meta_list) throws IOException, ServiceException {
    byte[] header;
    byte[] ext_name_bs;
    String new_group_name;
    String remote_filename;
    boolean bNewStorageServer;
    Connection connection = null;
    byte[] sizeBytes;
    byte[] hexLenBytes;
    byte[] masterFilenameBytes;
    boolean bUploadSlave;
    int offset;
    long body_len;
    bUploadSlave = ((group_name != null && group_name.length() > 0) && (master_filename != null && master_filename.length() > 0) && (prefix_name != null));
    if (bUploadSlave) {
        bNewStorageServer = this.newUpdatableStorageConnection(group_name, master_filename);
    } else {
        bNewStorageServer = this.newWritableStorageConnection(group_name);
    }
    try {
        connection = this.storageServer.getConnection();
        ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN];
        Arrays.fill(ext_name_bs, (byte) 0);
        if (file_ext_name != null && file_ext_name.length() > 0) {
            byte[] bs = file_ext_name.getBytes(ClientGlobal.g_charset);
            int ext_name_len = bs.length;
            if (ext_name_len > ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN) {
                ext_name_len = ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN;
            }
            System.arraycopy(bs, 0, ext_name_bs, 0, ext_name_len);
        }
        if (bUploadSlave) {
            masterFilenameBytes = master_filename.getBytes(ClientGlobal.g_charset);
            sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE];
            body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + masterFilenameBytes.length + file_size;
            hexLenBytes = ProtoCommon.long2buff(master_filename.length());
            System.arraycopy(hexLenBytes, 0, sizeBytes, 0, hexLenBytes.length);
            offset = hexLenBytes.length;
        } else {
            masterFilenameBytes = null;
            sizeBytes = new byte[1 + 1 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE];
            body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + file_size;
            sizeBytes[0] = (byte) this.storageServer.getStorePathIndex();
            offset = 1;
        }
        hexLenBytes = ProtoCommon.long2buff(file_size);
        System.arraycopy(hexLenBytes, 0, sizeBytes, offset, hexLenBytes.length);
        OutputStream out = connection.getOutputStream();
        header = ProtoCommon.packHeader(cmd, body_len, (byte) 0);
        byte[] wholePkg = new byte[(int) (header.length + body_len - file_size)];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length);
        offset = header.length + sizeBytes.length;
        if (bUploadSlave) {
            byte[] prefix_name_bs = new byte[ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN];
            byte[] bs = prefix_name.getBytes(ClientGlobal.g_charset);
            int prefix_name_len = bs.length;
            Arrays.fill(prefix_name_bs, (byte) 0);
            if (prefix_name_len > ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN) {
                prefix_name_len = ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN;
            }
            if (prefix_name_len > 0) {
                System.arraycopy(bs, 0, prefix_name_bs, 0, prefix_name_len);
            }
            System.arraycopy(prefix_name_bs, 0, wholePkg, offset, prefix_name_bs.length);
            offset += prefix_name_bs.length;
        }
        System.arraycopy(ext_name_bs, 0, wholePkg, offset, ext_name_bs.length);
        offset += ext_name_bs.length;
        if (bUploadSlave) {
            System.arraycopy(masterFilenameBytes, 0, wholePkg, offset, masterFilenameBytes.length);
            offset += masterFilenameBytes.length;
        }
        out.write(wholePkg);
        if ((this.errno = (byte) callback.send(out)) != 0) {
            return null;
        }
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        if (pkgInfo.body.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) {
            throw new ServiceException("body length: " + pkgInfo.body.length + " <= " + ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
        }
        new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim();
        remote_filename = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.body.length - ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
        String[] results = new String[2];
        results[0] = new_group_name;
        results[1] = remote_filename;
        if (meta_list == null || meta_list.length == 0) {
            return results;
        }
        int result = 0;
        try {
            result = this.set_metadata(new_group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE);
        } catch (IOException ex) {
            result = 5;
            throw ex;
        } finally {
            if (result != 0) {
                this.errno = (byte) result;
                this.delete_file(new_group_name, remote_filename);
                return null;
            }
        }
        return results;
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        releaseConnection(connection, bNewStorageServer);
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Example 17 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class StorageClient method regenerate_appender_filename.

/**
 * regenerate filename for appender file
 *
 * @param group_name        the group name of appender file
 * @param appender_filename the appender filename
 * @return 2 elements string array if success:<br>
 * <ul><li> results[0]: the group name to store the file</li></ul>
 * <ul><li> results[1]: the new created filename</li></ul>
 * return null if fail
 */
public String[] regenerate_appender_filename(String group_name, String appender_filename) throws IOException, ServiceException {
    byte[] header;
    boolean bNewStorageServer;
    Connection connection = null;
    byte[] hexLenBytes;
    byte[] appenderFilenameBytes;
    int offset;
    long body_len;
    if ((group_name == null || group_name.length() == 0) || (appender_filename == null || appender_filename.length() == 0)) {
        this.errno = ProtoCommon.ERR_NO_EINVAL;
        return null;
    }
    bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
    try {
        connection = this.storageServer.getConnection();
        appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
        body_len = appenderFilenameBytes.length;
        header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_REGENERATE_APPENDER_FILENAME, body_len, (byte) 0);
        byte[] wholePkg = new byte[(int) (header.length + body_len)];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        offset = header.length;
        System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
        offset += appenderFilenameBytes.length;
        OutputStream out = connection.getOutputStream();
        out.write(wholePkg);
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        if (pkgInfo.body.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) {
            throw new ServiceException("body length: " + pkgInfo.body.length + " <= " + ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
        }
        String new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim();
        String remote_filename = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.body.length - ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
        String[] results = new String[2];
        results[0] = new_group_name;
        results[1] = remote_filename;
        return results;
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } finally {
        releaseConnection(connection, bNewStorageServer);
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection)

Example 18 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class ClientGlobal method init.

/**
 * load global variables
 *
 * @param conf_filename config filename
 */
public static void init(String conf_filename) throws IOException, ServiceException {
    IniFileReader iniReader;
    String[] szTrackerServers;
    String[] parts;
    iniReader = new IniFileReader(conf_filename);
    g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
    if (g_connect_timeout < 0) {
        g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
    }
    // millisecond
    g_connect_timeout *= 1000;
    g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
    if (g_network_timeout < 0) {
        g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
    }
    // millisecond
    g_network_timeout *= 1000;
    g_charset = iniReader.getStrValue("charset");
    if (g_charset == null || g_charset.length() == 0) {
        g_charset = "ISO8859-1";
    }
    szTrackerServers = iniReader.getValues("tracker_server");
    if (szTrackerServers == null) {
        throw new ServiceException("item \"tracker_server\" in " + conf_filename + " not found");
    }
    InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
    for (int i = 0; i < szTrackerServers.length; i++) {
        parts = szTrackerServers[i].split("\\:", 2);
        if (parts.length != 2) {
            throw new ServiceException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
        }
        tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
    }
    g_tracker_group = new TrackerGroup(tracker_servers);
    g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
    g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
    if (g_anti_steal_token) {
        g_secret_key = iniReader.getStrValue("http.secret_key");
    }
    g_connection_pool_enabled = iniReader.getBoolValue("connection_pool.enabled", DEFAULT_CONNECTION_POOL_ENABLED);
    g_connection_pool_max_count_per_entry = iniReader.getIntValue("connection_pool.max_count_per_entry", DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
    g_connection_pool_max_idle_time = iniReader.getIntValue("connection_pool.max_idle_time", DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME);
    if (g_connection_pool_max_idle_time < 0) {
        g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
    }
    g_connection_pool_max_idle_time *= 1000;
    g_connection_pool_max_wait_time_in_ms = iniReader.getIntValue("connection_pool.max_wait_time_in_ms", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
    if (g_connection_pool_max_wait_time_in_ms < 0) {
        g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) InetSocketAddress(java.net.InetSocketAddress) IniFileReader(com.zhouzifei.tool.media.file.common.fastdfs.common.IniFileReader)

Example 19 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class TrackerClient method listGroups.

/**
 * list groups
 *
 * @param trackerServer the tracker server
 * @return group stat array, return null if fail
 */
public StructGroupStat[] listGroups(TrackerServer trackerServer) throws IOException, ServiceException {
    byte[] header;
    String ip_addr;
    int port;
    byte cmd;
    int out_len;
    byte store_path;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
        if (trackerServer == null) {
            return null;
        }
    }
    connection = trackerServer.getConnection();
    OutputStream out = connection.getOutputStream();
    try {
        header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_GROUP, 0, (byte) 0);
        out.write(header);
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        ProtoStructDecoder<StructGroupStat> decoder = new ProtoStructDecoder<StructGroupStat>();
        return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize());
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        } finally {
            connection = null;
        }
        throw ex;
    } catch (Exception ex) {
        ex.printStackTrace();
        this.errno = ProtoCommon.ERR_NO_EINVAL;
        return null;
    } finally {
        if (connection != null) {
            try {
                connection.release();
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Connection(com.zhouzifei.tool.common.fastdfs.pool.Connection) IOException(java.io.IOException) ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException)

Example 20 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class AwsS3ApiClient method uploadInputStream.

@Override
public String uploadInputStream(InputStream is, String imageUrl) {
    try (InputStream uploadIs = StreamUtil.clone(is)) {
        final PutObjectResult putObjectResult = amazonS3.putObject(bucketName, imageUrl, uploadIs, null);
        System.out.println(putObjectResult);
        return this.newFileName;
    } catch (IOException e) {
        throw new ServiceException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
    } finally {
        this.amazonS3.shutdown();
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

ServiceException (com.zhouzifei.tool.common.ServiceException)28 IOException (java.io.IOException)12 Connection (com.zhouzifei.tool.common.fastdfs.pool.Connection)4 SdkClientException (com.amazonaws.SdkClientException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)1 DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)1 BosClient (com.baidubce.services.bos.BosClient)1 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)1 ObsClient (com.obs.services.ObsClient)1 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1