use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ServiceClient method sendRequest.
/**
* Send HTTP request with specified context to OSS and wait for HTTP
* response.
*/
public ResponseMessage sendRequest(RequestMessage request, ExecutionContext context) throws ServiceException, ClientException {
assertParameterNotNull(request, "request");
assertParameterNotNull(context, "context");
try {
return sendRequestImpl(request, context);
} finally {
// Close the request stream as well after the request is completed.
try {
request.close();
} catch (IOException ex) {
logException("Unexpected io exception when trying to close http request: ", ex);
throw new ClientException("Unexpected io exception when trying to close http request: ", ex);
}
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ServiceClient method sendRequestImpl.
private ResponseMessage sendRequestImpl(RequestMessage request, ExecutionContext context) throws ClientException, ServiceException {
RetryStrategy retryStrategy = context.getRetryStrategy() != null ? context.getRetryStrategy() : this.getDefaultRetryStrategy();
// Sign the request if a signer provided.
if (context.getSigner() != null && !request.isUseUrlSignature()) {
context.getSigner().sign(request);
}
for (RequestSigner signer : context.getSignerHandlers()) {
signer.sign(request);
}
InputStream requestContent = request.getContent();
if (requestContent != null && requestContent.markSupported()) {
requestContent.mark(OSSConstants.DEFAULT_STREAM_BUFFER_SIZE);
}
int retries = 0;
ResponseMessage response = null;
while (true) {
try {
if (retries > 0) {
pause(retries, retryStrategy);
if (requestContent != null && requestContent.markSupported()) {
try {
requestContent.reset();
} catch (IOException ex) {
logException("Failed to reset the request input stream: ", ex);
throw new ClientException("Failed to reset the request input stream: ", ex);
}
}
}
/*
* The key four steps to send HTTP requests and receive HTTP
* responses.
*/
// Step 1. Preprocess HTTP request.
handleRequest(request, context.getResquestHandlers());
// Step 2. Build HTTP request with specified request parameters
// and context.
Request httpRequest = buildRequest(request, context);
// Step 3. Send HTTP request to OSS.
long startTime = System.currentTimeMillis();
response = sendRequestCore(httpRequest, context);
long duration = System.currentTimeMillis() - startTime;
if (duration > config.getSlowRequestsThreshold()) {
LogUtils.getLog().warn(formatSlowRequestLog(request, response, duration));
}
// Step 4. Preprocess HTTP response.
handleResponse(response, context.getResponseHandlers());
return response;
} catch (ServiceException sex) {
logException("[Server]Unable to execute HTTP request: ", sex, request.getOriginalRequest().isLogEnabled());
// Notice that the response should not be closed in the
// finally block because if the request is successful,
// the response should be returned to the callers.
closeResponseSilently(response);
if (!shouldRetry(sex, request, response, retries, retryStrategy)) {
throw sex;
}
} catch (ClientException cex) {
logException("[Client]Unable to execute HTTP request: ", cex, request.getOriginalRequest().isLogEnabled());
closeResponseSilently(response);
if (!shouldRetry(cex, request, response, retries, retryStrategy)) {
throw cex;
}
} catch (Exception ex) {
logException("[Unknown]Unable to execute HTTP request: ", ex, request.getOriginalRequest().isLogEnabled());
closeResponseSilently(response);
throw new ClientException(COMMON_RESOURCE_MANAGER.getFormattedString("ConnectionError", ex.getMessage()), ex);
} finally {
retries++;
}
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ChunkedUploadStream method fillInputBuffer.
private int fillInputBuffer() {
if (innerStreamConsumed) {
return 0;
}
int inputBufferPos = 0;
while (inputBufferPos < inputBuffer.length && !innerStreamConsumed) {
int chunkBufferRemaining = inputBuffer.length - inputBufferPos;
if (chunkBufferRemaining > innerStreamBufferSize) {
chunkBufferRemaining = innerStreamBufferSize;
}
int bytesRead = 0;
try {
bytesRead = innerStream.read(inputBuffer, inputBufferPos, chunkBufferRemaining);
if (bytesRead == -1) {
innerStreamConsumed = true;
} else {
inputBufferPos += bytesRead;
}
} catch (IOException e) {
throw new ClientException("Unexpected IO exception, " + e.getMessage(), e);
}
}
return inputBufferPos;
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ChunkedUploadStream method constructOutputBufferChunk.
private void constructOutputBufferChunk(int dataLen) {
StringBuilder chunkHeader = new StringBuilder();
chunkHeader.append(Integer.toHexString(dataLen));
chunkHeader.append(CLRF);
try {
byte[] header = chunkHeader.toString().getBytes(DEFAULT_CHARTSET_NAME);
byte[] trailer = CLRF.getBytes(DEFAULT_CHARTSET_NAME);
int writePos = 0;
System.arraycopy(header, 0, outputBuffer, writePos, header.length);
writePos += header.length;
if (dataLen > 0) {
System.arraycopy(inputBuffer, 0, outputBuffer, writePos, dataLen);
writePos += dataLen;
}
System.arraycopy(trailer, 0, outputBuffer, writePos, trailer.length);
outputBufferPos = 0;
outputBufferDataLen = header.length + dataLen + trailer.length;
} catch (Exception e) {
throw new ClientException("Unable to sign the chunked data, " + e.getMessage(), e);
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class RequestTimeoutTest method testObjectOperationsNegative.
/**
* Negative test cases
* Reading a non-existing bucket, object.
* Uploading a null stream.
* Uploading an object with 1ms timeout value.
*/
@Test
public void testObjectOperationsNegative() throws Exception {
String bucket = "test-object-operations-negative";
String key = "test-object-operations-negative";
try {
ossClient.getBucketInfo(bucket);
Assert.fail("Get bucket info should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
}
try {
ossClient.getObject(bucketName, key);
Assert.fail("Get object should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
}
try {
InputStream inputStream = null;
ossClient.putObject(bucket, key, inputStream);
Assert.fail("Put object should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
try {
ossClient.getClientConfiguration().setRequestTimeout(1);
ossClient.getObject(bucketName, key);
Assert.fail("Get object should not be successful");
} catch (ClientException e) {
Assert.assertEquals(OSSErrorCode.REQUEST_TIMEOUT, e.getErrorCode());
} finally {
ossClient.getClientConfiguration().setRequestTimeout(requestTimeout);
}
}
Aggregations