use of com.aliyun.oss.common.auth.RequestSigner 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.common.auth.RequestSigner in project aliyun-oss-java-sdk by aliyun.
the class ProxySignTest method testProxyAuth.
@Ignore
public void testProxyAuth() {
String bucketName = "sdk-test-md-1";
String key = "mingdi/test.txt";
String content = "Hello OSS.";
String proxyHost = "";
String endpoint = "";
String accessKeyId = "";
String secretAccessKey = "";
try {
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setProxyHost(proxyHost);
conf.setProxyPort(8080);
Credentials credentials = new ProxyCredentials("mingditest");
ProxyRequestSigner proxySigner = new ProxyRequestSigner(credentials);
List<RequestSigner> signerHandlers = new LinkedList<RequestSigner>();
signerHandlers.add(proxySigner);
conf.setSignerHandlers(signerHandlers);
Map<String, String> proxyHeaders = new LinkedHashMap<String, String>();
proxyHeaders.put(HEADER_PROXY_TYPE, "default");
proxyHeaders.put(HEADER_PROXY_USER, "diff_bucket_sync_test_user_1");
proxyHeaders.put(HEADER_PROXY_DEST, "cn-qingdao");
proxyHeaders.put(HEADER_PROXY_DEST_REGION, "cn-qingdao");
proxyHeaders.put(HEADER_PROXY_REAL_HOST, endpoint);
conf.setDefaultHeaders(proxyHeaders);
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secretAccessKey, conf);
ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
ossClient.getObject(bucketName, key);
ossClient.deleteObject(bucketName, key);
} catch (Throwable e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.RequestSigner in project aliyun-oss-java-sdk by aliyun.
the class OSSOperation method doOperation.
protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, String bucketName, String key, boolean keepResponseOpen, List<RequestHandler> requestHandlers, List<ResponseHandler> reponseHandlers) throws OSSException, ClientException {
final WebServiceRequest originalRequest = request.getOriginalRequest();
request.getHeaders().putAll(client.getClientConfiguration().getDefaultHeaders());
request.getHeaders().putAll(originalRequest.getHeaders());
request.getParameters().putAll(originalRequest.getParameters());
ExecutionContext context = createDefaultContext(request.getMethod(), bucketName, key);
if (context.getCredentials().useSecurityToken() && !request.isUseUrlSignature()) {
request.addHeader(OSSHeaders.OSS_SECURITY_TOKEN, context.getCredentials().getSecurityToken());
}
context.addRequestHandler(new RequestProgressHanlder());
if (requestHandlers != null) {
for (RequestHandler handler : requestHandlers) context.addRequestHandler(handler);
}
if (client.getClientConfiguration().isCrcCheckEnabled()) {
context.addRequestHandler(new RequestChecksumHanlder());
}
context.addResponseHandler(new ResponseProgressHandler(originalRequest));
if (reponseHandlers != null) {
for (ResponseHandler handler : reponseHandlers) context.addResponseHandler(handler);
}
if (client.getClientConfiguration().isCrcCheckEnabled()) {
context.addResponseHandler(new ResponseChecksumHandler());
}
List<RequestSigner> signerHandlers = this.client.getClientConfiguration().getSignerHandlers();
if (signerHandlers != null) {
for (RequestSigner signer : signerHandlers) {
context.addSignerHandler(signer);
}
}
ResponseMessage response = send(request, context, keepResponseOpen);
try {
return parser.parse(response);
} catch (ResponseParseException rpe) {
OSSException oe = ExceptionFactory.createInvalidResponseException(response.getRequestId(), rpe.getMessage(), rpe);
logException("Unable to parse response error: ", rpe);
throw oe;
}
}
Aggregations