How to pass variable from xml to java class in mule? - groovy

I have a variable in the mule config xml, which I want to access in the Java file. How do I do that?
I find below ways:
use component
use transformer
use scripting such as groovy
I want to send that variable to java file, and I import it to database in java file.
my config is:
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd ">
<set-variable variableName="remoteClientAddress"
value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
<message-properties-transformer name="setProperty"
scope="session" doc:name="Message Properties">
<add-message-property key="remoteClientAddress"
value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
</message-properties-transformer>
<flow name="service-cxf-wsdlfirstFlow1" doc:name="service-cxf-wsdlfirstFlow1">
<http:inbound-endpoint host="localhost" port="8085"
path="Weather/Service" exchange-pattern="request-response" doc:name="HTTP">
</http:inbound-endpoint>
<set-variable variableName="remoteClientAddress"
value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
<message-properties-transformer doc:name="myproperty"
scope="session">
<add-message-property key="remoteClientAddress"
value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
</message-properties-transformer>
<component doc:name="classTest" class="org.mule.example.scripting.IpClient" />
<logger
message="#[groovy:message.getInboundProperty('MULE_REMOTE_CLIENT_ADDRESS')]"
level="INFO" doc:name="Logger" />
<cxf:proxy-service payload="envelope" service="Weather"
wsdlLocation="http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl"
namespace="http://ws.cdyne.com/WeatherWS/" validationEnabled="true"
doc:name="SOAP">
</cxf:proxy-service>
<copy-properties propertyName="SOAPAction" doc:name="Property" />
<cxf:proxy-client payload="body"
enableMuleSoapHeaders="true" doc:name="SOAP" />
<outbound-endpoint address="http://wsf.cdyne.com/WeatherWS/Weather.asmx"
exchange-pattern="request-response" doc:name="Generic">
</outbound-endpoint>
</flow>
My mule version is CE 3.3.0. I use component for define location of java class, but after running my project I have some error:
ERROR 2013-02-27 11:15:20,129 [[mule-sample_].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
Message : Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]}" does not implement required interface "interface org.mule.api.lifecycle.Callable"
ReflectionEntryPointResolver: Found too many possible methods on object "org.mule.example.scripting.IpClient" that accept parameters "{class java.lang.String}", Methods matched are "[public void org.mule.transformer.AbstractTransformer.setMimeType(java.lang.String) throws javax.activation.MimeTypeParseException, public final java.lang.Object org.mule.transformer.AbstractTransformer.transform(java.lang.Object) throws org.mule.api.transformer.TransformerException, public void org.mule.transformer.AbstractTransformer.setName(java.lang.String), public void org.mule.transformer.AbstractTransformer.setEncoding(java.lang.String)]"
AnnotatedEntryPointResolver: Component: IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]} doesn't have any annotated methods, skipping.
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
]
Code : MULE_ERROR-321
--------------------------------------------------------------------------------
Exception stack is:
1. Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]}" does not implement required interface "interface org.mule.api.lifecycle.Callable"
ReflectionEntryPointResolver: Found too many possible methods on object "org.mule.example.scripting.IpClient" that accept parameters "{class java.lang.String}", Methods matched are "[public void org.mule.transformer.AbstractTransformer.setMimeType(java.lang.String) throws javax.activation.MimeTypeParseException, public final java.lang.Object org.mule.transformer.AbstractTransformer.transform(java.lang.Object) throws org.mule.api.transformer.TransformerException, public void org.mule.transformer.AbstractTransformer.setName(java.lang.String), public void org.mule.transformer.AbstractTransformer.setEncoding(java.lang.String)]"
AnnotatedEntryPointResolver: Component: IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]} doesn't have any annotated methods, skipping.
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
] (org.mule.model.resolvers.EntryPointNotFoundException)
org.mule.model.resolvers.DefaultEntryPointResolverSet:52 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.model.resolvers.EntryPointNotFoundException: Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]}" does not implement required interface "interface org.mule.api.lifecycle.Callable"
ReflectionEntryPointResolver: Found too many possible methods on object "org.mule.example.scripting.IpClient" that accept parameters "{class java.lang.String}", Methods matched are "[public void org.mule.transformer.AbstractTransformer.setMimeType(java.lang.String) throws javax.activation.MimeTypeParseException, public final java.lang.Object org.mule.transformer.AbstractTransformer.transform(java.lang.Object) throws org.mule.api.transformer.TransformerException, public void org.mule.transformer.AbstractTransformer.setName(java.lang.String), public void org.mule.transformer.AbstractTransformer.setEncoding(java.lang.String)]"
AnnotatedEntryPointResolver: Component: IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]} doesn't have any annotated methods, skipping.
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
]
at org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:52)
at org.mule.component.DefaultComponentLifecycleAdapter.invoke(DefaultComponentLifecycleAdapter.java:343)
at org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:86)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

As per the cod eoyu have given to add the property
<set-variable variableName="remoteClientAddress" value ="#[message.inboundProperties ['MULE_REMOTE_CLIENT_ADDRESS']]"/>
This sets the property into the mulemessage in the INVOCATION scope.
You can access the property in your IpClient component with the following line of code
msg.getProperty("remoteClientAddress", PropertyScope.INVOCATION);
EX:
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
MuleMessage msg = eventContext.getMessage();
String remClient = msg.getProperty("remoteClientAddress", PropertyScope.INVOCATION);
Hope this helps.

<message-properties-transformer name="myScope" scope="session">
<add-message-property key="remoteClientAddress"
value ="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
</message-properties-transformer>

Related

Spring Integration - XML Schema validator - Dynamic schema file selection

I am trying to implement XML validation using Spring Integration <int-xml:validating-filter />. I followed the discussion in usage of spring integration's schema validator?. The problem statement is the same but with an additional parameter. Instead to hard coding the value in schema-location="xyz.xsd", rather I want to dynamically select the appropriate xsd file for respective incoming xml or DOMSource inputs.
I also followed http://forum.spring.io/forum/spring-projects/integration/121115-dynamic-schema-location-for-xml-validating-filter-component where Gary Russell mentioned:
There's no direct support for dynamic schemas, but you can provide a
custom XmlValidator using the xml-validator attribute (mutually
exclusive with schema location)
Once you've introspected your document to find the schema you wish to
validate against, simply delegate to a validator that has been
configured to validate against that schema.
You can use a XmlValidatorFactory to create each validator; see the
XmlValidatingMessageSelector for how to create a validator, once you
know the schema location
Since the comments dates back to the year 2012, is there an approach now in spring integration to validate input xml by dynamically selecting appropriate schema? If not can anyone provide an example on how to implement?
Following is my spring integration configuration:
<int:gateway id="applicationServiceGateway" service-interface="abc.IGateway"
default-request-channel="applicationRequestChannel" default-reply-channel="applicationResponseChannel"
error-channel="errorProcessingChannel" />
<int:chain id="serviceRequestValidation" input-channel="applicationRequestChannel" output-channel="responseChannel">
<!-- How to do -->
<int-xml:validating-filter xml-validator="xmlValidator"
schema-type="xml-schema"
throw-exception-on-rejection="true" />
<int:service-activator id="schematronValidationActivator" ref="schematronValidator" method="validate" />
</int:chain>
<bean id="xmlValidator" class="abc.validator.DomSourceValidator" />
Here is my Validator class defined:
import org.springframework.xml.validation.ValidationErrorHandler;
import org.springframework.xml.validation.XmlValidator;
import org.xml.sax.SAXParseException;
public class DomSourceValidator implements XmlValidator {
#Override
public SAXParseException[] validate(Source source) throws IOException {
/* How to implement this method?
Using XPath I can identify the root node from 'source' and then load
the appropriate XSD file. But don't know how to proceed
or what should be 'return'(ed) from here.
Any example is much appreciated.
*/
return null;
}
#Override
public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException {
// TODO Auto-generated method stub
return null;
}
}
Which is the best way of implementing the XML validator using Spring Integration?
There have been no changes since that comment.
As I said there, your validator needs to use XmlValidatorFactory to create a validator for each schema; then call a specific validator for each message; something like:
String schema = determineSchema(source);
XmlValidator val = lookupValidatorForSchema(schema);
if (val == null) {
// create a new one and add it to the map.
}
return val.validate(source);
If it helps other folks who are trying to do the same
Based on Gary's suggestion, I have come out with an implementation of XmlValidator by dynamically identifying input XML and then selecting appropriate Schema file to apply the validation.
Below is my spring integration configuration:
<int:gateway id="applicationServiceGateway" service-interface="abc.IGateway" default-request-channel="applicationRequestChannel" default-reply-channel="applicationResponseChannel" error-channel="errorProcessingChannel" />
<int:chain id="serviceRequestValidation" input-channel="applicationRequestChannel" output-channel="responseChannel">
<int-xml:validating-filter xml-validator="xmlValidator"
schema-type="xml-schema"
throw-exception-on-rejection="true" /> <!-- a MessageRejectedException is thrown in case validation fails -->
<int:service-activator id="schematronValidationActivator" ref="schematronValidator" method="validate" />
</int:chain>
<bean id="xmlValidator" class="abc.validator.DomSourceValidator">
<constructor-arg>
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="OTA_AirAvailRQ" value="common/schemas/FS_OTA_AirAvailRQ.xsd" />
<entry key="OTA_AirBookModifyRQ" value="common/schemas/FS_OTA_AirBookModifyRQ.xsd" />
<entry key="OTA_AirBookRQ" value="common/schemas/FS_OTA_AirBookRQ.xsd" />
</map>
</constructor-arg>
</bean>
To demonstrate I have used the OTA schema files to construct a map as constructor-arg. The map key is the root node from the XML file from the gateway and value is the location of the xsd file; and form the key-value pair map.
Refer to the below implementation class how this map is being used to identify the input XML and apply the validation.
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
import org.springframework.xml.validation.ValidationErrorHandler;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
import org.xml.sax.SAXParseException;
public class DomSourceValidator implements XmlValidator {
private static final Log LOGGER = LogFactory.getLog(DomSourceValidator.class);
private Map<String, String> schemaMap;
private static Map<String, XmlValidator> validatorMap = new HashMap<>();
public DomSourceValidator(Map<String, String> schemaMap) {
this.schemaMap = schemaMap;
}
#PostConstruct
private void init() throws IOException {
LOGGER.info("Constructing Validators from schema resource list ...");
Assert.notEmpty(schemaMap, "No schema resource map found");
if (validatorMap.isEmpty()) {
XmlValidator validator = null;
for (Map.Entry<String, String> entry : schemaMap.entrySet()) {
validator = createValidatorFromResourceUri(entry.getValue());
validatorMap.put(entry.getKey(), validator);
}
}
}
#Override
public SAXParseException[] validate(Source source) throws IOException {
Assert.notNull(schemaMap, "No validator(s) defined");
XmlValidator validator = lookupValidator(source);
return validator.validate(source);
}
#Override
public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException {
// Skip implementation
return null;
}
private XmlValidator lookupValidator(Source source) {
String reqType = determineRequestType(source);
LOGGER.info("Loading validator for type: " + reqType);
XmlValidator xmlValidator = validatorMap.get(reqType);
Assert.notNull(xmlValidator, "No validator found for type: " + reqType);
return xmlValidator;
}
private String determineRequestType(Source source) {
if (source instanceof DOMSource) {
return ((DOMSource) source).getNode().getFirstChild().getNodeName();
}
return null;
}
private XmlValidator createValidatorFromResourceUri(String schemaResource) throws IOException {
Assert.notNull(schemaResource);
return XmlValidatorFactory.createValidator(new ClassPathResource(schemaResource), XmlValidatorFactory.SCHEMA_W3C_XML);
}
}
As soon as the spring bean id="xmlValidator" is initialized, the #PostConstruct kicks in to create Validator instances using XmlValidatorFactory from the resource URIs to have a pre-initialized validators.
If there is a validation error, a org.springframework.integration.MessageRejectedException: Message was rejected due to XML Validation errors is thrown (ref. throw-exception-on-rejection="true" in the <int-xml:validating-filter />).
The above implementation works perfectly fine for me. One can customize it further, or post another version to achieve the same.
Note
Instead of using a <int-xml:validating-filter />, one can also use a <int:service-activator /> in the <int-chain />, as logically <int-xml:validating-filter /> does not actually do any filter logic. But it serves the purpose.

Passing multiple parameters in outbound gateway of Spring Integration

I have the following Configuration :
<int-http:outbound-gateway id="opgateway" request-channel="rallyUpdateRequest"
url="https://rally1.rallydev.com/slm/webservice/v2.0/defect/9792864541?key={key}"
http-method="POST" request-factory="rallyHttpRequestFactory"
expected-response-type="java.lang.String"
transfer-cookies="true"
reply-channel="rallyUpdateResponse">
<int-http:uri-variable
name="key"
expression="payload.secondArg" />
</int-http:outbound-gateway>
And the Gateway interface :
//#interface
public interface RallyUpdater {
String updateDefect(#Payload String payload, #Header String key);
}
I have main Function:
public static void main(String... args) throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"context.xml");
....
RallyUpdater updater = ctx.getBean("rallyUpdateGateway", RallyUpdater.class);
String res=updater.updateDefect(defect,key);
System.out.println("Response ::::::::::"+res);
This throws me an error:
*
Exception in thread "main" java.lang.IllegalArgumentException: Cannot determine header name. Possible reasons: -debug is disabled or header name is not explicitly provided via #Header annotation.
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.determineHeaderName(GatewayMethodInboundMessageMapper.java:229)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.access$500(GatewayMethodInboundMessageMapper.java:80)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper$DefaultMethodArgsMessageMapper.toMessage(GatewayMethodInboundMessageMapper.java:300)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper$DefaultMethodArgsMessageMapper.toMessage(GatewayMethodInboundMessageMapper.java:262)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.mapArgumentsToMessage(GatewayMethodInboundMessageMapper.java:163)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.toMessage(GatewayMethodInboundMessageMapper.java:158)
at org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.toMessage(GatewayMethodInboundMessageMapper.java:80)
at org.springframework.integration.support.converter.SimpleMessageConverter.toMessage(SimpleMessageConverter.java:82)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.doConvert(AbstractMessageSendingTemplate.java:157)
at org.springframework.messaging.core.AbstractMessagingTemplate.convertSendAndReceive(AbstractMessagingTemplate.java:78)
at org.springframework.messaging.core.AbstractMessagingTemplate.convertSendAndReceive(AbstractMessagingTemplate.java:70)
at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:347)
at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceive(MessagingGatewaySupport.java:324)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:414)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:374)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:365)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy9.updateDefect(Unknown Source)
at com.aa.qcrallysyncmgmt.base.Main.main(Main.java:49)
*
**I need to pass the payload as post data and the key in the URL.
Why this throws error while we can give the payload.secondArg?
Any other configurations ?
Why just don't follow with the recommendation from the StackTrace?
updateDefect(#Payload String payload, #Header("key") String key);
From other side it isn't clear to me how you are going to do this payload.secondArg, since you payload is String.
For me the config should be like this:
<int-http:uri-variable name="key" expression="headers.key" />
Otherwise you should show the <gateway> configuration too.

JAXB (Moxy) XML Metadata mapping issue

I am trying to map the below interface using Moxy's XML Metadata extension. But when I try to load it, I am getting the below error. I can't add a public constructor to the AddressType as it is an enum.
My question is: Why is Moxy impl looking at AddressType even though I didn't specify in the xml metadata?
public interface TokenizedUnitedStatesAddress
{
class AddressType extends Enum
{
public static final AddressType STREET = new AddressType("street");
public static final AddressType PO_BOX = new AddressType("poBox");
public static final AddressType RURAL_ROUTE = new AddressType("ruralRoute");
public static AddressType getEnum(final String type)
{
return (AddressType) getEnum(AddressType.class, type);
}
protected AddressType(final String name)
{
super(name);
}
}
String getApartmentNumber();
//removed other getters for brevity
}
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
version="2.4" package-name="com.abc.ic.domain.country.us">
<java-types>
<java-type name="TokenizedUnitedStatesAddress">
<xml-root-element />
<xml-type
prop-order="StreetPreDirection StreetNumber StreetName StreetType StreetPostDirection UnitDesignator UnitNumber AddressLine1 AddressLine2 City State PostalCode CarrierRoute LengthAtAddress OwnershipStatus" />
<java-attributes>
<xml-element name="StreetPreDirection" java-attribute="preDirectional" />
<xml-element name="StreetNumber" java-attribute="houseNumber" />
<xml-element name="StreetName" java-attribute="streetName" />
<xml-element name="StreetType" java-attribute="streetType" />
<xml-element name="StreetPostDirection" java-attribute="postDirection" />
<xml-element name="UnitNumber" java-attribute="apartmentNumber" />
<xml-element name="AddressLine1" java-attribute="primaryAddress" />
<xml-element name="AddressLine2" java-attribute="secondaryAddress" />
<xml-element name="City" java-attribute="cityName" />
<xml-element name="State" java-attribute="stateAbbreviation" />
<xml-element name="PostalCode" java-attribute="zipCode" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
javax.xml.bind.JAXBException:
Exception Description: The class com.abc.ic.domain.country.us.TokenizedUnitedStatesAddress$AddressType requires a zero argument constructor or a specified factory method. Note that non-static inner classes do not have zero argument constructors and are not supported.
- with linked exception:
[Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The class com.abc.ic.domain.country.us.TokenizedUnitedStatesAddress$AddressType requires a zero argument constructor or a specified factory method. Note that non-static inner classes do not have zero argument constructors and are not supported.]
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:908)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:157)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:170)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:157)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:117)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:107)
Mxoy impl still introspects the class even though XML is used to provide metadata. This is because, by design, the external mapping file is used to augment metadata supplied by annotations.
The issue however is that the commons-land Enum abstraction requires us to have a non-public single argument constructor for the enums. I fixed this issue by adding a public no-arg constructor that initializes a default enum. This is sufficient for my application. I've however created a bug which can be followed here.
Note: I also looked at the foctory-method option of Moxy but it requires an empty arg method as the factory method which is not the case in case of Enum.
I am a developer on the EclipseLink MOXy team, and I've been looking at this issue. You are correct as to why the AddressType class was introspected, and I see that you have a workaround.
Another solution would be to create an XmlAdapter that can convert between Apache Enum classes and their XML (string) representation, as follows:
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.apache.commons.lang.enums.Enum;
import enumbindings.TokenizedUnitedStatesAddress.AddressType;
public class ApacheEnumAdapter extends XmlAdapter<String, Enum> {
public ApacheEnumAdapter() {
}
#Override
public Enum unmarshal(String s) throws Exception {
return AddressType.getEnum(s);
}
#Override
public String marshal(Enum e) throws Exception {
if (null == e) {
return null;
}
return e.getName();
}
}
And then hook up the adapter in your bindings file like this:
...
<xml-element name="StreetType" java-attribute="streetType">
<xml-java-type-adapter value="enumbindings.ApacheEnumAdapter" />
</xml-element>
...
As far as the bug you entered, EclipseLink is actually behaving correctly in this situation, we do not do any special handling of Apache Commons classes and so a default no-arg constructor (or some other handling mechanism) is still required. However I will update your bug and change it to an enhancement request to support Apache Enums out of the box, and we will evaluate it.
Thanks,
Rick

Jaxb EclipseLink/MOXy : Is it possible to specify the names of get/set methods

I have a quite simple question :
Say I have a model class defined like this :
public class Test{
private String testAttribute;
public Test(){
}
public String getFormattedTestAttribute(){
return testAttribute + "A nice formatted thingy"; //right, this is just an example
}
public void setTestAttribute(String value){
testAttribute = value;
}
}
You can see that I have a standard setter for testProperty but the getter has a different name : getFormattedTestProperty().
Is it possible into Jaxb/Moxy to specify which getter to use for a specific property ?
I'm using MOXy implementation with external metadata bindings file. The project which I'm working on used tu use Castor. Into Castor's mapping files, you could specify which getter/setter to use like that :
<field name="testAttribute"
get-method="getFormattedTestAttribute">
<bind-xml name="test-attribute" node="attribute"/>
</field>
Is the same kind of thing possible with moxy's external metadata ?
If that kind of customization isn't supported, is it possible to mark a field as read-only and another as write-only ? so I could declare a read-only property named "formattedTestAttribute" and a write-only property named "testAttribute" into the metadata bindings file ?
<!-- read only property -->
<xml-element java-attribute="formattedTestAttribute" xml-path="#test-attribute" />
<!-- write only property -->
<xml-element java-attribute="testAttribute" xml-path="#test-attribute" />
Please note that I have very limited control over the model classes.
Thanks in advance for your answers.
You could represent this in EclipseLink JAXB (MOXy)'s external mapping document as follows:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum8834871">
<java-types>
<java-type name="Test" xml-accessor-type="PUBLIC_MEMBER">
<xml-root-element/>
<java-attributes>
<xml-element
java-attribute="testAttribute"
name="test-attribute">
<xml-access-methods
get-method="getFormattedTestAttribute"
set-method="setTestAttribute"/>
</xml-element>
<xml-transient java-attribute="formattedTestAttribute"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Test
I have modified your Test class, to put some logic in the get/set methods.
package forum8834871;
public class Test{
private String testAttribute;
public Test(){
}
public String getFormattedTestAttribute(){
return "APPENDED_ON_GET " + testAttribute;
}
public void setTestAttribute(String value){
testAttribute = "APPENDED_ON_SET " + value;
}
}
Demo
package forum8834871;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8834871/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Test.class}, properties);
File xml = new File("src/forum8834871/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Test test = (Test) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(test, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<test>
<test-attribute>ORIGINAL</test-attribute>
</test>
Output
<?xml version="1.0" encoding="UTF-8"?>
<test>
<test-attribute>APPENDED_ON_GET APPENDED_ON_SET ORIGINAL</test-attribute>
</test>

Unmarshalling MTOM message using Camel Spring-WS and JAXB dataformat

1) Is there a way to unmarshall MTOM message using Camel Spring-WS component?
2) I tried with Camel JAXB dataformat. It didn't work. Datahandler doesn't have any content. Does JAXB dataformat support MTOM?
<dataFormats>
<jaxb id="jaxb" contextPath="com.mycompany.hr.jaxb"/>
</dataFormats>
<route>
<from uri="spring-ws:rootqname:{http://mycompany.com/hr/schemas}HolidayRequest?endpointMapping=#endpointMapping" />
<unmarshal ref="jaxb"/>
<process ref="testProcessor" />
</route>
3) I thought MTOM is not enabled in JAXB dataformat. So I created a custom dataformat using MTOM enabled JAXB2Marshaller. But still facing the same issue.
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.transform.Source;
import org.apache.camel.Exchange;
import org.apache.camel.spi.DataFormat;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
public class MtomDataFormat implements DataFormat {
public void marshal(Exchange arg0, Object arg1, OutputStream arg2)
throws Exception {
// TODO Auto-generated method stub
}
public Object unmarshal(Exchange exchange, InputStream is) throws Exception {
Source source = exchange.getContext().getTypeConverter().mandatoryConvertTo(Source.class, is);
Jaxb2Marshaller mar = new Jaxb2Marshaller();
mar.setContextPath("com.mycompany.hr.jaxb");
mar.setMtomEnabled(true);
return mar.unmarshal(source);
}
}
Spring configuration
<bean id="endpointMapping"
class="org.apache.camel.component.spring.ws.bean.CamelEndpointMapping">
</bean>
<bean id="testProcessor" class="TestProcessor" />
<bean id="mtomDataFormat" class="MtomDataFormat" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="spring-ws:rootqname:{http://mycompany.com/hr/schemas}HolidayRequest?endpointMapping=#endpointMapping" />
<unmarshal ref="mtomDataFormat"/>
<process ref="testProcessor" />
</route>
</camelContext>

Resources