Search in sources :

Example 6 with ClientException

use of com.aliyuncs.exceptions.ClientException in project aliyun-oss-java-sdk by aliyun.

the class InstanceProfileCredentialsFetcherTest method testFetchExceptionalCredentials.

@Test
public void testFetchExceptionalCredentials() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Exceptional);
        credentialsFetcher.fetch(3);
        Assert.fail("EcsInstanceCredentialsFetcher.fetch should not be successful.");
    } catch (ClientException e) {
    }
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Exceptional);
        credentialsFetcher.fetch();
        Assert.fail("EcsInstanceCredentialsFetcher.fetch should not be successful.");
    } catch (ClientException e) {
    }
}
Also used : InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) ClientException(com.aliyuncs.exceptions.ClientException) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) Test(org.junit.Test)

Example 7 with ClientException

use of com.aliyuncs.exceptions.ClientException in project aliyun-oss-java-sdk by aliyun.

the class STSAssumeRoleSessionCredentialsProvider method getNewSessionCredentials.

private BasicCredentials getNewSessionCredentials() {
    AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest();
    assumeRoleRequest.setRoleArn(roleArn);
    assumeRoleRequest.setRoleSessionName(roleSessionName);
    assumeRoleRequest.setDurationSeconds(expiredDurationSeconds);
    AssumeRoleResponse response = null;
    try {
        response = ramClient.getAcsResponse(assumeRoleRequest);
    } catch (ClientException e) {
        LogUtils.logException("RamClient.getAcsResponse Exception:", e);
        return null;
    }
    return new BasicCredentials(response.getCredentials().getAccessKeyId(), response.getCredentials().getAccessKeySecret(), response.getCredentials().getSecurityToken(), expiredDurationSeconds).withExpiredFactor(expiredFactor);
}
Also used : AssumeRoleRequest(com.aliyuncs.auth.sts.AssumeRoleRequest) AssumeRoleResponse(com.aliyuncs.auth.sts.AssumeRoleResponse) ClientException(com.aliyuncs.exceptions.ClientException)

Example 8 with ClientException

use of com.aliyuncs.exceptions.ClientException in project aliyun-oss-java-sdk by aliyun.

the class HttpCredentialsFetcher method fetch.

@Override
public Credentials fetch() throws ClientException {
    URL url = buildUrl();
    HttpRequest request = new HttpRequest(url.toString());
    request.setMethod(MethodType.GET);
    request.setConnectTimeout(AuthUtils.DEFAULT_HTTP_SOCKET_TIMEOUT_IN_MILLISECONDS);
    request.setReadTimeout(AuthUtils.DEFAULT_HTTP_SOCKET_TIMEOUT_IN_MILLISECONDS);
    HttpResponse response = null;
    try {
        response = send(request);
    } catch (IOException e) {
        throw new ClientException("CredentialsFetcher.fetch exception: " + e);
    }
    return parse(response);
}
Also used : HttpRequest(com.aliyuncs.http.HttpRequest) HttpResponse(com.aliyuncs.http.HttpResponse) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) URL(java.net.URL)

Example 9 with ClientException

use of com.aliyuncs.exceptions.ClientException in project aliyun-oss-java-sdk by aliyun.

the class CustomSessionCredentialsFetcherMock method send.

@Override
public HttpResponse send(HttpRequest request) throws IOException {
    HttpResponse response;
    try {
        response = new HttpResponse(buildUrl().toString());
    } catch (ClientException e) {
        throw new IOException("CredentialsFetcher.buildUrl Exception.");
    }
    switch(responseCategory) {
        case Normal:
            response.setStatus(200);
            response.setHttpContent(NORMAL_METADATA.getBytes(), "UTF-8", FormatType.JSON);
            break;
        case NormalWithoutExpiration:
            response.setStatus(200);
            response.setHttpContent(NORMAL_WITHOUT_EXPIRATION_METADATA.getBytes(), "UTF-8", FormatType.JSON);
            break;
        case NormalWithoutToken:
            response.setStatus(200);
            response.setHttpContent(NORMAL_WITHOUT_TOKEN_METADATA.getBytes(), "UTF-8", FormatType.JSON);
            break;
        case Expired:
            response.setStatus(200);
            response.setHttpContent(EXPIRED_METADATA.getBytes(), "UTF-8", FormatType.JSON);
            break;
        case FormatInvalid:
            response.setStatus(200);
            response.setHttpContent(FORMAT_INVALID_METADATA.getBytes(), "UTF-8", FormatType.JSON);
            break;
        case ServerHalt:
            response.setStatus(500);
            response.setHttpContent("".getBytes(), "UTF-8", null);
            break;
        case Exceptional:
            throw new IOException("CredentialsFetcher.send Exception.");
        default:
            break;
    }
    return response;
}
Also used : HttpResponse(com.aliyuncs.http.HttpResponse) ClientException(com.aliyuncs.exceptions.ClientException) IOException(java.io.IOException)

Example 10 with ClientException

use of com.aliyuncs.exceptions.ClientException in project ranch by heisedebaise.

the class AliyunAppSenderImpl method send.

@Override
public boolean send(PushModel push, String receiver, JSONObject args) {
    AliyunModel aliyun = aliyunService.find(push.getAppCode());
    if (aliyun == null)
        return false;
    PushRequest pushRequest = new PushRequest();
    pushRequest.setAppKey(numeric.toLong(aliyun.getAppKey()));
    pushRequest.setTarget("DEVICE");
    pushRequest.setTargetValue(receiver);
    pushRequest.setPushType("NOTICE");
    pushRequest.setDeviceType("ALL");
    pushRequest.setTitle(pushService.parse(PushService.Type.Subject, push.getKey(), push.getSubject(), args));
    pushRequest.setBody(pushService.parse(PushService.Type.Content, push.getKey(), push.getContent(), args));
    pushRequest.setIOSApnsEnv(json.hasTrue(args, "product") ? "PRODUCT" : "DEV");
    pushRequest.setIOSBadge((json.containsKey(args, "badge") ? args.getIntValue("badge") : 1) + 1);
    pushRequest.setIOSMusic(json.containsKey(args, "ios-music") ? args.getString("ios-music") : "default");
    pushRequest.setAndroidMusic(json.containsKey(args, "android-music") ? args.getString("android-music") : "default");
    if (json.containsKey(args, "url")) {
        pushRequest.setAndroidOpenType("URL");
        pushRequest.setAndroidOpenUrl(args.getString("url"));
    } else if (json.containsKey(args, "activity")) {
        pushRequest.setAndroidOpenType("ACTIVITY");
        pushRequest.setAndroidActivity(args.getString("activity"));
    } else
        pushRequest.setAndroidOpenType("APPLICATION");
    try {
        aliyunService.getIAcsClient(aliyun.getAppCode()).getAcsResponse(pushRequest);
        return true;
    } catch (ClientException e) {
        logger.warn(e, "使用阿里云APP推送[{}:{}:{}]时发生异常!", modelHelper.toJson(push), receiver, args);
    }
    return false;
}
Also used : AliyunModel(org.lpw.ranch.push.aliyun.AliyunModel) ClientException(com.aliyuncs.exceptions.ClientException) PushRequest(com.aliyuncs.push.model.v20160801.PushRequest)

Aggregations

ClientException (com.aliyuncs.exceptions.ClientException)16 HttpResponse (com.aliyuncs.http.HttpResponse)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 InstanceProfileCredentialsFetcher (com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher)3 InstanceProfileCredentialsFetcherMock (com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock)2 SendSmsResponse (com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse)2 JsonObject (io.vertx.core.json.JsonObject)2 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)1 PublicKey (com.aliyun.oss.common.auth.PublicKey)1 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 AssumeRoleRequest (com.aliyuncs.auth.sts.AssumeRoleRequest)1 AssumeRoleResponse (com.aliyuncs.auth.sts.AssumeRoleResponse)1 GetSessionAccessKeyRequest (com.aliyuncs.auth.sts.GetSessionAccessKeyRequest)1 GetSessionAccessKeyResponse (com.aliyuncs.auth.sts.GetSessionAccessKeyResponse)1 SingleSendMailRequest (com.aliyuncs.dm.model.v20151123.SingleSendMailRequest)1 ServerException (com.aliyuncs.exceptions.ServerException)1 HttpRequest (com.aliyuncs.http.HttpRequest)1 IClientProfile (com.aliyuncs.profile.IClientProfile)1 PushRequest (com.aliyuncs.push.model.v20160801.PushRequest)1