Uso de result em Vraptor 3

Alguem jà tem desenvolvido alguma aplicação com vraptor3 ?

estou com um problema que não consigo resolver … cuando o metodo save é executado a classe não consigue ser redireciona pelo controller alguen tem uma ideia de como solucionar o problema…

package br.eg.collabcomponent.coord.userMgr;

import static br.com.caelum.vraptor.view.Results.logic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.eg.componentFramework.controller.ComponentFrameworks;

@RequestScoped
@Resource
public class UserMgrController{

	private UserMgrInstance userMgr;	
	private Result result;
	//private HttpServletRequest request;

	public UserMgrController(Result result, HttpServletRequest request) throws Exception {		
		this.result = result;
		//String idToolInstance = request.getParameter("idToolInstance");
		System.out.println(request.getParameter("idToolInstance"));
		//this.userMgr = componentFrameworks.getCollabCF();
	}

	/*
	 * This is a basic operation set: list (to show items), edit (to view for edit) save (to create and to update), and remove (to delete) 
	 */
	@Get
	@Path(value="/groupware-api/{idToolInstance}/userMgr")
	public List<User> list(String idToolInstance) throws Exception {

		this.userMgr = (UserMgrInstance) ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance).getCollabComponentInstance(UserMgrInstance.class);
		
		result.include("toolInstance", ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance));
		
		return (List<User>) this.userMgr.findAll();
	}

	@Get
	@Path(value="/groupware-api/{idToolInstance}/userMgr/{user.id}")
	public User show(User user,String idToolInstance) throws Exception{
		
		this.userMgr = (UserMgrInstance) ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance).getCollabComponentInstance(UserMgrInstance.class);
		
		result.include("toolInstance", ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance));
		
		if ((user!=null)&&(user.getId()!=0)) {
			return this.userMgr.find(user.getId()+"");
		} else {
			return new User();
		}
	}

	@Post
	@Path(value="/groupware-api/{idToolInstance}/userMgr")
	public void save(User user,String idToolInstance) throws Exception{
		
		this.userMgr = (UserMgrInstance) ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance).getCollabComponentInstance(UserMgrInstance.class);
		
		this.userMgr.save(user);
		
		//result.include("contextPath", request.getContextPath());
		result.use(logic()).redirectTo(UserMgrController.class).list(idToolInstance);
	}

	@Delete
	@Path(value="/groupware-api/{idToolInstance}/userMgr/{user.id}")
	public void remove(User user,String idToolInstance) throws Exception{
		
		this.userMgr = (UserMgrInstance) ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance).getCollabComponentInstance(UserMgrInstance.class);
		
		this.userMgr.delete(user.getId());
		
		
		//result.include("contextPath", request.getContextPath());
		result.use(logic()).redirectTo(UserMgrController.class).list(idToolInstance);
	}


}

Aug 17, 2009 10:22:34 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
br.com.caelum.vraptor.InterceptionException: br.com.caelum.vraptor.proxy.ProxyCreationException: Tried to instantiate type: class br.eg.collabcomponent.coord.userMgr.UserMgrController 1 times, but none of the attempts worked. The exceptions are: [net.sf.cglib.core.CodeGenerationException: java.lang.NullPointerException–>null]
at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:76)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:57)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.intercept(ParametersInstantiatorInterceptor.java:96)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:57)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:55)
at br.com.caelum.vraptor.core.InstantiatedInterceptorHandler.execute(InstantiatedInterceptorHandler.java:51)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.interceptor.multipart.MultipartInterceptor.intercept(MultipartInterceptor.java:61)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:57)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.interceptor.InterceptorListPriorToExecutionExtractor.intercept(InterceptorListPriorToExecutionExtractor.java:59)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:57)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.core.URLParameterExtractorInterceptor.intercept(URLParameterExtractorInterceptor.java:45)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:57)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:71)
at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:57)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:71)
at br.com.caelum.vraptor.core.DefaultRequestExecution.execute(DefaultRequestExecution.java:71)
at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:99)
at br.com.caelum.vraptor.ioc.pico.PicoProvider.provideForRequest(PicoProvider.java:140)
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:97)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:636)
Caused by: br.com.caelum.vraptor.proxy.ProxyCreationException: Tried to instantiate type: class br.eg.collabcomponent.coord.userMgr.UserMgrController 1 times, but none of the attempts worked. The exceptions are: [net.sf.cglib.core.CodeGenerationException: java.lang.NullPointerException–>null]
at br.com.caelum.vraptor.proxy.DefaultProxifier.tryAllConstructors(DefaultProxifier.java:112)
at br.com.caelum.vraptor.proxy.DefaultProxifier.useCGLib(DefaultProxifier.java:60)
at br.com.caelum.vraptor.proxy.DefaultProxifier.proxify(DefaultProxifier.java:41)
at br.com.caelum.vraptor.view.DefaultLogicResult.redirectTo(DefaultLogicResult.java:108)
at br.eg.collabcomponent.coord.userMgr.UserMgrController.save(UserMgrController.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:60)
… 36 more

@gcclovecraft

No momento existe um bug que impede ações complexas no construtor (quando é criado um proxy, está sendo passado null no construtor). Existe um issue com relação a isso, mas não sei como está no momento.

Um workaround momentaneo é criar um tipo para o seu idToolInstance e implementar um ComponentFactory que instancie ele a partir do request.

@Resource
@RequestScoped
public class SeuComponentFactory implements ComponentFactory<IdToolInstance> {
       //recebe request no construtor
       IdToolInstance getComponent(){
                return new IdToolInstance(request.getParameter("idToolInstance"); 
       }
}

e o seu component passa a receber IdToolInstance no construtor no lugar o HttpServletRequest.

Obrigado Jonasabreu

Alguem tem outra solução embora eu tinha criado o factory… o problema persiste

@RequestScoped
@Resource
public class UserMgrController{

	final private ToolInstance toolInstance;
	final private UserMgrInstance userMgr;	
	final private Result result;

	public UserMgrController(Result result, ToolInstance toolInstance) throws Exception {		
		this.result = result;
		this.toolInstance = toolInstance;
		this.userMgr = (UserMgrInstance) toolInstance.getCollabComponentInstance(UserMgrInstance.class);
	}

	/*
	 * This is a basic operation set:
	 * 	list (to show items),
	 * 	edit (to view for edit)
	 * 	save (to create and to update), and remove (to delete) 
	 */
	@Get
	@Path(value="/groupware-api/{idToolInstance}/userMgr")
	public void list() throws Exception {
		result.include("userList", (List<User>) this.userMgr.findAll());
		result.include("toolInstance", this.toolInstance);

	}

	@Get
	@Path(value="/groupware-api/{idToolInstance}/userMgr/{user.id}")
	public User show(User user) throws Exception{

		result.include("toolInstance", this.toolInstance);
		if ((user!=null)&&(user.getId()!=0)) {
			return this.userMgr.find(user.getId()+"");
		} else {
			return new User();
		}
	}

	@Post
	@Path(value="/groupware-api/{idToolInstance}/userMgr")
	public void save(User user) throws Exception{

		this.userMgr.save(user);
		result.use(Results.logic()).redirectTo(UserMgrController.class).list();
	}

	@Delete
	@Path(value="/groupware-api/{idToolInstance}/userMgr/{user.id}")
	public void remove(User user) throws Exception{

		this.userMgr.delete(user.getId());
		result.use(Results.logic()).redirectTo(UserMgrController.class).list();
	}

}

O factory

@RequestScoped
@Resource
public class ToolInstanceFactory implements ComponentFactory<ToolInstance> {

	HttpServletRequest request;
	
	public ToolInstanceFactory(HttpServletRequest request) {
		this.request = request;
	}
	
	public ToolInstance getInstance() {
		String idToolInstance = request.getParameter("idToolInstance");
		ToolInstance toolInstance = null;
		if ((idToolInstance!=null) && (!idToolInstance.isEmpty())) {
                        toolInstance = ComponentFrameworks.getToolCF().getComponentInstance(idToolInstance);
		}else {
			toolInstance = ComponentFrameworks.getToolCF().getMainTool();
		}
		return toolInstance;
	}

}

O problema persiste

Caused by: br.com.caelum.vraptor.proxy.ProxyCreationException: Tried to instantiate type: class br.eg.collabcomponent.coord.userMgr.UserMgrController 1 times, but none of the attempts worked. The exceptions are: [net.sf.cglib.core.CodeGenerationException: java.lang.NullPointerException–>null]
at br.com.caelum.vraptor.proxy.DefaultProxifier.tryAllConstructors(DefaultProxifier.java:112)
at br.com.caelum.vraptor.proxy.DefaultProxifier.useCGLib(DefaultProxifier.java:60)
at br.com.caelum.vraptor.proxy.DefaultProxifier.proxify(DefaultProxifier.java:41)

o que acontece é que quando você manda redirecionar, o vraptor instancia sua lógica passando null para todos
os parametros do construtor…

então quando você faz:

toolInstance.getCollabComponentInstance(UserMgrInstance.class);

dá nullpointer…

o que você pode fazer:

  • fazer um if pra verificar se o parâmetro é null
  • fazer um component factory que te retorna direto o UserMgrInstance… algo como:
@Component
public clas UserMgrInstanceFactory implements ComponentFactory<UserMgrInstance> {
     public UserMgrInstanceFactory(ToolInstance instance) {
          this.instance = instance
     }

    public UserMgrInstance getInstance() {
       return instance.getCollabComponentInstance(UserMgrInstance.class);
   }
}

de qualquer forma, esse bug já foi reportado e será corrigido em breve

[]'s