Search in sources :

Example 1 with PrepareUrl

use of com.aliyuncs.fc.model.PrepareUrl in project fc-java-sdk by aliyun.

the class DefaultFcClient method doAction.

public HttpResponse doAction(HttpRequest request, String form, String method) throws ClientException, ServerException {
    request.validate();
    try {
        PrepareUrl prepareUrl = signRequest(request, form, method);
        int retryTimes = 1;
        HttpResponse response = HttpResponse.getResponse(prepareUrl.getUrl(), prepareUrl.getHeader(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
        while (500 <= response.getStatus() && AUTO_RETRY && retryTimes < MAX_RETRIES) {
            prepareUrl = signRequest(request, form, method);
            response = HttpResponse.getResponse(prepareUrl.getUrl(), prepareUrl.getHeader(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
            retryTimes++;
        }
        if (response.getStatus() >= 500) {
            String requestId = response.getHeaderValue(HeaderKeys.REQUEST_ID);
            String stringContent = response.getContent() == null ? "" : new String(response.getContent());
            ServerException se;
            try {
                se = new Gson().fromJson(stringContent, ServerException.class);
            } catch (JsonParseException e) {
                se = new ServerException("InternalServiceError", "Failed to parse response content", requestId);
            }
            se.setStatusCode(response.getStatus());
            se.setRequestId(requestId);
            throw se;
        } else if (response.getStatus() >= 300) {
            ClientException ce;
            if (response.getContent() == null) {
                ce = new ClientException("SDK.ServerUnreachable", "Failed to get response content from server");
            } else {
                try {
                    ce = new Gson().fromJson(new String(response.getContent()), ClientException.class);
                } catch (JsonParseException e) {
                    ce = new ClientException("SDK.ResponseNotParsable", "Failed to parse response content", e);
                }
            }
            if (ce == null) {
                ce = new ClientException("SDK.UnknownError", "Unknown client error");
            }
            ce.setStatusCode(response.getStatus());
            ce.setRequestId(response.getHeaderValue(HeaderKeys.REQUEST_ID));
            throw ce;
        }
        return response;
    } catch (InvalidKeyException exp) {
        throw new ClientException("SDK.InvalidAccessSecret", "Speicified access secret is not valid.");
    } catch (SocketTimeoutException exp) {
        throw new ClientException("SDK.ServerUnreachable", "SocketTimeoutException has occurred on a socket read or accept.");
    } catch (IOException exp) {
        throw new ClientException("SDK.ServerUnreachable", "Server unreachable: " + exp.toString());
    } catch (NoSuchAlgorithmException exp) {
        throw new ClientException("SDK.InvalidMD5Algorithm", "MD5 hash is not supported by client side.");
    }
}
Also used : PrepareUrl(com.aliyuncs.fc.model.PrepareUrl) ServerException(com.aliyuncs.fc.exceptions.ServerException) SocketTimeoutException(java.net.SocketTimeoutException) HttpResponse(com.aliyuncs.fc.http.HttpResponse) Gson(com.google.gson.Gson) ClientException(com.aliyuncs.fc.exceptions.ClientException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonParseException(com.google.gson.JsonParseException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with PrepareUrl

use of com.aliyuncs.fc.model.PrepareUrl in project fc-java-sdk by aliyun.

the class AsyncInternalClient method asyncSend.

protected <Res> void asyncSend(HttpRequest request, AbstractResponseConsumer<Res> consumer, FutureCallback<Res> callback, String contentType, HttpMethod method, boolean httpInvoke) throws ClientException {
    try {
        // try refresh credentials if CredentialProvider set
        config.refreshCredentials();
        // Add all needed headers
        if (!httpInvoke || !ANONYMOUS.equals(((HttpInvokeFunctionRequest) request).getAuthType())) {
            FcUtil.signRequest(config, request, contentType, method, httpInvoke);
        }
        // Construct HttpRequest
        PrepareUrl prepareUrl = FcUtil.prepareUrl(request.getPath(), request.getQueryParams(), this.config);
        RequestBuilder requestBuilder = RequestBuilder.create(method.name()).setUri(prepareUrl.getUrl());
        copyToRequest(request, requestBuilder);
        HttpUriRequest httpRequest = requestBuilder.build();
        HttpHost httpHost = URIUtils.extractHost(httpRequest.getURI());
        httpClient.execute(new FcRequestProducer(httpHost, httpRequest), consumer, callback);
    } catch (Exception e) {
        throw new ClientException(e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) PrepareUrl(com.aliyuncs.fc.model.PrepareUrl) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpHost(org.apache.http.HttpHost) ClientException(com.aliyuncs.fc.exceptions.ClientException) IOException(java.io.IOException) ClientException(com.aliyuncs.fc.exceptions.ClientException) IOReactorException(org.apache.http.nio.reactor.IOReactorException)

Example 3 with PrepareUrl

use of com.aliyuncs.fc.model.PrepareUrl in project fc-java-sdk by aliyun.

the class DefaultFcClient method doAction.

/**
 * if form paramter is null, it will use content-type of request.headers
 */
public HttpResponse doAction(HttpRequest request, String form, HttpMethod method) throws ClientException, ServerException {
    request.validate();
    // try refresh credentials if CredentialProvider set
    this.config.refreshCredentials();
    try {
        int retryTimes = 0;
        HttpResponse response = null;
        boolean httpInvoke = false;
        if (request instanceof HttpInvokeFunctionRequest) {
            httpInvoke = true;
        }
        do {
            if (!httpInvoke || !ANONYMOUS.equals(((HttpInvokeFunctionRequest) request).getAuthType())) {
                FcUtil.signRequest(config, request, form, method, httpInvoke);
            }
            PrepareUrl prepareUrl = FcUtil.prepareUrl(request.getPath(), request.getQueryParams(), config);
            response = HttpResponse.getResponse(prepareUrl.getUrl(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
            retryTimes++;
            if (httpInvoke) {
                return response;
            }
        } while (500 <= response.getStatus() && AUTO_RETRY && retryTimes < MAX_RETRIES);
        if (response.getStatus() >= 500) {
            String requestId = response.getHeader(HeaderKeys.REQUEST_ID);
            String stringContent = response.getContent() == null ? "" : FcUtil.toDefaultCharset(response.getContent());
            ServerException se;
            try {
                se = new Gson().fromJson(stringContent, ServerException.class);
            } catch (JsonParseException e) {
                se = new ServerException("InternalServiceError", "Failed to parse response content", requestId);
            }
            se.setStatusCode(response.getStatus());
            se.setRequestId(requestId);
            throw se;
        } else if (response.getStatus() >= 300) {
            ClientException ce;
            if (response.getContent() == null) {
                ce = new ClientException("SDK.ServerUnreachable", "Failed to get response content from server");
            } else {
                try {
                    ce = new Gson().fromJson(FcUtil.toDefaultCharset(response.getContent()), ClientException.class);
                } catch (JsonParseException e) {
                    ce = new ClientException("SDK.ResponseNotParsable", "Failed to parse response content", e);
                }
            }
            if (ce == null) {
                ce = new ClientException("SDK.UnknownError", "Unknown client error");
            }
            ce.setStatusCode(response.getStatus());
            ce.setRequestId(response.getHeader(HeaderKeys.REQUEST_ID));
            throw ce;
        }
        return response;
    } catch (InvalidKeyException exp) {
        throw new ClientException("SDK.InvalidAccessSecret", "Speicified access secret is not valid.");
    } catch (SocketTimeoutException exp) {
        throw new ClientException("SDK.ServerUnreachable", "SocketTimeoutException has occurred on a socket read or accept.");
    } catch (IOException exp) {
        throw new ClientException("SDK.ServerUnreachable", "Server unreachable: " + exp.toString());
    } catch (NoSuchAlgorithmException exp) {
        throw new ClientException("SDK.InvalidMD5Algorithm", "MD5 hash is not supported by client side.");
    } catch (URISyntaxException e) {
        throw new ClientException("SDK.InvalidURL", "url is not valid: " + e.getMessage());
    }
}
Also used : HttpInvokeFunctionRequest(com.aliyuncs.fc.request.HttpInvokeFunctionRequest) ServerException(com.aliyuncs.fc.exceptions.ServerException) HttpResponse(com.aliyuncs.fc.http.HttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) JsonParseException(com.google.gson.JsonParseException) InvalidKeyException(java.security.InvalidKeyException) PrepareUrl(com.aliyuncs.fc.model.PrepareUrl) SocketTimeoutException(java.net.SocketTimeoutException) ClientException(com.aliyuncs.fc.exceptions.ClientException)

Aggregations

ClientException (com.aliyuncs.fc.exceptions.ClientException)3 PrepareUrl (com.aliyuncs.fc.model.PrepareUrl)3 IOException (java.io.IOException)3 ServerException (com.aliyuncs.fc.exceptions.ServerException)2 HttpResponse (com.aliyuncs.fc.http.HttpResponse)2 Gson (com.google.gson.Gson)2 JsonParseException (com.google.gson.JsonParseException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HttpInvokeFunctionRequest (com.aliyuncs.fc.request.HttpInvokeFunctionRequest)1 URISyntaxException (java.net.URISyntaxException)1 HttpHost (org.apache.http.HttpHost)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 IOReactorException (org.apache.http.nio.reactor.IOReactorException)1