Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor can't override a Result's attribute after use redirectTo. #696

Closed
armoucar opened this issue Jul 21, 2014 · 66 comments
Closed
Assignees
Labels

Comments

@armoucar
Copy link

This bug is happening on 4.0.0-Final version.

I've created a new project with the blank-project to emulate the problem.
An Interceptor was created to populate a String in the Result instance. It populates well the strings although when I show this parameter in my view, it shows me the first value populated by the interceptor (it doesn't override it). For the code below, If I try to show ${module} in my JSP, when requesting /, it shows me IndexController/index - it should show IndexController/index2

// IndexController

@Path("/")
public void index() {
  result.redirectTo(this).index2();
}

@Path("/index2")
public void index2() {
}
@Intercepts
public class ModuleInterceptor {

    @Inject
    private Result result;

    @Accepts
    public boolean accepts(ControllerMethod method) {
        String moduleName = method.getController().getType().getSimpleName() + "/" + method.getMethod().getName();
        result.include("module", moduleName);

        return false;
    }
}

I tested the code below and it works fine. The problem happens only when populating using interceptors.

@Path("/")
public void index() {
  result.include("module", "a");
  result.redirectTo(this).index2();
}

@Path("/index2")
public void index2() {
  result.include("module", "b");
}
@renanigt
Copy link
Contributor

Try do this using the @AroundCall annotation.

Something like:

@AroundCall
public void intercept(SimpleInterceptorStack stack) {
    String moduleName = method.getController().getType().getSimpleName() + "/" + method.getMethod().getName();
    result.include("module", moduleName);

    stack.next();
}

@armoucar
Copy link
Author

Renan, I think I don't have a method object available in this intercept method, right?

@renanigt
Copy link
Contributor

Really, sorry. Add it as attribute.

Try this:

@Intercepts
public class ModuleInterceptor {

    @Inject
    private Result result;
    @Inject
    private ControllerMethod method;

    @AroundCall
    public void intercept(SimpleInterceptorStack stack) {
        String moduleName = method.getController().getType().getSimpleName() + "/" + method.getMethod().getName();
        result.include("module", moduleName);

        stack.next();
    }
}

@renanigt
Copy link
Contributor

@armoucar when you test it, please report here.
I'll do some tests too.

@armoucar
Copy link
Author

Tested the suggested code, problem is still happening.

@renanigt
Copy link
Contributor

I'll do some tests and will report the result here.

Thanks for the test.

@renanigt
Copy link
Contributor

Have you tried requesting /index2 directly ?

@armoucar
Copy link
Author

Yes. When I call /index2 it shows me 'ControllerName/index2', when I call / and it redirects me for index2, it shows me ControllerName/index.

@renanigt
Copy link
Contributor

Thanks.

When I'm at home, I'll do some tests.

@garcia-jj
Copy link
Member

I don't understand the problem and the goal. What's happens right now? And
what's desired behavior?

Remember that redirect don't stop the flow. So if there us some code after
redirect line, will be executed. This behavior is the same from servlet
spec.

Sorry if I don't understand your case.

@renanigt
Copy link
Contributor

If I really understand your problem @armoucar, here worked fine, with no problem.

But remember what @garcia-jj said:

Remember that redirect don't stop the flow. So if there us some code after
redirect line, will be executed. This behavior is the same from servlet
spec.

@renanigt
Copy link
Contributor

You can't have just a method with @Accepts annotation, you need at least one method with @BeforeCall, @AfterCall or @AroundCall.

@armoucar
Copy link
Author

Otávio,

The interceptor is intercepting both requests - normal requisition and redirect. On the second request (redirect), it tries to override an attribute on result object. However when I try to show it on my JSP, the stored attribute is the one of the first request.

My solution now is use HttpServletRequest object. I just put an attribute there and it works like a charm even when redirecting. In some place that I don't remember, I read that vraptor stores result's values after a redirect (like rails flash) and it's very useful. But I think I should be allowed to override this attribute if I wanted, shouldn't I? Just remembering like I said in the 'opener issue' comment, it works well when overriding inside controllers, it only doesn't work when I try to override it inside an interceptor.

Renan,

I made that on the last test.

Thank you guys.

@garcia-jj
Copy link
Member

Arthur, thank you. Now I see your point.

And you is right: vraptor uses flash to store all result values to use in
the next request.

@renanigt
Copy link
Contributor

I did the follow test and worked fine:

Contoller:

@Path("/index")
public void index() {
    result.redirectTo(this).add();
}

@Path("/add")
public void add() {
}

Interceptor:

@Intercepts
public class ModuleInterceptor {

    @Inject
    private Result result;
    @Inject
    private ControllerMethod method;

    @AroundCall
    public void intercept(SimpleInterceptorStack stack) {
        String moduleName = method.getController().getType().getSimpleName() + "/" + method.getMethod().getName();
        result.include("module", moduleName);

        stack.next();
    }

}

@armoucar
Copy link
Author

OK Renan,

I'll test it today again and give you a feedback just after.

Thankyou

@armoucar
Copy link
Author

I did some tests and for me it's still happening. Tested with 4.0.0.Final and 4.1.0-RC2-SNAPSHOT. I've pushed the project here: https://github.com/armoucar/vraptor-blank-project/
server: apache-tomcat-8.0.9

@renanigt
Copy link
Contributor

@armoucar when you try access the path /, the index method will automatically redirect to index2, so, you'll always see "IndexController/index2" message.

I tried exactly as you pushed.

@armoucar
Copy link
Author

Weird :/. Does someone could test the same project as well?

This is what I get:

screen shot 2014-07-23 at 00 13 59

@renanigt
Copy link
Contributor

I cloned you project and run here, I got the expected result.

It works!! IndexController/index2

@garcia-jj
Copy link
Member

It works for me too. When I access /, the browser was redirected to /index2 and printed: It works!! IndexController/index2.

@armoucar
Copy link
Author

Same server, guys? The only change I made to do to run the blank project on tomcat was turn the javax.inject dependency to compile scope.

To run with vraptor 4.1.0-RC2-SNAPSHOT I had to add org.slf4j.slf4j-api.

@renanigt
Copy link
Contributor

Try remove the scope and run it with vraptor 4.1.0-RC1.

@armoucar
Copy link
Author

Tomcat doens't have an own javax.inject to provide. If I remove it it'll start throw me NoClassDefFoundError. Are you testing in a tomcat as well?

@renanigt
Copy link
Contributor

The default scope is compile, so if you don't define it, the scope is compile.
I think Tomcat just try provide the javax.inject if you define scope as provided or system.

I'm using tomcat 7.

@garcia-jj
Copy link
Member

On Wildfly

@armoucar
Copy link
Author

Yeah, this 'problem' (at least happening to me) is very weird. I made some tests using: apache-tomcat-7.0.54, apache-tomcat-8.0.9 and wildfly-8.1.0.Final. Tested 4.0.0.Final and 4.1.0-RC2-SNAPSHOT.

It has worked on wildfly but no on both tomcats. To be sure that was testing the same thing, I generated a package with maven and deployed them using the managing UI of each server.

Uploaded the war here: http://tempsend.com/663DE9E61D

@garcia-jj
Copy link
Member

Thank you Arthur. I'll do some tests tonight.

@renanigt
Copy link
Contributor

I downloaded and tested it on tomcat 7, and I got the expected result again.

It works!! IndexController/index2

@renanigt
Copy link
Contributor

No. But I'll do it.

@garcia-jj
Copy link
Member

Arthur, you can enable debug for the logger category br.com.caelum.vraptor.core. And when a new object is included, you will see: including attribute myparam: thevalue. So with this you can see if the object isn't included.

I don't know if I can do this test tonight. So if you can do this debug, will help us to understand what's happens.

Thank you.

@Turini
Copy link
Member

Turini commented Jul 24, 2014

There is a bug involving tomcat 7 jsp compiler when using java 8. Maybe this is an collateral effect. But it should to work when using tomcat 8. Did you test it, right?

@renanigt
Copy link
Contributor

I'll do this test when I'm at home.

Tomcat 7 + Java 8.

@renanigt
Copy link
Contributor

renanigt commented Aug 3, 2014

Sorry for delay... @Turini @garcia-jj @lucascs

I did the test and when using Tomcat 7 + Java 8 I really got the wrong result.

So, I think this problem isn't a VRaptor bug.

@Turini
Copy link
Member

Turini commented Aug 4, 2014

But It works using tomcat 8 + java 8?

@renanigt
Copy link
Contributor

renanigt commented Aug 4, 2014

I didn't test tomcat 8 + java 8. I tested tomcat 8 + java 7 and since
@armoucar tested with Wildfly + Java 8 and worked fine, I think it's only
an inconsistency with Tomcat + Java 8.

But I can do this test. :-)

On Monday, August 4, 2014, Rodrigo Turini notifications@github.com wrote:

But It works using tomcat 8 + java 8?


Reply to this email directly or view it on GitHub
#696 (comment).

@Turini
Copy link
Member

Turini commented Aug 4, 2014

As I said before: "There is a bug involving tomcat 7 jsp compiler when using java 8. Maybe this is an collateral effect. But it should to work when using tomcat 8."

@renanigt
Copy link
Contributor

renanigt commented Aug 4, 2014

I'll do a test with Tomcat 8 + Java 8. :-)

On Monday, August 4, 2014, Rodrigo Turini notifications@github.com wrote:

As I said before: "There is a bug involving tomcat 7 jsp compiler when
using java 8. Maybe this is an collateral effect. But it should to work
when using tomcat 8."


Reply to this email directly or view it on GitHub
#696 (comment).

@renanigt
Copy link
Contributor

renanigt commented Aug 5, 2014

I did the test, and with Tomcat 8 + Java 8 the error also occur.

@Turini
Copy link
Member

Turini commented Aug 5, 2014

so we can't close the issue. It must to work at servlet containers.

@Turini Turini added the bug label Aug 5, 2014
@renanigt
Copy link
Contributor

renanigt commented Aug 5, 2014

Really.

I understood was a Tomcat bug and not a VRaptor bug. Sorry. :-)

@Turini
Copy link
Member

Turini commented Aug 12, 2014

@armoucar, I've tested with:

(1) Tomcat 7 + JDK 8 
(2) Tomcat 8 + JDK 8

and both works as expected.
I've runned the tests on MAC OS and Windows.
which operational system are u using?

@Turini Turini self-assigned this Aug 12, 2014
@renanigt
Copy link
Contributor

@Turini, I know the question was to @armoucar, but I'll leave my case:

I tested using Linux(Ubuntu), if do you want I can do more tests. 😃

@armoucar
Copy link
Author

OS X Mavericks. I'll one more test in few moments with the last version of VRaptor.

@Turini
Copy link
Member

Turini commented Aug 12, 2014

can you test again with this war? (The same use case)

@victorkendy
Copy link

Works on my machine too:

Windows 8.1
Java 8
Tomcat 7 e 8

@armoucar
Copy link
Author

Tried to use your .war but it doesn't start on my Tomcat. There are no logs about the application, only tomcat's:

12-Aug-2014 23:14:45.460 INFO [http-nio-8080-exec-3] org.jboss.weld.environment.tomcat7.Tomcat7Container.initialize Tomcat 7+ detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported¬
12-Aug-2014 23:14:46.010 SEVERE [http-nio-8080-exec-3] org.apache.catalina.core.StandardContext.startInternal Error listenerStart¬
12-Aug-2014 23:14:46.012 SEVERE [http-nio-8080-exec-3] org.apache.catalina.core.StandardContext.startInternal Context [/vraptor-blank-project] startup failed due to previous errors¬
12-Aug-2014 23:14:46.131 INFO [http-nio-8080-exec-3] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /Users/arthur/Dev/apache-tomcat-8.0.9/webapps/vraptor-blank-project.war has finished in 11,527 ms

Also,

Fetched the last vraptor master branch, installed it, used in the same blank-project that I pushed to github and it is still the same:

Java 8 + Tomcat 7, Tomcat 8. 👎
Java 7 + Tomcat 7, Tomcat 8. 👍

@felipeweb
Copy link
Contributor

i have tested and work too.

@Turini
Copy link
Member

Turini commented Aug 13, 2014

this is really weird, I ask for different users to test it and works as
expected. We already test all combinations on Windows, Mac and
Linux... Can you download a new (clean) tomcat and update your
project using the current master ou vr 4.1.0-RC2 version? Thanks!

@renanigt
Copy link
Contributor

@Turini I'll also do a test when I'm at home, using Linux + Tomcat 7 and 8 + Java 8.

@armoucar
Copy link
Author

New Tomcat:

apache-tomcat-8.0.9.zip

Java 8:

java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b05)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b05, mixed mode)

OS infos:

$ uname -a
Darwin Arthurs-MacBook-Pro.local 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun  3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64

VRaptor4 local master:
image

mvn clean install:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] VRaptor4 .......................................... SUCCESS [0.981s]
[INFO] vraptor ........................................... SUCCESS [1:02.895s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:04.135s
[INFO] Finished at: Wed Aug 13 18:36:39 IST 2014
[INFO] Final Memory: 27M/167M
[INFO] ------------------------------------------------------------------------

blank-project vraptor deps:

<groupId>br.com.caelum</groupId>
<artifactId>vraptor-blank-project</artifactId>
<version>4.1.0-RC3-SNAPSHOT</version>

<dependencies>
    <dependency>
      <groupId>br.com.caelum</groupId>
      <artifactId>vraptor</artifactId>
      <version>${project.version}</version>
    </dependency>
   ...

Final result:
image

@Turini
Copy link
Member

Turini commented Oct 20, 2014

Can someone here test it using VRaptor 4.1.1 (newest) version?
I've tested again (and I ask for 3 devs too), but it doesn't happen
at our environment. Even creating an flesh instance on ec2 to test.

@armoucar
Copy link
Author

yep, as the problem was happening to me, i'll try it again.
after I do it I come to tell you guys the result.

_Ar_thur _Mou_ra _Car_valho
github http://github.com/armoucar / blog http://www.arthurcarvalho.com /
twitter http://twitter.com/armoucar

2014-10-20 13:36 GMT+01:00 Rodrigo Turini notifications@github.com:

Can someone here test it using VRaptor 4.1.1 (newest) version?
I've tested again (and I ask for 3 devs too), but it doesn't happen
at our environment. Even creating an flesh instance on ec2 to test.


Reply to this email directly or view it on GitHub
#696 (comment).

@garcia-jj
Copy link
Member

Works fine for me too.

@Turini
Copy link
Member

Turini commented Oct 27, 2014

I'm closing the issue, but we can reopen if the problem persist in your env :) thanks

@Turini Turini closed this as completed Oct 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants