How to generate header using SoapJaxbDataFormat - apache-camel

First I have generated pojo class using jsonschema2pojo plugin using WSDL file.
My WSDL file contains Header and Body.
Body Root Pojo looks like this:
public class SubmitCustomerOrderRequest {
#XmlElement(required = true)
protected List<Order> order;
}
Header looks like this:
public class MessageHeader {
.....
}
Now in a process class of camel i am putting SubmitCustomerOrderRequest (Here this pojo only consider body not header) into body like this:
submitCustomerOrderRequest.setOrder(orderList);
exchange.getIn().setBody(submitCustomerOrderRequest);
Now in the route i am marshalling using this concept
SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("org.com.model",
new ServiceInterfaceStrategy(order.class, true));
And marshalling into xml like this:
.marshal(soapDF)
Now here the problem here is, its generating xml but without header, how to include header also in the process class so that while converting into xml, its generates header also with body
Its generating like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Envelope xmlns:ns2="org.com" xmlns:ns3="org.com.something">
<ns2:Body>
<ns3:submitCustomerOrderV3Request>
</ns3:submitCustomerOrderV3Request>
</ns2:Body>
</ns2:Envelope>
whereas i need like this with header:
<SOAP-ENV:Envelope xmlns:SOAP-ENV=org.com>
<SOAP-ENV:Header>
<messageHeader xmlns=org.om>
</messageHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:submitCustomerOrderV3Request xmlns:ns2=org.com>
</ns2:submitCustomerOrderV3Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Any help would be appreciable.

Related

Camel JAXB marshalling return XML object type?

I am sending a Java object to producer endpoint and waiting for the marshalled XML object. I tried changing it to Node object/ File object but it is giving ClassCastException.
So took the xmlObj in an object class type. What should be the correct class to capture the response?
public class ClientEight {
#Produce(uri = "direct:invoice")
ProducerTemplate template;
public static void main(String args[]) throws InterruptedException {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("resources/camel-configTen.xml");
InvoiceXml invoice = new InvoiceXml("fdf3443", 3454, 435345.44 f, "hfhfddfdg"); //any java object we are passing
ClientEight client = (ClientEight) ctx.getBean("client");
Object xmlObj = client.template.requestBody(invoice);
System.out.println(xmlObj);
}
}
Above is a client code which you are using to send the Java object to a producer endpoint and since you are using template.requestBody, you are getting back the object returned.
<camel:camelContext>
<camel:dataFormats>
<!-- path to jaxb annotated class -->
<camel:jaxb id="invoiceJaxb" contextPath="com.java.bean"
prettyPrint="true" />
</camel:dataFormats>
<camel:route>
<camel:from uri="direct:invoice" />
<camel:marshal ref="invoiceJaxb" />
<camel:log message=" ${body}" />
<camel:to uri="file://src/resources?fileName=One.xml"/>
</camel:route>
</camel:camelContext>
the unmarshall processor return a stream, not a single object. In camel, more generally, if you want a specific type, you shouldn't get directly a body as an object, but use the various methods to convert the body into that type.
Try :
Document document = client.template.requestBody(invoice, org.w3c.dom.Document.class);

Element does not have a match in class

I am working with Simple XML framework, and just renamed some XML layouts, which now don't seem to work any more.
This is my XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<orderListReply id="R000000000006">
<order orderid="12" type="outbound" state="available">
<todo>2</todo>
<done>0</done>
<lines>1</lines>
<erporderid>0</erporderid>
</order>
</orderListReply>
And this is my code class definition:
#Root(name="orderListReply")
public class OrderListReplyTelegram extends Telegram {
#ElementList(name="order", inline=true, required=false)
private List<OrderListItem> orders;
...
This is the error I get:
org.simpleframework.xml.core.ElementException: Element 'order' does not have a match in class nl.minerall.sapphire.pocket.telegrams.OrderListReplyTelegram at line 1
Unfortunately, debugging Simple XML Framework isn't easy, but some trial and error helped me.
My OrderListItem class had this header:
#Element(name="order")
public class OrderListItem {
when changed to this:
#Root(name="order")
public class OrderListItem {
it worked. Strange thing is, in other code, the #Element annotation seemed to work (this code comes from another, working, tree).

How to use internationalization in JSF that retrieve the data from DB

I want to use the standard internationalization from JSF (in property files )and the possibility to switch to database. Is it possible to replace JSF internationalization with own implementation that retrieve the data from DB, so I can configure it ? Or is in this case another aproach better ?
I've found the following example: http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/ . In this example the own resource bundle class is defined. To use it only the reference in xml to implementation class is replaced.
As BalusC pointed, you need to create a ResourceBundle and register it to the app or individually per page.
Simple example:
index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:body>
<h:outputText value="[helloworld]: #{msgs.helloworld}" />
</h:body>
</html>
faces-config.xml
<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<resource-bundle>
<base-name>com.myproject.CustomResourceBundle</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
CustomResourceBundle.java
package com.myproject;
import java.util.ListResourceBundle;
public class CustomResourceBundle extends ListResourceBundle {
#Override
protected Object[][] getContents() {
return getMapOfWordsFromDatabase();
}
private Object[][] getMapOfWordsFromDatabase() {
// TODO get key and words relation from database!
return map;
}
}
Theory:
http://docs.oracle.com/javaee/5/tutorial/doc/bnaxv.html
http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html

Avoid creation of JDK classes in Apache CXF wsdl2java task

I have a WSDL file with defines a java.io.Exception:
<xsd:schema xmlns:tns="http://io.java" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://io.java">
<xsd:complexType name="IOException">
<xsd:sequence/>
</xsd:complexType>
</xsd:schema>
When generating Java classes using the Apache CXf wsdl2java task, it generates a class like this (which causes compile errors, as it is not a valid java.io.IoException):
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "IOException")
public class IOException {
}
Is there a way to prevent CXF from generating JDK classes?
Thx! :)
You definitely need to change your namespace.
targetNamespace="http://io.java"
xmlns:tns="http://io.java"
If you have such namespace and the complex type named IOException of course there will a problem. And why in the world you named the namespace like this http://io.java?
Change the namespace for e.g.:
targetNamespace="http://yourcompany.com/yourservice"
xmlns:tns="http://yourcompany.com/yourservice"
You you'll be good.

how to use an external configuration file with WPF?

i would like to set up an external configuration file that I can store in a directory for my WPF app, not necessarily the directory of my exe when I create my program either.
I created an App.Config file and added System.Configuration to my assembly. My App.Config has:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="sd.config">
<add key="username" value="joesmith" />
</appSettings>
</configuration>
and my sd.config (external file) which is in the root of my project for now, has
<?xml version="1.0"?>
<appSettings>
<add key="username1" value="janedoe" />
</appSettings>
in my MainWindow cs class I used
string username = ConfigurationManager.AppSettings.Get("username1");
which returns a null string. when i just retrieve the username field from App.Config it works. What did i miss? Thanks so much!
See the documentation on ConfigurationManager:
The AppSettings property:
Gets the AppSettingsSection data for the current application's default
configuration.
You need to do a little extra work to get data that isn't in your application's default configuration file.
Instead of using the file= attribute, add a key to your <appSettings> that defines the location of the secondary config file, like so:
<add key="configFile" value="sd.config"/>
Then, in order to use ConfigurationManager to pull settings from the secondary config file, you need to use its OpenMappedExeConfiguration method, which should look a little something like this:
var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Path.Combine(
AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
ConfigurationManager.AppSettings["configFile"]
);
//Once you have a Configuration reference to the secondary config file,
//you can access its appSettings collection:
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var userName1 = config.AppSettings["username1"];
That code might not be dead-on for your example, but hopefully it gets you on the right track!

Resources