Search in sources :

Example 11 with ServiceException

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

the class UpaiyunOssApiClient method init.

@Override
public UpaiyunOssApiClient init(FileProperties fileProperties) {
    final UpaiFileProperties upaiFileProperties = fileProperties.getUpai();
    String userName = upaiFileProperties.getUserName();
    String passWord = upaiFileProperties.getPassWord();
    String url = upaiFileProperties.getUrl();
    String bucketName = upaiFileProperties.getBucketName();
    checkDomainUrl(url);
    if (StringUtils.isNullOrEmpty(userName) || StringUtils.isNullOrEmpty(passWord) || StringUtils.isNullOrEmpty(bucketName)) {
        throw new ServiceException("[" + this.storageType + "]尚未配置又拍云,文件上传功能暂时不可用!");
    }
    upaiManager = new UpaiManager(bucketName, userName, passWord);
    return this;
}
Also used : UpaiFileProperties(com.zhouzifei.tool.config.UpaiFileProperties) UpaiManager(com.zhouzifei.tool.common.upaiyun.UpaiManager) ServiceException(com.zhouzifei.tool.common.ServiceException)

Example 12 with ServiceException

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

the class TrackerClient method listStorages.

/**
 * query storage server stat info of the group
 *
 * @param trackerServer the tracker server
 * @param groupName     the group name of storage server
 * @param storageIpAddr the storage server ip address, can be null or empty
 * @return storage server stat array, return null if fail
 */
public StructStorageStat[] listStorages(TrackerServer trackerServer, String groupName, String storageIpAddr) throws IOException, ServiceException {
    byte[] header;
    byte[] bGroupName;
    byte[] bs;
    int len;
    Connection connection;
    if (trackerServer == null) {
        trackerServer = getTrackerServer();
        if (trackerServer == null) {
            return null;
        }
    }
    connection = trackerServer.getConnection();
    OutputStream out = connection.getOutputStream();
    try {
        bs = groupName.getBytes(ClientGlobal.g_charset);
        bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
        if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) {
            len = bs.length;
        } else {
            len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
        }
        Arrays.fill(bGroupName, (byte) 0);
        System.arraycopy(bs, 0, bGroupName, 0, len);
        int ipAddrLen;
        byte[] bIpAddr;
        if (storageIpAddr != null && storageIpAddr.length() > 0) {
            bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset);
            if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) {
                ipAddrLen = bIpAddr.length;
            } else {
                ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1;
            }
        } else {
            bIpAddr = null;
            ipAddrLen = 0;
        }
        header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte) 0);
        byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen];
        System.arraycopy(header, 0, wholePkg, 0, header.length);
        System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length);
        if (ipAddrLen > 0) {
            System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen);
        }
        out.write(wholePkg);
        ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(), ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
        this.errno = pkgInfo.errno;
        if (pkgInfo.errno != 0) {
            return null;
        }
        ProtoStructDecoder<StructStorageStat> decoder = new ProtoStructDecoder<StructStorageStat>();
        return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize());
    } catch (IOException ex) {
        try {
            connection.close();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        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 13 with ServiceException

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

the class ConnectionManager method getConnection.

public Connection getConnection() throws ServiceException {
    lock.lock();
    try {
        Connection connection = null;
        while (true) {
            if (freeCount.get() > 0) {
                freeCount.decrementAndGet();
                connection = freeConnections.poll();
                if (!connection.isAvaliable() || (System.currentTimeMillis() - connection.getLastAccessTime()) > ClientGlobal.g_connection_pool_max_idle_time) {
                    closeConnection(connection);
                    continue;
                }
                if (connection.isNeedActiveTest()) {
                    boolean isActive = false;
                    try {
                        isActive = connection.activeTest();
                    } catch (IOException e) {
                        System.err.println("send to server[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] active test error ,emsg:" + e.getMessage());
                        isActive = false;
                    }
                    if (!isActive) {
                        closeConnection(connection);
                        continue;
                    } else {
                        connection.setNeedActiveTest(false);
                    }
                }
            } else if (ClientGlobal.g_connection_pool_max_count_per_entry == 0 || totalCount.get() < ClientGlobal.g_connection_pool_max_count_per_entry) {
                connection = ConnectionFactory.create(this.inetSocketAddress);
                totalCount.incrementAndGet();
            } else {
                try {
                    if (condition.await(ClientGlobal.g_connection_pool_max_wait_time_in_ms, TimeUnit.MILLISECONDS)) {
                        // wait single success
                        continue;
                    }
                    throw new ServiceException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    throw new ServiceException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage());
                }
            }
            return connection;
        }
    } finally {
        lock.unlock();
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException)

Example 14 with ServiceException

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

the class ConnectionFactory method create.

/**
 * create from InetSocketAddress
 *
 * @param socketAddress
 * @return
 * @throws IOException
 */
public static Connection create(InetSocketAddress socketAddress) throws ServiceException {
    try {
        Socket sock = new Socket();
        sock.setReuseAddress(true);
        sock.setSoTimeout(ClientGlobal.g_network_timeout);
        sock.connect(socketAddress, ClientGlobal.g_connect_timeout);
        return new Connection(sock, socketAddress);
    } catch (Exception e) {
        throw new ServiceException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg:" + e.getMessage());
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) Socket(java.net.Socket) ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException)

Example 15 with ServiceException

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

the class UpYunUtils method sign.

public static String sign(String method, String date, String path, String userName, String password, String md5) throws ServiceException {
    StringBuilder sb = new StringBuilder();
    String sp = "&";
    sb.append(method);
    sb.append(sp);
    sb.append(path);
    sb.append(sp);
    sb.append(date);
    if (md5 != null && md5.length() > 0) {
        sb.append(sp);
        sb.append(md5);
    }
    String raw = sb.toString().trim();
    byte[] hmac = null;
    try {
        hmac = calculateRFC2104HMACRaw(password, raw);
    } catch (Exception e) {
        throw new ServiceException("calculate SHA1 wrong.");
    }
    if (hmac != null) {
        return "UPYUN " + userName + ":" + Base64Coder.encodeLines(hmac).trim();
    }
    return null;
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) ServiceException(com.zhouzifei.tool.common.ServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

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