Search in sources :

Example 1 with RequestSigner

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++;
        }
    }
}
Also used : ServiceException(com.aliyun.oss.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ClientException(com.aliyun.oss.ClientException) IOException(java.io.IOException) LogUtils.logException(com.aliyun.oss.common.utils.LogUtils.logException) ClientException(com.aliyun.oss.ClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceException(com.aliyun.oss.ServiceException) RequestSigner(com.aliyun.oss.common.auth.RequestSigner)

Example 2 with RequestSigner

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());
    }
}
Also used : LinkedList(java.util.LinkedList) OSS(com.aliyun.oss.OSS) LinkedHashMap(java.util.LinkedHashMap) ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) Credentials(com.aliyun.oss.common.auth.Credentials) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) RequestSigner(com.aliyun.oss.common.auth.RequestSigner) Ignore(org.junit.Ignore)

Example 3 with RequestSigner

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;
    }
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) RequestChecksumHanlder(com.aliyun.oss.common.comm.RequestChecksumHanlder) OSSException(com.aliyun.oss.OSSException) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) RequestProgressHanlder(com.aliyun.oss.common.comm.RequestProgressHanlder) ResponseProgressHandler(com.aliyun.oss.common.comm.ResponseProgressHandler) ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) WebServiceRequest(com.aliyun.oss.model.WebServiceRequest) RequestHandler(com.aliyun.oss.common.comm.RequestHandler) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ResponseChecksumHandler(com.aliyun.oss.common.comm.ResponseChecksumHandler) RequestSigner(com.aliyun.oss.common.auth.RequestSigner)

Aggregations

RequestSigner (com.aliyun.oss.common.auth.RequestSigner)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)1 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 OSSException (com.aliyun.oss.OSSException)1 ServiceException (com.aliyun.oss.ServiceException)1 Credentials (com.aliyun.oss.common.auth.Credentials)1 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)1 RequestChecksumHanlder (com.aliyun.oss.common.comm.RequestChecksumHanlder)1 RequestHandler (com.aliyun.oss.common.comm.RequestHandler)1 RequestProgressHanlder (com.aliyun.oss.common.comm.RequestProgressHanlder)1 ResponseChecksumHandler (com.aliyun.oss.common.comm.ResponseChecksumHandler)1 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)1 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)1 ResponseProgressHandler (com.aliyun.oss.common.comm.ResponseProgressHandler)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 LogUtils.logException (com.aliyun.oss.common.utils.LogUtils.logException)1 WebServiceRequest (com.aliyun.oss.model.WebServiceRequest)1