Search in sources :

Example 1 with OSSSymlink

use of com.aliyun.oss.model.OSSSymlink in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseSymbolicLink.

/**
 * Unmarshall symlink link from response headers.
 */
public static OSSSymlink parseSymbolicLink(ResponseMessage response) throws ResponseParseException {
    try {
        OSSSymlink smyLink = null;
        String targetObject = response.getHeaders().get(OSSHeaders.OSS_HEADER_SYMLINK_TARGET);
        if (targetObject != null) {
            targetObject = HttpUtil.urlDecode(targetObject, "UTF-8");
            smyLink = new OSSSymlink(null, targetObject);
        }
        smyLink.setMetadata(parseObjectMetadata(response.getHeaders()));
        return smyLink;
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) OSSSymlink(com.aliyun.oss.model.OSSSymlink)

Example 2 with OSSSymlink

use of com.aliyun.oss.model.OSSSymlink in project aliyun-oss-java-sdk by aliyun.

the class SymlinkTest method testNormalCreateSymlink.

@Test
public void testNormalCreateSymlink() {
    final String symLink = "normal-create-sym-link";
    try {
        ossClient.putObject(bucketName, targetObject, new ByteArrayInputStream(content.getBytes()));
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType("text/plain");
        metadata.addUserMetadata("property", "property-value");
        CreateSymlinkRequest createSymlinkRequest = new CreateSymlinkRequest(bucketName, symLink, targetObject);
        createSymlinkRequest.setMetadata(metadata);
        ossClient.createSymlink(createSymlinkRequest);
        OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
        Assert.assertEquals(symbolicLink.getSymlink(), symLink);
        Assert.assertEquals(symbolicLink.getTarget(), targetObject);
        Assert.assertEquals(symbolicLink.getMetadata().getContentType(), "text/plain");
        Assert.assertEquals(symbolicLink.getMetadata().getUserMetadata().get("property"), "property-value");
        Assert.assertEquals(symbolicLink.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteObject(bucketName, symLink);
        ossClient.deleteObject(bucketName, targetObject);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CreateSymlinkRequest(com.aliyun.oss.model.CreateSymlinkRequest) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) OSSSymlink(com.aliyun.oss.model.OSSSymlink) Test(org.junit.Test)

Example 3 with OSSSymlink

use of com.aliyun.oss.model.OSSSymlink in project aliyun-oss-java-sdk by aliyun.

the class SymlinkTest method testUnnormalCreateSymlink.

@Test
public void testUnnormalCreateSymlink() {
    final String symLink = "unnormal-create-sym-link";
    try {
        ossClient.createSymlink(bucketName, symLink, symLink);
        OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
        Assert.assertEquals(symbolicLink.getSymlink(), symLink);
        Assert.assertEquals(symbolicLink.getTarget(), symLink);
        try {
            ossClient.getObject(bucketName, symLink);
        } catch (OSSException e) {
            Assert.assertEquals("InvalidTargetType", e.getErrorCode());
        }
        ossClient.deleteObject(bucketName, symLink);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) OSSSymlink(com.aliyun.oss.model.OSSSymlink) Test(org.junit.Test)

Example 4 with OSSSymlink

use of com.aliyun.oss.model.OSSSymlink in project aliyun-oss-java-sdk by aliyun.

the class SymlinkTest method testUnnormalgetSymlink.

@Test
public void testUnnormalgetSymlink() {
    final String symLink = "unnormal-get-sym-link";
    try {
        OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
        Assert.assertNull(symbolicLink.getSymlink());
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
    }
    try {
        ossClient.createSymlink(bucketName, symLink, targetObject);
        ossClient.getObject(bucketName, symLink);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_SYM_LINK_TARGET, e.getErrorCode());
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) OSSSymlink(com.aliyun.oss.model.OSSSymlink) Test(org.junit.Test)

Example 5 with OSSSymlink

use of com.aliyun.oss.model.OSSSymlink in project aliyun-oss-java-sdk by aliyun.

the class SymlinkTest method testNormalgetSymlinkContent.

@Test
public void testNormalgetSymlinkContent() {
    final String symLink = "normal-create-sym-link-content";
    try {
        ossClient.putObject(bucketName, targetObject, new ByteArrayInputStream(content.getBytes()));
        ossClient.createSymlink(bucketName, symLink, targetObject);
        OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
        Assert.assertEquals(symbolicLink.getSymlink(), symLink);
        Assert.assertEquals(symbolicLink.getTarget(), targetObject);
        Assert.assertEquals(symbolicLink.getRequestId().length(), REQUEST_ID_LEN);
        // content
        OSSObject ossObject = ossClient.getObject(bucketName, symLink);
        StringBuilder contentBuilder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
        while (true) {
            String line = reader.readLine();
            if (line == null)
                break;
            contentBuilder.append(line);
        }
        reader.close();
        Assert.assertEquals(contentBuilder.toString(), content);
        // size
        ObjectMetadata meta = ossClient.getObjectMetadata(bucketName, symLink);
        Assert.assertEquals(meta.getContentLength(), content.length());
        ossClient.deleteObject(bucketName, symLink);
        ossClient.deleteObject(bucketName, targetObject);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) OSSSymlink(com.aliyun.oss.model.OSSSymlink) Test(org.junit.Test)

Aggregations

OSSSymlink (com.aliyun.oss.model.OSSSymlink)7 OSSException (com.aliyun.oss.OSSException)5 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)2 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 CreateSymlinkRequest (com.aliyun.oss.model.CreateSymlinkRequest)1 OSSObject (com.aliyun.oss.model.OSSObject)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 JDOMParseException (org.jdom.input.JDOMParseException)1