You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Ayush Gupta <ay...@cisco.com> on 2003/09/16 20:25:20 UTC

Serving Image From Turbine/Velocity Screen

Greetings!
 
I'm trying to write a Screen under Turbine/Velocity which serves image
files. I've tried two approaches:
 
1. Extending org.apache.turbine.modules.screens.RawScreen
2. Extending org.apache.turbine.modules.Screen (Based on ImageServer in
older TDKs)
 
The image DOES load on the browser but it is corrupted/distorted in both
cases. The code which illustrates both of these approaches is attached. 
 
Would appreciate any thoughts if you've done this in the past or if you
spot something amiss with the code.
 
Thanks
 
- Ayush

RE: Serving Image From Turbine/Velocity Screen

Posted by oron ogdan <or...@netada.co.uk>.
Hi Ayush

This is the code I use successfully , it gets the image out of the Torque DB
But it should be fairly easy to convert it to use streams

public class ImageGetter extends RawScreen
{

    protected String getContentType(RunData data)
    {
        return "image/jpeg";
    }

    protected void doOutput(RunData data) throws Exception
    {
        try
        {
            int photoId = data.getParameters().getInt("src");
            data.getResponse().setHeader(
                "Content-Disposition",
                "inline	filename=" + "photo.jpg");

            data.getResponse().setHeader(
                "Expires",
                "Fri, 30 Oct 2003 14:19:41 GMT");

            ServletOutputStream out = data.getResponse().getOutputStream();
            Photo photo = PhotoManager.getInstance(photoId);
            if (photo.getUnread())
            {
                photo.setUnread(false);
                photo.save();
            }

            byte[] picData = photo.getData();
            out.write(picData);
            out.flush();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

hope this helps

Oron

http://netada.co.uk

-----Original Message-----
From: Ayush Gupta [mailto:aygupta@cisco.com]
Sent: Tuesday, September 16, 2003 8:02 PM
To: 'Turbine Users List'
Subject: RE: Serving Image From Turbine/Velocity Screen


It appears that the attachements were blocked. Pasting the code inline
in the mail.

- Ayush

************************************************************************
***********
/**
 * Date: Apr 16, 2003
 * Time: 11:01:26 AM
 */
import org.apache.turbine.modules.screens.RawScreen;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ImageServerRawScreen extends RawScreen {
    private static final String JPEG_IMAGE_TYPE = "image/jpeg";

    protected void doOutput(RunData data) throws Exception {
        final HttpServletResponse response = data.getResponse();
        sendImage(response);
    }

    protected String getContentType(RunData data) {
        return JPEG_IMAGE_TYPE;
    }

    private void sendImage(HttpServletResponse response) {
        try {
            ServletOutputStream out = response.getOutputStream();

            final String image = "d:\\out.jpg";
            File file = new File(image);

            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();

            response.setContentLength(buffer.length);
            out.write(buffer);
        } catch (IOException e) {
            Log.error(e);
        }
    }

}

************************************************************************
***********

/**
 * Date: Apr 16, 2003
 * Time: 11:01:26 AM
 */
import org.apache.ecs.ConcreteElement;
import org.apache.turbine.modules.Screen;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ImageServerScreen extends Screen {
    private static final int GIF = 1;
    private static final int JPEG = 2;
    private static final int PNG = 3;

    /**
     * Set the Layout to null. This causes the DefaultPage to try to
execute just the Screen without a Layout.
     *
     * @param  data                Turbine information.
     * @return                     A null String.
     */
    public String getLayout(RunData data) {
        return null;
    }

    /**
     * Build the Screen.
     *
     * @param     data                Turbine information.
     * @return                        A ConcreteElement.
     * @exception Exception a generic exception.
     */
    public ConcreteElement doBuild(RunData data)
            throws Exception {
        byte[] buffer = null;

        final String image = "d:\\out.jpg";
        File file = new File(image);
        if (!file.exists()) {
            // Should do more logging here.
            Log.error(new FileNotFoundException(image));
            return null;
        }

        AnImage aimage = null;
        // checkCache(file);
        if (aimage == null) {
            FileInputStream fis = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();
            // cacheImage(file, buffer, type);
            this.setContentType(data, file.getName());
        } else {
            buffer = aimage.data;
            this.setContentType(data, aimage.type);
        }

        data.getResponse().setDateHeader("Last-Modified",
file.lastModified());
        data.getResponse().setHeader("Accept-Ranges", "bytes");
        data.getResponse().setStatus(data.getStatusCode());
        data.getResponse().setContentType(data.getContentType());
        data.getResponse().setContentLength(buffer.length);
        data.getResponse().getOutputStream().write(buffer);
        return null;
    }

    /**
     * Set the content type.
     *
     * @param data Turbine information.
     * @param filename Name of file.
     */
    private void setContentType(RunData data,
                                String filename) {
        if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) {
            setContentType(data, JPEG);
        } else if (filename.endsWith(".gif")) {
            setContentType(data, GIF);
        } else if (filename.endsWith(".png")) {
            setContentType(data, PNG);
        }
    }

    /**
     * Set the content type.
     *
     * @param data Turbine information.
     * @param type Type of file.
     */
    private void setContentType(RunData data, int type) {
        switch (type) {
            case JPEG:
                data.setContentType("image/jpeg");
                break;
            case GIF:
                data.setContentType("image/gif");
                break;
            case PNG:
                data.setContentType("image/png");
                break;
            default:
                data.setContentType("image/jpeg");
                break;
        }
    }

    private class AnImage {
        byte[] data = null;
        int type = -1;
    }
}

************************************************************************
***********

-----Original Message-----
From: Ayush Gupta [mailto:aygupta@cisco.com]
Sent: Tuesday, September 16, 2003 11:25 AM
To: turbine-user@jakarta.apache.org
Subject: Serving Image From Turbine/Velocity Screen


Greetings!

I'm trying to write a Screen under Turbine/Velocity which serves image
files. I've tried two approaches:

1. Extending org.apache.turbine.modules.screens.RawScreen
2. Extending org.apache.turbine.modules.Screen (Based on ImageServer in
older TDKs)

The image DOES load on the browser but it is corrupted/distorted in both
cases. The code which illustrates both of these approaches is attached.

Would appreciate any thoughts if you've done this in the past or if you
spot something amiss with the code.

Thanks

- Ayush



RE: Serving Image From Turbine/Velocity Screen

Posted by oron ogdan <or...@netada.co.uk>.
Hi Ayush

This is the code I use successfully , it gets the image out of the Torque DB
But it should be fairly easy to convert it to use streams

public class ImageGetter extends RawScreen
{

    protected String getContentType(RunData data)
    {
        return "image/jpeg";
    }

    protected void doOutput(RunData data) throws Exception
    {
        try
        {
            int photoId = data.getParameters().getInt("src");
            data.getResponse().setHeader(
                "Content-Disposition",
                "inline	filename=" + "photo.jpg");

            data.getResponse().setHeader(
                "Expires",
                "Fri, 30 Oct 2003 14:19:41 GMT");

            ServletOutputStream out = data.getResponse().getOutputStream();
            Photo photo = PhotoManager.getInstance(photoId);
            if (photo.getUnread())
            {
                photo.setUnread(false);
                photo.save();
            }

            byte[] picData = photo.getData();
            out.write(picData);
            out.flush();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

hope this helps

Oron

http://netada.co.uk

-----Original Message-----
From: Ayush Gupta [mailto:aygupta@cisco.com]
Sent: Tuesday, September 16, 2003 8:02 PM
To: 'Turbine Users List'
Subject: RE: Serving Image From Turbine/Velocity Screen


It appears that the attachements were blocked. Pasting the code inline
in the mail.

- Ayush

************************************************************************
***********
/**
 * Date: Apr 16, 2003
 * Time: 11:01:26 AM
 */
import org.apache.turbine.modules.screens.RawScreen;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ImageServerRawScreen extends RawScreen {
    private static final String JPEG_IMAGE_TYPE = "image/jpeg";

    protected void doOutput(RunData data) throws Exception {
        final HttpServletResponse response = data.getResponse();
        sendImage(response);
    }

    protected String getContentType(RunData data) {
        return JPEG_IMAGE_TYPE;
    }

    private void sendImage(HttpServletResponse response) {
        try {
            ServletOutputStream out = response.getOutputStream();

            final String image = "d:\\out.jpg";
            File file = new File(image);

            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();

            response.setContentLength(buffer.length);
            out.write(buffer);
        } catch (IOException e) {
            Log.error(e);
        }
    }

}

************************************************************************
***********

/**
 * Date: Apr 16, 2003
 * Time: 11:01:26 AM
 */
import org.apache.ecs.ConcreteElement;
import org.apache.turbine.modules.Screen;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ImageServerScreen extends Screen {
    private static final int GIF = 1;
    private static final int JPEG = 2;
    private static final int PNG = 3;

    /**
     * Set the Layout to null. This causes the DefaultPage to try to
execute just the Screen without a Layout.
     *
     * @param  data                Turbine information.
     * @return                     A null String.
     */
    public String getLayout(RunData data) {
        return null;
    }

    /**
     * Build the Screen.
     *
     * @param     data                Turbine information.
     * @return                        A ConcreteElement.
     * @exception Exception a generic exception.
     */
    public ConcreteElement doBuild(RunData data)
            throws Exception {
        byte[] buffer = null;

        final String image = "d:\\out.jpg";
        File file = new File(image);
        if (!file.exists()) {
            // Should do more logging here.
            Log.error(new FileNotFoundException(image));
            return null;
        }

        AnImage aimage = null;
        // checkCache(file);
        if (aimage == null) {
            FileInputStream fis = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();
            // cacheImage(file, buffer, type);
            this.setContentType(data, file.getName());
        } else {
            buffer = aimage.data;
            this.setContentType(data, aimage.type);
        }

        data.getResponse().setDateHeader("Last-Modified",
file.lastModified());
        data.getResponse().setHeader("Accept-Ranges", "bytes");
        data.getResponse().setStatus(data.getStatusCode());
        data.getResponse().setContentType(data.getContentType());
        data.getResponse().setContentLength(buffer.length);
        data.getResponse().getOutputStream().write(buffer);
        return null;
    }

    /**
     * Set the content type.
     *
     * @param data Turbine information.
     * @param filename Name of file.
     */
    private void setContentType(RunData data,
                                String filename) {
        if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) {
            setContentType(data, JPEG);
        } else if (filename.endsWith(".gif")) {
            setContentType(data, GIF);
        } else if (filename.endsWith(".png")) {
            setContentType(data, PNG);
        }
    }

    /**
     * Set the content type.
     *
     * @param data Turbine information.
     * @param type Type of file.
     */
    private void setContentType(RunData data, int type) {
        switch (type) {
            case JPEG:
                data.setContentType("image/jpeg");
                break;
            case GIF:
                data.setContentType("image/gif");
                break;
            case PNG:
                data.setContentType("image/png");
                break;
            default:
                data.setContentType("image/jpeg");
                break;
        }
    }

    private class AnImage {
        byte[] data = null;
        int type = -1;
    }
}

************************************************************************
***********

-----Original Message-----
From: Ayush Gupta [mailto:aygupta@cisco.com]
Sent: Tuesday, September 16, 2003 11:25 AM
To: turbine-user@jakarta.apache.org
Subject: Serving Image From Turbine/Velocity Screen


Greetings!

I'm trying to write a Screen under Turbine/Velocity which serves image
files. I've tried two approaches:

1. Extending org.apache.turbine.modules.screens.RawScreen
2. Extending org.apache.turbine.modules.Screen (Based on ImageServer in
older TDKs)

The image DOES load on the browser but it is corrupted/distorted in both
cases. The code which illustrates both of these approaches is attached.

Would appreciate any thoughts if you've done this in the past or if you
spot something amiss with the code.

Thanks

- Ayush



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


RE: Serving Image From Turbine/Velocity Screen

Posted by Ayush Gupta <ay...@cisco.com>.
It appears that the attachements were blocked. Pasting the code inline
in the mail.
 
- Ayush
 
************************************************************************
***********
/**
 * Date: Apr 16, 2003
 * Time: 11:01:26 AM
 */
import org.apache.turbine.modules.screens.RawScreen;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
public class ImageServerRawScreen extends RawScreen {
    private static final String JPEG_IMAGE_TYPE = "image/jpeg";
 
    protected void doOutput(RunData data) throws Exception {
        final HttpServletResponse response = data.getResponse();
        sendImage(response);
    }
 
    protected String getContentType(RunData data) {
        return JPEG_IMAGE_TYPE;
    }
 
    private void sendImage(HttpServletResponse response) {
        try {
            ServletOutputStream out = response.getOutputStream();
 
            final String image = "d:\\out.jpg";
            File file = new File(image);
 
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();
 
            response.setContentLength(buffer.length);
            out.write(buffer);
        } catch (IOException e) {
            Log.error(e);
        }
    }
 
}

************************************************************************
***********
 
/**
 * Date: Apr 16, 2003
 * Time: 11:01:26 AM
 */
import org.apache.ecs.ConcreteElement;
import org.apache.turbine.modules.Screen;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
public class ImageServerScreen extends Screen {
    private static final int GIF = 1;
    private static final int JPEG = 2;
    private static final int PNG = 3;
 
    /**
     * Set the Layout to null. This causes the DefaultPage to try to
execute just the Screen without a Layout.
     *
     * @param  data                Turbine information.
     * @return                     A null String.
     */
    public String getLayout(RunData data) {
        return null;
    }
 
    /**
     * Build the Screen.
     *
     * @param     data                Turbine information.
     * @return                        A ConcreteElement.
     * @exception Exception a generic exception.
     */
    public ConcreteElement doBuild(RunData data)
            throws Exception {
        byte[] buffer = null;
 
        final String image = "d:\\out.jpg";
        File file = new File(image);
        if (!file.exists()) {
            // Should do more logging here.
            Log.error(new FileNotFoundException(image));
            return null;
        }
 
        AnImage aimage = null;
        // checkCache(file);
        if (aimage == null) {
            FileInputStream fis = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();
            // cacheImage(file, buffer, type);
            this.setContentType(data, file.getName());
        } else {
            buffer = aimage.data;
            this.setContentType(data, aimage.type);
        }
 
        data.getResponse().setDateHeader("Last-Modified",
file.lastModified());
        data.getResponse().setHeader("Accept-Ranges", "bytes");
        data.getResponse().setStatus(data.getStatusCode());
        data.getResponse().setContentType(data.getContentType());
        data.getResponse().setContentLength(buffer.length);
        data.getResponse().getOutputStream().write(buffer);
        return null;
    }
 
    /**
     * Set the content type.
     *
     * @param data Turbine information.
     * @param filename Name of file.
     */
    private void setContentType(RunData data,
                                String filename) {
        if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) {
            setContentType(data, JPEG);
        } else if (filename.endsWith(".gif")) {
            setContentType(data, GIF);
        } else if (filename.endsWith(".png")) {
            setContentType(data, PNG);
        }
    }
 
    /**
     * Set the content type.
     *
     * @param data Turbine information.
     * @param type Type of file.
     */
    private void setContentType(RunData data, int type) {
        switch (type) {
            case JPEG:
                data.setContentType("image/jpeg");
                break;
            case GIF:
                data.setContentType("image/gif");
                break;
            case PNG:
                data.setContentType("image/png");
                break;
            default:
                data.setContentType("image/jpeg");
                break;
        }
    }
 
    private class AnImage {
        byte[] data = null;
        int type = -1;
    }
}
 
************************************************************************
***********

-----Original Message-----
From: Ayush Gupta [mailto:aygupta@cisco.com] 
Sent: Tuesday, September 16, 2003 11:25 AM
To: turbine-user@jakarta.apache.org
Subject: Serving Image From Turbine/Velocity Screen


Greetings!
 
I'm trying to write a Screen under Turbine/Velocity which serves image
files. I've tried two approaches:
 
1. Extending org.apache.turbine.modules.screens.RawScreen
2. Extending org.apache.turbine.modules.Screen (Based on ImageServer in
older TDKs)
 
The image DOES load on the browser but it is corrupted/distorted in both
cases. The code which illustrates both of these approaches is attached. 
 
Would appreciate any thoughts if you've done this in the past or if you
spot something amiss with the code.
 
Thanks
 
- Ayush