The reflection impl

import java.lang.reflect.Method;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;


public class RestAction extends HttpBaseAction {

    @Action(value="/rest/*",
        result...@result(name=HttpBaseAction.SUCCESS,
location="/WEB-INF/content/rest-success.jsp")}
    )
    public final String execute() throws Exception {
        System.out.println();
        System.out.println("PARENT : " + getParentPath());
        System.out.println("RESOURCE : " + getResourceName());
        return HttpBaseAction.SUCCESS;
    }

    // GET /rest/1
    public String show() {
        return HttpBaseAction.SUCCESS;
    }

    // GET /rest/
    public String index() {

        //list = ordersService.getAll();
        return HttpBaseAction.SUCCESS;
    }

    // POST /rest
    public String create() {
        System.out.println("create " + getResourceName());
        //ordersService.save(model);
        addActionMessage("New order created successfully");
        return HttpBaseAction.SUCCESS;
    }

    public String getParentPath() throws SecurityException,
NoSuchMethodException {
        Method m = this.getClass().getMethod("execute", null);
        Action aa = m.getAnnotation(Action.class);
        int valueLength = aa.value().substring(0,
aa.value().lastIndexOf('/')).length();
        String parentPath =
getRequestURI().substring(getContextPath().length() + valueLength,
getRequestURI().lastIndexOf('/'));
        return parentPath;
    }

    public String getResourceName() {
        return getRequestURI().substring(getRequestURI().lastIndexOf('/') +
1);
    }


}

The output:

curl http://localhost:8080/context/rest/2/1/4553/45

PARENT : /2/1/4553
RESOURCE : 45

On Tue, Dec 23, 2008 at 7:25 AM, Luis Gervaso <luis.gerv...@gmail.com>wrote:

> OK with convention plugin is possible to do
>
> code from RestAction
>
> @Action(value="/rest/*",
>         result...@result(name=BaseAction.SUCCESS,
> location="/WEB-INF/content/rest-success.jsp")}
>     )
>     public final String execute() {
>         System.out.println();
>         System.out.println("PARENT : " + getParentPath());
>         System.out.println("RESOURCE : " + getResourceName());
>
>
> //examine http method and other stuff
>
>         return HttpBaseAction.SUCCESS;
>     }
>
> now the requestURI returns
>
> /contextPath/rest/folder1/folder2/folder3/file1
>
> contextPath is fixed length OK
> rest is annotated (is possible access without reflection? may be from the
> current ActionContext?) ????
> getParentPath returns folder1/folder2/folder3 (from /* to last /) OK
> getResourceName file1
>
> Here is the fast code for getParentPath() and getResourceName() [UNSOLVED
> the Action current name]
>
> protected String getRequestURI() {
>         String uri = getRequest().getRequestURI();
>         return (uri.charAt(uri.length() - 1) == '/') ?
> uri.substring(0,uri.length() - 1) : uri;
> }
>
>     protected String getParentPath() {
>         String parentPath =
> getRequestURI().substring(getContextPath().length(),getRequestURI().lastIndexOf('/'));
>         if (parentPath.length() < 1) parentPath = "/";
>         return parentPath;
>     }
>
>     protected String getResourceName() {
>         String resourceName =
> getRequestURI().substring(getContextPath().concat(getParentPath()).length());
>         return resourceName.charAt(0) == '/' ? resourceName.substring(1) :
> resourceName;
>
>     }
>
>
>
>
> On Tue, Dec 23, 2008 at 6:45 AM, Luis Gervaso <luis.gerv...@gmail.com>wrote:
>
>> I have been trying to figure the solution,
>>
>> I'm using convention plugin and i need to map first every first child of
>> http://www.domain.com/myapp/restController/<http://www.domain.com/myapp/restController/resource1/resource2/resource3/resource4/resource5>to
>>  a "restaction"
>>
>> so
>>
>>
>> http://www.domain.com/myapp/restController/fileSystem/folder1/folder2/folder3/file1
>>
>> http://www.domain.com/myapp/restController/fileSystem/folder1/folder2/folder3/file2
>>
>> both to FileSystemRestController (which inherit from
>> AbstractRestController)
>>
>> AND
>>
>> http://www.domain.com/myapp/restController/roles/user1/role1/subrole1
>> http://www.domain.com/myapp/restController/roles/user2/role1/subrole1
>> http://www.domain.com/myapp/restController/roles/user3/role1/subrole2
>>
>> both to RolesRestController
>>
>> these are just an example
>>
>> the main purpose of this is be able to use
>>
>> curl -X POST
>> http://www.domain.com/myapp/restController/roles/user1/role1/subrole1/othermorerole
>>
>> and create othermorerole under
>> http://www.domain.com/myapp/restController/roles/user1/role1/subrole1
>>
>>
>>
>>
>> On Tue, Dec 23, 2008 at 3:05 AM, Luis Gervaso <luis.gerv...@gmail.com>wrote:
>>
>>> I'm wondering why not the behaviour of the rest plugin is ...
>>>
>>> *For example*
>>>
>>> URL:
>>> http://www.domain.com/myapp/restController/resource1/resource2/resource3/resource4/resource5
>>>
>>> creates a mapping parent & child
>>>
>>> i.e:
>>>
>>> parentId: resource1/resource2/resource3/resource4
>>> childId: resource5
>>>
>>> these can be obtained from request URI
>>>
>>> now in the logic of the RestController we are aware to map these ids
>>> (really paths) with the model
>>>
>>> ParentObject - getFrom(parentId)
>>> ChildObject - getFrom(childId)
>>>
>>> This way to create new rest services we have to extend this base class
>>>
>>> *Another example*
>>>
>>>
>>> http://www.domain.com/myapp/restController/resource1/resource2/resource3/resource4/editNew
>>>
>>> parentId : resource1/resource2/resource3/resource4
>>> childId: a new ModelObject from the specific restController (which
>>> extends BaseRestController)
>>>
>>> *The create method *
>>>
>>> uses the post request parameters for the child
>>> uses the url to locate the parent
>>>
>>> I have put here childId or parentId, BUT really are ChildPath or
>>> ParentPath (the ids are for databases)
>>>
>>> if we need to use this concept may be more appropiate to use the term
>>> "Reference"
>>>
>>> Thanks for readme
>>>
>>> Luis
>>> --
>>> -------------------------------------------
>>> Luis Alberto Gervaso Martin
>>> Java EE Architect & Instructor
>>> C/ Cuenca 4A, 2ºB
>>> Getafe (Madrid)
>>> SPAIN
>>> mobile: (+34) 627983344
>>>  luis.gerv...@gmail.com
>>>
>>
>>
>>
>> --
>> -------------------------------------------
>> Luis Alberto Gervaso Martin
>> Java EE Architect & Instructor
>> C/ Cuenca 4A, 2ºB
>> Getafe (Madrid)
>> SPAIN
>> mobile: (+34) 627983344
>> luis.gerv...@gmail.com
>>
>
>
>
> --
> -------------------------------------------
> Luis Alberto Gervaso Martin
> Java EE Architect & Instructor
> C/ Cuenca 4A, 2ºB
> Getafe (Madrid)
> SPAIN
> mobile: (+34) 627983344
> luis.gerv...@gmail.com
>



-- 
-------------------------------------------
Luis Alberto Gervaso Martin
Java EE Architect & Instructor
C/ Cuenca 4A, 2ºB
Getafe (Madrid)
SPAIN
mobile: (+34) 627983344
luis.gerv...@gmail.com

Reply via email to