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

Getting wrong path when trying to use logic forwardTo #733

Closed
adolfoweloy opened this issue Aug 6, 2014 · 6 comments
Closed

Getting wrong path when trying to use logic forwardTo #733

adolfoweloy opened this issue Aug 6, 2014 · 6 comments
Labels

Comments

@adolfoweloy
Copy link

This issue is related to 4.1.0-RC2-SNAPSHOT version of VRaptor4.

When trying to invoke a forwardTo as result.forwardTo(this).form(), I'm getting an error message that says "Resource not found". That's happening because of wrong path resolving inside DefaultLogicResult.java. The code below, shows the usage of forwardTo that leads to this wrong behavior.

@Controller
public class BooksController {
    private BookShelf shelf;
    private Result result;

    @Inject
    public BooksController(BookShelf shelf, Result result) {
        this.shelf = shelf;
        this.result = result;
    }

        @Deprecated 
    BooksController() {}

    public void form() {
        List<Author> authors = shelf.findAll();
        result.include("authors", authors);
    }

    public void edit(String isbn) {
        Book found = shelf.findByIsbn(isbn);

        if (found == null) {
            result.notFound();
        } else {
            result.include(found);
            result.forwardTo(this).form();
        }

    }
}

Take a look at the code snippet that starts at line 113 inside DefaultLogicResult.java:

if (!response.isCommitted()) {
    String path = resolver.pathFor(DefaultControllerMethod.instanceFor(type, method));
    logger.debug("Forwarding to {}", path);
    request.getRequestDispatcher(path).forward(request, response);
}

When resolver.pathFor returns the path, the path is resolved as follows:

/WEB-INF/jsp/booksController$Proxy$_$$_WeldSubclass/form.jsp

The real path should be:

/WEB-INF/jsp/books/form.jsp

Instead of trying to change code inside DefaultLogicResult.java, I made some change at DefaultPathResolver.extractControllerFromName as follows:

protected String extractControllerFromName(String baseName) {
        // I've created getFilteredControllerBaseName method to filter proxified baseName classes
    baseName = getFilteredControllerBaseName(
        lowerFirstCharacter(baseName));

    if (baseName.endsWith("Controller")) {
        return baseName.substring(0, baseName.lastIndexOf("Controller"));
    }

    return baseName;
}

Using such change, I've got my version working as I was expecting. I ran all unit tests using mvn test and got no errors.

I think that is an issue, and maybe someone knows a better solution than I gave.

@Turini Turini added the bug label Aug 6, 2014
@csokol
Copy link
Contributor

csokol commented Aug 6, 2014

I think that a better solution is to get the superclass of the weld proxy
(it should be original controller class).

I don't know why this issue haven't happened until now...

Chico Sokol

On Wed, Aug 6, 2014 at 12:35 AM, astronauta [email protected]
wrote:

This issue is related to 4.1.0-RC2-SNAPSHOT version of VRaptor4.

When trying to invoke a forwardTo as result.forwardTo(this).form(), I'm
getting an error message that says "Resource not found". That's happening
because of wrong path resolving inside DefaultLogicResult.java. The code
below, shows the usage of forwardTo that leads to this wrong behavior.

@Controllerpublic class BooksController {
private BookShelf shelf;
private Result result;

@Inject
public BooksController(BookShelf shelf, Result result) {
    this.shelf = shelf;
    this.result = result;
}

    @Deprecated
BooksController() {}

public void form() {
    List<Author> authors = shelf.findAll();
    result.include("authors", authors);
}

public void edit(String isbn) {
    Book found = shelf.findByIsbn(isbn);

    if (found == null) {
        result.notFound();
    } else {
        result.include(found);
        result.forwardTo(this).form();
    }

}}

Take a look at the code snippet that starts at line 113 inside
DefaultLogicResult.java:

if (!response.isCommitted()) {
    String path = resolver.pathFor(DefaultControllerMethod.instanceFor(type, method));
    logger.debug("Forwarding to {}", path);
    request.getRequestDispatcher(path).forward(request, response);
}

When resolver.pathFor returns the path, the path is resolved as follows:

/WEB-INF/jsp/booksController$Proxy$_$$_WeldSubclass/form.jsp

The real path should be:

/WEB-INF/jsp/books/form.jsp

Instead of trying to change code inside DefaultLogicResult.java, I made
some change at DefaultPathResolver.extractControllerFromName as follows:

protected String extractControllerFromName(String baseName) {
            // I've created getFilteredControllerBaseName method to filter proxified baseName classes
    baseName = getFilteredControllerBaseName(
        lowerFirstCharacter(baseName));

    if (baseName.endsWith("Controller")) {
        return baseName.substring(0, baseName.lastIndexOf("Controller"));
    }

    return baseName;
}

Using such change, I've got my version working as I was expecting. I ran
all unit tests using mvn test and got no errors.

I think that is an issue, and maybe someone knows a better solution than I
gave.


Reply to this email directly or view it on GitHub
#733.

adolfoweloy pushed a commit to adolfoweloy/vraptor4 that referenced this issue Aug 6, 2014
@adolfoweloy
Copy link
Author

@csokol ,
There is a pool request for this bug here

@csokol
Copy link
Contributor

csokol commented Aug 6, 2014

Yes, I was late in this issue :-)

The solution in that PR is fine for me.

Chico Sokol

On Wed, Aug 6, 2014 at 12:25 PM, astronauta [email protected]
wrote:

@csokol https://github.com/csokol ,
There is a pool request for this bug here
#734


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

@adolfoweloy
Copy link
Author

thank you @csokol

@lucascs
Copy link
Member

lucascs commented Aug 6, 2014

I think it was not happening before because it's not always necessary to proxify a controller.

Maybe only if you use CDI interceptors, maybe for @Transactional, etc.

@garcia-jj
Copy link
Member

Closed by #734. Thank you @adolfoweloy to help us.

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

5 participants