use of com.aliyun.oss.common.comm.ResponseHandler in project aliyun-oss-java-sdk by aliyun.
the class ServiceClientTest method testRetryWithServiceException.
@Test
public void testRetryWithServiceException() throws Exception {
// This request will fail after 1 retries
ClientConfiguration config = new ClientConfiguration();
config.setMaxErrorRetry(1);
// It should be always successful.
int maxFailures = 0;
String content = "Let's retry!";
byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
RequestMessage request = new RequestMessage(null, null);
request.setEndpoint(new URI("http://localhost"));
request.setMethod(HttpMethod.GET);
request.setContent(contentStream);
request.setContentLength(contentBytes.length);
ExecutionContext context = new ExecutionContext();
context.getResponseHandlers().add(new ResponseHandler() {
@Override
public void handle(ResponseMessage responseData) throws ServiceException, ClientException {
throw new ServiceException();
}
});
// This request will succeed after 2 retries
ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, null, 500, content);
// Fix the max error retry count to 3
try {
client.sendRequest(request, context);
fail("ServiceException has not been thrown.");
} catch (ServiceException e) {
assertEquals(2, client.getRequestAttempts());
}
}
use of com.aliyun.oss.common.comm.ResponseHandler in project aliyun-oss-java-sdk by aliyun.
the class OSSObjectOperation method writeObjectInternal.
private <RequestType extends PutObjectRequest, ResponseType> ResponseType writeObjectInternal(WriteMode mode, RequestType originalRequest, ResponseParser<ResponseType> responseParser) {
final String bucketName = originalRequest.getBucketName();
final String key = originalRequest.getKey();
InputStream originalInputStream = originalRequest.getInputStream();
ObjectMetadata metadata = originalRequest.getMetadata();
if (metadata == null) {
metadata = new ObjectMetadata();
}
assertParameterNotNull(bucketName, "bucketName");
assertParameterNotNull(key, "key");
ensureBucketNameValid(bucketName);
ensureObjectKeyValid(key);
ensureCallbackValid(originalRequest.getCallback());
InputStream repeatableInputStream = null;
if (originalRequest.getFile() != null) {
File toUpload = originalRequest.getFile();
if (!checkFile(toUpload)) {
getLog().info("Illegal file path: " + toUpload.getPath());
throw new ClientException("Illegal file path: " + toUpload.getPath());
}
metadata.setContentLength(toUpload.length());
if (metadata.getContentType() == null) {
metadata.setContentType(Mimetypes.getInstance().getMimetype(toUpload, key));
}
try {
repeatableInputStream = new RepeatableFileInputStream(toUpload);
} catch (IOException ex) {
logException("Cannot locate file to upload: ", ex);
throw new ClientException("Cannot locate file to upload: ", ex);
}
} else {
assertTrue(originalInputStream != null, "Please specify input stream or file to upload");
if (metadata.getContentType() == null) {
metadata.setContentType(Mimetypes.getInstance().getMimetype(key));
}
try {
repeatableInputStream = newRepeatableInputStream(originalInputStream);
} catch (IOException ex) {
logException("Cannot wrap to repeatable input stream: ", ex);
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
}
}
Map<String, String> headers = new HashMap<String, String>();
populateRequestMetadata(headers, metadata);
populateRequestCallback(headers, originalRequest.getCallback());
Map<String, String> params = new LinkedHashMap<String, String>();
populateWriteObjectParams(mode, originalRequest, params);
RequestMessage httpRequest = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(WriteMode.getMappingMethod(mode)).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params).setInputStream(repeatableInputStream).setInputSize(determineInputStreamLength(repeatableInputStream, metadata.getContentLength())).setOriginalRequest(originalRequest).build();
List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
reponseHandlers.add(new OSSCallbackErrorResponseHandler());
final ProgressListener listener = originalRequest.getProgressListener();
ResponseType result = null;
try {
publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
if (originalRequest.getCallback() == null) {
result = doOperation(httpRequest, responseParser, bucketName, key, true);
} else {
result = doOperation(httpRequest, responseParser, bucketName, key, true, null, reponseHandlers);
}
publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
} catch (RuntimeException e) {
publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
throw e;
}
return result;
}
Aggregations