use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.
the class AliyunSendSmsClient method init.
public void init(AliyunSmsProfile profile) throws CloudSDKException {
AssertUtils.notNull(profile.getAccessKeyId(), "Access key id is null.");
AssertUtils.notNull(profile.getAccessKeySecret(), "Access key secret is null.");
AssertUtils.notNull(profile.getEndpoint(), "Endpoint is null.");
Config config = new Config().setAccessKeyId(profile.getAccessKeyId()).setAccessKeySecret(profile.getAccessKeySecret()).setEndpoint(profile.getEndpoint());
try {
this.client = new com.aliyun.dysmsapi20170525.Client(config);
} catch (Exception e) {
throw new CloudSDKException("Init sms client failed.", e);
}
}
use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.
the class AliyunSendSmsClient method send.
/**
* {@inheritDoc}
*/
@Override
public void send(String phoneNumber, Map<String, String> paramMap, CloudSmsTemplate smsTemplate) throws CloudSDKException {
if (null == smsTemplate) {
throw new CloudSDKException("Sms template is null.");
}
String signName = smsTemplate.getSignName();
String templateId = smsTemplate.getTemplateId().toString();
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phoneNumber);
request.setSignName(signName);
request.setTemplateCode(templateId);
try {
if (null != paramMap) {
String templateParam = JSONUtils.toJSONString(paramMap);
request.setTemplateParam(templateParam);
}
SendSmsResponse result = this.client.sendSms(request);
SendSmsResponseBody body = result.getBody();
if (null == body || !"ok".equalsIgnoreCase(body.getCode())) {
throw new CloudSDKException("Send sms failed, code is " + body.code + ", message is " + body.getMessage() + ", request id is " + body.getRequestId() + ", bizId is " + body.getBizId() + '.');
}
} catch (Exception e) {
throw new CloudSDKException("Send sms failed.", e);
}
}
use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.
the class TestAliyunObjectStorageServiceImpl method testgenPresignedUploadUrl.
@Test
public void testgenPresignedUploadUrl() {
String objectName = "/pre/testd/test.txt";
URL url = null;
try {
String filePath = TestAliyunObjectStorageServiceImpl.class.getClassLoader().getResource("test.txt").getPath();
File localFile = new File(filePath);
String contentMd5 = FileDigestUtils.md5AsBase64(localFile);
long signExpired = 60 * 1000;
url = objectStoreageService.genPresignedUploadUrl(BUCKETNAME, objectName, signExpired, contentMd5);
System.out.println(url.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("content-md5", contentMd5);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
FileInputStream fStream = new FileInputStream(localFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
while ((length = fStream.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
fStream.close();
out.close();
int responseCode = connection.getResponseCode();
System.out.println("Service returned response code " + responseCode);
} catch (CloudSDKException e) {
Assert.fail(e.getMessage());
} catch (ProtocolException e) {
Assert.fail(e.getMessage());
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.
the class TestAliyunObjectStorageServiceImpl method testgetObjectMetadata.
@Test
public void testgetObjectMetadata() {
try {
ObjectMetadata objectMetadata = objectStoreageService.getObjectMetadata(BUCKETNAME, OBJECTNAME);
System.out.println(objectMetadata.toString());
} catch (CloudSDKException e) {
Assert.fail(e.getMessage());
}
}
use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.
the class FileDigestUtils method md5AsBase64.
public static String md5AsBase64(File file) throws CloudSDKException {
try (InputStream istream = new FileInputStream(file)) {
byte[] binaryData = DigestUtils.md5(IOUtils.toByteArray(istream));
String md5 = Base64.encodeBase64String(binaryData);
return md5;
} catch (IOException e) {
throw new CloudSDKException("Read file failed.", e);
}
}
Aggregations