Back Forum Reply New

Rendering images uploaded via fileUpload flow

I've had a look at the file upload example flow, which puts an uploaded file in the flash scope

Code:
MultipartFile file = context.getRequestParameters().getRequiredMultipartFile(quot;filequot;);
if (file.getSize() gt; 0) {
// data was uploaded
context.getFlashScope().put(quot;filequot;, new String(file.getBytes()));
return success();
}
Question:

How can I get something out of the flash scope (or any other scope) into a servlet (or perhaps something better), to render what I have uploaded on another page in the flow ?

I'm assuming I have to use some sort of servlet to render the image as in the following:

Code:

protected void doPost(fromServletRequest request, fromServletResponse response) throws ServletException, IOException {
response.setContentType(quot;image/jpegquot;);
byte[] image = ((byte[])(request.getAttribute(quot;imagequot;)));
response.setContentLength(image.length);

java.io.OutputStream outstream = response.getOutputStream();
outstream.write(image);
outstream.flush();

}
I am a bit stumped on how to transfer stuff from a particular scope on to the request for the servlet. If there is a better way of doing this I am all ears !

You have 2 options:

1) Render the image with a normal Servlet that grabs the byte[] from flow scope. Check the following tip for details on how to get to flow scope from outside of SWF:

products/swf/tips/tip1.html

2) Have a SWF Action render the image. The Action can obtain the ExternalContext from the RequestContext and downcast that to a ServletExternalContext, getting access to the fromServletResponse. Use that to write the image to the response. Then add your action as a render action to a view state that has no 'view' attribute, basically indicating that something else has already generated the response (in this case the Action).

Erwin


Originally Posted by klr8You have 2 options:Erwin

Thanks for that. Option 2 sounds nicer, I'll give that a go and get back with my results

This is what I ended up doing:Code:
public Event doRender(
org..webflow.execution.RequestContext context)
throws Exception {

ServletExternalContext servletContext = (ServletExternalContext) context
.getExternalContext();

fromServletResponse response = servletContext.getResponse();
response.setContentType(quot;image/jpegquot;);
GenericDatabaseMaintenanceEditTableDataCommand command = getCommand(context);

Object object = command.getImageColumnToView();
if (object != null) {
byte[] image = (byte[]) object;
response.setContentLength(image.length);

java.io.OutputStream outstream = response.getOutputStream();
LOG.debug(quot;Image was quot; + image);
outstream.write(image);
outstream.flush();
}
return success();
}
Ideally I'd like to display multiple images on one page, I guess a custom tag is the way to go for this ?

If you want to display multiple images on one page, you'll have to go for option 1 and include links (lt;ing .../gt;) to that servlet from a normal HTML page, probably generated as a normal view-state view.

Erwin


Originally Posted by klr8If you want to display multiple images on one page, you'll have to go for option 1 and include links (lt;ing .../gt;) to that servlet from a normal HTML page, probably generated as a normal view-state view.

Erwin

If I go down that route, how does a new servlet get access to the servlet context of the web flow servlet, so it can get access to the flow repository ?

The example on products/swf/tips/tip1.html seems to be for a servlet filter, which, as it sits on top of the dispatcher servlet, has easy access to the dispatcher servlet context.

Can I define a basic servlet that is Spring enabled, and then inject the flow controller into there ?

Take a look at FrameworkServlet (part of Spring proper).

Also, the tip just uses a servlet filter as an example, but the techniques explained there can also be used in a Servlet.

Erwin


Originally Posted by klr8Take a look at FrameworkServlet (part of Spring proper).

Also, the tip just uses a servlet filter as an example, but the techniques explained there can also be used in a Servlet.

Erwin

Sorry but I still don't get how a completely different servlet (my image renderer one) can get access to the application context of another servlet.

In the example

Code:
ApplicationContext context =
(ApplicationContext)getServletContext().getAttribute(
DispatcherServlet.SERVLET_CONTEXT_PREFIX + quot;phonebookquot;);
the context is got from the servlet context of the dispatcher servlet, as the example is a servlet filter associated with the servlet.If I create a secondary image rendering servlet, how does this get access to the dispatcher servlet, in order to get access to that servlet's flow repository ?

Should I put the flow registry in the application context (somehow) as mentioned by Keith here ?

sho...t=flow+servlet

Use ContextLoaderListener (ServletContextListener) to create a ApplicationContext inside the ServletContext and use WebApplicationContextUtils.getWebApplicationContex  t(servletContext) to look it up from any Servlet.

Keith


Originally Posted by Keith DonaldUse ContextLoaderListener (ServletContextListener) to create a ApplicationContext inside the ServletContext and use WebApplicationContextUtils.getWebApplicationContex  t(servletContext) to look it up from any Servlet.

Keith

Ta, that worked perfectly. Have a great Christmas !
¥
Back Forum Reply New