org.jboss.resteasy.core.ResourceMethodInvoker Java Examples

The following examples show how to use org.jboss.resteasy.core.ResourceMethodInvoker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CreateQuerySessionIDFilterTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    annotation = new GenerateQuerySessionId() {
        @Override
        public String cookieBasePath() {
            return "/test/path/";
        }
        
        @Override
        public Class<? extends Annotation> annotationType() {
            return GenerateQuerySessionId.class;
        }
    };
    
    request = new ResponseContainerRequestContext(MockHttpRequest.post("/mock"));
    request.setProperty(ResourceMethodInvoker.class.getName(), method);
    
    response = new ContainerResponseContextImpl(request.getHttpRequest(), new MockHttpResponse(), new BuiltResponse());
    
    filter = new CreateQuerySessionIDFilter();
}
 
Example #2
Source File: SiestaResourceMethodFinder.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
public String getResourceMethodPath(final HttpServletRequest request,
                                    final HttpServletResponse response)
{
  StringBuilder buffer = new StringBuilder();
  ResourceMethodInvoker method = getResourceMethod(request, response);

  Path classPath = method.getResourceClass().getAnnotation(Path.class);
  if (nonNull(classPath)) {
    buffer.append(maybePrependWithForwardSlash(classPath.value()));
  }

  Path methodPath = method.getMethod().getDeclaredAnnotation(Path.class);
  if (nonNull(methodPath)) {
    buffer.append(maybePrependWithForwardSlash(methodPath.value()));
  }

  return cleanForwardSlashes(buffer.toString());
}
 
Example #3
Source File: CreateQuerySessionIDFilter.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
    ResourceMethodInvoker method = (ResourceMethodInvoker) request.getProperty(ResourceMethodInvoker.class.getName());
    
    GenerateQuerySessionId annotation = FindAnnotation.findAnnotation(method.getMethodAnnotations(), GenerateQuerySessionId.class);
    
    String path = annotation.cookieBasePath();
    String id = "";
    String cookieValue = generateCookieValue();
    boolean setCookie = true;
    switch (response.getStatusInfo().getFamily()) {
        case SERVER_ERROR:
        case CLIENT_ERROR:
            // If we're sending an error response, then there's no need to set a cookie since
            // there's no query "session" to stick to this server.
            setCookie = false;
            QUERY_ID.set(null);
            break;
        
        default:
            if (StringUtils.isEmpty(QUERY_ID.get())) {
                log.error(method.getResourceClass() + "." + method.getMethod().getName() + " did not set QUERY_ID threadlocal.");
            } else {
                id = QUERY_ID.get();
                QUERY_ID.set(null);
            }
            break;
    }
    
    if (setCookie) {
        response.getHeaders().add(HttpHeaderNames.SET_COOKIE,
                        new NewCookie(Constants.QUERY_COOKIE_NAME, cookieValue, path + id, null, null, NewCookie.DEFAULT_MAX_AGE, false));
    }
}
 
Example #4
Source File: NotFoundExceptionMapper.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void addMethod(String path, ResourceMethodInvoker method) {
    String produces = mostPreferredOrNull(method.getProduces());
    String consumes = mostPreferredOrNull(method.getConsumes());

    for (String verb : method.getHttpMethods()) {
        calls.add(new MethodDescription(verb, path, produces, consumes));
    }
}
 
Example #5
Source File: SiestaResourceMethodFinder.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public ResourceMethodInvoker getResourceMethod(final HttpServletRequest request,
                                               final HttpServletResponse response)
{
  HttpRequest httpRequest = new HttpServletInputMessage(
      request,
      response,
      request.getServletContext(),
      null,
      extractHttpHeaders(request),
      extractUriInfo(request, MOUNT_POINT),
      request.getMethod(),
      (SynchronousDispatcher) this.componentContainer.getDispatcher());

  return (ResourceMethodInvoker) deployment.getRegistry().getResourceInvoker(httpRequest);
}
 
Example #6
Source File: LoggingInterceptor.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method)
        throws Failure, WebApplicationException {
    if (logger.isDebugEnabled()) {

        String httpMethod = request.getHttpMethod();

        URI uri = ui.getRequestUri();

        String uriPath = uri.getPath();
        if (uri.getQuery() != null) {
            uriPath += "?" + uri.getQuery();
        }
        if (uri.getFragment() != null) {
            uriPath += "#" + uri.getFragment();
        }

        String sessionid = null;
        List<String> headerSessionId = request.getHttpHeaders().getRequestHeader("sessionid");
        if (headerSessionId != null) {
            sessionid = headerSessionId.get(0);
        }
        if (logger.isDebugEnabled()) {
            // log only in debug mode
            logger.debug(sessionid + "|" + httpMethod + "|" + uriPath);
        }
    }
    return null;
}
 
Example #7
Source File: NotFoundExceptionMapper.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static List<ResourceDescription> fromBoundResourceInvokers(
        Set<Map.Entry<String, List<ResourceInvoker>>> bound) {
    Map<String, ResourceDescription> descriptions = new HashMap<>();

    for (Map.Entry<String, List<ResourceInvoker>> entry : bound) {
        for (ResourceInvoker invoker : entry.getValue()) {
            // skip those for now
            if (!(invoker instanceof ResourceMethodInvoker)) {
                continue;
            }
            ResourceMethodInvoker method = (ResourceMethodInvoker) invoker;
            Class<?> resourceClass = method.getResourceClass();
            String resourceClassName = resourceClass.getName();
            String basePath = null;
            NonJaxRsClassMappings nonJaxRsClassMappings = null;
            Path path = resourceClass.getAnnotation(Path.class);
            if (path == null) {
                nonJaxRsClassMappings = nonJaxRsClassNameToMethodPaths.get(resourceClassName);
                if (nonJaxRsClassMappings != null) {
                    basePath = nonJaxRsClassMappings.getBasePath();
                }
            } else {
                basePath = path.value();
            }

            if (basePath == null) {
                continue;
            }

            if (!descriptions.containsKey(basePath)) {
                descriptions.put(basePath, new ResourceDescription(basePath));
            }

            String subPath = "";
            for (Annotation annotation : method.getMethodAnnotations()) {
                if (annotation.annotationType().equals(Path.class)) {
                    subPath = ((Path) annotation).value();
                    break;
                }
            }
            // attempt to find a mapping in the non JAX-RS paths
            if (subPath.isEmpty() && (nonJaxRsClassMappings != null)) {
                String methodName = method.getMethod().getName();
                String subPathFromMethodName = nonJaxRsClassMappings.getMethodNameToPath().get(methodName);
                if (subPathFromMethodName != null) {
                    subPath = subPathFromMethodName;
                }
            }

            descriptions.get(basePath).addMethod(basePath + subPath, method);
        }
    }

    return new LinkedList<>(descriptions.values());
}
 
Example #8
Source File: SecurityInterceptor.java    From maven-framework-project with MIT License 4 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) {
	ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext
			.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
	Method method = methodInvoker.getMethod();
	// Access allowed for all
	if (!method.isAnnotationPresent(PermitAll.class)) {
		// Access denied for all
		if (method.isAnnotationPresent(DenyAll.class)) {
			requestContext.abortWith(ACCESS_FORBIDDEN);
			return;
		}

		// Get request headers
		final MultivaluedMap<String, String> headersMap = requestContext.getHeaders();

		// Fetch authorization header
		final List<String> authorization = headersMap.get(AUTHORIZATION_PROPERTY);

		// If no authorization information present; block access
		if (authorization == null || authorization.isEmpty()) {
			requestContext.abortWith(ACCESS_DENIED);
			return;
		}

		// Get encoded username and password
		final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");

		// Decode username and password
		String usernameAndPassword = new String(Base64.decodeBase64(encodedUserPassword));

		// Split username and password tokens
		final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
		final String username = tokenizer.nextToken();
		final String password = tokenizer.nextToken();

		// Verify user access
		if (method.isAnnotationPresent(RolesAllowed.class)) {
			RolesAllowed rolesAnnotation = method.getAnnotation(RolesAllowed.class);
			Set<String> rolesSet = new HashSet<String>(Arrays.asList(rolesAnnotation.value()));

			// Is user valid?
			if (!isUserAllowed(username, password, rolesSet)) {
				requestContext.abortWith(ACCESS_DENIED);
				return;
			}
		}
	}
}
 
Example #9
Source File: SecurityFilter.java    From maven-framework-project with MIT License 4 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) {
	ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext
			.getProperty(RESOURCE_METHOD_INVOKER);
	Method method = methodInvoker.getMethod();
	// Access allowed for all
	if (!method.isAnnotationPresent(PermitAll.class)) {
		// Access denied for all
		if (method.isAnnotationPresent(DenyAll.class)) {
			requestContext.abortWith(ACCESS_FORBIDDEN);
			return;
		}

		// Get request headers
		final MultivaluedMap<String, String> headersMap = requestContext.getHeaders();

		// Fetch authorization header
		final List<String> authorizationList = headersMap.get(AUTHORIZATION_PROPERTY);

		// If no authorization information present; block access
		if (authorizationList == null || authorizationList.isEmpty()) {
			requestContext.abortWith(ACCESS_DENIED);
			return;
		}

		// Get encoded username and password
		final String encodedUserPassword = authorizationList.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");

		// Decode username and password
		String usernameAndPassword = new String(Base64.decodeBase64(encodedUserPassword));

		// Split username and password tokens
		final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
		final String userName = tokenizer.nextToken();
		final String password = tokenizer.nextToken();

		// Verify user access
		if (method.isAnnotationPresent(RolesAllowed.class)) {
			RolesAllowed rolesAnnotation = method.getAnnotation(RolesAllowed.class);
			Set<String> rolesSet = new HashSet<String>(Arrays.asList(rolesAnnotation.value()));

			// Is user valid?
			if (!isUserAllowed(userName, password, rolesSet)) {
				requestContext.abortWith(ACCESS_DENIED);
				return;
			}
		}
	}
}