Search in sources :

Example 11 with Feature

use of javax.ws.rs.core.Feature in project jersey by jersey.

the class CommonConfig method processFeatureRegistration.

private void processFeatureRegistration(final Object component, final Class<?> componentClass) {
    final ContractProvider model = componentBag.getModel(componentClass);
    if (model.getContracts().contains(Feature.class)) {
        @SuppressWarnings("unchecked") final FeatureRegistration registration = (component != null) ? new FeatureRegistration((Feature) component) : new FeatureRegistration((Class<? extends Feature>) componentClass);
        newFeatureRegistrations.add(registration);
    }
}
Also used : ContractProvider(org.glassfish.jersey.model.ContractProvider) Feature(javax.ws.rs.core.Feature)

Example 12 with Feature

use of javax.ws.rs.core.Feature in project cxf by apache.

the class BookServer20 method run.

protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStore.class);
    List<Object> providers = new ArrayList<>();
    providers.add(new PreMatchContainerRequestFilter2());
    providers.add(new PreMatchContainerRequestFilter());
    providers.add(new PostMatchContainerResponseFilter());
    providers.add((Feature) context -> {
        context.register(new PostMatchContainerResponseFilter3());
        return true;
    });
    providers.add(new PostMatchContainerResponseFilter2());
    providers.add(new CustomReaderBoundInterceptor());
    providers.add(new CustomReaderInterceptor());
    providers.add(new CustomWriterInterceptor());
    providers.add(new CustomDynamicFeature());
    providers.add(new PostMatchContainerRequestFilter());
    providers.add(new FaultyContainerRequestFilter());
    providers.add(new PreMatchReplaceStreamOrAddress());
    providers.add(new ServerTestFeature());
    providers.add(new JacksonJaxbJsonProvider());
    providers.add(new IOExceptionMapper());
    sf.setApplication(new Application());
    sf.setProviders(providers);
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
}
Also used : StringUtils(org.apache.cxf.common.util.StringUtils) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) Application(javax.ws.rs.core.Application) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) MediaType(javax.ws.rs.core.MediaType) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) URI(java.net.URI) Method(java.lang.reflect.Method) Context(javax.ws.rs.core.Context) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) PreMatching(javax.ws.rs.container.PreMatching) Priority(javax.annotation.Priority) DynamicFeature(javax.ws.rs.container.DynamicFeature) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Bus(org.apache.cxf.Bus) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) Feature(javax.ws.rs.core.Feature) ReaderInterceptorContext(javax.ws.rs.ext.ReaderInterceptorContext) HashMap(java.util.HashMap) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) FeatureContext(javax.ws.rs.core.FeatureContext) ResourceInfo(javax.ws.rs.container.ResourceInfo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOUtils(org.apache.cxf.helpers.IOUtils) IOException(java.io.IOException) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) NameBinding(javax.ws.rs.NameBinding) BusFactory(org.apache.cxf.BusFactory) AbstractBusTestServerBase(org.apache.cxf.testutil.common.AbstractBusTestServerBase) InjectionUtils(org.apache.cxf.jaxrs.utils.InjectionUtils) InputStream(java.io.InputStream) RetentionPolicy(java.lang.annotation.RetentionPolicy) Bus(org.apache.cxf.Bus) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ArrayList(java.util.ArrayList) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) Application(javax.ws.rs.core.Application)

Example 13 with Feature

use of javax.ws.rs.core.Feature in project cxf by apache.

the class ProviderFactoryTest method testRegisterInFeature.

@Test
public void testRegisterInFeature() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    final Object provider = new WebApplicationExceptionMapper();
    pf.registerUserProvider((Feature) context -> {
        context.register(provider);
        return true;
    });
    ExceptionMapper<WebApplicationException> em = pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
    assertSame(provider, em);
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Assert.assertNotSame(org.junit.Assert.assertNotSame) Priorities(javax.ws.rs.Priorities) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ParamConverter(javax.ws.rs.ext.ParamConverter) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ContextResolver(javax.ws.rs.ext.ContextResolver) JAXBContextProvider2(org.apache.cxf.jaxrs.JAXBContextProvider2) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) RuntimeType(javax.ws.rs.RuntimeType) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Collection(java.util.Collection) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Customer(org.apache.cxf.jaxrs.Customer) Priority(javax.annotation.Priority) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) Assert.assertFalse(org.junit.Assert.assertFalse) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) Bus(org.apache.cxf.Bus) MessageImpl(org.apache.cxf.message.MessageImpl) Feature(javax.ws.rs.core.Feature) DataHandler(javax.activation.DataHandler) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ConstrainedTo(javax.ws.rs.ConstrainedTo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) Before(org.junit.Before) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Message(org.apache.cxf.message.Message) Assert.assertNotNull(org.junit.Assert.assertNotNull) JAXBElement(javax.xml.bind.JAXBElement) IOUtils(org.apache.cxf.helpers.IOUtils) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Book(org.apache.cxf.jaxrs.resources.Book) Exchange(org.apache.cxf.message.Exchange) File(java.io.File) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) AbstractResourceInfo(org.apache.cxf.jaxrs.model.AbstractResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Assert.assertNull(org.junit.Assert.assertNull) BusFactory(org.apache.cxf.BusFactory) DataSource(javax.activation.DataSource) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) WebApplicationException(javax.ws.rs.WebApplicationException) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 14 with Feature

use of javax.ws.rs.core.Feature in project cxf by apache.

the class ProviderFactoryTest method testRegisterMbrMbwProviderAsMbwOnly.

@Test
public void testRegisterMbrMbwProviderAsMbwOnly() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    JAXBElementProvider<Book> customProvider = new JAXBElementProvider<>();
    pf.registerUserProvider((Feature) context -> {
        context.register(customProvider, MessageBodyWriter.class);
        return true;
    });
    MessageBodyWriter<Book> writer = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertSame(writer, customProvider);
    MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertTrue(reader instanceof JAXBElementProvider);
    assertNotSame(reader, customProvider);
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Assert.assertNotSame(org.junit.Assert.assertNotSame) Priorities(javax.ws.rs.Priorities) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ParamConverter(javax.ws.rs.ext.ParamConverter) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ContextResolver(javax.ws.rs.ext.ContextResolver) JAXBContextProvider2(org.apache.cxf.jaxrs.JAXBContextProvider2) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) RuntimeType(javax.ws.rs.RuntimeType) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Collection(java.util.Collection) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Customer(org.apache.cxf.jaxrs.Customer) Priority(javax.annotation.Priority) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) Assert.assertFalse(org.junit.Assert.assertFalse) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) Bus(org.apache.cxf.Bus) MessageImpl(org.apache.cxf.message.MessageImpl) Feature(javax.ws.rs.core.Feature) DataHandler(javax.activation.DataHandler) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ConstrainedTo(javax.ws.rs.ConstrainedTo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) Before(org.junit.Before) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Message(org.apache.cxf.message.Message) Assert.assertNotNull(org.junit.Assert.assertNotNull) JAXBElement(javax.xml.bind.JAXBElement) IOUtils(org.apache.cxf.helpers.IOUtils) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Book(org.apache.cxf.jaxrs.resources.Book) Exchange(org.apache.cxf.message.Exchange) File(java.io.File) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) AbstractResourceInfo(org.apache.cxf.jaxrs.model.AbstractResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Assert.assertNull(org.junit.Assert.assertNull) BusFactory(org.apache.cxf.BusFactory) DataSource(javax.activation.DataSource) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 15 with Feature

use of javax.ws.rs.core.Feature in project cxf by apache.

the class ConfigurableImpl method doRegister.

private C doRegister(Object provider, Map<Class<?>, Integer> contracts) {
    if (!checkConstraints(provider)) {
        return configurable;
    }
    if (provider instanceof Feature) {
        Feature feature = (Feature) provider;
        boolean enabled = feature.configure(new FeatureContextImpl(this));
        config.setFeature(feature, enabled);
        return configurable;
    }
    config.register(provider, contracts);
    return configurable;
}
Also used : Feature(javax.ws.rs.core.Feature)

Aggregations

Feature (javax.ws.rs.core.Feature)19 Response (javax.ws.rs.core.Response)9 Test (org.junit.Test)9 IOException (java.io.IOException)7 List (java.util.List)7 InputStream (java.io.InputStream)6 Annotation (java.lang.annotation.Annotation)6 Type (java.lang.reflect.Type)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Priority (javax.annotation.Priority)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 Client (javax.ws.rs.client.Client)6 MediaType (javax.ws.rs.core.MediaType)6 ExceptionMapper (javax.ws.rs.ext.ExceptionMapper)6 OutputStream (java.io.OutputStream)5 URI (java.net.URI)5 Arrays (java.util.Arrays)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)5