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

The following examples show how to use org.apache.cxf.jaxrs.JAXRSServerFactoryBean#setServiceBean() . 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: CXFITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) {
  System.setProperty("sa.integration.cxf.interceptors.client.in", ClientSpanTagInterceptor.class.getName());
  System.setProperty("sa.integration.cxf.interceptors.server.out", ServerSpanTagInterceptor.class.getName());
  System.setProperty("sa.integration.cxf.interceptors.classpath", "taget/test-classes");

  final String msg = "hello";

  final JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
  serverFactory.setAddress(BASE_URI);
  serverFactory.setServiceBean(new EchoImpl());
  final Server server = serverFactory.create();

  final JAXRSClientFactoryBean clientFactory = new JAXRSClientFactoryBean();
  clientFactory.setServiceClass(Echo.class);
  clientFactory.setAddress(BASE_URI);
  final Echo echo = clientFactory.create(Echo.class);

  echo.echo(msg);

  // CXF Tracing span has no "component" tag, cannot use TestUtil.checkSpan()
  checkSpans(2);
  checkTag();

  server.destroy();
  serverFactory.getBus().shutdown(true);
}
 
Example 2
Source File: CxfServerBean.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
private static int publishRest(JAXRSServerFactoryBean sf, List<TbSysWsConfig> configs) {
	int c = 0;
	for (TbSysWsConfig config : configs) {
		if (!WSConfig.TYPE_REST.equals(config.getType())) {
			continue;
		}
		try {
			sf.setServiceBean(AppContext.getBean(config.getBeanId()));
			c++;				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}		
	sf.setProviders(getProvider());
	sf.setAddress(WSConfig.getJAXRSServerFactoryBeanAddress());
	return c;
}
 
Example 3
Source File: JAXRSClientFactoryBeanTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testVoidResponseAcceptWildcard() throws Exception {
    String address = "local://store";
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setServiceBean(new BookStore());
    sf.setAddress(address);
    Server s = sf.create(); 

    BookStore store = JAXRSClientFactory.create(address, BookStore.class);
    store.addBook(new Book());

    s.stop();

    ResponseImpl response = (ResponseImpl) WebClient.client(store).getResponse();
    Map<String, List<String>> headers =
        CastUtils.cast((Map<?, ?>) response.getOutMessage().get(Message.PROTOCOL_HEADERS));
    assertTrue(headers.get(HttpHeaders.ACCEPT).contains(MediaType.WILDCARD));
}
 
Example 4
Source File: BookServerResourceCreatedOutside.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    setBus(BusFactory.getDefaultBus());
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(getBus());
    BookStore bs = new BookStore();
    sf.setServiceBean(bs);
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
}
 
Example 5
Source File: RESTLoggingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Server createService(String serviceURI, Object serviceImpl, LoggingFeature loggingFeature) {
    JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
    factory.setAddress(serviceURI);
    factory.setFeatures(Collections.singletonList(loggingFeature));
    factory.setServiceBean(serviceImpl);
    factory.setTransportId(LocalTransportFactory.TRANSPORT_ID);
    return factory.create();
}
 
Example 6
Source File: JAXRSClientServerUserResourceTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress("http://localhost:" + PORT + "/");

    UserResource ur = new UserResource();
    ur.setName(BookStoreNoAnnotations.class.getName());
    ur.setPath("/bookstoreNoAnnotations");
    UserOperation op = new UserOperation();
    op.setPath("/books/{id}");
    op.setName("getBook");
    op.setVerb("GET");
    op.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH, "id")));

    UserOperation op2 = new UserOperation();
    op2.setPath("/books/{id}/chapter");
    op2.setName("getBookChapter");
    op2.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH, "id")));

    List<UserOperation> ops = new ArrayList<>();
    ops.add(op);
    ops.add(op2);

    ur.setOperations(ops);

    UserResource ur2 = new UserResource();
    ur2.setName(ChapterNoAnnotations.class.getName());
    UserOperation op3 = new UserOperation();
    op3.setPath("/");
    op3.setName("getItself");
    op3.setVerb("GET");
    ur2.setOperations(Collections.singletonList(op3));

    sf.setModelBeans(ur, ur2);

    String modelRef = "classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml";
    sf.setModelRefWithServiceClass(modelRef, BookStoreNoAnnotationsInterface.class);
    sf.setServiceBean(new BookStoreNoAnnotationsImpl());
    server = sf.create();
}
 
Example 7
Source File: CxfTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testRs(final MockTracer tracer) {
  System.setProperty(Configuration.INTERCEPTORS_CLIENT_IN, ClientSpanTagInterceptor.class.getName());
  System.setProperty(Configuration.INTERCEPTORS_SERVER_OUT, ServerSpanTagInterceptor.class.getName());
  final String msg = "hello";

  // prepare server
  final JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
  serverFactory.setAddress(BASE_URI);
  serverFactory.setServiceBean(new EchoImpl());
  final Server server = serverFactory.create();

  // prepare client
  final JAXRSClientFactoryBean clientFactory = new JAXRSClientFactoryBean();
  clientFactory.setServiceClass(Echo.class);
  clientFactory.setAddress(BASE_URI);
  final Echo echo = clientFactory.create(Echo.class);

  final String response = echo.echo(msg);

  assertEquals(msg, response);
  assertEquals(2, tracer.finishedSpans().size());

  final WebClient client = WebClient.create(BASE_URI);
  final String result = client.post(msg, String.class);

  assertEquals(msg, result);
  assertEquals(4, tracer.finishedSpans().size());

  final List<MockSpan> spans = tracer.finishedSpans();
  for (final MockSpan span : spans) {
    assertEquals(AbstractSpanTagInterceptor.SPAN_TAG_VALUE, span.tags().get(AbstractSpanTagInterceptor.SPAN_TAG_KEY));
  }

  server.destroy();
  serverFactory.getBus().shutdown(true);
}
 
Example 8
Source File: JAXRSClientServerUserResourceAsteriskTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress("http://localhost:" + PORT + CONTEXT);

    UserResource ur = new UserResource();
    ur.setName(BookStoreNoAnnotations.class.getName());
    ur.setPath("/bookstoreNoAnnotations");
    UserOperation op = new UserOperation();
    op.setPath("/books/{id}");
    op.setName("getBook");
    op.setVerb("GET");
    op.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH, "id")));

    UserOperation op2 = new UserOperation();
    op2.setPath("/books/{id}/chapter");
    op2.setName("getBookChapter");
    op2.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH, "id")));

    List<UserOperation> ops = new ArrayList<>();
    ops.add(op);
    ops.add(op2);

    ur.setOperations(ops);

    UserResource ur2 = new UserResource();
    ur2.setName(ChapterNoAnnotations.class.getName());
    UserOperation op3 = new UserOperation();
    op3.setPath("/");
    op3.setName("getItself");
    op3.setVerb("GET");
    ur2.setOperations(Collections.singletonList(op3));

    sf.setModelBeans(ur, ur2);

    String modelRef = "classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml";
    sf.setModelRefWithServiceClass(modelRef, BookStoreNoAnnotationsInterface.class);
    sf.setServiceBean(new BookStoreNoAnnotationsImpl());
    server = sf.create();
}
 
Example 9
Source File: AppConfig.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Bean
org.apache.cxf.endpoint.Server server() {
    final JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean ();
    factory.setFeatures(Arrays.asList(openApiFeature()));
    factory.setServiceBean(sampleResource());
    factory.setAddress("http://localhost:9000/");
    factory.setProvider(new JsonbJaxrsProvider<>());
    return factory.create();
}
 
Example 10
Source File: StatsConfig.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Bean @DependsOn("cxf")
Server jaxRsServer() {
    final JAXRSServerFactoryBean factory = RuntimeDelegate
        .getInstance()
        .createEndpoint(new StatsApplication(), JAXRSServerFactoryBean.class);
    factory.setServiceBean(statsRestService);
    factory.setProvider(new JacksonJsonProvider());
    return factory.create();
}
 
Example 11
Source File: RestAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
    testService = new TestService();
    jaxrsServerFactoryBean.setAddress("http://localhost:9091/test");
    jaxrsServerFactoryBean.setServiceBean(testService);
    cxfServer = jaxrsServerFactoryBean.create();
    cxfServer.start();
}
 
Example 12
Source File: RestCollectorTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
    TestService service = new TestService();
    jaxrsServerFactoryBean.setAddress("http://localhost:9090/test");
    jaxrsServerFactoryBean.setServiceBean(service);
    cxfServer = jaxrsServerFactoryBean.create();
    cxfServer.start();
}
 
Example 13
Source File: SseEventSourceImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void startNotAuthorizedServer(Type type) {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress(LOCAL_ADDRESS + type.name());
    sf.setServiceBean(new ProtectedEventServer());
    SERVERS.put(type, sf.create());
}
 
Example 14
Source File: SseEventSourceImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void startBusyServer(Type type) {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress(LOCAL_ADDRESS + type.name());
    sf.setServiceBean(new BusyEventServer());
    SERVERS.put(type, sf.create());
}
 
Example 15
Source File: SseEventSourceImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void startServer(Type type, String payload) {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress(LOCAL_ADDRESS + type.name());
    sf.setServiceBean(new EventServer(payload));
    SERVERS.put(type, sf.create());
}
 
Example 16
Source File: SseEventSourceImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void startDynamicServer(Type type, Function<HttpHeaders, String> function) {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setAddress(LOCAL_ADDRESS + type.name());
    sf.setServiceBean(new DynamicServer(function, type == Type.EVENT_RETRY_LAST_EVENT_ID));
    SERVERS.put(type, sf.create());
}
 
Example 17
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);
        }
    }
}