Search in sources :

Example 51 with Service

use of org.apache.cxf.service.Service in project cxf by apache.

the class DynamicClientFactory method createClient.

public Client createClient(String wsdlUrl, QName service, ClassLoader classLoader, QName port, List<String> bindingFiles) {
    if (classLoader == null) {
        classLoader = Thread.currentThread().getContextClassLoader();
    }
    LOG.log(Level.FINE, "Creating client from WSDL " + wsdlUrl);
    WSDLServiceFactory sf = (service == null) ? (new WSDLServiceFactory(bus, wsdlUrl)) : (new WSDLServiceFactory(bus, wsdlUrl, service));
    sf.setAllowElementRefs(allowRefs);
    Service svc = sf.create();
    // all SI's should have the same schemas
    SchemaCollection schemas = svc.getServiceInfos().get(0).getXmlSchemaCollection();
    SchemaCompiler compiler = createSchemaCompiler();
    InnerErrorListener listener = new InnerErrorListener(wsdlUrl);
    Object elForRun = ReflectionInvokationHandler.createProxyWrapper(listener, JAXBUtils.getParamClass(compiler, "setErrorListener"));
    compiler.setErrorListener(elForRun);
    OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
    hackInNewInternalizationLogic(compiler, catalog);
    addSchemas(compiler.getOptions(), compiler, svc.getServiceInfos(), schemas);
    addBindingFiles(bindingFiles, compiler);
    applySchemaCompilerOptions(compiler);
    S2JJAXBModel intermediateModel = compiler.bind();
    listener.throwException();
    JCodeModel codeModel = intermediateModel.generateCode(null, elForRun);
    StringBuilder sb = new StringBuilder();
    boolean firstnt = false;
    for (Iterator<JPackage> packages = codeModel.packages(); packages.hasNext(); ) {
        JPackage jpackage = packages.next();
        if (!isValidPackage(jpackage)) {
            continue;
        }
        if (firstnt) {
            sb.append(':');
        } else {
            firstnt = true;
        }
        sb.append(jpackage.name());
    }
    JAXBUtils.logGeneratedClassNames(LOG, codeModel);
    String packageList = sb.toString();
    // our hashcode + timestamp ought to be enough.
    String stem = toString() + "-" + System.currentTimeMillis();
    File src = new File(tmpdir, stem + "-src");
    if (!src.mkdir()) {
        throw new IllegalStateException("Unable to create working directory " + src.getPath());
    }
    try {
        Object writer = JAXBUtils.createFileCodeWriter(src);
        codeModel.build(writer);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to write generated Java files for schemas: " + e.getMessage(), e);
    }
    File classes = new File(tmpdir, stem + "-classes");
    if (!classes.mkdir()) {
        throw new IllegalStateException("Unable to create working directory " + classes.getPath());
    }
    StringBuilder classPath = new StringBuilder();
    try {
        setupClasspath(classPath, classLoader);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    List<File> srcFiles = FileUtils.getFilesRecurseUsingSuffix(src, ".java");
    if (!srcFiles.isEmpty() && !compileJavaSrc(classPath.toString(), srcFiles, classes.toString())) {
        LOG.log(Level.SEVERE, new Message("COULD_NOT_COMPILE_SRC", LOG, wsdlUrl).toString());
    }
    FileUtils.removeDir(src);
    final URL[] urls;
    try {
        urls = new URL[] { classes.toURI().toURL() };
    } catch (MalformedURLException mue) {
        throw new IllegalStateException("Internal error; a directory returns a malformed URL: " + mue.getMessage(), mue);
    }
    final ClassLoader cl = ClassLoaderUtils.getURLClassLoader(urls, classLoader);
    JAXBContext context;
    Map<String, Object> contextProperties = jaxbContextProperties;
    if (contextProperties == null) {
        contextProperties = Collections.emptyMap();
    }
    try {
        if (StringUtils.isEmpty(packageList)) {
            context = JAXBContext.newInstance(new Class[0], contextProperties);
        } else {
            context = JAXBContext.newInstance(packageList, cl, contextProperties);
        }
    } catch (JAXBException jbe) {
        throw new IllegalStateException("Unable to create JAXBContext for generated packages: " + jbe.getMessage(), jbe);
    }
    JAXBDataBinding databinding = new JAXBDataBinding();
    databinding.setContext(context);
    svc.setDataBinding(databinding);
    ClientImpl client = new DynamicClientImpl(bus, svc, port, getEndpointImplFactory(), cl);
    ServiceInfo svcfo = client.getEndpoint().getEndpointInfo().getService();
    // Setup the new classloader!
    ClassLoaderUtils.setThreadContextClassloader(cl);
    TypeClassInitializer visitor = new TypeClassInitializer(bus, svcfo, intermediateModel, allowWrapperOps());
    visitor.walk();
    // delete the classes files
    FileUtils.removeDir(classes);
    return client;
}
Also used : MalformedURLException(java.net.MalformedURLException) Message(org.apache.cxf.common.i18n.Message) JAXBContext(javax.xml.bind.JAXBContext) SchemaCompiler(org.apache.cxf.common.jaxb.JAXBUtils.SchemaCompiler) URL(java.net.URL) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) OASISCatalogManager(org.apache.cxf.catalog.OASISCatalogManager) URLClassLoader(java.net.URLClassLoader) S2JJAXBModel(org.apache.cxf.common.jaxb.JAXBUtils.S2JJAXBModel) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) WSDLServiceFactory(org.apache.cxf.wsdl11.WSDLServiceFactory) JAXBException(javax.xml.bind.JAXBException) JPackage(org.apache.cxf.common.jaxb.JAXBUtils.JPackage) Service(org.apache.cxf.service.Service) ClientImpl(org.apache.cxf.endpoint.ClientImpl) JCodeModel(org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) JDefinedClass(org.apache.cxf.common.jaxb.JAXBUtils.JDefinedClass) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 52 with Service

use of org.apache.cxf.service.Service in project cxf by apache.

the class ReflectionServiceFactoryTest method testWrappedBuild.

@Test
public void testWrappedBuild() throws Exception {
    Service service = createService(true);
    ServiceInfo si = service.getServiceInfos().get(0);
    InterfaceInfo intf = si.getInterface();
    assertEquals(4, intf.getOperations().size());
    String ns = si.getName().getNamespaceURI();
    OperationInfo sayHelloOp = intf.getOperation(new QName(ns, "sayHello"));
    assertNotNull(sayHelloOp);
    assertEquals("sayHello", sayHelloOp.getInput().getName().getLocalPart());
    List<MessagePartInfo> messageParts = sayHelloOp.getInput().getMessageParts();
    assertEquals(1, messageParts.size());
    assertNotNull(messageParts.get(0).getXmlSchema());
    // test unwrapping
    assertTrue(sayHelloOp.isUnwrappedCapable());
    OperationInfo unwrappedOp = sayHelloOp.getUnwrappedOperation();
    assertEquals("sayHello", unwrappedOp.getInput().getName().getLocalPart());
    messageParts = unwrappedOp.getInput().getMessageParts();
    assertEquals(0, messageParts.size());
    // test output
    messageParts = sayHelloOp.getOutput().getMessageParts();
    assertEquals(1, messageParts.size());
    assertEquals("sayHelloResponse", sayHelloOp.getOutput().getName().getLocalPart());
    messageParts = unwrappedOp.getOutput().getMessageParts();
    assertEquals("sayHelloResponse", unwrappedOp.getOutput().getName().getLocalPart());
    assertEquals(1, messageParts.size());
    MessagePartInfo mpi = messageParts.get(0);
    assertEquals("return", mpi.getName().getLocalPart());
    assertEquals(String.class, mpi.getTypeClass());
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) SoapOperationInfo(org.apache.cxf.binding.soap.model.SoapOperationInfo) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Example 53 with Service

use of org.apache.cxf.service.Service in project cxf by apache.

the class ReflectionServiceFactoryTest method testServerFactoryBean.

@Test
public void testServerFactoryBean() throws Exception {
    Service service = createService(true);
    assertEquals("test", service.get("test"));
    ServerFactoryBean svrBean = new ServerFactoryBean();
    svrBean.setAddress("http://localhost/Hello");
    svrBean.setServiceFactory(serviceFactory);
    svrBean.setServiceBean(new HelloServiceImpl());
    svrBean.setBus(getBus());
    Map<String, Object> props = new HashMap<>();
    props.put("test", "test");
    serviceFactory.setProperties(props);
    svrBean.setProperties(props);
    Server server = svrBean.create();
    assertNotNull(server);
    Map<QName, Endpoint> eps = service.getEndpoints();
    assertEquals(1, eps.size());
    Endpoint ep = eps.values().iterator().next();
    EndpointInfo endpointInfo = ep.getEndpointInfo();
    assertEquals("test", ep.get("test"));
    BindingInfo b = endpointInfo.getService().getBindings().iterator().next();
    assertTrue(b instanceof SoapBindingInfo);
    SoapBindingInfo sb = (SoapBindingInfo) b;
    assertEquals("HelloServiceSoapBinding", b.getName().getLocalPart());
    assertEquals("document", sb.getStyle());
    assertEquals(4, b.getOperations().size());
    BindingOperationInfo bop = b.getOperations().iterator().next();
    SoapOperationInfo sop = bop.getExtensor(SoapOperationInfo.class);
    assertNotNull(sop);
    assertEquals("", sop.getAction());
    assertEquals("document", sop.getStyle());
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Server(org.apache.cxf.endpoint.Server) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) ServerFactoryBean(org.apache.cxf.frontend.ServerFactoryBean) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BindingInfo(org.apache.cxf.service.model.BindingInfo) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) SoapOperationInfo(org.apache.cxf.binding.soap.model.SoapOperationInfo) Test(org.junit.Test)

Example 54 with Service

use of org.apache.cxf.service.Service in project cxf by apache.

the class AbstractSTSClient method createClient.

protected void createClient() throws BusException, EndpointException {
    if (client != null) {
        return;
    }
    if (wsdlLocation != null) {
        WSDLServiceFactory factory = new WSDLServiceFactory(bus, wsdlLocation, serviceName);
        SourceDataBinding dataBinding = new SourceDataBinding();
        factory.setDataBinding(dataBinding);
        Service service = factory.create();
        service.setDataBinding(dataBinding);
        EndpointInfo ei = service.getEndpointInfo(endpointName);
        Endpoint endpoint = new EndpointImpl(bus, service, ei);
        client = new ClientImpl(bus, endpoint);
    } else if (location != null) {
        Endpoint endpoint = STSUtils.createSTSEndpoint(bus, namespace, null, location, soapVersion, policy, endpointName);
        client = new ClientImpl(bus, endpoint);
    } else {
        throw new TrustException(LOG, "NO_LOCATION");
    }
    client.getInFaultInterceptors().addAll(inFault);
    client.getInInterceptors().addAll(in);
    client.getOutInterceptors().addAll(out);
    client.getOutFaultInterceptors().addAll(outFault);
    if (tlsClientParameters != null) {
        HTTPConduit http = (HTTPConduit) client.getConduit();
        http.setTlsClientParameters(tlsClientParameters);
    }
    in = null;
    out = null;
    inFault = null;
    outFault = null;
    if (features != null) {
        for (Feature f : features) {
            f.initialize(client, bus);
        }
    }
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) WSDLServiceFactory(org.apache.cxf.wsdl11.WSDLServiceFactory) Endpoint(org.apache.cxf.endpoint.Endpoint) EndpointImpl(org.apache.cxf.endpoint.EndpointImpl) Service(org.apache.cxf.service.Service) ClientImpl(org.apache.cxf.endpoint.ClientImpl) SourceDataBinding(org.apache.cxf.databinding.source.SourceDataBinding) Feature(org.apache.cxf.feature.Feature)

Example 55 with Service

use of org.apache.cxf.service.Service in project cxf by apache.

the class DocLiteralInInterceptorTest method testUnmarshalSourceData.

@Test
public void testUnmarshalSourceData() throws Exception {
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(getClass().getResourceAsStream("resources/multiPartDocLitBareReq.xml"));
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
    XMLStreamReader filteredReader = new PartialXMLStreamReader(reader, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));
    // advance the xml reader to the message parts
    StaxUtils.read(filteredReader);
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
    Message m = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    Service service = control.createMock(Service.class);
    exchange.put(Service.class, service);
    EasyMock.expect(service.getDataBinding()).andReturn(new SourceDataBinding());
    EasyMock.expect(service.size()).andReturn(0).anyTimes();
    EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();
    Endpoint endpoint = control.createMock(Endpoint.class);
    exchange.put(Endpoint.class, endpoint);
    OperationInfo operationInfo = new OperationInfo();
    operationInfo.setProperty("operation.is.synthetic", Boolean.TRUE);
    MessageInfo messageInfo = new MessageInfo(operationInfo, Type.INPUT, new QName("http://foo.com", "bar"));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo1"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo2"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo3"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo4"), null));
    for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
        mpi.setMessageContainer(messageInfo);
    }
    operationInfo.setInput("inputName", messageInfo);
    BindingOperationInfo boi = new BindingOperationInfo(null, operationInfo);
    exchange.put(BindingOperationInfo.class, boi);
    EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
    BindingInfo binding = control.createMock(BindingInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
    EasyMock.expect(endpointInfo.getBinding()).andReturn(binding).anyTimes();
    EasyMock.expect(binding.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    ServiceInfo serviceInfo = control.createMock(ServiceInfo.class);
    EasyMock.expect(endpointInfo.getService()).andReturn(serviceInfo).anyTimes();
    EasyMock.expect(serviceInfo.getName()).andReturn(new QName("http://foo.com", "service")).anyTimes();
    InterfaceInfo interfaceInfo = control.createMock(InterfaceInfo.class);
    EasyMock.expect(serviceInfo.getInterface()).andReturn(interfaceInfo).anyTimes();
    EasyMock.expect(interfaceInfo.getName()).andReturn(new QName("http://foo.com", "interface")).anyTimes();
    EasyMock.expect(endpointInfo.getName()).andReturn(new QName("http://foo.com", "endpoint")).anyTimes();
    EasyMock.expect(endpointInfo.getProperty("URI", URI.class)).andReturn(new URI("dummy")).anyTimes();
    List<OperationInfo> operations = new ArrayList<>();
    EasyMock.expect(interfaceInfo.getOperations()).andReturn(operations).anyTimes();
    m.setExchange(exchange);
    m.put(Message.SCHEMA_VALIDATION_ENABLED, false);
    m.setContent(XMLStreamReader.class, reader);
    control.replay();
    new DocLiteralInInterceptor().handleMessage(m);
    MessageContentsList params = (MessageContentsList) m.getContent(List.class);
    assertEquals(4, params.size());
    assertEquals("StringDefaultInputElem", ((DOMSource) params.get(0)).getNode().getFirstChild().getNodeName());
    assertEquals("IntParamInElem", ((DOMSource) params.get(1)).getNode().getFirstChild().getNodeName());
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SourceDataBinding(org.apache.cxf.databinding.source.SourceDataBinding) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) URI(java.net.URI) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BindingInfo(org.apache.cxf.service.model.BindingInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) MessageInfo(org.apache.cxf.service.model.MessageInfo) Exchange(org.apache.cxf.message.Exchange) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

Service (org.apache.cxf.service.Service)270 Test (org.junit.Test)167 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)138 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)138 Client (org.apache.cxf.endpoint.Client)128 HashMap (java.util.HashMap)100 WSSSecurityProperties (org.apache.wss4j.stax.ext.WSSSecurityProperties)89 ArrayList (java.util.ArrayList)81 QName (javax.xml.namespace.QName)69 Properties (java.util.Properties)65 Endpoint (org.apache.cxf.endpoint.Endpoint)45 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)44 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)37 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)33 Server (org.apache.cxf.endpoint.Server)27 OperationInfo (org.apache.cxf.service.model.OperationInfo)26 InterfaceInfo (org.apache.cxf.service.model.InterfaceInfo)23 Exchange (org.apache.cxf.message.Exchange)22 Bus (org.apache.cxf.Bus)21 JaxWsServerFactoryBean (org.apache.cxf.jaxws.JaxWsServerFactoryBean)21