javax.ws.rs.ext.ContextResolver Java Examples
The following examples show how to use
javax.ws.rs.ext.ContextResolver.
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: JAXRSUtilsTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testContextResolverFields() throws Exception { ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true); cri.setResourceProvider(new PerRequestResourceProvider(Customer.class)); OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[]{}), cri); Message m = createMessage(); HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class); m.put(AbstractHTTPDestination.HTTP_RESPONSE, response); Customer c = new Customer(); ContextResolver<JAXBContext> cr = new JAXBContextProvider(); ProviderFactory.getInstance(m).registerUserProvider(cr); m.put(Message.BASE_PATH, "/"); InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m); assertSame(cr.getClass(), c.getContextResolver().getClass()); }
Example #2
Source File: ProviderFactory.java From cxf with Apache License 2.0 | 6 votes |
public <T> ContextResolver<T> createContextResolver(Type contextType, Message m) { boolean isRequestor = MessageUtils.isRequestor(m); Message requestMessage = isRequestor ? m.getExchange().getOutMessage() : m.getExchange().getInMessage(); Message responseMessage = isRequestor ? m.getExchange().getInMessage() : m.getExchange().getOutMessage(); Object ctProperty = null; if (responseMessage != null) { ctProperty = responseMessage.get(Message.CONTENT_TYPE); } else { ctProperty = requestMessage.get(Message.CONTENT_TYPE); } MediaType mt = ctProperty != null ? JAXRSUtils.toMediaType(ctProperty.toString()) : MediaType.WILDCARD_TYPE; return createContextResolver(contextType, m, mt); }
Example #3
Source File: ProviderBinderTest.java From everrest with Eclipse Public License 2.0 | 6 votes |
@Test @UseDataProvider("contextResolverByClassAndMediaTypeData") public void retrievesContextResolverByClassAndMediaType(boolean singletonOrPerRequest, Class<?> aClass, MediaType mediaType, Object expectedContextResolverClassOrInstance) throws Exception { if (singletonOrPerRequest == SINGLETON) { registerSingletonContextResolvers(); } else { registerPerRequestContextResolvers(); } ContextResolver<?> contextResolver = providers.getContextResolver(aClass, mediaType); if (singletonOrPerRequest == SINGLETON) { assertSame(expectedContextResolverClassOrInstance, contextResolver); } else { if (expectedContextResolverClassOrInstance == null) { assertNull(contextResolver); } else { assertNotNull(contextResolver); assertEquals(expectedContextResolverClassOrInstance, contextResolver.getClass()); } } }
Example #4
Source File: Contexts.java From tomee with Apache License 2.0 | 6 votes |
private static Set<Class<?>> contextClasses() { final Set<Class<?>> classes = new HashSet<>(); classes.add(UriInfo.class); classes.add(SecurityContext.class); classes.add(HttpHeaders.class); classes.add(ContextResolver.class); classes.add(Providers.class); classes.add(Request.class); /* TODO: when we have jaxrs 2 classes.add(ResourceInfo.class); classes.add(ResourceContext.class); */ classes.add(Application.class); classes.add(HttpServletRequest.class); classes.add(HttpServletResponse.class); classes.add(ServletConfig.class); classes.add(ServletContext.class); classes.add(MessageContext.class); return classes; }
Example #5
Source File: AbstractJAXBProvider.java From cxf with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public JAXBContext getJAXBContext(Class<?> type, Type genericType) throws JAXBException { if (mc != null) { ContextResolver<JAXBContext> resolver = mc.getResolver(ContextResolver.class, JAXBContext.class); if (resolver != null) { JAXBContext customContext = resolver.getContext(type); if (customContext != null) { return customContext; } } } JAXBContext context = classContexts.get(type); if (context != null) { return context; } context = getPackageContext(type, genericType); return context != null ? context : getClassContext(type, genericType); }
Example #6
Source File: MessageContextImplTest.java From cxf with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Test public void testContextResolver() { ContextResolver<JAXBContext> resolver = new CustomContextResolver(); ProviderFactory factory = ServerProviderFactory.getInstance(); factory.registerUserProvider(resolver); Message m = new MessageImpl(); Exchange ex = new ExchangeImpl(); m.setExchange(ex); ex.setInMessage(m); Endpoint e = EasyMock.createMock(Endpoint.class); EasyMock.expect(e.get(ServerProviderFactory.class.getName())).andReturn(factory); EasyMock.replay(e); ex.put(Endpoint.class, e); MessageContext mc = new MessageContextImpl(m); ContextResolver<JAXBContext> resolver2 = mc.getResolver(ContextResolver.class, JAXBContext.class); assertNotNull(resolver2); assertSame(resolver2, resolver); }
Example #7
Source File: JAXRSUtilsTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testContextResolverParam() throws Exception { ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true); OperationResourceInfo ori = new OperationResourceInfo( Customer.class.getMethod("testContextResolvers", new Class[]{ContextResolver.class}), cri); ori.setHttpMethod("GET"); Message m = createMessage(); ContextResolver<JAXBContext> cr = new JAXBContextProvider(); ProviderFactory.getInstance(m).registerUserProvider(cr); m.put(Message.BASE_PATH, "/"); List<Object> params = JAXRSUtils.processParameters(ori, new MetadataMap<String, String>(), m); assertEquals("1 parameters expected", 1, params.size()); assertSame(cr.getClass(), params.get(0).getClass()); }
Example #8
Source File: ProviderFactoryTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testCustomResolverProxy() throws Exception { ProviderFactory pf = ServerProviderFactory.getInstance(); pf.registerUserProvider(new JAXBContextProvider()); pf.registerUserProvider(new JAXBContextProvider2()); Message message = prepareMessage("text/xml+*", null); ContextResolver<JAXBContext> cr = pf.createContextResolver(JAXBContext.class, message); assertTrue(cr instanceof ProviderFactory.ContextResolverProxy); assertTrue(((ProviderFactory.ContextResolverProxy<?>)cr).getResolvers().get(0) instanceof JAXBContextProvider); assertTrue(((ProviderFactory.ContextResolverProxy<?>)cr).getResolvers().get(1) instanceof JAXBContextProvider2); }
Example #9
Source File: VersionResource.java From mrgeo with Apache License 2.0 | 5 votes |
private void getService() { if (service == null) { ContextResolver<VersionService> resolver = providers.getContextResolver(VersionService.class, MediaType.WILDCARD_TYPE); service = resolver.getContext(VersionService.class); } }
Example #10
Source File: ConstraintViolationExceptionMapper.java From microservices-springboot with MIT License | 5 votes |
/** * Handle an invalid property. Can be: * <p> * 1. Invalid request parameter (annotated bean param field or annotated resource class field) * 2. Invalid request entity property (annotated bean param field) * * @param constraintViolation */ private ValidationError handleInvalidProperty(ConstraintViolation constraintViolation) { Path.Node leafNode = getLeafNode(constraintViolation.getPropertyPath()).get(); Class<?> beanClass = constraintViolation.getLeafBean().getClass(); // Can be an invalid request parameter (annotated bean param field or annotated resource class field) Optional<Field> optionalField = getField(leafNode.getName(), beanClass); if (optionalField.isPresent()) { Optional<ParameterDetails> optionalParameterDetails = getParameterDetails(optionalField.get().getAnnotations()); if (optionalParameterDetails.isPresent()) { return createErrorForParameter(optionalParameterDetails.get(), constraintViolation); } } // Get Jackson ObjectMapper ContextResolver<ObjectMapper> resolver = providers.getContextResolver(ObjectMapper.class, MediaType.WILDCARD_TYPE); ObjectMapper mapper = resolver.getContext(ObjectMapper.class); // Can be an invalid request entity property (annotated bean param field) Optional<String> optionalJsonProperty = getJsonPropertyName(mapper, beanClass, leafNode.getName()); if (optionalJsonProperty.isPresent()) { ValidationError error = new ValidationError(); error.setType(REQUEST_ENTITY_PROPERTY); error.setName(optionalJsonProperty.get()); error.setMessage(constraintViolation.getMessage()); return error; } return handleUnknownSource(constraintViolation); }
Example #11
Source File: RESTService.java From tomee with Apache License 2.0 | 5 votes |
private static <T> boolean isProvider(final Class<T> clazz) { return MessageBodyReader.class.isAssignableFrom(clazz) || MessageBodyWriter.class.isAssignableFrom(clazz) || ParamConverter.class.isAssignableFrom(clazz) || ContainerRequestFilter.class.isAssignableFrom(clazz) || ContainerResponseFilter.class.isAssignableFrom(clazz) || ReaderInterceptor.class.isAssignableFrom(clazz) || WriterInterceptor.class.isAssignableFrom(clazz) || ParamConverterProvider.class.isAssignableFrom(clazz) || ContextResolver.class.isAssignableFrom(clazz) || Feature.class.isAssignableFrom(clazz) || new MetaAnnotatedClass<>(clazz).isAnnotationPresent(Provider.class); }
Example #12
Source File: EverrestJetty.java From everrest with Eclipse Public License 2.0 | 5 votes |
private boolean isRestResource(Class<?> resourceClass) { return resourceClass.isAnnotationPresent(Path.class) || resourceClass.isAnnotationPresent(Provider.class) || resourceClass.isAnnotationPresent(Filter.class) || resourceClass.isAssignableFrom(ExceptionMapper.class) || resourceClass.isAssignableFrom(ContextResolver.class) || resourceClass.isAssignableFrom(MessageBodyReader.class) || resourceClass.isAssignableFrom(MessageBodyWriter.class) || resourceClass.isAssignableFrom(MethodInvokerFilter.class) || resourceClass.isAssignableFrom(RequestFilter.class) || resourceClass.isAssignableFrom(ResponseFilter.class); }
Example #13
Source File: ApplicationPublisherTest.java From everrest with Eclipse Public License 2.0 | 5 votes |
@Test public void publishesSingletonContextResolver() { ContextResolver contextResolver = new ContextResolverText(); Application application = mock(Application.class); when(application.getSingletons()).thenReturn(newHashSet(contextResolver)); publisher.publish(application); verify(providers).addContextResolver(contextResolver); }
Example #14
Source File: ProviderFactoryTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testNoCustomResolver() throws Exception { ProviderFactory pf = ServerProviderFactory.getInstance(); pf.registerUserProvider(new JAXBContextProvider()); pf.registerUserProvider(new JAXBContextProvider2()); Message message = prepareMessage("text/xml+c", null); ContextResolver<JAXBContext> cr = pf.createContextResolver(JAXBContext.class, message); assertNull(cr); }
Example #15
Source File: MattermostModelMapperProviderTest.java From mattermost4j with Apache License 2.0 | 5 votes |
@Test public void throwExceptionStrict() { ContextResolver<ObjectMapper> provider = new MattermostModelMapperProvider(false); ObjectMapper objectMapper = provider.getContext(ObjectMapper.class); assertThrows(JsonMappingException.class, () -> objectMapper.readValue(JSON_INCLUDE_UNKNOWN_PROPERTY, User.class)); }
Example #16
Source File: ProviderBinder.java From everrest with Eclipse Public License 2.0 | 5 votes |
public void addContextResolver(ObjectFactory<ProviderDescriptor> contextResolverFactory) { for (Type type : contextResolverFactory.getObjectModel().getObjectClass().getGenericInterfaces()) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType)type; if (ContextResolver.class == parameterizedType.getRawType()) { Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length != 1) { throw new IllegalArgumentException("Unable strong determine actual type argument"); } Class<?> aClass = (Class<?>)typeArguments[0]; NavigableMap<MediaType, ObjectFactory<ProviderDescriptor>> contextResolversForType = contextResolvers.get(aClass); if (contextResolversForType == null) { NavigableMap<MediaType, ObjectFactory<ProviderDescriptor>> newContextResolversForType = new ConcurrentSkipListMap<>(mediaTypeComparator); contextResolversForType = contextResolvers.putIfAbsent(aClass, newContextResolversForType); if (contextResolversForType == null) { contextResolversForType = newContextResolversForType; } } for (MediaType mediaType : contextResolverFactory.getObjectModel().produces()) { if (contextResolversForType.putIfAbsent(mediaType, contextResolverFactory) != null) { throw new DuplicateProviderException( String.format("ContextResolver for %s and media type %s already registered", aClass.getName(), mediaType)); } } } } } }
Example #17
Source File: ProviderFactoryTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testRegisterCustomResolver() throws Exception { ProviderFactory pf = ServerProviderFactory.getInstance(); pf.registerUserProvider(new JAXBContextProvider()); Message message = prepareMessage("*/*", null); ContextResolver<JAXBContext> cr = pf.createContextResolver(JAXBContext.class, message); assertFalse(cr instanceof ProviderFactory.ContextResolverProxy); assertTrue("JAXBContext ContextProvider can not be found", cr instanceof JAXBContextProvider); }
Example #18
Source File: ThreadLocalContextManager.java From tomee with Apache License 2.0 | 5 votes |
public static Object findThreadLocal(final Class<?> type) { if (Request.class.equals(type)) { return REQUEST; } else if (UriInfo.class.equals(type)) { return URI_INFO; } else if (HttpHeaders.class.equals(type)) { return HTTP_HEADERS; } else if (SecurityContext.class.equals(type)) { return SECURITY_CONTEXT; } else if (ContextResolver.class.equals(type)) { return CONTEXT_RESOLVER; } else if (Providers.class.equals(type)) { return PROVIDERS; } else if (ServletRequest.class.equals(type)) { return SERVLET_REQUEST; } else if (HttpServletRequest.class.equals(type)) { return HTTP_SERVLET_REQUEST; } else if (HttpServletResponse.class.equals(type)) { return HTTP_SERVLET_RESPONSE; } else if (ServletConfig.class.equals(type)) { return SERVLET_CONFIG; } else if (ServletContext.class.equals(type)) { return SERVLET_CONTEXT; } else if (ResourceInfo.class.equals(type)) { return RESOURCE_INFO; } else if (ResourceContext.class.equals(type)) { return RESOURCE_CONTEXT; } else if (Application.class.equals(type)) { return APPLICATION; } else if (Configuration.class.equals(type)) { return CONFIGURATION; } return null; }
Example #19
Source File: ProviderBinder.java From everrest with Eclipse Public License 2.0 | 5 votes |
/** * Add singleton ContextResolver. * * @param contextResolver * ContextResolver instance */ public void addContextResolver(ContextResolver contextResolver) { try { ProviderDescriptor descriptor = new ProviderDescriptorImpl(contextResolver); addContextResolver(new SingletonObjectFactory<>(descriptor, contextResolver)); } catch (Exception e) { LOG.error(String.format("Failed add ContextResolver %s. %s", contextResolver.getClass().getName(), e.getMessage()), e); } }
Example #20
Source File: ProviderBinder.java From everrest with Eclipse Public License 2.0 | 5 votes |
/** * Add per-request ContextResolver. * * @param contextResolverClass * class of implementation ContextResolver */ public void addContextResolver(Class<? extends ContextResolver> contextResolverClass) { try { ProviderDescriptor descriptor = new ProviderDescriptorImpl(contextResolverClass); addContextResolver(new PerRequestObjectFactory<>(descriptor)); } catch (Exception e) { LOG.error(String.format("Failed add ContextResolver %s. %s", contextResolverClass.getName(), e.getMessage()), e); } }
Example #21
Source File: ApplicationContext.java From everrest with Eclipse Public License 2.0 | 5 votes |
/** * @param methodDescriptor * method descriptor * @return invoker that must be used for processing methods */ public MethodInvoker getMethodInvoker(GenericResourceMethod methodDescriptor) { String method = request.getMethod(); if ("OPTIONS".equals(method) && methodDescriptor.getMethod() == null) { // GenericMethodResource.getMethod() always return null if method for // "OPTIONS" request was not described in source code of service. In // this case we provide mechanism for "fake" method invoking. return new OptionsRequestMethodInvoker(new WadlProcessor()); } MethodInvoker invoker = null; // Never use AsynchronousMethodInvoker for process SubResourceLocatorDescriptor. // Locators can't be processed in asynchronous mode since it is not end point of request. if (isAsynchronous() && methodDescriptor instanceof ResourceMethodDescriptor) { ContextResolver<AsynchronousJobPool> asyncJobsResolver = getProviders().getContextResolver(AsynchronousJobPool.class, null); if (asyncJobsResolver == null) { throw new IllegalStateException("Asynchronous jobs feature is not configured properly. "); } invoker = new AsynchronousMethodInvoker(asyncJobsResolver.getContext(null), new ParameterResolverFactory()); } if (invoker == null) { invoker = new DefaultMethodInvoker(new ParameterResolverFactory()); } if (methodInvokerDecoratorFactory != null) { return methodInvokerDecoratorFactory.makeDecorator(invoker); } return invoker; }
Example #22
Source File: JAXRSUtils.java From cxf with Apache License 2.0 | 5 votes |
public static ContextResolver<?> createContextResolver(Type genericType, Message m) { if (genericType instanceof ParameterizedType) { return ProviderFactory.getInstance(m).createContextResolver( ((ParameterizedType)genericType).getActualTypeArguments()[0], m); } else if (m != null) { return ProviderFactory.getInstance(m).createContextResolver(genericType, m); } else { return null; } }
Example #23
Source File: ProviderBinder.java From everrest with Eclipse Public License 2.0 | 5 votes |
protected <T> ContextResolver<T> doGetContextResolver(Class<T> contextType, MediaType mediaType) { NavigableMap<MediaType, ObjectFactory<ProviderDescriptor>> mediaTypeToContextResolverMap = contextResolvers.get(contextType); ContextResolver<T> contextResolver = null; if (mediaTypeToContextResolverMap != null) { Iterator<MediaType> mediaTypeRange = MediaTypeHelper.createDescendingMediaTypeIterator(mediaType); while (mediaTypeRange.hasNext() && contextResolver == null) { MediaType actual = mediaTypeRange.next(); contextResolver = doGetContextResolver(mediaTypeToContextResolverMap, actual); } } return contextResolver; }
Example #24
Source File: ProviderFactory.java From cxf with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> ContextResolver<T> createContextResolver(Type contextType, Message m, MediaType type) { Class<?> contextCls = InjectionUtils.getActualType(contextType); if (contextCls == null) { return null; } List<ContextResolver<T>> candidates = new LinkedList<>(); for (ProviderInfo<ContextResolver<?>> cr : contextResolvers) { Type[] types = cr.getProvider().getClass().getGenericInterfaces(); for (Type t : types) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType)t; Type[] args = pt.getActualTypeArguments(); if (args.length > 0) { Class<?> argCls = InjectionUtils.getActualType(args[0]); if (argCls != null && argCls.isAssignableFrom(contextCls)) { List<MediaType> mTypes = JAXRSUtils.getProduceTypes( cr.getProvider().getClass().getAnnotation(Produces.class)); if (JAXRSUtils.doMimeTypesIntersect(mTypes, type)) { injectContextValues(cr, m); candidates.add((ContextResolver<T>)cr.getProvider()); } } } } } } if (candidates.isEmpty()) { return null; } else if (candidates.size() == 1) { return candidates.get(0); } else { Collections.sort(candidates, new PriorityBasedClassComparator()); return new ContextResolverProxy<T>(candidates); } }
Example #25
Source File: TileMapServiceResource.java From mrgeo with Apache License 2.0 | 5 votes |
private void getService() { if (service == null) { ContextResolver<TmsService> resolver = providers.getContextResolver(TmsService.class, MediaType.WILDCARD_TYPE); if (resolver != null) { service = resolver.getContext(TmsService.class); } } }
Example #26
Source File: ColorScaleResource.java From mrgeo with Apache License 2.0 | 5 votes |
private void getService() { if (service == null) { ContextResolver<MrsPyramidService> resolver = providers.getContextResolver(MrsPyramidService.class, MediaType.WILDCARD_TYPE); if (resolver != null) { service = resolver.getContext(MrsPyramidService.class); } } }
Example #27
Source File: ProviderFactory.java From cxf with Apache License 2.0 | 5 votes |
public int compare(ProviderInfo<ContextResolver<?>> p1, ProviderInfo<ContextResolver<?>> p2) { ContextResolver<?> e1 = p1.getProvider(); ContextResolver<?> e2 = p2.getProvider(); List<MediaType> types1 = JAXRSUtils.sortMediaTypes(JAXRSUtils.getProduceTypes( e1.getClass().getAnnotation(Produces.class)), JAXRSUtils.MEDIA_TYPE_QS_PARAM); List<MediaType> types2 = JAXRSUtils.sortMediaTypes(JAXRSUtils.getProduceTypes( e2.getClass().getAnnotation(Produces.class)), JAXRSUtils.MEDIA_TYPE_QS_PARAM); return JAXRSUtils.compareSortedMediaTypes(types1, types2, JAXRSUtils.MEDIA_TYPE_QS_PARAM); }
Example #28
Source File: MetadataResource.java From mrgeo with Apache License 2.0 | 5 votes |
private void getService() { if (service == null) { ContextResolver<MrsPyramidService> resolver = providers.getContextResolver(MrsPyramidService.class, MediaType.WILDCARD_TYPE); if (resolver != null) { service = resolver.getContext(MrsPyramidService.class); } } }
Example #29
Source File: ProviderFactory.java From cxf with Apache License 2.0 | 5 votes |
public T getContext(Class<?> cls) { for (ContextResolver<T> resolver : candidates) { T context = resolver.getContext(cls); if (context != null) { return context; } } return null; }
Example #30
Source File: RasterResource.java From mrgeo with Apache License 2.0 | 5 votes |
private void getService() { if (service == null) { ContextResolver<MrsPyramidService> resolver = providers.getContextResolver(MrsPyramidService.class, MediaType.WILDCARD_TYPE); if (resolver != null) { service = resolver.getContext(MrsPyramidService.class); } } }