Search in sources :

Example 56 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class XmlResponsesSaxParser method parseXmlInputStream.

/**
 * Parses an XML document from an input stream using a document handler.
 *
 * @param handler the handler for the XML document
 * @param inputStream an input stream containing the XML document to parse
 *
 * @throws IOException on error reading from the input stream (ie connection reset)
 * @throws CosClientException on error with malformed XML, etc
 */
protected void parseXmlInputStream(DefaultHandler handler, InputStream inputStream) throws IOException {
    try {
        if (log.isDebugEnabled()) {
            log.debug("Parsing XML response document with handler: " + handler.getClass());
        }
        BufferedReader breader = new BufferedReader(new InputStreamReader(inputStream, StringUtils.UTF8));
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);
        xr.parse(new InputSource(breader));
    } catch (IOException e) {
        throw e;
    } catch (Throwable t) {
        try {
            inputStream.close();
        } catch (IOException e) {
            if (log.isErrorEnabled()) {
                log.error("Unable to close response InputStream up after XML parse failure", e);
            }
        }
        throw new CosClientException("Failed to parse XML document with handler " + handler.getClass(), t);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) CosClientException(com.qcloud.cos.exception.CosClientException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 57 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class XmlResponsesSaxParser method sanitizeXmlDocument.

protected InputStream sanitizeXmlDocument(DefaultHandler handler, InputStream inputStream) throws IOException {
    if (!sanitizeXmlDocument) {
        // No sanitizing will be performed, return the original input stream unchanged.
        return inputStream;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Sanitizing XML document destined for handler " + handler.getClass());
        }
        InputStream sanitizedInputStream = null;
        try {
            /*
                 * Read object listing XML document from input stream provided into a string buffer,
                 * so we can replace troublesome characters before sending the document to the XML
                 * parser.
                 */
            StringBuilder listingDocBuffer = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StringUtils.UTF8));
            char[] buf = new char[8192];
            int read = -1;
            while ((read = br.read(buf)) != -1) {
                listingDocBuffer.append(buf, 0, read);
            }
            br.close();
            /*
                 * Replace any carriage return (\r) characters with explicit XML character entities,
                 * to prevent the SAX parser from misinterpreting 0x0D characters as 0x0A and being
                 * unable to parse the XML.
                 */
            String listingDoc = listingDocBuffer.toString().replaceAll("\r", "
");
            sanitizedInputStream = new ByteArrayInputStream(listingDoc.getBytes(StringUtils.UTF8));
        } catch (IOException e) {
            throw e;
        } catch (Throwable t) {
            try {
                inputStream.close();
            } catch (IOException e) {
                if (log.isErrorEnabled()) {
                    log.error("Unable to close response InputStream after failure sanitizing XML document", e);
                }
            }
            throw new CosClientException("Failed to sanitize XML document destined for handler " + handler.getClass(), t);
        }
        return sanitizedInputStream;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 58 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project alluxio by Alluxio.

the class COSUnderFileSystem method createEmptyObject.

@Override
public boolean createEmptyObject(String key) {
    try {
        ObjectMetadata objMeta = new ObjectMetadata();
        objMeta.setContentLength(0);
        mClient.putObject(mBucketNameInternal, key, new ByteArrayInputStream(new byte[0]), objMeta);
        return true;
    } catch (CosClientException e) {
        LOG.error("Failed to create object: {}", key, e);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 59 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method retrieveBlock.

@Override
public InputStream retrieveBlock(String key, long byteRangeStart, long byteRangeEnd) throws IOException {
    LOG.debug("Retrieve the cos key: {}, byte range start: {}, byte range end: {}.", key, byteRangeStart, byteRangeEnd);
    try {
        GetObjectRequest request = new GetObjectRequest(this.bucketName, key);
        request.setRange(byteRangeStart, byteRangeEnd);
        if (this.trafficLimit >= 0) {
            request.setTrafficLimit(this.trafficLimit);
        }
        this.setEncryptionMetadata(request, new ObjectMetadata());
        COSObject cosObject = (COSObject) this.callCOSClientWithRetry(request);
        return cosObject.getObjectContent();
    } catch (CosServiceException e) {
        String errMsg = String.format("Retrieving the key %s with the byteRangeStart [%d] " + "occurs an exception: %s.", key, byteRangeStart, e);
        handleException(new Exception(errMsg), key);
    } catch (CosClientException e) {
        String errMsg = String.format("Retrieving key %s with the byteRangeStart [%d] and the byteRangeEnd [%d] " + "occurs an exception: %s.", key, byteRangeStart, byteRangeEnd, e);
        handleException(new Exception(errMsg), key);
    }
    return null;
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException)

Example 60 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method queryObjectMetadata.

private FileMetadata queryObjectMetadata(String key, CosNResultInfo info) throws IOException {
    LOG.debug("Query Object metadata. cos key: {}.", key);
    GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(bucketName, key);
    this.setEncryptionMetadata(getObjectMetadataRequest, new ObjectMetadata());
    try {
        ObjectMetadata objectMetadata = (ObjectMetadata) callCOSClientWithRetry(getObjectMetadataRequest);
        long mtime = 0;
        long fileSize;
        if (objectMetadata.getLastModified() != null) {
            mtime = objectMetadata.getLastModified().getTime();
        }
        fileSize = objectMetadata.getContentLength();
        String ETag = objectMetadata.getETag();
        String crc64ecm = objectMetadata.getCrc64Ecma();
        String crc32cm = (String) objectMetadata.getRawMetadataValue(Constants.CRC32C_RESP_HEADER);
        String versionId = objectMetadata.getVersionId();
        Map<String, byte[]> userMetadata = null;
        if (objectMetadata.getUserMetadata() != null) {
            userMetadata = new HashMap<>();
            for (Map.Entry<String, String> userMetadataEntry : objectMetadata.getUserMetadata().entrySet()) {
                if (userMetadataEntry.getKey().startsWith(ensureValidAttributeName(XATTR_PREFIX))) {
                    String xAttrJsonStr = new String(Base64.decode(userMetadataEntry.getValue()), StandardCharsets.UTF_8);
                    CosNXAttr cosNXAttr;
                    try {
                        cosNXAttr = Jackson.fromJsonString(xAttrJsonStr, CosNXAttr.class);
                    } catch (CosClientException e) {
                        LOG.warn("Parse the xAttr failed. name: {}, XAttJsonStr: {}.", userMetadataEntry.getKey(), xAttrJsonStr);
                        // Skip
                        continue;
                    }
                    if (null != cosNXAttr) {
                        userMetadata.put(cosNXAttr.getName(), cosNXAttr.getValue().getBytes(CosNFileSystem.METADATA_ENCODING));
                    }
                }
            }
        }
        boolean isFile = true;
        if (isPosixBucket) {
            if (objectMetadata.isFileModeDir() || key.equals(CosNFileSystem.PATH_DELIMITER)) {
                isFile = false;
            }
        } else {
            isFile = !key.endsWith(CosNFileSystem.PATH_DELIMITER);
        }
        FileMetadata fileMetadata = new FileMetadata(key, fileSize, mtime, isFile, ETag, crc64ecm, crc32cm, versionId, objectMetadata.getStorageClass(), userMetadata);
        // record the last request result info
        if (info != null) {
            info.setRequestID(objectMetadata.getRequestId());
        }
        LOG.debug("Retrieve the file metadata. cos key: {}, ETag:{}, length:{}, crc64ecm: {}.", key, objectMetadata.getETag(), objectMetadata.getContentLength(), objectMetadata.getCrc64Ecma());
        return fileMetadata;
    } catch (CosServiceException e) {
        if (info != null) {
            info.setRequestID(e.getRequestId());
        }
        if (e.getStatusCode() != 404) {
            String errorMsg = String.format("Retrieve the file metadata file failure. " + "cos key: %s, exception: %s", key, e);
            handleException(new Exception(errorMsg), key);
        }
    }
    return null;
}
Also used : CosClientException(com.qcloud.cos.exception.CosClientException) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CosClientException (com.qcloud.cos.exception.CosClientException)111 CosServiceException (com.qcloud.cos.exception.CosServiceException)64 COSCredentials (com.qcloud.cos.auth.COSCredentials)41 ClientConfig (com.qcloud.cos.ClientConfig)39 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)39 Region (com.qcloud.cos.region.Region)39 COSClient (com.qcloud.cos.COSClient)37 IOException (java.io.IOException)31 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)13 URISyntaxException (java.net.URISyntaxException)13 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 SecretKey (javax.crypto.SecretKey)12 MalformedURLException (java.net.MalformedURLException)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)10