Serve dynamic generated images using restlet
Asked Answered
S

3

7

I have searched online for a while and almost all the questions regarding image serving using restlet are about static images. What I want to do is to serve dynamic generated image from restlet.

I have tried serving static images using restlet, it's working. Also, I can successfully generate a dynamic image and store them in a local folder, so the problem goes to how to serving it. If it's an http response, what I shall do is to attach all the bytes of the image to the body of the response. However, I am not sure about how to use restlet to do that? Is it FileRepresentation?

Newbie in this field, and any suggestion will be welcomed.

Thanks

Shin answered 2/10, 2009 at 14:29 Comment(1)
So what have you tried ? Can you serve a static image ? Is the real problem that you can't generate a dynamic image (not the serving of it?)Codeine
K
0

FileRepresentation should work if you write the image to a file first. For a more efficient approach, you can create your own Representation class by extending OutputRepresentation and overriding the write(OutputStream) method.

Kierstenkieselguhr answered 2/12, 2009 at 16:41 Comment(0)
F
5

I'm a little late to the party, but here is a class with which you can serve your images:

package za.co.shopfront.server.api.rest.representations;

import java.io.IOException;
import java.io.OutputStream;

import org.restlet.data.MediaType;
import org.restlet.representation.OutputRepresentation;

public class DynamicFileRepresentation extends OutputRepresentation {

    private byte[] fileData;

    public DynamicFileRepresentation(MediaType mediaType, long expectedSize, byte[] fileData) {
        super(mediaType, expectedSize);
        this.fileData = fileData;
    }

    @Override
    public void write(OutputStream outputStream) throws IOException {
        outputStream.write(fileData);
    }

}

In the restlet handler, you can then return it like this:

@Get
public Representation getThumbnail() {

    String imageId = getRequest().getResourceRef().getQueryAsForm().getFirstValue("imageId");
    SDTO_ThumbnailData thumbnailData = CurrentSetup.PLATFORM.getImageAPI().getThumbnailDataByUrlAndImageId(getCustomerUrl(), imageId);
    return new DynamicFileRepresentation(
            MediaType.valueOf(thumbnailData.getThumbNailContentType()), 
            thumbnailData.getSize(), 
            thumbnailData.getImageData());
}

Hope this helps! :)

Froemming answered 7/9, 2010 at 11:43 Comment(0)
U
3

There is an easyer way you can use ByteArrayRepresentation:

@Get
public ByteArrayRepresentation getThumbnail() {
    byte[] image = this.getImage();
    return new ByteArrayRepresentation(image , MediaType.IMAGE_PNG);
}
Unprecedented answered 31/7, 2013 at 6:26 Comment(0)
K
0

FileRepresentation should work if you write the image to a file first. For a more efficient approach, you can create your own Representation class by extending OutputRepresentation and overriding the write(OutputStream) method.

Kierstenkieselguhr answered 2/12, 2009 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.