How to display a Blob (byte[]) from database in a Tapestry template

"Christopher Dodunski" <[email protected]>
Newsgroups gmane.comp.java.tapestry.user
Message-ID <[email protected]>
Hi all,

I am having a little difficulty with what I imagine is a very common use
case: displaying a user's profile image alongside their personal details
following successful login.  Displaying the JavaBean strings served up by
Hibernate is easy.  Just displaying the database Blob (stored as a byte
array in the JavaBean entity) is proving more challenging.


[Welcome.tml]

    ...
    <div>
        <img src="${imageLink}" />
        <strong>${user.firstName} ${user.lastName}</strong><br/>
        User name: "${user.userName}"<br/>
        E-mail: ${user.email}<br/>
    </div>
    ...


[Welcome.java]

    ...
    public void onActivate(){
        user = authenticator.getLoggedUser();

        company = user.getCompany();

        ports = company.getPorts();
    }


    public Link getImageLink(){
        return pageLink.createPageRenderLinkWithContext(BlobImage.class,
user.getImage());
    }
    ...


[BlobImage.java]

package com.example.harbour.pages;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.services.Response;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Class created for displaying Blob (profile) images read from the
database, as per the below howto:
 *
https://stackoverflow.com/questions/13213236/tapestry-display-blob-using-markup
 */
public class BlobImage {

    public StreamResponse onActivate(byte[] byteArray){

        //Retrieve your image using the context parameter(s)
        final InputStream imageStream = new ByteArrayInputStream(byteArray);

        return new StreamResponse(){

            @Override
            public InputStream getStream() throws IOException {
                return imageStream;
            }

            @Override
            String getContentType(){
                return "image/png";
            }

            @Override
            void prepareResponse(Response response){}
        };
    }
}


[The Compilation Error]

[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
(default-compile) on project harbour: Compilation failure: Compilation
failure:
[ERROR]
\Users\Setup\Desktop\IdeaProjects\Harbour\src\main\java\com\example\harbour\pages\BlobImage.java:[37,17]
error: prepareResponse(Response) in <anonymous
com.example.harbour.pages.BlobImage$1> cannot implement
prepareResponse(Response) in StreamResponse
[ERROR]
\Users\Setup\Desktop\IdeaProjects\Harbour\src\main\java\com\example\harbour\pages\BlobImage.java:[32,19]
error: getContentType() in <anonymous
com.example.harbour.pages.BlobImage$1> cannot implement getContentType()
in StreamResponse


Is creating a separate image streaming class, like above, the best way of
displaying a database image in a Tapestry 5 template, as the 'howto' I
followed is 5+ years' old now?  I couldn't find any newer examples on the
Tapestry website.

Thanks for your assistance,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.