use of com.aliyun.oss.model.BucketReferer in project aliyun-oss-java-sdk by aliyun.
the class BucketRefererTest method testNormalSetBucketReferer.
@Test
public void testNormalSetBucketReferer() {
final String bucketName = "normal-set-bucket-referer-source";
final String referer0 = "http://www.aliyun.com";
final String referer1 = "https://www.aliyun.com";
final String referer2 = "http://www.*.com";
final String referer3 = "https://www.?.aliyuncs.com";
try {
ossClient.createBucket(bucketName);
// Set non-empty referer list
BucketReferer r = new BucketReferer();
List<String> refererList = new ArrayList<String>();
refererList.add(referer0);
refererList.add(referer1);
refererList.add(referer2);
refererList.add(referer3);
r.setRefererList(refererList);
ossClient.setBucketReferer(bucketName, r);
waitForCacheExpiration(5);
r = ossClient.getBucketReferer(bucketName);
List<String> returedRefererList = r.getRefererList();
Assert.assertTrue(r.isAllowEmptyReferer());
Assert.assertTrue(returedRefererList.contains(referer0));
Assert.assertTrue(returedRefererList.contains(referer1));
Assert.assertTrue(returedRefererList.contains(referer2));
Assert.assertTrue(returedRefererList.contains(referer3));
Assert.assertEquals(4, returedRefererList.size());
Assert.assertEquals(r.getRequestId().length(), REQUEST_ID_LEN);
// Set empty referer list
r.clearRefererList();
ossClient.setBucketReferer(bucketName, r);
r = ossClient.getBucketReferer(bucketName);
returedRefererList = r.getRefererList();
Assert.assertTrue(r.isAllowEmptyReferer());
Assert.assertEquals(0, returedRefererList.size());
Assert.assertEquals(r.getRequestId().length(), REQUEST_ID_LEN);
// Referer list not allowed to be empty
refererList.clear();
refererList.add(referer0);
refererList.add(referer3);
r.setRefererList(refererList);
r.setAllowEmptyReferer(false);
ossClient.setBucketReferer(bucketName, r);
r = ossClient.getBucketReferer(bucketName);
returedRefererList = r.getRefererList();
Assert.assertFalse(r.isAllowEmptyReferer());
Assert.assertTrue(returedRefererList.contains(referer0));
Assert.assertTrue(returedRefererList.contains(referer3));
Assert.assertEquals(2, returedRefererList.size());
Assert.assertEquals(r.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.BucketReferer in project aliyun-oss-java-sdk by aliyun.
the class ResponseParsers method parseGetBucketReferer.
/**
* Unmarshall get bucket referer response body to bucket referer list.
*/
@SuppressWarnings("unchecked")
public static BucketReferer parseGetBucketReferer(InputStream responseBody) throws ResponseParseException {
try {
Element root = getXmlRootElement(responseBody);
boolean allowEmptyReferer = Boolean.valueOf(root.getChildText("AllowEmptyReferer"));
List<String> refererList = new ArrayList<String>();
if (root.getChild("RefererList") != null) {
Element refererListElem = root.getChild("RefererList");
List<Element> refererElems = refererListElem.getChildren("Referer");
if (refererElems != null && !refererElems.isEmpty()) {
for (Element e : refererElems) {
refererList.add(e.getText());
}
}
}
return new BucketReferer(allowEmptyReferer, refererList);
} catch (JDOMParseException e) {
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
} catch (Exception e) {
throw new ResponseParseException(e.getMessage(), e);
}
}
Aggregations