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

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

    final Map< String, Object > properties = new HashMap<>();
    properties.put("search.query.parameter.name", "$filter");
    properties.put("search.parser", new FiqlParser< SearchBean >(SearchBean.class));
    properties.put(SearchUtils.DATE_FORMAT_PROPERTY, "yyyy/MM/dd");

    sf.setResourceClasses(BookCatalog.class);
    sf.setResourceProvider(BookCatalog.class, new SingletonResourceProvider(new BookCatalog()));
    sf.setAddress("http://localhost:" + PORT + "/");
    sf.setProperties(properties);
    sf.setProvider(new MultipartProvider());
    sf.setProvider(new SearchContextProvider());
    sf.setProvider(new JacksonJsonProvider());

    sf.create();
}
 
Example 2
Source File: JAXRSClientServerStreamingTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStore.class);
    sf.setResourceProvider(BookStore.class,
                           new SingletonResourceProvider(new BookStore()));
    JAXBElementProvider<?> p1 = new JAXBElementProvider<>();
    p1.setEnableBuffering(true);
    p1.setEnableStreaming(true);

    JAXBElementProvider<?> p2 = new CustomJaxbProvider();
    p2.setProduceMediaTypes(Collections.singletonList("text/xml"));

    List<Object> providers = new ArrayList<>();
    providers.add(p1);
    providers.add(p2);
    sf.setProviders(providers);
    sf.setAddress("http://localhost:" + PORT + "/");
    Map<String, Object> properties = new HashMap<>();
    properties.put("org.apache.cxf.serviceloader-context", "true");
    sf.setProperties(properties);
    sf.create();

}
 
Example 3
Source File: MultipartServer.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(MultipartStore.class);

    Map<String, Object> props = new HashMap<>();
    props.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, String.valueOf(1024 * 10));
    props.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String.valueOf(1024 * 5));
    props.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE, String.valueOf(400));
    sf.setProperties(props);
    //default lifecycle is per-request, change it to singleton
    sf.setResourceProvider(MultipartStore.class,
                           new SingletonResourceProvider(new MultipartStore()));
    sf.setAddress("http://localhost:" + PORT + "/");

    server = sf.create();
}
 
Example 4
Source File: SyncopeCoreTestingServer.java    From syncope with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    synchronized (ADDRESS) {
        if (serviceOps.list(NetworkService.Type.CORE).isEmpty()) {
            // 1. start (mocked) Core as embedded CXF
            JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
            sf.setAddress(ADDRESS);
            sf.setResourceClasses(WAClientAppService.class, GoogleMfaAuthTokenService.class);
            sf.setResourceProvider(
                    WAClientAppService.class,
                    new SingletonResourceProvider(new StubWAClientAppService(), true));
            sf.setResourceProvider(
                    GoogleMfaAuthTokenService.class,
                    new SingletonResourceProvider(new StubGoogleMfaAuthTokenService(), true));
            sf.setProviders(List.of(new JacksonJsonProvider()));
            sf.create();

            // 2. register Core in Keymaster
            NetworkService core = new NetworkService();
            core.setType(NetworkService.Type.CORE);
            core.setAddress(ADDRESS);
            serviceOps.register(core);
        }
    }
}
 
Example 5
Source File: RestfulServer.java    From tutorials with MIT License 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
    factoryBean.setResourceClasses(CourseRepository.class);
    factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
    factoryBean.setAddress("http://localhost:8080/");
    Server server = factoryBean.create();

    System.out.println("Server ready...");
    Thread.sleep(60 * 1000);
    System.out.println("Server exiting");
    server.destroy();
    System.exit(0);
}
 
Example 6
Source File: Server.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Server() throws Exception {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(CustomerServiceImpl.class);
    sf.setResourceProvider(CustomerServiceImpl.class,
        new SingletonResourceProvider(new CustomerServiceImpl()));
    sf.setAddress("https://localhost:9000/");

    sf.create();
}
 
Example 7
Source File: BookCxfContinuationServer.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookCxfContinuationStore.class);
    sf.setResourceProvider(BookCxfContinuationStore.class,
                           new SingletonResourceProvider(new BookCxfContinuationStore()));
    sf.setAddress("http://localhost:" + PORT + "/");

    sf.create();
}
 
Example 8
Source File: Server.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Server() throws Exception {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(CustomerService.class);
    sf.setResourceProvider(CustomerService.class,
        new SingletonResourceProvider(new CustomerService()));
    sf.setAddress("http://localhost:9000/");

    sf.create();
}
 
Example 9
Source File: OpenApiCustomizerSubclassTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() {
    final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStoreOpenApi.class);
    sf.setResourceClasses(BookStoreStylesheetsOpenApi.class);
    sf.setResourceProvider(BookStoreOpenApi.class,
        new SingletonResourceProvider(new BookStoreOpenApi()));
    sf.setProvider(new JacksonJsonProvider());
    final OpenApiFeature feature = createOpenApiFeature();
    sf.setFeatures(Arrays.asList(feature));
    sf.setAddress("http://localhost:" + port + "/");
    sf.create();
}
 
Example 10
Source File: BookCxfContinuationServlet3Server.java    From cxf with Apache License 2.0 5 votes vote down vote up
private JAXRSServerFactoryBean serverFactory(Bus bus) {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookCxfContinuationStore.class);
    sf.setResourceProvider(BookCxfContinuationStore.class,
                           new SingletonResourceProvider(new BookCxfContinuationStore()));
    sf.setAddress("/");
    return sf;
}
 
Example 11
Source File: BookServerAsyncClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStore.class);
    sf.setResourceProvider(BookStore.class,
                           new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    sf.setProvider(new BooleanReaderWriter());
    sf.getProperties(true).put("default.content.type", "*/*");
    server = sf.create();
    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
}
 
Example 12
Source File: UDDIInquiryJAXRSTest.java    From juddi with Apache License 2.0 5 votes vote down vote up
private static void startServer() throws Exception {
                JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
                sf.setResourceClasses(UDDIInquiryJAXRS.class);

                List<Object> providers = new ArrayList<Object>();
                // add custom providers if any
                providers.add(new org.apache.cxf.jaxrs.provider.JAXBElementProvider());

                Map<String, Object> properties = new HashMap<String, Object>();

// Create a mapping between the XML namespaces and the JSON prefixes.
// The JSON prefix can be "" to specify that you don't want any prefix.
                HashMap<String, String> nstojns = new HashMap<String, String>();
                nstojns.put("urn:uddi-org:api_v3", "urn:uddi-org:api_v3");
                nstojns.put("urn:uddi-org:sub_v3", "urn:uddi-org:sub_v3");
                nstojns.put("urn:uddi-org:custody_v3", "urn:uddi-org:custody_v3");
                nstojns.put("urn:uddi-org:repl_v3", "urn:uddi-org:repl_v3");
                nstojns.put("urn:uddi-org:subr_v3", "urn:uddi-org:subr_v3");
                nstojns.put("urn:uddi-org:repl_v3", "urn:uddi-org:repl_v3");
                nstojns.put("urn:uddi-org:vs_v3", "urn:uddi-org:vs_v3");
                nstojns.put("urn:uddi-org:vscache_v3", "urn:uddi-org:vscache_v3");
                nstojns.put("urn:uddi-org:policy_v3", "urn:uddi-org:policy_v3");
                nstojns.put("urn:uddi-org:policy_instanceParms_v3", "urn:uddi-org:policy_instanceParms_v3");
                nstojns.put("http://www.w3.org/2000/09/xmldsig#", "http://www.w3.org/2000/09/xmldsig#");

                properties.put("namespaceMap", nstojns);
                JSONProvider jsonProvider = new org.apache.cxf.jaxrs.provider.json.JSONProvider();
                jsonProvider.setNamespaceMap(nstojns);

                providers.add(jsonProvider);
                sf.setProviders(providers);

                sf.setResourceProvider(UDDIInquiryJAXRS.class,
                        new SingletonResourceProvider(new UDDIInquiryJAXRS(), true));
                sf.setAddress(ENDPOINT_ADDRESS);

                server = sf.create();
        }
 
Example 13
Source File: BookServerSub.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setStaticSubresourceResolution(true);
    sf.setResourceClasses(BookStoreSubObject.class);
    sf.setResourceProvider(BookStoreSubObject.class,
                           new SingletonResourceProvider(new BookStoreSubObject(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
}
 
Example 14
Source File: RxJava3FlowableServer.java    From cxf with Apache License 2.0 5 votes vote down vote up
private JAXRSServerFactoryBean createFactoryBean(Bus bus, boolean useStreamingSubscriber,
                                                 String relAddress) {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.getProperties(true).put("useStreamingSubscriber", useStreamingSubscriber);
    sf.setProvider(new JacksonJsonProvider());
    new ReactiveIOCustomizer().customize(sf);
    sf.getOutInterceptors().add(new LoggingOutInterceptor());
    sf.setResourceClasses(RxJava3FlowableService.class);
    sf.setResourceProvider(RxJava3FlowableService.class,
                           new SingletonResourceProvider(new RxJava3FlowableService(), true));
    sf.setAddress("http://localhost:" + PORT + relAddress);
    return sf;
}
 
Example 15
Source File: BookServerWebSocket.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStoreWebSocket.class, BookStorePerRequest.class);
    sf.setProvider(new StreamingResponseProvider<Book>());
    sf.setResourceProvider(BookStoreWebSocket.class,
                           new SingletonResourceProvider(new BookStoreWebSocket(), true));
    sf.setAddress("ws://localhost:" + port + "/websocket");
    server = sf.create();

    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
}
 
Example 16
Source File: AbstractSwagger2ServiceDescriptionTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() {
    final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStoreSwagger2.class);
    sf.setResourceClasses(BookStoreStylesheetsSwagger2.class);
    sf.setResourceProvider(BookStoreSwagger2.class,
        new SingletonResourceProvider(new BookStoreSwagger2()));
    sf.setProvider(new JacksonJsonProvider());
    final Swagger2Feature feature = createSwagger2Feature();
    sf.setFeatures(Arrays.asList(feature));
    sf.setAddress("http://localhost:" + port + "/");
    sf.setExtensionMappings(
         Collections.singletonMap("json", "application/json;charset=UTF-8"));
    sf.create();
}
 
Example 17
Source File: Server.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Server() throws Exception {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(JaxrsODataService.class);
    sf.setResourceProvider(JaxrsODataService.class,
        new SingletonResourceProvider(new JaxrsODataService()));
    sf.setFeatures(Collections.singletonList(new LoggingFeature()));
    sf.setAddress("http://localhost:9000/DemoService");

    sf.create();
}
 
Example 18
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 19
Source File: CxfRsHttpListener.java    From tomee with Apache License 2.0 4 votes vote down vote up
private void deploy(final String contextRoot, final Class<?> clazz, final String address, final ResourceProvider rp, final Object serviceBean,
                    final Application app, final Invoker invoker, final Collection<Object> additionalProviders, final ServiceConfiguration configuration,
                    final WebBeansContext webBeansContext) {
    final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(CxfUtil.initBusLoader());
    try {
        final JAXRSServerFactoryBean factory = newFactory(address, createServiceJmxName(clazz.getClassLoader()), createEndpointName(app));
        configureFactory(additionalProviders, configuration, factory, webBeansContext);
        factory.setResourceClasses(clazz);
        context = contextRoot;
        if (context == null) {
            context = "";
        }
        if (!context.startsWith("/")) {
            context = "/" + context;
        }

        if (rp != null) {
            factory.setResourceProvider(rp);
        }
        if (app != null) {
            factory.setApplication(app);
        }
        if (invoker != null) {
            factory.setInvoker(invoker);
        }
        if (serviceBean != null) {
            factory.setServiceBean(serviceBean);
        } else {
            factory.setServiceClass(clazz);
        }

        server = factory.create();
        destination = (HttpDestination) server.getDestination();

        fireServerCreated(oldLoader);
    } finally {
        if (oldLoader != null) {
            CxfUtil.clearBusLoader(oldLoader);
        }
    }
}
 
Example 20
Source File: Server.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected Server(String[] args) throws Exception {

        ServerSocket sparkServerSocket = new ServerSocket(9999);
        ServerSocket jaxrsResponseServerSocket = new ServerSocket(10000);
        Socket jaxrsResponseClientSocket = new Socket("localhost", 10000);


        SparkConf sparkConf = new SparkConf().setMaster("local[*]")
            .setAppName("JAX-RS Spark Socket Connect");
        JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, Durations.seconds(1));

        SparkStreamingOutput streamOut = new SparkStreamingOutput(jssc);
        SparkStreamingListener sparkListener = new SparkStreamingListener(streamOut);
        jssc.addStreamingListener(sparkListener);

        JavaDStream<String> receiverStream = jssc.socketTextStream(
            "localhost", 9999, StorageLevels.MEMORY_ONLY);

        JavaPairDStream<String, Integer> wordCounts = SparkUtils.createOutputDStream(receiverStream, true);
        PrintStream sparkResponseOutputStream = new PrintStream(jaxrsResponseClientSocket.getOutputStream(), true);
        wordCounts.foreachRDD(new SocketOutputFunction(sparkResponseOutputStream));

        jssc.start();

        Socket receiverClientSocket = sparkServerSocket.accept();
        PrintStream sparkOutputStream = new PrintStream(receiverClientSocket.getOutputStream(), true);
        BufferedReader sparkInputStream =
            new BufferedReader(new InputStreamReader(jaxrsResponseServerSocket.accept().getInputStream()));


        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();

        sf.setResourceClasses(StreamingService.class);
        sf.setResourceProvider(StreamingService.class,
            new SingletonResourceProvider(new StreamingService(sparkInputStream,
                                                                     sparkOutputStream)));
        sf.setAddress("http://localhost:9000/spark");
        sf.create();

        jssc.awaitTermination();
        sparkServerSocket.close();
        jaxrsResponseServerSocket.close();
        jaxrsResponseClientSocket.close();

    }