org.apache.cxf.service.invoker.Invoker Java Examples

The following examples show how to use org.apache.cxf.service.invoker.Invoker. 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: AbstractValidationInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected Object getServiceObject(Message message) {
    if (serviceObject != null) {
        return serviceObject;
    }
    Object current = message.getExchange().get(Message.SERVICE_OBJECT);
    if (current != null) {
        return current;
    }
    Endpoint e = message.getExchange().getEndpoint();
    if (e != null && e.getService() != null) {
        Invoker invoker = e.getService().getInvoker();
        if (invoker instanceof FactoryInvoker) {
            FactoryInvoker factoryInvoker = (FactoryInvoker)invoker;
            if (factoryInvoker.isSingletonFactory()) {
                return factoryInvoker.getServiceObject(message.getExchange());
            }
        }
    }
    return null;
}
 
Example #2
Source File: InstrumentedInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void timedAnnotation() {

    Timer timer = testMetricRegistry.timer("timed");
    Meter meter = testMetricRegistry.meter("metered");
    when(mockMetricRegistry.timer(anyString())).thenReturn(timer);
    when(mockMetricRegistry.meter(anyString())).thenReturn(meter);

    long oldtimervalue = timer.getCount();
    long oldmetervalue = meter.getCount();

    Invoker invoker = invokerBuilder.create(instrumentedService, new TimedInvoker());
    this.setTargetMethod(exchange, "timed"); // simulate CXF behavior

    Object result = invoker.invoke(exchange, null);
    assertEquals("timedReturn", result);

    assertThat(timer.getCount(), is(1 + oldtimervalue));
    assertThat(meter.getCount(), is(oldmetervalue));
}
 
Example #3
Source File: InstrumentedInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void meteredAnnotation() {

    Timer timer = testMetricRegistry.timer("timed");
    Meter meter = testMetricRegistry.meter("metered");
    when(mockMetricRegistry.timer(anyString())).thenReturn(timer);
    when(mockMetricRegistry.meter(anyString())).thenReturn(meter);

    long oldtimervalue = timer.getCount();
    long oldmetervalue = meter.getCount();

    Invoker invoker = invokerBuilder.create(instrumentedService, new MeteredInvoker());
    this.setTargetMethod(exchange, "metered"); // simulate CXF behavior

    Object result = invoker.invoke(exchange, null);
    assertEquals("meteredReturn", result);

    assertThat(timer.getCount(), is(oldtimervalue));
    assertThat(meter.getCount(), is(1 + oldmetervalue));
}
 
Example #4
Source File: InstrumentedInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void noAnnotation() {

    Timer timer = testMetricRegistry.timer("timed");
    Meter meter = testMetricRegistry.meter("metered");
    when(mockMetricRegistry.timer(anyString())).thenReturn(timer);
    when(mockMetricRegistry.meter(anyString())).thenReturn(meter);

    long oldtimervalue = timer.getCount();
    long oldmetervalue = meter.getCount();

    Invoker invoker = invokerBuilder.create(instrumentedService, new FooInvoker());
    this.setTargetMethod(exchange, "foo"); // simulate CXF behavior

    Object result = invoker.invoke(exchange, null);
    assertEquals("fooReturn", result);

    assertThat(timer.getCount(), is(oldtimervalue));
    assertThat(meter.getCount(), is(oldmetervalue));
}
 
Example #5
Source File: UnitOfWorkInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void unitOfWorkWithException() {
    // use underlying invoker which invokes fooService.unitOfWork(true) - exception is thrown
    Invoker invoker = invokerBuilder.create(fooService, new UnitOfWorkInvoker(true), sessionFactory);
    this.setTargetMethod(exchange, "unitOfWork", boolean.class); // simulate CXF behavior

    try {
        invoker.invoke(exchange, null);
    }
    catch (Exception e) {
        assertEquals("Uh oh", e.getMessage());
    }

    verify(session, times(1)).beginTransaction();
    verify(transaction, times(0)).commit();
    verify(transaction, times(1)).rollback();
    verify(session, times(1)).close();
}
 
Example #6
Source File: InstrumentedInvokerFactory.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
/**
 * Factory method for ExceptionMeteredInvoker.
 */
private Invoker exceptionMetered(Invoker invoker, List<Method> meteredMethods) {

    ImmutableMap.Builder<String, InstrumentedInvokers.ExceptionMeter> meters =
            new ImmutableMap.Builder<>();

    for (Method m : meteredMethods) {

        ExceptionMetered annotation = m.getAnnotation(ExceptionMetered.class);
        final String name = chooseName(
                annotation.name(),
                annotation.absolute(),
                m,
                ExceptionMetered.DEFAULT_NAME_SUFFIX);
        Meter meter = metricRegistry.meter(name);
        meters.put(m.getName(), new InstrumentedInvokers.ExceptionMeter(meter, annotation.cause()));
    }

    return new InstrumentedInvokers.ExceptionMeteredInvoker(invoker, meters.build());
}
 
Example #7
Source File: JAXWSEnvironmentTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void publishEndpointWithHibernateInvoker() throws Exception {

    jaxwsEnvironment.publishEndpoint(
            new EndpointBuilder("local://path", service)
                .sessionFactory(mock(SessionFactory.class)));

    verify(mockInvokerBuilder).create(any(), any(Invoker.class));
    verify(mockUnitOfWorkInvokerBuilder).create(any(), any(Invoker.class), any(SessionFactory.class));

    Node soapResponse = testutils.invoke("local://path",
            LocalTransportFactory.TRANSPORT_ID, soapRequest.getBytes());

    verify(mockInvoker).invoke(any(Exchange.class), any());

    testutils.assertValid("/soap:Envelope/soap:Body/a:fooResponse", soapResponse);
}
 
Example #8
Source File: JAXWSEnvironmentTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void publishEndpointWithMtom() throws Exception {

    jaxwsEnvironment.publishEndpoint(
            new EndpointBuilder("local://path", service)
                    .enableMtom());

    verify(mockInvokerBuilder).create(any(), any(Invoker.class));

    byte[] response = testutils.invokeBytes("local://path", LocalTransportFactory.TRANSPORT_ID, soapRequest.getBytes());

    verify(mockInvoker).invoke(any(Exchange.class), any());

    MimeMultipart mimeMultipart = new MimeMultipart(new ByteArrayDataSource(response,
            "application/xop+xml; charset=UTF-8; type=\"text/xml\""));
    assertThat(mimeMultipart.getCount(), equalTo(1));
    testutils.assertValid("/soap:Envelope/soap:Body/a:fooResponse",
            StaxUtils.read(mimeMultipart.getBodyPart(0).getInputStream()));
}
 
Example #9
Source File: JAXWSEnvironmentTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void publishEndpointWithProperties() throws Exception {

    HashMap<String, Object> props = new HashMap<>();
    props.put("key", "value");

    Endpoint e = jaxwsEnvironment.publishEndpoint(
            new EndpointBuilder("local://path", service)
                .properties(props));

    assertThat(e, is(notNullValue()));
    assertThat(e.getProperties().get("key"), equalTo("value"));

    verify(mockInvokerBuilder).create(any(), any(Invoker.class));
    verifyZeroInteractions(mockUnitOfWorkInvokerBuilder);

    Node soapResponse = testutils.invoke("local://path",
            LocalTransportFactory.TRANSPORT_ID, soapRequest.getBytes());

    verify(mockInvoker).invoke(any(Exchange.class), any());

    testutils.assertValid("/soap:Envelope/soap:Body/a:fooResponse", soapResponse);
}
 
Example #10
Source File: JAXWSEnvironmentTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void publishEndpointWithPublishedUrlPrefix() throws WSDLException {

    jaxwsEnvironment.setPublishedEndpointUrlPrefix("http://external/prefix");

    jaxwsEnvironment.publishEndpoint(
            new EndpointBuilder("/path", service)
    );

    verify(mockInvokerBuilder).create(any(), any(Invoker.class));
    verifyZeroInteractions(mockUnitOfWorkInvokerBuilder);

    Server server = testutils.getServerForAddress("/path");
    AbstractDestination destination = (AbstractDestination) server.getDestination();
    String publishedEndpointUrl = destination.getEndpointInfo().getProperty(WSDLGetUtils.PUBLISHED_ENDPOINT_URL, String.class);

    assertThat(publishedEndpointUrl, equalTo("http://external/prefix/path"));
}
 
Example #11
Source File: UnitOfWorkInvokerFactory.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
/**
 * Factory method for creating UnitOfWorkInvoker.
 */
public Invoker create(Object service, Invoker rootInvoker, SessionFactory sessionFactory) {

    ImmutableMap.Builder<String, UnitOfWork> unitOfWorkMethodsBuilder =
            new ImmutableMap.Builder<>();

    for (Method m : service.getClass().getMethods()) {
        if (m.isAnnotationPresent(UnitOfWork.class)) {
            unitOfWorkMethodsBuilder.put(m.getName(), m.getAnnotation(UnitOfWork.class));
        }
    }
    ImmutableMap<String, UnitOfWork> unitOfWorkMethods = unitOfWorkMethodsBuilder.build();

    Invoker invoker = rootInvoker;

    if (unitOfWorkMethods.size() > 0) {
        invoker = new UnitOfWorkInvoker(invoker, unitOfWorkMethods, sessionFactory);
    }

    return invoker;
}
 
Example #12
Source File: ReflectionServiceFactoryBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Invoker createInvoker() {
    Class<?> cls = getServiceClass();
    if (cls.isInterface()) {
        return null;
    }
    return new FactoryInvoker(new SingletonFactory(getServiceClass()));
}
 
Example #13
Source File: InstrumentedInvokerFactory.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for MeteredInvoker.
 */
private Invoker metered(Invoker invoker, List<Method> meteredMethods) {

    ImmutableMap.Builder<String, Meter> meters = new ImmutableMap.Builder<>();

    for (Method m : meteredMethods) {
        Metered annotation = m.getAnnotation(Metered.class);
        final String name = chooseName(annotation.name(), annotation.absolute(), m);
        Meter meter = metricRegistry.meter(name);
        meters.put(m.getName(), meter);
    }

    return new InstrumentedInvokers.MeteredInvoker(invoker, meters.build());
}
 
Example #14
Source File: ServiceInvokerInterceptorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
Endpoint createEndpoint(Invoker i) throws Exception {
    IMocksControl control = EasyMock.createNiceControl();
    Endpoint endpoint = control.createMock(Endpoint.class);

    ServiceImpl service = new ServiceImpl((ServiceInfo)null);
    service.setInvoker(i);
    service.setExecutor(new SimpleExecutor());
    EasyMock.expect(endpoint.getService()).andReturn(service).anyTimes();

    control.replay();

    return endpoint;
}
 
Example #15
Source File: JaxWsServerFactoryBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Invoker createInvoker() {
    if (getServiceBean() == null) {
        return new JAXWSMethodInvoker(new SingletonFactory(getServiceClass()));
    }
    return new JAXWSMethodInvoker(getServiceBean());
}
 
Example #16
Source File: JAXWSEnvironmentTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
@Test
public void publishEndpointWithCxfInterceptors() throws Exception {

    TestInterceptor inInterceptor = new TestInterceptor(Phase.UNMARSHAL);
    TestInterceptor inInterceptor2 = new TestInterceptor(Phase.PRE_INVOKE);
    TestInterceptor outInterceptor = new TestInterceptor(Phase.MARSHAL);

    jaxwsEnvironment.publishEndpoint(
            new EndpointBuilder("local://path", service)
                    .cxfInInterceptors(inInterceptor, inInterceptor2)
                    .cxfOutInterceptors(outInterceptor));

    verify(mockInvokerBuilder).create(any(), any(Invoker.class));

    Node soapResponse = testutils.invoke("local://path",
            LocalTransportFactory.TRANSPORT_ID, soapRequest.getBytes());

    verify(mockInvoker).invoke(any(Exchange.class), any());
    assertThat(inInterceptor.getInvocationCount(), equalTo(1));
    assertThat(inInterceptor2.getInvocationCount(), equalTo(1));
    assertThat(outInterceptor.getInvocationCount(), equalTo(1));

    testutils.assertValid("/soap:Envelope/soap:Body/a:fooResponse", soapResponse);

    soapResponse = testutils.invoke("local://path",
            LocalTransportFactory.TRANSPORT_ID, soapRequest.getBytes());

    verify(mockInvoker, times(2)).invoke(any(Exchange.class), any());
    assertThat(inInterceptor.getInvocationCount(), equalTo(2));
    assertThat(inInterceptor2.getInvocationCount(), equalTo(2));
    assertThat(outInterceptor.getInvocationCount(), equalTo(2));

    testutils.assertValid("/soap:Envelope/soap:Body/a:fooResponse", soapResponse);
}
 
Example #17
Source File: InstrumentedInvokerFactory.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for TimedInvoker.
 */
private Invoker timed(Invoker invoker, List<Method> timedMethods) {

    ImmutableMap.Builder<String, Timer> timers = new ImmutableMap.Builder<>();

    for (Method m : timedMethods) {
        Timed annotation = m.getAnnotation(Timed.class);
        final String name = chooseName(annotation.name(), annotation.absolute(), m);
        Timer timer = metricRegistry.timer(name);
        timers.put(m.getName(), timer);
    }

    return new InstrumentedInvokers.TimedInvoker(invoker, timers.build());
}
 
Example #18
Source File: AnnotationsFactoryBeanListener.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setScope(Server server, Class<?> cls) {
    FactoryType scope = cls.getAnnotation(FactoryType.class);
    if (scope != null) {
        Invoker i = server.getEndpoint().getService().getInvoker();
        if (i instanceof FactoryInvoker) {
            Factory f;
            if (scope.factoryClass() == FactoryType.DEFAULT.class) {
                switch (scope.value()) {
                case Session:
                    if (scope.args().length > 0) {
                        f = new SessionFactory(cls, Boolean.parseBoolean(scope.args()[0]));
                    } else {
                        f = new SessionFactory(cls);
                    }
                    break;
                case PerRequest:
                    f = new PerRequestFactory(cls);
                    break;
                case Pooled:
                    f = new PooledFactory(cls, Integer.parseInt(scope.args()[0]));
                    break;
                default:
                    f = new SingletonFactory(cls);
                    break;
                }
            } else {
                try {
                    f = scope.factoryClass().getConstructor(Class.class, String[].class)
                        .newInstance(cls, scope.args());
                } catch (Throwable t) {
                    throw new ServiceConstructionException(t);
                }
            }
            ((FactoryInvoker)i).setFactory(f);
        }

    }
}
 
Example #19
Source File: JaxWsServiceFactoryBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Invoker createInvoker() {
    Class<?> cls = getServiceClass();
    if (cls.isInterface()) {
        return null;
    }
    return new JAXWSMethodInvoker(new SingletonFactory(getServiceClass()));
}
 
Example #20
Source File: JAXWSEnvironmentTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {

    ((ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.apache.cxf")).setLevel(Level.INFO);

    jaxwsEnvironment = new JAXWSEnvironment("soap") {
        /*
        We create BasicAuthenticationInterceptor mock manually, because Mockito provided mock
        does not get invoked by CXF
        */
        @Override
        protected BasicAuthenticationInterceptor createBasicAuthenticationInterceptor() {
            return new BasicAuthenticationInterceptor() {
                @Override
                public void handleMessage(Message message) throws Fault {
                    mockBasicAuthInterceptorInvoked++;
                }
            };
        }
    };

    when(mockInvokerBuilder.create(any(), any(Invoker.class))).thenReturn(mockInvoker);
    jaxwsEnvironment.setInstrumentedInvokerBuilder(mockInvokerBuilder);

    when(mockUnitOfWorkInvokerBuilder
            .create(any(), any(Invoker.class), any(SessionFactory.class)))
            .thenReturn(mockInvoker);
    jaxwsEnvironment.setUnitOfWorkInvokerBuilder(mockUnitOfWorkInvokerBuilder);

    mockBasicAuthInterceptorInvoked = 0;

    testutils.setBus(jaxwsEnvironment.bus);
    testutils.addNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
    testutils.addNamespace("a", "http://jaxws.dropwizard.roskart.com/");
}
 
Example #21
Source File: ValidatingInvokerTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    underlying = mock(Invoker.class);
    invoker = new ValidatingInvoker(underlying, Validation.buildDefaultValidatorFactory().getValidator());
    exchange = mock(Exchange.class);
    when(exchange.getInMessage()).thenReturn(mock(Message.class));
    BindingOperationInfo boi = mock(BindingOperationInfo.class);
    when(exchange.getBindingOperationInfo()).thenReturn(boi);
    OperationInfo oi = mock(OperationInfo.class);
    when(boi.getOperationInfo()).thenReturn(oi);
}
 
Example #22
Source File: UnitOfWorkInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
@Test
public void unitOfWorkAnnotation() {
    // use underlying invoker which invokes fooService.unitOfWork(false)
    Invoker invoker = invokerBuilder.create(fooService, new UnitOfWorkInvoker(false), sessionFactory);
    this.setTargetMethod(exchange, "unitOfWork", boolean.class); // simulate CXF behavior

    Object result = invoker.invoke(exchange, null);
    assertEquals("unitOfWork return", result);

    verify(session, times(1)).beginTransaction();
    verify(transaction, times(1)).commit();
    verify(transaction, times(0)).rollback();
    verify(session, times(1)).close();
}
 
Example #23
Source File: UnitOfWorkInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
@Test
public void noAnnotation() {
    Invoker invoker = invokerBuilder.create(fooService, new FooInvoker(), null);
    this.setTargetMethod(exchange, "foo"); // simulate CXF behavior

    Object result = invoker.invoke(exchange, null);
    assertEquals("foo return", result);

    verifyZeroInteractions(sessionFactory);
    verifyZeroInteractions(session);
    verifyZeroInteractions(transaction);
}
 
Example #24
Source File: ReactiveIOCustomizer.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Invoker createInvoker(JAXRSServerFactoryBean bean) {
    Boolean useStreamingSubscriber = (Boolean)bean.getProperties(true)
            .getOrDefault("useStreamingSubscriber", null);
    ReactiveIOInvoker invoker = new ReactiveIOInvoker();
    if (useStreamingSubscriber != null) {
        invoker.setUseStreamingSubscriberIfPossible(useStreamingSubscriber);
    }
    return invoker;
}
 
Example #25
Source File: ReactiveIOCustomizer.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Invoker createInvoker(JAXRSServerFactoryBean bean) {
    Boolean useStreamingSubscriber = (Boolean)bean.getProperties(true)
            .getOrDefault("useStreamingSubscriber", null);
    ReactiveIOInvoker invoker = new ReactiveIOInvoker();
    if (useStreamingSubscriber != null) {
        invoker.setUseStreamingSubscriberIfPossible(useStreamingSubscriber);
    }
    return invoker;
}
 
Example #26
Source File: ReactorCustomizer.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Invoker createInvoker(JAXRSServerFactoryBean bean) {
    Boolean useStreamingSubscriber = (Boolean)bean.getProperties(true)
            .getOrDefault("useStreamingSubscriber", null);
    ReactorInvoker invoker = new ReactorInvoker();
    if (useStreamingSubscriber != null) {
        invoker.setUseStreamingSubscriberIfPossible(useStreamingSubscriber);
    }
    return invoker;
}
 
Example #27
Source File: InstrumentedInvokerFactory.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for creating instrumented invoker chain.
 */
public Invoker create(Object service, Invoker rootInvoker) {

    List<Method> timedmethods = new ArrayList<>();
    List<Method> meteredmethods = new ArrayList<>();
    List<Method> exceptionmeteredmethods = new ArrayList<>();

    for (Method m : service.getClass().getMethods()) {

        if (m.isAnnotationPresent(Timed.class)) {
            timedmethods.add(m);
        }

        if (m.isAnnotationPresent(Metered.class)) {
            meteredmethods.add(m);
        }

        if (m.isAnnotationPresent(ExceptionMetered.class)) {
            exceptionmeteredmethods.add(m);
        }
    }

    Invoker invoker = rootInvoker;

    if (timedmethods.size() > 0) {
        invoker = this.timed(invoker, timedmethods);
    }

    if (meteredmethods.size() > 0) {
        invoker = this.metered(invoker, meteredmethods);
    }

    if (exceptionmeteredmethods.size() > 0) {
        invoker = this.exceptionMetered(invoker, exceptionmeteredmethods);
    }

    return invoker;
}
 
Example #28
Source File: ServiceImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void setInvoker(Invoker invoker) {
    this.invoker = invoker;
}
 
Example #29
Source File: AbstractInvoker.java    From dropwizard-jaxws with Apache License 2.0 4 votes vote down vote up
public AbstractInvoker(Invoker underlying) {
    this.underlying = underlying;
}
 
Example #30
Source File: ValidatingInvoker.java    From dropwizard-jaxws with Apache License 2.0 4 votes vote down vote up
public ValidatingInvoker(Invoker underlying, Validator validator) {
    super(underlying);
    this.validator = validator;
}