Ele não funciona como planejado… Hehehe… Deve ter algo muito errado.
Como está o projeto:
Uma annotation que serve para anotar os métodos que serão “retornáveis”.
Um Interceptor que armazena esses métodos em um list.
Duas classes para resources:
public class Resource {
private ResourceMethod resourceMethod;
private ResourceClass resourceClass;
private MethodInfo methodInfo;
}
[code]@Component
@SessionScoped
public class ResourceList {
private List<Resource> resourceList = new ArrayList<Resource>();
public List<Resource> getResourceList() {
return resourceList;
}
}[/code]
Duas interfaces que extendem Result e Validator do VRaptor:
[code]public interface WSValidator extends Validator {
public void onErrorRedirectToLastAnnotated();
public void onErrorForwardToLastAnnotated();
}[/code]
[code]public interface WSResult extends Result {
public void redirectToLastAnnotated();
public void forwardToLastAnnotated();
}[/code]
Dois factories para os meus Defaults:
[code]@Component
public class WSValidatorFactory implements ComponentFactory<WSValidator> {
private final WSValidator wsValidator;
public WSValidatorFactory(Validator validator, ResourceList resourceList) {
this.wsValidator = new DefaultWSValidator(validator, resourceList);
}
@Override
public WSValidator getInstance() {
return wsValidator;
}
}[/code]
[code]@Component
public class WSResultFactory implements ComponentFactory<WSResult> {
private final WSResult wsResult;
public WSResultFactory(Result result, ResourceList resourceList) {
this.wsResult = new DefaultWSResult(result, resourceList);
}
@Override
public WSResult getInstance() {
return wsResult;
}
}[/code]
e os dois Defaults que implementam de fato a funcionalidade…
[code]@Component
public class DefaultWSValidator implements WSValidator {
private Validator validator;
private Resource resource;
public DefaultWSValidator(Validator validator,
ResourceList resourceList) {
this.validator = validator;
resource = resourceList.getResourceList().get(
resourceList.getResourceList().size() - 1);
}
public void onErrorRedirectToLastAnnotated() {
Object controller = validator.onErrorRedirectTo(resource
.getResourceClass().getType());
try {
if (resource.getMethodInfo().getParameters() != null
&& resource.getMethodInfo().getParameters().length > 0) {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withArgs(resource.getMethodInfo().getParameters());
} else {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withoutArgs();
}
} catch (ReflectionProviderException e) {
throw (ValidationException) e.getCause();
}
}
public void onErrorForwardToLastAnnotated() {
Object controller = validator.onErrorForwardTo(resource
.getResourceClass().getType());
try {
if (resource.getMethodInfo().getParameters() != null
&& resource.getMethodInfo().getParameters().length > 0) {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withArgs(resource.getMethodInfo().getParameters());
} else {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withoutArgs();
}
} catch (ReflectionProviderException e) {
throw (ValidationException) e.getCause();
}
}
// resto das implementações
}[/code]
[code]@Component
public class DefaultWSResult implements WSResult {
private Resource resource;
private Result result;
public DefaultWSResult(Result result, ResourceList resourceList) {
this.result = result;
resource = resourceList.getResourceList().get(
resourceList.getResourceList().size() - 1);
}
@Override
public void redirectToLastAnnotated() {
Object controller = result.redirectTo(resource.getResourceClass()
.getType());
if (resource.getMethodInfo().getParameters() != null
&& resource.getMethodInfo().getParameters().length > 0) {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withArgs(resource.getMethodInfo().getParameters());
} else {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withoutArgs();
}
}
@Override
public void forwardToLastAnnotated() {
Object controller = result.forwardTo(resource.getResourceClass()
.getType());
if (resource.getMethodInfo().getParameters() != null
&& resource.getMethodInfo().getParameters().length > 0) {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withArgs(resource.getMethodInfo().getParameters());
} else {
new Mirror().on(controller).invoke()
.method(resource.getResourceMethod().getMethod())
.withoutArgs();
}
}
// resto das implementações
}[/code]