Created
September 13, 2013 19:21
-
-
Save marceloemanoel/6554905 to your computer and use it in GitHub Desktop.
JsonHttpView
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.smartcoders.easyclinic.components; | |
import org.springframework.http.HttpStatus; | |
import br.com.caelum.vraptor.Intercepts; | |
import br.com.caelum.vraptor.Result; | |
import br.com.caelum.vraptor.InterceptionException; | |
import br.com.caelum.vraptor.core.InterceptorStack; | |
import br.com.caelum.vraptor.interceptor.Interceptor; | |
import br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor; | |
import br.com.caelum.vraptor.resource.ResourceMethod; | |
import br.com.smartcoders.easyclinic.services.security.AccessDeniedException; | |
@Intercepts(before=ParametersInstantiatorInterceptor.class) | |
public class ErrorInterceptor implements Interceptor { | |
private Result result; | |
private ErrorInterceptor(Result result){ | |
this.result = result; | |
} | |
@Override | |
public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException { | |
try{ | |
try { | |
stack.next(method, resourceInstance); | |
} | |
catch(InterceptionException error) { | |
Class<? extends Throwable> errorClass = error.getCause().getClass(); | |
throw errorClass.cast(error.getCause()); | |
} | |
} | |
catch(HttpStatusException error) { | |
error.printStackTrace(); | |
result.use(JsonHttpView.class) | |
.withStatus(error.getHttpStatus()) | |
.withoutRoot() | |
.from(new ResultWrapper(error)) | |
.serialize(); | |
} | |
catch(AccessDeniedException error) { | |
error.printStackTrace(); | |
result.use(JsonHttpView.class) | |
.withStatus(HttpStatus.FORBIDDEN) | |
.withoutRoot() | |
.from(new ResultWrapper(error)) | |
.serialize(); | |
} | |
catch(Throwable error) { | |
error.printStackTrace(); | |
result.use(JsonHttpView.class) | |
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR) | |
.withoutRoot() | |
.from(new ResultWrapper(error)) | |
.serialize(); | |
} | |
} | |
@Override | |
public boolean accepts(ResourceMethod method) { | |
return true; | |
} | |
} |
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.smartcoders.easyclinic.components; | |
import static br.com.caelum.vraptor.view.Results.*; | |
import javax.servlet.http.HttpServletResponse; | |
import org.springframework.http.HttpStatus; | |
import br.com.caelum.vraptor.Result; | |
import br.com.caelum.vraptor.View; | |
import br.com.caelum.vraptor.ioc.Component; | |
import br.com.caelum.vraptor.serialization.NoRootSerialization; | |
import br.com.caelum.vraptor.serialization.Serializer; | |
@Component | |
public class JsonHttpView implements View { | |
private HttpServletResponse response; | |
private Result result; | |
public JsonHttpView(HttpServletResponse response, Result result) { | |
this.response = response; | |
this.result = result; | |
} | |
public JsonHttpView withStatus(HttpStatus status){ | |
response.setStatus(status.value()); | |
return this; | |
} | |
public NoRootSerialization withoutRoot() { | |
return result.use(json()).withoutRoot(); | |
} | |
public Serializer from(Object object) { | |
return result.use(json()).from(object); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment