Search in sources :

Example 1 with ContextResolver

use of javax.ws.rs.ext.ContextResolver in project jersey by jersey.

the class JaxbStringReaderProviderTest method stringReaderDoesNotReadExternalDtds.

@Test
public void stringReaderDoesNotReadExternalDtds() {
    Provider<SAXParserFactory> saxParserFactoryProvider = new Provider<SAXParserFactory>() {

        final SaxParserFactoryInjectionProvider spf = new SaxParserFactoryInjectionProvider(new CommonConfig(RuntimeType.SERVER, ComponentBag.INCLUDE_ALL));

        @Override
        public SAXParserFactory get() {
            return spf.get();
        }
    };
    JaxbStringReaderProvider.RootElementProvider provider = new JaxbStringReaderProvider.RootElementProvider(saxParserFactoryProvider, new Providers() {

        @Override
        public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(Class<T> type) {
            return null;
        }

        @Override
        public <T> ContextResolver<T> getContextResolver(Class<T> contextType, MediaType mediaType) {
            return null;
        }
    });
    String content = "<!DOCTYPE x SYSTEM 'file:///no-such-file'> <rootObject/>";
    provider.getConverter(RootObject.class, null, null).fromString(content);
}
Also used : CommonConfig(org.glassfish.jersey.model.internal.CommonConfig) Providers(javax.ws.rs.ext.Providers) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) RuntimeType(javax.ws.rs.RuntimeType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MediaType(javax.ws.rs.core.MediaType) ContextResolver(javax.ws.rs.ext.ContextResolver) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 2 with ContextResolver

use of javax.ws.rs.ext.ContextResolver in project cxf by apache.

the class ProviderFactory method createContextResolver.

@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<ContextResolver<T>>();
    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.intersectMimeTypes(mTypes, type).size() > 0) {
                            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);
    }
}
Also used : LinkedList(java.util.LinkedList) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Produces(javax.ws.rs.Produces) MediaType(javax.ws.rs.core.MediaType) ContextResolver(javax.ws.rs.ext.ContextResolver)

Example 3 with ContextResolver

use of javax.ws.rs.ext.ContextResolver in project cxf by apache.

the class JAXRSUtilsTest method testContextResolverParam.

@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());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) JAXBContext(javax.xml.bind.JAXBContext) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) ContextResolver(javax.ws.rs.ext.ContextResolver) Test(org.junit.Test)

Aggregations

ContextResolver (javax.ws.rs.ext.ContextResolver)3 Type (java.lang.reflect.Type)2 MediaType (javax.ws.rs.core.MediaType)2 Test (org.junit.Test)2 Annotation (java.lang.annotation.Annotation)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 WildcardType (java.lang.reflect.WildcardType)1 LinkedList (java.util.LinkedList)1 Provider (javax.inject.Provider)1 Produces (javax.ws.rs.Produces)1 RuntimeType (javax.ws.rs.RuntimeType)1 ExceptionMapper (javax.ws.rs.ext.ExceptionMapper)1 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)1 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)1 Providers (javax.ws.rs.ext.Providers)1 JAXBContext (javax.xml.bind.JAXBContext)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 Customer (org.apache.cxf.jaxrs.Customer)1 JAXBContextProvider (org.apache.cxf.jaxrs.JAXBContextProvider)1 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)1