You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by aniro <a....@sailingweb.it> on 2011/06/20 18:54:29 UTC

filtering Ajax requests

Hi everyone,
    I'm working on some small application, and I need to register every user
action into a proprietary module. I need to record the total time consumed
by a request, the action that triggered the event and some http parameters.
The Tapestry version is 5.0.18, under Jboss 4.0.5.

To avoid code duplication I thought to use some kind of request filter, for
page rendering and ajax requests both... I read the APIs and found
ComponentEventRequestFilter, which works fine when the URL change. But I
can't find a similar class for the Ajax events! I found AjaxFilter /
AjaxComponentEventRequestHandler .. but they are under the
org.apache.tapestry5.internal.services package, which is bad to use - and
they don't work. 
I get this error when I start jboss: java.lang.RuntimeException: Service id
'AjaxComponentEventRequestHandler' has already been defined by
org.apache.tapestry5.services.TapestryModule.buildAjaxComponentEventRequestHandler(List,
Logger, ServiceResources) (at TapestryModule.java:1267) and may not be
redefined by [myapp].AppModule.buildAjaxComponentEventRequestHandler(List,
Logger, ServiceResources) (at AppModule.java:127). You should rename one of
the service builder methods.
	at
org.apache.tapestry5.ioc.internal.RegistryImpl.<init>(RegistryImpl.java:170)
	at org.apache.tapestry5.ioc.RegistryBuilder.build(RegistryBuilder.java:168)
	at
org.apache.tapestry5.internal.TapestryAppInitializer.getRegistry(TapestryAppInitializer.java:146)
	at org.apache.tapestry5.TapestryFilter.init(TapestryFilter.java:80)
	at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:223)

This is what I put in the AppModule:

	public void
contributeComponentEventRequestHandler(OrderedConfiguration<ComponentEventRequestFilter>
configuration,
		    @InjectService("AjaxComponentEventFilter") AjaxFilter compFilter) {
		  configuration.add("ajaxComponentEventFilter", compFilter);
	}

[...]

	public AjaxFilter buildAjaxComponentEventRequestHandler(
			final List<ComponentEventRequestFilter> configuration,
			final Logger logger, final ServiceResources resources) {
		return new AjaxFilter(null, null) {
			public void handle(ComponentEventRequestParameters parameters,
					@Ajax ComponentEventRequestHandler handler)
					throws IOException {

				System.out.println("Before handle");
				handler.handle(parameters);
				System.out.println("After handle");
			}
		};
	}

I also tried to write a ComponentEventRequestFilter with the @Ajax
annotation on the ComponentEventRequestHandler... but it doesn't intercept
anything:

	public ComponentEventRequestFilter buildMyComponentEvent(final Logger log,
			final RequestGlobals globals) {
		return new ComponentEventRequestFilter() {

			public void handle(ComponentEventRequestParameters parameters,
					@Ajax
					ComponentEventRequestHandler handler) throws IOException {
				long millis1 = System.currentTimeMillis();
				handler.handle(parameters);
				long millis2 = System.currentTimeMillis();
			}
		};
	}


How could I workaround this problem? Is there a better way to solve my
issue? 

Thanks in advance, Andrea

--
View this message in context: http://tapestry.1045711.n5.nabble.com/filtering-Ajax-requests-tp4507057p4507057.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: filtering Ajax requests

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 20 Jun 2011 13:54:29 -0300, aniro <a....@sailingweb.it> wrote:

> Hi everyone,

Hi!

>     I'm working on some small application, and I need to register every  
> user action into a proprietary module. I need to record the total time  
> consumed by a request, the action that triggered the event and some http  
> parameters. The Tapestry version is 5.0.18, under Jboss 4.0.5.

The TimingFilter generated by the Tapestry archetype could be adapted for  
you for that. Just check if there's an HTTP header X-Requested-With with  
value XMLHttpRequest.

> To avoid code duplication I thought to use some kind of request filter,  
> for page rendering and ajax requests both... I read the APIs and found
> ComponentEventRequestFilter, which works fine when the URL change. But I
> can't find a similar class for the Ajax events! I found AjaxFilter /
> AjaxComponentEventRequestHandler .. but they are under the
> org.apache.tapestry5.internal.services package, which is bad to use - and
> they don't work.

They work. Your contribution added another implementation of a service,  
and this is an error. Instead, you should override it or, better yet,  
decoreate it using Tapestry-IoC.

-- 
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: filtering Ajax requests

Posted by LLTYK <LL...@mailinator.com>.
The TimingFilter is part of the tapestry quickstart project. Here's mine:


  public RequestFilter buildTimingFilter(final Logger log)
  {
    return new RequestFilter() {
      @Override
      public boolean service(Request request, Response response,
RequestHandler handler) throws IOException {
        long startTime = System.currentTimeMillis();
        String path = request.getPath();
        boolean doit = !path.contains("asset");
        try {
          return handler.service(request, response);
        } finally {
          long elapsed = System.currentTimeMillis() - startTime;
          if (doit)
            log.info(String.format("Request time: %d ms " + path, elapsed));
        }
      }
    };
  }

  /**
   * This is a contribution to the RequestHandler service configuration.
This is
   * how we extend Tapestry using the timing filter. A common use for this
kind
   * of filter is transaction management or security. The @Local annotation
   * selects the desired service by type, but only from the same module.
Without
   * @Local, there would be an error due to the other service(s) that
implement
   * RequestFilter (defined in other modules).
   */
  public void contributeRequestHandler(OrderedConfiguration<RequestFilter>
configuration,
    @Local RequestFilter filter)
  {
    // Each contribution to an ordered configuration has a name, When
necessary,
    // you may
    // set constraints to precisely control the invocation order of the
    // contributed filter
    // within the pipeline.

    configuration.add("Timing", filter);
  }

--
View this message in context: http://tapestry-users.832.n2.nabble.com/filtering-Ajax-requests-tp6496667p6496792.html
Sent from the Tapestry Users mailing list archive at Nabble.com.

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