@Override protected Factory<?> createValueFactory(Parameter parameter) { if (!parameter.isAnnotationPresent(Prop.class)) { return null; } String parameterName = parameter.getSourceName(); if (parameterName == null || parameterName.length() == 0) { // Invalid parameter name return null; } MultivaluedParameterExtractor e = get(parameter); if (e == null) { return null; } PropertyFactory factory = new PropertyFactory(e, parameter, i18nEnabled); serviceLocator.inject(factory); return factory; }
/** * Converts {@link Parameter} to {@link InputStream} or {@link List} of * {@link InputStream} by {@link Parameter} nature * * @param parameter * @return {@link Object} */ private Object getEntityStream(Parameter parameter) { Object stream; List<String> paramValues = uriParams.get(parameter.getSourceName()); String value; if (CollectionUtils.valid(paramValues)) { if (paramValues.size() == PARAM_VALIES_LENGTH) { value = CollectionUtils.getFirst(paramValues); stream = RequestUtils.textToStream(value); } else { stream = RequestUtils.textsToStreams(paramValues); } } else { stream = null; } return stream; }
/** * Reads from {@link InputStream} or {@link List} of {@link InputStream} in * case that parameters are multi valued * * @param stream * @param parameter * @throws IOException */ private void fillParamList(Object stream, Parameter parameter) throws IOException { InputStream entityStream; if (stream instanceof InputStream) { entityStream = ObjectUtils.cast(stream, InputStream.class); readFromStream(entityStream, parameter); } else if (stream instanceof List) { List<InputStream> streamsList = ObjectUtils.cast(stream); Iterator<InputStream> streams = streamsList.iterator(); while (streams.hasNext()) { entityStream = streams.next(); readFromStream(entityStream, parameter); } } }
/** * Extracts parameters from {@link ContainerRequestContext} instance * * @return {@link List}<Object> * @throws IOException */ public List<Object> extractParams() throws IOException { paramsList = new ArrayList<Object>(); check = check(); httpHeaders = request.getHeaders(); uriParams = extractParameters(request); Object stream; for (Parameter parameter : parameters) { reader = RequestUtils.getReader(workers, parameter, mediaType); stream = getEntityStream(request, parameter); if (ObjectUtils.notNull(stream)) { fillParamList(stream, parameter); } else { addNullParam(parameter); } } return paramsList; }
@Inject public RateLimitingFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider, final ServiceLocator injector, final RateLimiterFactoryProvider rateLimiterFactoryProvider) { super(extractorProvider, injector, Parameter.Source.UNKNOWN); this.requestRateLimiterFactory = rateLimiterFactoryProvider.factory; }
@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>() { public RequestRateLimiterFactory provide() { return requestRateLimiterFactory; } }; } }
@Override protected Factory<?> createValueFactory(final Parameter parameter) { final RateLimiting annotation = parameter.getAnnotation(RateLimiting.class); if (null == annotation) { return null; } else { return new AbstractContainerRequestValueFactory<RequestRateLimiterFactory>() { public RequestRateLimiterFactory provide() { return requestRateLimiterFactory; } }; } }
@Override protected Factory<?> createValueFactory(Parameter parameter) { LOGGER.debug("{}", parameter); // parameter.getSourceName() JsonParamValueFactory factory = new JsonParamValueFactory(parameter); getLocator().inject(factory); return factory; }
@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; }
@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; }
@Inject protected CustomAnnotationProvider( MultivaluedParameterExtractorProvider mpep, ServiceLocator locator) { super(mpep, locator, Parameter.Source.UNKNOWN); }
@Override protected Factory<String> createValueFactory(Parameter parameter) { return new Factory<String>() { @Override public String provide() { return "hello world"; } @Override public void dispose(String instance) { } }; }
@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; }
@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; }
@Inject protected ConfigurationFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider, final ServiceLocator injector, final ConfigurationFactoryInfo configInfo) { super(extractorProvider, injector, Parameter.Source.UNKNOWN); this.configuration = configInfo.configuration; this.dataSource = configInfo.dataSource; this.multiTenantConnectionProvider = configInfo.multiTenantConnectionProvider; }
@Override protected Factory<?> createValueFactory(Parameter parameter) { Class<?> classType = parameter.getRawType(); if (classType == null || (!classType.equals(Configuration.class))) { return null; } return new ConfigurationFactory(configuration, dataSource, multiTenantConnectionProvider, parameter.getAnnotation(JooqConfiguration.class)); }
@Override protected Factory<?> createValueFactory(Parameter parameter) { Class<?> classType = parameter.getRawType(); if (classType == null || (!classType.equals(UnverifiedCredentials.class))) { return null; } return new MyContextValueFactory(); }
@Override Object getCachedPropery(Parameter parameter) { Locale locale = ThreadLocalLocale.get(); Map<Locale, Object> props = cachedBundles.get(parameter); if (props == null) { props = new ConcurrentHashMap<Locale, Object>(); cachedBundles.put(parameter, props); return null; } else { return props.get(locale); } }
@Override void putCachedProperty(Parameter key, Object value) { Map<Locale, Object> props = cachedBundles.get(key); if (props == null) { props = new ConcurrentHashMap<Locale, Object>(); } Locale locale = ThreadLocalLocale.get(); if (locale == null) { locale = defaultLocale; } props.put(locale, value); cachedBundles.put(key, props); }
@Inject public PropertiesValueFactoryProvider(MultivaluedParameterExtractorProvider mpep, ServiceLocator locator, Configuration configuration) { super(mpep, locator, Parameter.Source.UNKNOWN); this.serviceLocator = locator; this.i18nEnabled = PropertiesHelper.getValue(configuration.getProperties(), RuntimeType.SERVER, JerseyPropertiesFeature.ENABLE_I18N, false, null); }
@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; }
private Parameter getParameter(String methodName, Class clazz) throws NoSuchMethodException { return Parameter.create( clazz, SampleResource.class, SampleResource.class.getDeclaredMethod(methodName, clazz), true ).get(0); }
@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); }
@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); }
@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); }
@Inject public TokenFactoryProvider( final MultivaluedParameterExtractorProvider extractorProvider, ServiceLocator locator) { super(extractorProvider, locator, Parameter.Source.UNKNOWN); }
@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; }
@Override public Factory<?> getValueFactory(Parameter parameter) { if (type.equals(parameter.getRawType()) && parameter.isAnnotationPresent(Auth.class)) { return this; } return null; }
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; }
/** * Extract default value of a parameter from the parameter annotations of the handler method * * @param paramName value of {@link org.glassfish.jersey.media.multipart.FormDataParam} annotation on the handler * method. Which will be the name of the argument in the request parameters * @return value of {@link javax.ws.rs.DefaultValue} annotation on the handler method */ public String getDefaultValueForParam(final String paramName) { for (Parameter param : resourceMethod.getInvocable().getParameters()) { if (param.getSourceName().equals(paramName)) { return param.getDefaultValue(); } } return null; }
@Override public Factory<?> getValueFactory(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 : new DSLContextFactory(getConfiguration(configurationMap, jooqInjectParam.value()) ); }
@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; }
@Override protected Factory<?> createValueFactory(Parameter parameter) { if (!parameter.isAnnotationPresent(Auth.class)) { return null; } return new AbstractContainerRequestValueFactory<Principal>() { public Principal provide() { return user; } }; }
/** * Defines method for {@link Resource} to build * * @param builder * @param method * @param metaData */ private static void addMethod(Resource.Builder builder, ResourceMethod method, MetaData metaData) { List<MediaType> consumedTypes = method.getConsumedTypes(); List<MediaType> producedTypes = method.getProducedTypes(); Invocable invocable = method.getInvocable(); Method realMethod = invocable.getHandlingMethod(); List<Parameter> parameters = invocable.getParameters(); // Defines media type MediaType type; if (CollectionUtils.valid(consumedTypes)) { type = CollectionUtils.getFirst(consumedTypes); } else { type = null; } // REST Inflector to define bean methods Inflector<ContainerRequestContext, Response> inflector = new RestInflector( realMethod, metaData, type, parameters); // Builds new method for resource ResourceMethod.Builder methodBuilder = builder.addMethod(method .getHttpMethod()); methodBuilder.consumes(consumedTypes); methodBuilder.produces(producedTypes); methodBuilder.nameBindings(method.getNameBindings()); methodBuilder.handledBy(inflector); methodBuilder.build(); }
/** * Defines method for {@link Resource} to build * * @param builder * @param method * @param metaData */ private static void addMethod(Resource.Builder builder, ResourceMethod method, MetaData metaData) { List<MediaType> consumedTypes = method.getConsumedTypes(); List<MediaType> producedTypes = method.getProducedTypes(); Invocable invocable = method.getInvocable(); Method realMethod = invocable.getHandlingMethod(); List<Parameter> parameters = invocable.getParameters(); // Defines media type MediaType type; if (CollectionUtils.valid(consumedTypes)) { type = CollectionUtils.getFirst(consumedTypes); } else { type = null; } // Implementation of framework defined "Inflector" interface to define // bean // methods Inflector<ContainerRequestContext, Response> inflector = new RestInflector(realMethod, metaData, type, parameters); // Builds new method for resource ResourceMethod.Builder methodBuilder = builder.addMethod(method.getHttpMethod()); methodBuilder.consumes(consumedTypes); methodBuilder.produces(producedTypes); methodBuilder.nameBindings(method.getNameBindings()); methodBuilder.handledBy(inflector); methodBuilder.build(); }
public ParamBuilder(MediaType mediaType, List<Parameter> parameters, MessageBodyWorkers workers, ContainerRequestContext request) { this.mediaType = mediaType; this.parameters = parameters; this.workers = workers; this.request = request; }
/** * Reads {@link InputStream} from passed {@link ContainerRequestContext} for * associated {@link Parameter} instance * * @param request * @param parameter * @return {@link Object} */ private Object getEntityStream(ContainerRequestContext request, Parameter parameter) { Object entityStream; if (check) { entityStream = getEntityStream(parameter); } else { entityStream = request.getEntityStream(); } return entityStream; }
/** * Extracts parameter from passed {@link InputStream} (writes appropriate * value to {@link Object} instance) * * @param parameter * @param entityStream * @return {@link Object} * @throws IOException */ @SuppressWarnings("unchecked") private Object extractParam(Parameter parameter, InputStream entityStream) throws IOException { Object param; try { param = reader.readFrom(ObjectUtils.cast(parameter.getRawType(), Class.class), parameter.getType(), parameter.getAnnotations(), mediaType, httpHeaders, entityStream); } finally { close(entityStream); } return param; }
/** * Initializes and adds null parameter to {@link List} of parameters * * @param parameter */ private void addNullParam(Parameter parameter) { Class<?> paramType = parameter.getRawType(); Object nullParam; if (paramType.isPrimitive()) { nullParam = ClassUtils.getDefault(paramType); } else { nullParam = null; } paramsList.add(nullParam); }