Java Code Examples for org.glassfish.jersey.server.model.Parameter

The following examples show how to use org.glassfish.jersey.server.model.Parameter. These examples are extracted from open source projects. 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
public Function<ContainerRequest, Token> getValueProvider(final Parameter parameter) {
    return request -> {
        if (parameter.getRawType().equals(Token.class) && parameter.isAnnotationPresent(FernetToken.class)) {
            final Token xAuthorizationToken = getTokenHeaderUtility().getXAuthorizationToken(request);
            if (xAuthorizationToken != null) {
                return xAuthorizationToken;
            }
            final Token authorizationToken = getTokenHeaderUtility().getAuthorizationToken(request);
            if (authorizationToken != null) {
                return authorizationToken;
            }
            throw new NotAuthorizedException("Bearer error=\"invalid_token\", error_description=\"no token found in Authorization or X-Authorization header\"");
        }
        throw new IllegalStateException("misconfigured annotation");
    };
}
 
Example 2
public Function<ContainerRequest, T> getValueProvider(final Parameter parameter) {
    return request -> {
        if (parameter.isAnnotationPresent(FernetSecret.class)) {
            final Collection<? extends Key> keys = getKeySupplier().get();
            final Token xAuthorizationToken = getHeaderUtility().getXAuthorizationToken(request);
            if (xAuthorizationToken != null) {
                return getValidator().validateAndDecrypt(keys, xAuthorizationToken);
            }
            final Token authorizationToken = getHeaderUtility().getAuthorizationToken(request);
            if (authorizationToken != null) {
                return getValidator().validateAndDecrypt(keys, authorizationToken);
            }
            throw new NotAuthorizedException("Bearer error=\"invalid_token\", error_description=\"no token found in Authorization or X-Authorization header\"");
        }
        throw new IllegalStateException("misconfigured annotation");
    };
}
 
Example 3
private static HttpExecutionStrategy computeDefaultExecutionStrategy(final Invocable invocable) {
    final Parameter entityParam = invocable.getParameters().stream().filter(p -> p.getSource() == ENTITY)
            .findFirst()
            .orElse(null);

    final boolean consumesStreaming = entityParam != null &&
            Publisher.class.isAssignableFrom(entityParam.getRawType());
    final boolean consumesAsync = consumesStreaming ||
            (entityParam != null && Single.class.isAssignableFrom(entityParam.getRawType()));
    final boolean producesStreaming = Publisher.class.isAssignableFrom(invocable.getRawResponseType());
    final boolean producesAsync = producesStreaming ||
            Single.class.isAssignableFrom(invocable.getRawResponseType()) ||
            Completable.class.isAssignableFrom(invocable.getRawResponseType());

    if (!consumesAsync && !producesAsync) {
        // blocking-aggregated
        // HttpApiConversions/BlockingToStreamingService uses OFFLOAD_RECEIVE_DATA_STRATEGY because we are invoking
        // the service within a callback of Single<HttpRequest>, which may call the service on the eventloop.
        // Here we are not invoking the service as part of collecting the request payload,
        // so OFFLOAD_RECEIVE_META is sufficient.
        return OFFLOAD_RECEIVE_META;
    } else if (consumesAsync && !consumesStreaming && producesAsync && !producesStreaming) {
        // async-aggregated
        return OFFLOAD_RECEIVE_META_AND_SEND;
    } else {
        // default to async-streaming, as it has the most aggressive offloading strategy
        return OFFLOAD_ALL;
    }
}
 
Example 4
Source Project: ratelimitj   Source File: RateLimitBundle.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public RateLimitingFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider,
                                   final ServiceLocator injector,
                                   final RateLimiterFactoryProvider rateLimiterFactoryProvider) {
    super(extractorProvider, injector, Parameter.Source.UNKNOWN);
    this.requestRateLimiterFactory = rateLimiterFactoryProvider.factory;
}
 
Example 5
Source Project: ratelimitj   Source File: RateLimitBundle.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<RequestRateLimiterFactory> createValueFactory(final Parameter parameter) {
    final RateLimiting annotation = parameter.getAnnotation(RateLimiting.class);
    if (null == annotation) {
        return null;
    } else {
        return new AbstractContainerRequestValueFactory<RequestRateLimiterFactory>() {
            @Override
            public RequestRateLimiterFactory provide() {
                return requestRateLimiterFactory;
            }
        };
    }
}
 
Example 6
Source Project: ratelimitj   Source File: RateLimit429EnforcerFilterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(final Parameter parameter) {
    final RateLimiting annotation = parameter.getAnnotation(RateLimiting.class);
    if (null == annotation) {
        return null;
    } else {
        return new AbstractContainerRequestValueFactory<RequestRateLimiterFactory>() {
            @Override
            public RequestRateLimiterFactory provide() {
                return requestRateLimiterFactory;
            }
        };
    }
}
 
Example 7
Source Project: jax-rs-pac4j   Source File: Pac4JValueFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
protected Pac4JProfileValueFactoryProvider(ProfileManagerFactoryBuilder manager,
        OptionalProfileFactoryBuilder opt, ProfileFactoryBuilder profile,
        MultivaluedParameterExtractorProvider mpep, ServiceLocator locator) {
    super(mpep, locator, Parameter.Source.UNKNOWN);
    this.manager = manager;
    this.optProfile = opt;
    this.profile = profile;
}
 
Example 8
Source Project: jax-rs-pac4j   Source File: Pac4JValueFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
                + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
                "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example 9
Source Project: jax-rs-pac4j   Source File: Pac4JValueFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
protected Pac4JProfileValueFactoryProvider(
    ProfileManagerFactoryBuilder manager,
    OptionalProfileFactoryBuilder opt,
    ProfileFactoryBuilder profile,
    Provider<MultivaluedParameterExtractorProvider> mpep
) {
    super(mpep, Parameter.Source.UNKNOWN);
    this.manager = manager;
    this.optProfile = opt;
    this.profile = profile;
}
 
Example 10
Source Project: jax-rs-pac4j   Source File: Pac4JValueFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
            + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
            "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example 11
Source Project: jax-rs-pac4j   Source File: Pac4JValueFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
protected Pac4JProfileValueFactoryProvider(
    ProfileManagerFactoryBuilder manager,
    OptionalProfileFactoryBuilder opt,
    ProfileFactoryBuilder profile,
    Provider<MultivaluedParameterExtractorProvider> mpep
) {
    super(mpep, org.glassfish.jersey.model.Parameter.Source.UNKNOWN);
    this.manager = manager;
    this.optProfile = opt;
    this.profile = profile;
}
 
Example 12
Source Project: jax-rs-pac4j   Source File: Pac4JValueFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
            + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
            "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example 13
Source Project: keywhiz   Source File: AuthResolver.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public AuthValueFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider,
    final ServiceLocator injector, ClientAuthFactory clientAuthFactory,
    AutomationClientAuthFactory automationClientAuthFactory, UserAuthFactory userAuthFactory) {
  super(extractorProvider, injector, Parameter.Source.UNKNOWN);
  this.clientAuthFactory = clientAuthFactory;
  this.automationClientAuthFactory = automationClientAuthFactory;
  this.userAuthFactory = userAuthFactory;
}
 
Example 14
Source Project: keywhiz   Source File: AuthResolver.java    License: Apache License 2.0 5 votes vote down vote up
@Override protected Factory<?> createValueFactory(final Parameter parameter) {
  final Class<?> classType = parameter.getRawType();
  final Auth auth = parameter.getAnnotation(Auth.class);

  if (auth == null) {
    return null;
  }

  if (classType.equals(Client.class)) {
    return new AbstractContainerRequestValueFactory<Client>() {
      @Override public Client provide() {
        return clientAuthFactory.provide(getContainerRequest());
      }
    };
  }

  if (classType.equals(AutomationClient.class)) {
    return new AbstractContainerRequestValueFactory<AutomationClient>() {
      @Override public AutomationClient provide() {
        return automationClientAuthFactory.provide(getContainerRequest());
      }
    };
  }

  if (classType.equals(User.class)) {
    return new AbstractContainerRequestValueFactory<User>() {
      @Override public User provide() {
        return userAuthFactory.provide(getContainerRequest());
      }
    };
  }

  return null;
}
 
Example 15
@Override
public AbstractContainerRequestValueFactory<?> createValueFactory(final Parameter parameter) {
  if (parameter.getAnnotation(Auth.class) == null) {
    return null;
  }

  if (parameter.getRawType() == Optional.class) {
    return new OptionalContainerRequestValueFactory(parameter);
  } else {
    return new StandardContainerReqeustValueFactory(parameter);
  }
}
 
Example 16
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    Class<?> paramType = parameter.getRawType();
    SearchParam annotation = parameter.getAnnotation(SearchParam.class);
    if (annotation != null && paramType.isAssignableFrom(SearchModel.class)) {
        return new SearchFactory();
    }
    return null;
}
 
Example 17
private Parameter getParameter(String methodName, Class clazz) throws NoSuchMethodException {
    return Parameter.create(
            clazz,
            SampleResource.class,
            SampleResource.class.getDeclaredMethod(methodName, clazz),
            true
    ).get(0);
}
 
Example 18
@Test
public void createValueFactory() throws Exception {
    Parameter parameter = getParameter("test1", SearchModel.class);
    SearchFactoryProvider provider = Mockito.mock(SearchFactoryProvider.class);
    Mockito.when(provider.createValueFactory(parameter)).thenCallRealMethod();
    SearchFactory factory = (SearchFactory) provider.createValueFactory(parameter);
    assertNotNull(factory);
}
 
Example 19
@Test
public void createValueFactoryWithoutAnnotation() throws Exception {
    Parameter parameter = getParameter("test2", SearchModel.class);
    SearchFactoryProvider provider = Mockito.mock(SearchFactoryProvider.class);
    Mockito.when(provider.createValueFactory(parameter)).thenCallRealMethod();
    SearchFactory factory = (SearchFactory) provider.createValueFactory(parameter);
    assertNull(factory);
}
 
Example 20
@Test
public void createValueFactoryWrongType() throws Exception {
    Parameter parameter = getParameter("test3", String.class);
    SearchFactoryProvider provider = Mockito.mock(SearchFactoryProvider.class);
    Mockito.when(provider.createValueFactory(parameter)).thenCallRealMethod();
    SearchFactory factory = (SearchFactory) provider.createValueFactory(parameter);
    assertNull(factory);
}
 
Example 21
@Inject
public TokenFactoryProvider(
        final MultivaluedParameterExtractorProvider extractorProvider,
        ServiceLocator locator) {

    super(extractorProvider, locator, Parameter.Source.UNKNOWN);
}
 
Example 22
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    Class<?> paramType = parameter.getRawType();
    RobeAuth annotation = parameter.getAnnotation(RobeAuth.class);
    if (annotation != null && paramType.isAssignableFrom(BasicToken.class)) {
        return new TokenFactory<>(annotation.required());
    }
    return null;
}
 
Example 23
Source Project: shiro-jersey   Source File: TypeFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Factory<?> getValueFactory(Parameter parameter) {
    if (type.equals(parameter.getRawType()) && parameter.isAnnotationPresent(Auth.class)) {
        return this;
    }
    return null;
}
 
Example 24
protected Factory<? extends P> createValueFactory(final Parameter parameter) {
    final HmacAuth auth = parameter.getAnnotation(HmacAuth.class);
    if (auth != null) {
        // TODO introduce type checking
        return getFactory();
    }
    return null;
}
 
Example 25
Source Project: droptools   Source File: DSLContextValueParamProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Function<ContainerRequest, ?> getValueProvider(Parameter parameter) {
    final Class<?> classType = parameter.getRawType();
    final JooqInject jooqInjectParam = parameter.getAnnotation(JooqInject.class);

    return (classType == null || !classType.equals(DSLContext.class) || jooqInjectParam == null)
            ? null // return null when parameter not supported
            : (req) -> DSL.using(getConfiguration(jooqInjectParam.value()));
}
 
Example 26
Source Project: shiro-jersey   Source File: TypeFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Factory<?> getValueFactory(Parameter parameter) {
    if (type.equals(parameter.getRawType()) && parameter.isAnnotationPresent(Auth.class)) {
        return this;
    }
    return null;
}
 
Example 27
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
  Class<?> paramType = parameter.getRawType();
  Auth annotation = parameter.getAnnotation(Auth.class);
  if (annotation != null && paramType.isAssignableFrom(SingularityUser.class)) {
    return authFactory;
  }
  return null;
}
 
Example 28
Source Project: dropwizard-experiment   Source File: DummyAuth.java    License: MIT License 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    if (!parameter.isAnnotationPresent(Auth.class)) {
        return null;
    }

    return new AbstractContainerRequestValueFactory<Principal>() {
        public Principal provide() {
            return user;
        }
    };
}
 
Example 29
Source Project: commafeed   Source File: SessionHelperFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(final Parameter parameter) {
	final Class<?> classType = parameter.getRawType();

	Context context = parameter.getAnnotation(Context.class);
	if (context == null)
		return null;

	if (classType.isAssignableFrom(SessionHelper.class)) {
		return new SessionHelperFactory();
	} else {
		return null;
	}
}
 
Example 30
Source Project: commafeed   Source File: SecurityCheckFactoryProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(final Parameter parameter) {
	final Class<?> classType = parameter.getRawType();

	SecurityCheck securityCheck = parameter.getAnnotation(SecurityCheck.class);
	if (securityCheck == null)
		return null;

	if (classType.isAssignableFrom(User.class)) {
		return new SecurityCheckFactory(securityCheck.value(), securityCheck.apiKeyAllowed());
	} else {
		return null;
	}
}