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;
}
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());
}
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());
}
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);
}
}
}
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());
}
Aggregations