The LogicLocator

The logic-locator component is responsible for reading urls and translating them into a logic request.

This means that, given the request URL, they must come up with which component and logic the user is trying to access.

The default logic locator implementation uses the standard componentName.logicName.logic pattern.

You can override the default logic locator by implementing the LogicLocator interface and adding following lines on your web.xml:

<context-param>
        <param-name>
                org.vraptor.url.LogicLocator
        </param-name>
        <param-value>org.vraptor.MyLogicLocator</param-value>
</context-param>

A Concrete Example

Assume that you want to change default url pattern componentName.logicName.logic to componentName/logicName.logic. You may implement your own LogicLocator, like:

public class MyLogicLocator implements LogicLocator {

        private final ComponentManager manager;

        public MyLogicLocator(ComponentManager manager) {
                this.manager = manager;
        }

        public LogicMethod locate(HttpServletRequest req)
                        throws InvalidURLException, LogicNotFoundException,
                        ComponentNotFoundException {

                String requestURI = req.getRequestURI();
                
                //URI should be like http://mydomain.com/myapp/componentName/logicName.logic

                int lastSlash = requestURI.lastIndexOf('/');
                int beforeLastSlash = requestURI.lastIndexOf('/', lastSlash - 1);
                if (lastSlash == -1 || beforeLastSlash == -1) {
                        throw new InvalidURLException("I can't handle this url");
                }

                String logic = requestURI.substring(lastSlash + 1); // logic == logicName.logic
                int comma = logic.lastIndexOf('.'); 
                if (comma == -1) {
                        throw new InvalidURLException("I can't handle this url");
                }
                logic = logic.substring(0, comma - 1); //logic == logicName
                
                String component = requestURI.substring(beforeLastSlash + 1, lastSlash);


                ComponentType component = manager.getComponent(component, logic);
                LogicMethod method = component.getLogic(logic);
                
                return method;
                
        }

}