Return a Swift object from Jersey REST service


This scenario assumes that the clients want to grab some objects on OpenStack’s Swift via the RESTful request. The main concern is we don’t want to public our container or create Temporary URL every access. As shown in the figure above, the Tomcat instances can verify the access permission or wrap some information before return to the client.

In addition, our GET method didn’t indicate @Produces annotation to specify the MIME media types of representations a resource can produce and send back to the client. The following code snippet use the java-cloudfiles to communicate with the Swift service. First, we obtain the meta-data of the object to get the MIME type, then we store the object content into byte-array as return entity. In the end, we can return the content of a object with corresponding MIME type to the client.

public Response getSwiftObject() {
    FilesClient client = new FilesClient(username, password, authUrl);
    try {
        if (!client.login()) {
            throw new RuntimeException();
        FilesObjectMetaData metaData = client.getObjectMetaData(
            container_name, object_name);
        byte[] objContent = client.getObject(container_name, object_name);

        return Response.ok(objContent, metaData.getMimeType()).build();
    } catch (Exception e) {
        // do something
    return Response.noContent().build();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s