All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.att.ajsc.common.Interceptor Maven / Gradle / Ivy

There is a newer version: 6.2.0.4-oss
Show newest version
package com.att.ajsc.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;

import com.att.ajsc.logging.AjscEelfManager;
import com.att.eelf.configuration.EELFLogger;

@Component
public class Interceptor {

	private static final String URL = ".url";
	private static final String TRANSACTION_TRAIL = "transactionTrail";
	private static final String INTERCEPTOR_ERROR = "InterceptorError";
	private static EELFLogger logger = AjscEelfManager.getInstance().getLogger(Interceptor.class);

	private void invokeInterceptors(List interceptors, ContainerRequestContext requestContext,
			ContainerResponseContext responseContext, ApplicationContext context) {

		boolean isPreInterceptorChain = true;

		if (responseContext != null) {
			isPreInterceptorChain = false;
		}

		if (!interceptors.isEmpty()) {
			if (isPreInterceptorChain) {
				for (AjscInterceptor interceptor : interceptors) {
					try {

						boolean invokeNext = ((AjscPreInterceptor) interceptor).allowOrReject(requestContext);
						if (!invokeNext) {
							if (requestContext.getProperty(INTERCEPTOR_ERROR) != null
									&& requestContext.getProperty(INTERCEPTOR_ERROR).getClass().getSimpleName()
											.equals(INTERCEPTOR_ERROR)) {
								InterceptorError error = (InterceptorError) requestContext
										.getProperty(INTERCEPTOR_ERROR);
								if (Status.fromStatusCode(error.getResponseCode()) == null) {
									requestContext.abortWith(Response.status(Status.FORBIDDEN).build());
								} else {
									requestContext
											.abortWith(Response.status(error.getResponseCode()).entity(error).build());
								}
								requestContext.setProperty(INTERCEPTOR_ERROR, null);
							} else {
								requestContext.abortWith(Response.status(Status.FORBIDDEN).build());
								requestContext.setProperty(TRANSACTION_TRAIL, context.getBean(TRANSACTION_TRAIL));
							}
							requestContext.setProperty(TRANSACTION_TRAIL, context.getBean(TRANSACTION_TRAIL));
							logger.error(InterceptorMessages.INTERCEPTOR_FAILED_PRE_INTERCEPTOR,
									interceptor.getClass().toString());
							break;
						}
					} catch (Exception e) {
						logger.error(InterceptorMessages.INTERCEPTOR_ERROR_PRE_INTERCEPTOR, e,
								interceptor.getClass().toString());
						requestContext.abortWith(Response.status(Status.INTERNAL_SERVER_ERROR).build());
						break;
					}
				}
			} else {

				for (AjscInterceptor interceptor : interceptors) {
					try {
						boolean invokeNext = ((AjscPostInterceptor) interceptor).allowOrReject(requestContext,
								responseContext);
						if (!invokeNext) {
							logger.info(InterceptorMessages.INTERCEPTOR_FAILED_POST_INTERCEPTOR,
									interceptor.getClass().toString());
							break;
						}
					} catch (Exception e) {
						logger.error(InterceptorMessages.INTERCEPTOR_ERROR_POST_INTERCEPTOR, e,
								interceptor.getClass().toString());
						responseContext.setStatus(Status.INTERNAL_SERVER_ERROR.getStatusCode());
						break;
					}
				}

			}

		}

	}

	private List filterInterceptors(List interceptors, String pathinfo)
			throws InstantiationException, IllegalAccessException {

		List updtdInterceptors = new ArrayList();

		for (AjscInterceptor interceptor : interceptors) {
			if (hasMatch(interceptor.getUri(), pathinfo) && !updtdInterceptors.contains(interceptor)) {
				updtdInterceptors.add(interceptor);
			}
		}

		return sortIntereceptors(updtdInterceptors);
	}

	private List sortIntereceptors(List list) {

		Collections.sort(list, new Comparator() {
			public int compare(AjscInterceptor o1, AjscInterceptor o2) {
				return (o1.getPosition() > o2.getPosition() ? 1 : (o1.getPosition() == o2.getPosition() ? 0 : -1));
			}
		});
		return list;
	}

	private boolean hasMatch(String uri, String pathinfo) {

		boolean hasMatch = false;
		PathMatcher pathMatcher = new AntPathMatcher();

		if (!StringUtils.isEmpty(uri)) {
			String[] uris = uri.split(",");
			for (int i = 0; i < uris.length; i++) {
				if (pathMatcher.match(uris[i], pathinfo)) {
					hasMatch = true;
					break;
				}
			}
		}

		return hasMatch;

	}

	private String getActualClass(AjscInterceptor interceptor) {

		String className = null;
		if (interceptor.toString().indexOf("$$") > 0) {
			className = interceptor.toString().substring(0, interceptor.toString().indexOf("$$"));
		} else {
			className = interceptor.toString().substring(0, interceptor.toString().indexOf("@"));
		}
		return className;

	}

	public void filter(ContainerRequestContext requestContext, Environment environment, ApplicationContext context)
			throws InstantiationException, IllegalAccessException {

		List preInterceptors = null;

		String pathInfo = requestContext.getUriInfo().getAbsolutePath().getPath();

		List updatdPreInterceptors = new ArrayList();
		Map preInterceptorMap = context.getBeansOfType(AjscPreInterceptor.class);
		Collection preInterceptor = preInterceptorMap.values();

		for (AjscPreInterceptor ajscPreInterceptor : preInterceptor) {

			String uri = (String) environment.getProperty(getActualClass(ajscPreInterceptor) + URL);
			if (!StringUtils.isEmpty(uri)) {
				ajscPreInterceptor.setUri(uri);
			}
			updatdPreInterceptors.add(ajscPreInterceptor);
		}

		preInterceptors = filterInterceptors(updatdPreInterceptors, pathInfo);
		invokeInterceptors(preInterceptors, requestContext, null, context);

	}

	public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext,
			Environment environment, ApplicationContext context) throws InstantiationException, IllegalAccessException {

		List postInterceptors = null;

		String pathInfo = requestContext.getUriInfo().getAbsolutePath().getPath();

		List updatedPostinterceptors = new ArrayList();
		Map postInterceptorMap = context.getBeansOfType(AjscPostInterceptor.class);

		Collection postInterceptor = postInterceptorMap.values();
		for (AjscPostInterceptor ajscPostInterceptor : postInterceptor) {

			String uri = (String) environment.getProperty(getActualClass(ajscPostInterceptor) + URL);
			if (!StringUtils.isEmpty(uri)) {
				ajscPostInterceptor.setUri(uri);
			}
			updatedPostinterceptors.add(ajscPostInterceptor);

		}

		postInterceptors = filterInterceptors(updatedPostinterceptors, pathInfo);
		invokeInterceptors(postInterceptors, requestContext, responseContext, context);

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy