use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.
the class ServerFactoryTest method testJaxbExtraClass.
@Test
public void testJaxbExtraClass() throws Exception {
ServerFactoryBean svrBean = new ServerFactoryBean();
svrBean.setAddress("http://localhost/Hello");
svrBean.setServiceClass(HelloServiceImpl.class);
svrBean.setBus(getBus());
Map<String, Object> props = svrBean.getProperties();
if (props == null) {
props = new HashMap<>();
}
props.put("jaxb.additionalContextClasses", new Class[] { GreetMe.class, GreetMeOneWay.class });
svrBean.setProperties(props);
Server serv = svrBean.create();
Class<?>[] extraClass = ((JAXBDataBinding) serv.getEndpoint().getService().getDataBinding()).getExtraClass();
assertEquals(extraClass.length, 2);
assertEquals(extraClass[0], GreetMe.class);
assertEquals(extraClass[1], GreetMeOneWay.class);
}
use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.
the class MAPCodec method encodeMAP.
/**
* Encodes an MAP as a SOAP header.
*
* @param message the message to store the headers on
* @param value the value to encode
* @param qname the QName for the header
* @param clz the class
* @param ctx the JAXBContent
* @param mustUnderstand
*/
protected <T> void encodeMAP(SoapMessage message, T value, QName qname, Class<T> clz, JAXBContext ctx, boolean mustUnderstand) throws JAXBException {
JAXBDataBinding jaxbDataBinding = new JAXBDataBinding(ctx);
SoapHeader h = new SoapHeader(qname, new JAXBElement<T>(qname, clz, value), jaxbDataBinding);
h.setMustUnderstand(mustUnderstand);
message.getHeaders().add(h);
}
use of org.apache.cxf.jaxb.JAXBDataBinding in project cxf by apache.
the class GreeterImpl method testDocLitFault.
public BareDocumentResponse testDocLitFault(String in) throws BadRecordLitFault {
// System.out.println("Executing testDocLitFault sayHi\n");
List<Header> headers = new ArrayList<>();
Header header = null;
try {
header = new Header(new QName("http://test", "test"), new String("test"), new JAXBDataBinding(String.class));
} catch (JAXBException e) {
e.printStackTrace();
}
headers.add(header);
context.getMessageContext().put(Header.HEADER_LIST, headers);
throw new BadRecordLitFault("int fault", 5);
}
use of org.apache.cxf.jaxb.JAXBDataBinding 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.jaxb.JAXBDataBinding in project camel by apache.
the class CxfMessageHeadersRelayTest method buildOutOfBandHeaderList.
protected static List<Header> buildOutOfBandHeaderList(boolean invalid) throws JAXBException {
OutofBandHeader ob = new OutofBandHeader();
ob.setName("testOobHeader");
ob.setValue("testOobHeaderValue");
ob.setHdrAttribute(invalid ? "dontProcess" : "testHdrAttribute");
SoapHeader hdr = new SoapHeader(new QName(Constants.TEST_HDR_NS, Constants.TEST_HDR_REQUEST_ELEM), ob, new JAXBDataBinding(ob.getClass()));
hdr.setMustUnderstand(invalid);
List<Header> headers = new ArrayList<Header>();
headers.add(hdr);
return headers;
}
Aggregations