how to specify a codec in apache camel mina - dsl

I'm trying to use the hl7codec in apache mina. I can see how to do it using spring e.g.
beans:bean id="hl7codec" class="org.apache.camel.component.hl7.HL7MLLPCodec"
beans:property name="charset" value="iso-8859-1"
/beans:bean
but can't figure out how to do it using POJO / DSL i.e. my route is specified as
from("mina:tcp://0.0.0.0:21110?sync=true&codec=#hl7codec")
.to("file://test");
but this can't resolve the #hl7codec id.
Given an allergy to Spring, can anyone suggest an alternative way of specifying the codec or replicating the Spring dependency injection?

If you are not using Spring at all, you need to enlist the hl7codec in the Camel registry. In pure Java you can do this by
SimpleRegistry reg = new SimpleRegistry();
reg.put("hl7codec", new MyCodecObject());
CamelContext context = new DefaultCamelContext(reg);
The Camel in Action book covers this in chapter 4, section 4.3.1

Related

Is there any workaround for using channel name with dash character using spring integration 4 without xml?

I upgraded my spring integration 3.x to 4.x. And I removed all xml configuration and replace to java annotation in pojo files. But the problem is..
I want to use the channel name with '-' character. It is very good to recognize and distinguish each words in name. Besides, if the dash character is include, I can be sure it is a channel.
But, using dash character in naming method is not allowed. How can I solve this situation??
You just missed the simple hook from Spring Annotation configuration:
#Bean(name = "my-channel-with-dashes")
public MessageChannel myMessageChannel() {
return new DirectChannel();
}

Hibernate validator programmatic constraints declaration and JSF

I Have a JSF-2.2 web app on a WildFly 8.1 app server shiping Hibernate-validator 5.1
I want to set some constrainst programmaticaly using the fluent API, because they depends on the case for example a min and max of a #Size constraint could vary or a field could be #NotNull or not...
so I try to programmaticaly configure constraints such as describe here : http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html_single/#section-programmatic-api
I do somthing like that to try (in a EJB #Singleton #Startup):
HibernateValidatorConfiguration configuration = Validation
.byProvider( HibernateValidator.class )
.configure();
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
constraintMapping
.type( Car.class )
.property( "manufacturer", FIELD )
.constraint( new NotNullDef() )
.property( "licensePlate", FIELD )
.ignoreAnnotations()
.constraint( new NotNullDef() )
.constraint( new SizeDef().min( 2 ).max( 14 ) );
Validator validator = configuration.addMapping( constraintMapping )
.buildValidatorFactory()
.getValidator();
But then JSF don't use this new constraints mapping.
I can submit forms without problem even if I break the constraints programmaticaly set
I don't know how to configure the Validator or ValidatorFactory JSF is using or how to provide to JSF an other Validator or ValidatorFactory...
Or may be It's more about configuring WildFly server, something to do in a config file or JNDI, I don't have a clue...
EDIT
I try to bind new Validator and validator factory in JNDI
But I can't because "Naming context is read-only"
Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context = new InitialContext(jndiProperties);
context.bind("java:comp/Validator", factory.getValidator());
context.bind("java:comp/ValidatorFactory", factory);
Thank you Hardy
As you proposed I post Hibenate Validator improvement
https://hibernate.atlassian.net/browse/HV-955
There is no way atm to do what you are after. Hibernate Validator has indeed the programmatic mapping, but it is a Hibernate Validator specific feature. There is no way to bootstrap this functionality in a Bean Validation way. I am saying this, since the only way to customize your ValidatorFactory and hence Validator instance within the container is via validation.xml. And there is no mechanism for the fluent API in this configuration file.
Your JNDI idea is in principal good, but as you say, it is only read only.
validation.xml allows for vendor specific properties though. One could imagine a property like org.hibernate.validator.config_factory=acme.MyConfig. The value of the property would point to a fully specified class which would contain some sort of factory method which returns the programmatic mapping to be added to the configuration. Unfortunately, such a property does not yet exist. You could open an issue here though ;-)

JAXB marshalling in Apache Camel

I am new to Apache camel and need to perform a task where I need to marshal an object to XML file. I am using the below code but it is not working. Here, foo.pojo is package where JAXB annotated classes are present
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat("foo.pojo");
from("direct:start").marshal(jaxbDataFormat).to("file:C:/Users/Anand.Jain/Desktop/hello/abc.xml").end();
Option 1: Configure the context path
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat("foo.pojo");
OptionFactory or jaxb.index file must be defined in the given package as explained here.
Option 2: Configure the class(es) to be bound
JAXBContext jaxbContext = JAXBContext.newInstance(MyAnnotatedClass.class);
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(jaxbContext);
I prefere Option 2.
Option 1 is recently impossible as JaxbDataFormat(String) constructor is not available as you can see in official javadoc
Documentation seems to be outdated about this point.
EDIT: BE CAREFUL there's TWO JaxbDataFormat
I have understood: there's TWO jaxbDataFormat in camel ecosystem
one is in camel-core package org.apache.camel.model.dataformat
another in camel-jaxb package org.apache.camel.converter.jaxb

What is JAXB and JAXRS ? How are they related?

Sorry for this blunt question . But many use these 2 terms day in and day out yet I don't know .I did some study on this and knew what it is separately . But don't understand how it is related . I will share what I understood about these two first .
JAXB is XML-to-Java binding technology enabling transformations
between schema and Java objects and between XML instance documents
and Java object instances. Internally JAXB does all this conversions
between xml and java . This is a parser of xml and then it knows what
component in xml corresponds to what in java and it breaks .
Conversion of this answer from JAXB is done by tools like xjc ( or
codgen plugin) . Mapping may be like
xsd:string java.lang.String
xsd:integer java.math.BigInteger
JaxRs is different . This is set of specifications for handling
requests . Meaning that it says "GET("/foo") " means handle a get
call with url /foo . It only states that . How it is done ? Yes , that
is called implementation of this spec . There are number of
implementations like restlet , resteasy , jersey , apache cxf etc .
This is analogus to logic and way you implement in maths . the
algorithm idea is bucket search .This can be implemented in any way .
In java terms JaxRs is interface and these 4 restlet , resteasy ,
jersey , apache cxf are implementations of the interface .
Now please say if my understanding is correct . Then tell how they are related . Please help . If possible a pictorial explanation will be more helpful.
Your understanding is basically correct. JAXB and JAX-RS are both Java Community Process (JCP) standards with multiple implementations.
JAXB - Defines standardized metadata and runtime API for converting Java domain objects to/from XML.
JAX-RS - Defines standardized metadata and runtime API for the creation of RESTful services. By default for the application/xml media type JAX-RS will use JAXB to convert the objects to/from XML.
Example
In the following example when a GET operation is performed the JAX-RS implementation will return a Customer. A JAXB implementation will be used to convert that instance of Customer to the XML that the client will actually receive.
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
#Path("/customers")
public class CustomerResource {
#GET
#Produces(MediaType.APPLICATION_XML)
#Path("{id}")
public Customer read(#PathParam("id") int id) {
Customer customer = new Customer();
customer.setId(id);
customer.setFirstName("Jane");
customer.setLastName(null);
PhoneNumber pn = new PhoneNumber();
pn.setType("work");
pn.setValue("5551111");
customer.getPhoneNumbers().add(pn);
return customer;
}
}

Configure CXF JAX-WS service to work with MOXY

Although I've added a jaxb.properties with MOXY factory and I see that the JAXB was switched to moxy, CXF has a method named createRIContext in the JAXBUtils class which loads hard coded the sun JAXB implementation.
Is there a way to override it and use moxy instead?
The problematic code is the following:
// fall back if we're using another jaxb implementation
try {
riContext = JAXBUtils.createRIContext(contextClasses
.toArray(new Class[contextClasses.size()]), tns);
}
It loads hard coded the "com.sun.xml.bind.v2.ContextFactory" class and use it to create a JAXB context.
The 3.0.0-milestone2 version of CXF should handle Moxy quite a bit better. That said, there are still bugs in Moxy that have prevented all of the CXF unit and system tests to pass with it so we don't have the same level of confidence with Moxy as we do with the JAXB RI.
(any help with testing 3.0.0 would be greatly appreciated)

Resources