You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Fernando Benjamin <fe...@gmail.com> on 2011/03/12 19:29:21 UTC

Filter in tapestry

Fellow tapestry developers,

I need to upload an image to a certain url, but I can't use any Normal file
upload mechanism because I am running Tapestry5 on GAE!
So I decided to resolve this with a Servlet and an url-filter mapping the
request to the Servlet(in web.xml)!
When the user posts the form, the servlet's doPost method will
handle(request) the whole upload of the image.


I've read this article in howards blog:
http://tapestryjava.blogspot.com/2009/12/securing-tapestry-pages-with.html

After reading Howard's blog about security in tapestry pages, my solution
feels quite primitive!
I would like to implement a filter like this one in the article, but I want
to map it to a certain url!


Well I don't know from which Class my Filter should implement.
Where can I found more info about the request life-cycle in Tapestry5?


*What I would like to do is:*
1-Implement a filter with some functionality
2- Inject the filter it at the right place with TapestryIOC.
2-Map an url to this filter


Hope someone can help me out here...

thank you in advance,


Fernando

Re: Filter in tapestry

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Wed, 23 Mar 2011 19:33:57 -0300, Fernando Benjamin  
<fe...@gmail.com> wrote:

> Hello Thiago,

Hi!

> The problem here is that the request Parameter in is not an  
> implementation of  the HttpServletRequest!

It isn't, but you can grap the requests HttpServletRequest: add a  
constructor to your RequestFilter that receives an HttpServletRequest and  
assign it to a field.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Filter in tapestry

Posted by Fernando Benjamin <fe...@gmail.com>.
Hello Thiago,

I have implemented a Requestfilter as you wrote here before!
But I still encounter major issues when retrieving the file as an Array of
of Bytes, since thats how we can put it on a Blob(googles Binary Large
Object).

The problem here is that the request Parameter in is not an implementation
of  the HttpServletRequest!

Is there another way of retrieving the posted file as a byte of arrays out
of the *Request* parameter?

Or How can I else retrieve the original HttpServletRequest in Tapestry5?

here is my example:

*public class MyUploadFilter implements RequestFilter {*

*private final Logger log;*

*private final String urlPath = "/personal/addproduct.upload";*

*private static final String IMAGE = "imageField";*

*@Inject*

*private MyFileUploadService uploadService;*

*
*

*public MyUploadFilter(Logger log) {*

*this.log = log;*

*}*

*
*

*@Override*

*public boolean service(Request request, Response response, RequestHandler
handler) throws IOException {*

*log.info("PATH = " + request.getPath());*

*if (urlPath.equalsIgnoreCase(request.getPath())) {*

*log.info("################# GOOOOOO ############");*

*
*

*request.getParameterNames();*

*for (String param : request.getParameterNames()) {*

*System.out.println(" # " + param);*

*if(IMAGE.equalsIgnoreCase(param)){*

*Object obj=request.getAttribute(param);*

*System.out.println(" # " + obj.getClass());*

*System.out.println(" # " + obj.getClass().getCanonicalName());*

**

*}*

*}*

**

*try*

*{*

*
*

*ServletFileUpload upload = new ServletFileUpload(); *

*FileItemIterator iterator = upload.getItemIterator(request);*

*FileItemStream item = iterator.next();*

*InputStream stream = item.openStream();*

*String filename = item.getName();*

*Blob content = new Blob(IOUtils.toByteArray(stream));*

*
*

*if (filename.substring(filename.length() -
3,filename.length()).equalsIgnoreCase("JPG"))*

*{*

*PersistenceManager pm = PMF.get().getPersistenceManager();*

*Image doc = new Image(filename, content);*

*pm.makePersistent(doc);*

*pm.close();*

*}*

*}*

*catch (Exception ex) {*

*log.error(ex.getMessage());*

*return false;*

*}*

*
*

*}*

*return handler.service(request, response);*

*}*

*}*

On 12 March 2011 20:47, Thiago H. de Paula Figueiredo <th...@gmail.com>wrote:

> On Sat, 12 Mar 2011 15:29:21 -0300, Fernando Benjamin <
> fernandobenjamin@gmail.com> wrote:
>
>  Fellow tapestry developers,
>>
>
> Hi!
>
>
>  I need to upload an image to a certain url, but I can't use any Normal
>> file upload mechanism because I am running Tapestry5 on GAE!
>>
>
> So you want to write an application that receives a file upload, right?
>
>
>  Well I don't know from which Class my Filter should implement.
>>
>
> I'd implement RequestFilter. Being it part of Tapestry, you don't need to
> add anything to web.xml. You just need to contribute your implementation to
> the RequestHandler service. This is done by adding a method like this in
> your AppModule:
>
> public void contributeRequestHandler(OrderedConfiguration<RequestFilter>
> configuration) {
>        configuration.addInstance("YourRequestFilterClassName",
> YourRequestFilterClass.class, "before:*");
>
> }
>
>  Where can I found more info about the request life-cycle in Tapestry5?
>>
>
> http://tapestry.apache.org/request-processing.html. Don't be scared: you
> don't need to know even 10% of it to use Tapestry very well. The diagram at
> the end of that page is just showing every single thing that can take part
> of processing a request, including many internal classes you never touch or
> use.
>
>  *What I would like to do is:*
>>
>> 1-Implement a filter with some functionality
>> 2- Inject the filter it at the right place with TapestryIOC.
>> 3-Map an url to this filter
>>
>
> Implementing a RequestFilter, you don't need to map an URL to it. Inside
> the RequestFilter's service method, you use request.getPath() to know if
> that request's URL is handled by your filter. If yes, process it and return
> true. If not, return service(request, response, handler).
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Filter in tapestry

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sat, 12 Mar 2011 15:29:21 -0300, Fernando Benjamin  
<fe...@gmail.com> wrote:

> Fellow tapestry developers,

Hi!

> I need to upload an image to a certain url, but I can't use any Normal  
> file upload mechanism because I am running Tapestry5 on GAE!

So you want to write an application that receives a file upload, right?

> Well I don't know from which Class my Filter should implement.

I'd implement RequestFilter. Being it part of Tapestry, you don't need to  
add anything to web.xml. You just need to contribute your implementation  
to the RequestHandler service. This is done by adding a method like this  
in your AppModule:

public void contributeRequestHandler(OrderedConfiguration<RequestFilter>  
configuration) {
	configuration.addInstance("YourRequestFilterClassName",  
YourRequestFilterClass.class, "before:*");
}

> Where can I found more info about the request life-cycle in Tapestry5?

http://tapestry.apache.org/request-processing.html. Don't be scared: you  
don't need to know even 10% of it to use Tapestry very well. The diagram  
at the end of that page is just showing every single thing that can take  
part of processing a request, including many internal classes you never  
touch or use.

> *What I would like to do is:*
> 1-Implement a filter with some functionality
> 2- Inject the filter it at the right place with TapestryIOC.
> 3-Map an url to this filter

Implementing a RequestFilter, you don't need to map an URL to it. Inside  
the RequestFilter's service method, you use request.getPath() to know if  
that request's URL is handled by your filter. If yes, process it and  
return true. If not, return service(request, response, handler).

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org