Created
March 13, 2015 13:35
-
-
Save dtelaroli/40be08b054459fc7631d to your computer and use it in GitHub Desktop.
LoggedInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.flexait.sorricred.application.interceptor; | |
import static br.com.caelum.vraptor.view.Results.http; | |
import javax.inject.Inject; | |
import br.com.caelum.vraptor.InterceptionException; | |
import br.com.caelum.vraptor.Intercepts; | |
import br.com.caelum.vraptor.Result; | |
import br.com.caelum.vraptor.core.InterceptorStack; | |
import br.com.caelum.vraptor.interceptor.Interceptor; | |
import br.com.caelum.vraptor.resource.ResourceMethod; | |
import br.com.flexait.core.SessionComponent; | |
import br.com.flexait.core.component.RequestComponent; | |
import br.com.flexait.core.interceptor.ExceptionInterceptor; | |
import br.com.flexait.sorricred.application.controller.AccessController; | |
@Intercepts(before = ExceptionInterceptor.class, after = {}) | |
public class LoggedInterceptor implements Interceptor { | |
private final SessionComponent sessionComponent; | |
private final Result result; | |
private final RequestComponent request; | |
@Inject | |
public LoggedInterceptor(Result result, SessionComponent sessionComponent, RequestComponent request) { | |
this.result = result; | |
this.sessionComponent = sessionComponent; | |
this.request = request; | |
} | |
@Override | |
public void intercept(InterceptorStack stack, ResourceMethod method, | |
Object resourceInstance) throws InterceptionException { | |
if(isLogged()) { | |
stack.next(method, resourceInstance); | |
} | |
else { | |
if(request.isAjax()) { | |
result.use(http()).body("{\"errors\": [{\"message\":\"Sua sessão expirou\", \"category\":\"session\"}]").setStatusCode(403); | |
} | |
else { | |
result.redirectTo(AccessController.class).index(); | |
} | |
} | |
} | |
private boolean isLogged() { | |
return sessionComponent.isLogged(); | |
} | |
@Override | |
public boolean accepts(ResourceMethod method) { | |
return !method.getMethod().isAnnotationPresent(Public.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment