Java Code Examples for org.apache.cxf.jaxrs.JAXRSServerFactoryBean#setInInterceptors()

The following examples show how to use org.apache.cxf.jaxrs.JAXRSServerFactoryBean#setInInterceptors() . 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: JAXRSClientServerValidationTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();

    sf.setResourceClasses(BookStoreWithValidation.class);
    sf.setResourceProvider(BookStoreWithValidation.class,
        new SingletonResourceProvider(new BookStoreWithValidation()));
    sf.setProvider(new ValidationExceptionMapper() {
        @Override
        public Response toResponse(ValidationException exception) {
            Response r = super.toResponse(exception);
            return JAXRSUtils.toResponseBuilder(
                r.getStatus()).type("application/xml").entity(new Book("Validation", 123L)).build();
        }
    });

    sf.setAddress("http://localhost:" + PORT + "/");
    BeanValidationInInterceptor in = new JAXRSBeanValidationInInterceptor();
    in.setProvider(new BeanValidationProvider(new JAXRSParameterNameProvider()));
    sf.setInInterceptors(Arrays.< Interceptor< ? extends Message > >asList(
        in));

    sf.setOutInterceptors(Arrays.< Interceptor< ? extends Message > >asList(
        new JAXRSBeanValidationOutInterceptor()));

    sf.create();
}
 
Example 2
Source File: JAXRSLocalTransportTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStore.class, BookStoreSpring.class);
    sf.setResourceProvider(BookStore.class,
                           new SingletonResourceProvider(new BookStore(), true));
    sf.setResourceProvider(BookStoreSpring.class,
                           new SingletonResourceProvider(new BookStoreSpring(), true));
    sf.setProvider(new JacksonJsonProvider());
    List<Interceptor<? extends Message>> outInts = new ArrayList<>();
    outInts.add(new CustomOutInterceptor());
    sf.setOutInterceptors(outInts);

    List<Interceptor<? extends Message>> inInts = new ArrayList<>();
    inInts.add(new CustomInFaultyInterceptor());
    sf.setInInterceptors(inInts);

    sf.setTransportId(LocalTransportFactory.TRANSPORT_ID);
    sf.setAddress("local://books");
    localServer = sf.create();
}
 
Example 3
Source File: AbstractSpringConfigurationFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected Server createJaxRsServer() {

        JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
        factory.setAddress(getAddress());
        factory.setTransportId(getTransportId());
        factory.setBus(getBus());

        setJaxrsResources(factory);

        factory.setInInterceptors(getInInterceptors());
        factory.setOutInterceptors(getOutInterceptors());
        factory.setOutFaultInterceptors(getOutFaultInterceptors());
        factory.setFeatures(getFeatures());
        if (!StringUtils.isEmpty(jaxrsExtensions)) {
            factory.setExtensionMappings(CastUtils.cast((Map<?, ?>)parseMapSequence(jaxrsExtensions)));
        }
        finalizeFactorySetup(factory);
        return factory.create();
    }
 
Example 4
Source File: AnyObjectServiceTest.java    From syncope with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void setup() {
    if (SERVER == null) {
        AnyObjectDAO anyObjectDAO = mock(AnyObjectDAO.class);

        AnyObjectLogic logic = mock(AnyObjectLogic.class);
        when(logic.search(any(SearchCond.class), anyInt(), anyInt(), anyList(), anyString(), anyBoolean())).
                thenAnswer(ic -> {
                    AnyObjectTO printer1 = new AnyObjectTO();
                    printer1.setKey(UUID.randomUUID().toString());
                    printer1.setName("printer1");
                    printer1.setType("PRINTER");
                    printer1.getPlainAttrs().add(new Attr.Builder("location").value("here").build());

                    AnyObjectTO printer2 = new AnyObjectTO();
                    printer2.setKey(UUID.randomUUID().toString());
                    printer2.setName("printer2");
                    printer2.setType("PRINTER");
                    printer2.getPlainAttrs().add(new Attr.Builder("location").value("there").build());

                    return Pair.of(2, Arrays.asList(printer1, printer2));
                });
        when(logic.create(any(AnyObjectCR.class), anyBoolean())).thenAnswer(ic -> {
            AnyObjectTO anyObjectTO = new AnyObjectTO();
            EntityTOUtils.toAnyTO(ic.getArgument(0), anyObjectTO);
            anyObjectTO.setKey(UUID.randomUUID().toString());

            ProvisioningResult<AnyObjectTO> result = new ProvisioningResult<>();
            result.setEntity(anyObjectTO);
            return result;
        });

        SearchCondVisitor searchCondVisitor = mock(SearchCondVisitor.class);
        when(searchCondVisitor.getQuery()).thenReturn(new SearchCond());

        @SuppressWarnings("unchecked")
        SearchCondition<SearchBean> sc = mock(SearchCondition.class);
        doNothing().when(sc).accept(any());
        SearchContext searchContext = mock(SearchContext.class);
        when(searchContext.getCondition(anyString(), eq(SearchBean.class))).thenReturn(sc);

        UriInfo uriInfo = mock(UriInfo.class);
        when(uriInfo.getAbsolutePathBuilder()).thenReturn(new UriBuilderImpl());
        when(uriInfo.getQueryParameters()).thenReturn(new MetadataMap<>());

        MessageContext messageContext = mock(MessageContext.class);
        MockHttpServletRequest httpRequest = new MockHttpServletRequest();
        httpRequest.addHeader(RESTHeaders.NULL_PRIORITY_ASYNC, "false");
        when(messageContext.getHttpServletRequest()).thenReturn(httpRequest);
        when(messageContext.getHttpServletResponse()).thenReturn(new MockHttpServletResponse());

        Request request = mock(Request.class);
        when(request.evaluatePreconditions(any(Date.class))).thenReturn(Response.notModified());
        when(messageContext.getRequest()).thenReturn(request);

        AnyObjectServiceImpl service = new AnyObjectServiceImpl();
        ReflectionTestUtils.setField(service, "anyObjectDAO", anyObjectDAO);
        ReflectionTestUtils.setField(service, "logic", logic);
        ReflectionTestUtils.setField(service, "searchCondVisitor", searchCondVisitor);
        ReflectionTestUtils.setField(service, "searchContext", searchContext);
        ReflectionTestUtils.setField(service, "uriInfo", uriInfo);
        ReflectionTestUtils.setField(service, "messageContext", messageContext);

        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setAddress(LOCAL_ADDRESS);
        sf.setResourceClasses(AnyObjectService.class);
        sf.setResourceProvider(
                AnyObjectService.class,
                new SingletonResourceProvider(service, true));

        sf.setInInterceptors(Arrays.asList(gzipInInterceptor, validationInInterceptor));
        sf.setOutInterceptors(Arrays.asList(gzipOutInterceptor));

        sf.setProviders(Arrays.asList(dateParamConverterProvider, jsonProvider, xmlProvider, yamlProvider,
                exceptionMapper, searchContextProvider, addETagFilter));

        SERVER = sf.create();
    }

    assertNotNull(SERVER);
}
 
Example 5
Source File: BookServer.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    bus.setProperty(ExceptionMapper.class.getName(), new BusMapperExceptionMapper());
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStore.class, SimpleBookStore.class, BookStorePerRequest.class);
    List<Object> providers = new ArrayList<>();

    //default lifecycle is per-request, change it to singleton
    BinaryDataProvider<Object> p = new BinaryDataProvider<>();
    p.setProduceMediaTypes(Collections.singletonList("application/bar"));
    p.setEnableBuffering(true);
    p.setReportByteArraySize(true);
    providers.add(p);
    providers.add(new BookStore.PrimitiveIntArrayReaderWriter());
    providers.add(new BookStore.PrimitiveDoubleArrayReaderWriter());
    providers.add(new BookStore.StringArrayBodyReaderWriter());
    providers.add(new BookStore.StringListBodyReaderWriter());
    providers.add(new StreamingResponseProvider<Object>());
    providers.add(new ContentTypeModifyingMBW());
    JAXBElementProvider<?> jaxbProvider = new JAXBElementProvider<>();
    Map<String, String> jaxbElementClassMap = new HashMap<>();
    jaxbElementClassMap.put(BookNoXmlRootElement.class.getName(), "BookNoXmlRootElement");
    jaxbProvider.setJaxbElementClassMap(jaxbElementClassMap);
    providers.add(jaxbProvider);
    providers.add(new FormatResponseHandler());
    providers.add(new GenericHandlerWriter());
    providers.add(new FaultyRequestHandler());
    providers.add(new SearchContextProvider());
    providers.add(new QueryContextProvider());
    providers.add(new BlockingRequestFilter());
    providers.add(new FaultyResponseFilter());
    providers.add(new BlockedExceptionMapper());
    providers.add(new ParamConverterImpl());
    sf.setProviders(providers);
    List<Interceptor<? extends Message>> inInts = new ArrayList<>();
    inInts.add(new CustomInFaultyInterceptor());
    inInts.add(new LoggingInInterceptor());
    sf.setInInterceptors(inInts);
    List<Interceptor<? extends Message>> outInts = new ArrayList<>();
    outInts.add(new CustomOutInterceptor());
    outInts.add(new LoggingOutInterceptor());
    sf.setOutInterceptors(outInts);
    List<Interceptor<? extends Message>> outFaultInts = new ArrayList<>();
    outFaultInts.add(new CustomOutFaultInterceptor());
    sf.setOutFaultInterceptors(outFaultInts);
    sf.setResourceProvider(BookStore.class,
                           new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");

    sf.getProperties(true).put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", true);
    sf.getProperties().put("search.visitor", new SQLPrinterVisitor<SearchBean>("books"));
    sf.getProperties().put("org.apache.cxf.http.header.split", true);
    sf.getProperties().put("default.content.type", "*/*");
    sf.getProperties().putAll(properties);
    server = sf.create();
    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
}
 
Example 6
Source File: AbstractSpringComponentScanServer.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void setFactoryCxfProviders(JAXRSServerFactoryBean factory) {
    factory.setFeatures(getFeatures());
    factory.setInInterceptors(getInInterceptors());
    factory.setOutInterceptors(getOutInterceptors());
    factory.setOutFaultInterceptors(getOutFaultInterceptors());
}