Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2011 Douglas Campos <> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 package me.qmx.vraptor.authz;
Default authorization interceptor implementation. Check for situations on the test cases.

douglas campos
guilherme silveira
 public class Authz implements Interceptor {
 	private static final Logger log = LoggerFactory.getLogger(Authz.class);
 	private final AuthzInfo authInfo;
 	private final Authorizator authorizator;
 	private final Result result;
 	private final Router router;
 	private final HttpServletRequest request;
 	public Authz(Authorizator authorizatorAuthzInfo authInfoResult resultRouter routerHttpServletRequest request) {
 		this. = authorizator;
 		this. = authInfo;
 		this. = result;
 		this. = router;
 		this. = request;
 	public void intercept(InterceptorStack stackResourceMethod methodObject resourceInstancethrows InterceptionException {
 		Authorizable authorizable = .getAuthorizable();
 		if (authorizable == null) {
 			.error("no AuthInfo found!");
 			throw new IllegalStateException("No AuthInfo found");
 		} else if (isAllowed(authorizable)) {;
 		} else {
 	private boolean isAllowed(Authorizable authorizable) {
 		String currentURL = getCurrentURL();
 		String method = .getMethod();
 		HttpMethod httpMethod = HttpMethod.valueOf(method);
 		EnumSet<HttpMethodhttpMethods = EnumSet.of(httpMethod);
 		for (Role role : authorizable.roles()) {
 			if (.isAllowed(rolecurrentURLhttpMethods)) {
 				return true;
 		return false;
 	private String getCurrentURL() {
 		String requestURI = .getRequestURI();
 		String contextPath = .getContextPath();
 		return requestURI.replaceFirst(contextPath"");
 	public boolean accepts(ResourceMethod method) {
 			return false;
 		return true;
	private boolean isAnnotationPresent(Class<?> type) {
		return type.isAnnotationPresent(AuthzBypass.class) || (!type.equals(Object.class) && isAnnotationPresent(type.getSuperclass()));
New to GrepCode? Check out our FAQ X