I am using jaxrs with Apache cxf . Below is the xml config
<jaxrs:server id="accountrs" address="/rservice">
<jaxrs:serviceBeans>
<ref bean="accountService"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean='jsonProvider' />
</jaxrs:providers>
</jaxrs:server>
For some of my post methods i see that that the double byte charectors are getting distorted and appearing as garbled charectors and getting stored in the database .
I am reading the json body as string and not as any bean in my service implementation method . Below is the sample
#POST
#Path("/accounts/")
public Account getAccount(String jsonBody) {
//Business code goes here
}
I am stuck with this for a while now . Can some one help
Try adding #Consumes("application/json;charset=utf-8") to your getAccount resource. You may also need to specify a #Produces annotation too with a content type and a charset.
Related
I am trying to implement a module to send messages from a CXF client to a server (SOAP endpoint) using HTTPS. I am able to achieve this by following the guide here: https://camel.apache.org/how-to-switch-the-cxf-consumer-between-http-and-https-without-touching-the-spring-configuration.html
The following configuration is key:
<ctx:property-placeholder location="classpath:orderEntry.cfg" />
<!-- other properties -->
<http:conduit name="{http://www.company.com/product/orderEntry/service/1}OrderEntry.http-conduit">
<http:tlsClientParameters disableCNCheck="true">
<sec:trustManagers>
<sec:keyStore type="JKS" password="${trustStore.password}" file="${trustStore.file}"/>
</sec:trustManagers>
<!-- other config -->
</http:tlsClientParameters>
</http:conduit>
The above configuration refers to a config file that has these properties stored:
orderEntry.cfg
--------------
endpointUri=https://localhost:8181/OrderEntry
trustStore.password=password
trustStore.file=etc/myApp.ts
As noted earlier, I am able to send messages via https when I follow the guide.
But I am concerned about the password being stored in plain text here. Is there a way that I can have the password wired from Java code (which can probably read the password from an encrypted source) and provide it to the http conduit when it needs it?
Have you tried location attribute value with file prefix?
E.g. location="file:/my/custom/location/orderEntry.cfg"
See: https://stackoverflow.com/a/17303537
Update:
If it works with your custom bean, you can try create trust managers as a bean and inject it into the conduit configuration like bellow:
blueprint.xml
<bean id="serviceTrustManager"
class="my.app.security.KeyStores" factory-method="loadTrustManagers">
<argument index="0" value="${my.app.service.trustStorePath}"/>
<argument index="1" value="${my.app.service.trustStoreEncryptedPassword}"/>
</bean>
<http:conduit name="{http://www.company.com/product/orderEntry/service/1}OrderEntry.http-conduit">
<http:tlsClientParameters disableCNCheck="true">
<sec:trustManagers ref="serviceTrustManager"/>
</http:tlsClientParameters>
</http:conduit>
Java code:
public class KeyStores {
public static TrustManager[] loadTrustManagers(String trustStorePath, String trustStoreEncryptedPassword) {
String trustStoreDecryptedPassword = PasswordDescriptor.decryptPassword(trustStoreEncryptedPassword); //Password decryption logic here
KeyStore trustStore = KeyStores.loadKeyStore("JKS", trustStorePath, trustStoreDecryptedPassword); //IO logic here
TrustManagerFactory trustFactory;
try {
trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustFactory.init(trustStore);
} catch (NoSuchAlgorithmException | KeyStoreException ex) {
throw new IllegalStateException(ex);
}
return trustFactory.getTrustManagers();
}
}
I'm using camel 2.16.0
Created a camel route to invoke a web service, dataFormat as MESSAGE and i get the response normally.
and this route is invoked using ProducerTemlate
//payloadXml is a string which contains SOAP Request Message.
Object response = producerTemplate.requestBody("direct:invokeWS", payloadXml);
<route id="my_Sample_Camel_Route_with_CXF">
<from uri="direct:invokeWS" />
<to uri="cxf://http://localhost:8111/camel_MQ/TestService?wsdlURL=http://localhost:8111/camel_MQ/TestService?wsdl&serviceName={http://www.test.org/interface/test/ws}camel_MQ-ws&portName={http://www.test.org/interface/test}TestEndpoint&dataFormat=MESSAGE" />
<log message="------------->> ${body}" />
</route>
But once i change the dataFormat to "PAYLOAD"
I get exception.
Caused by: java.lang.IllegalArgumentException: The PayLoad elements cannot fit with the message parts of the BindingOperation. Please check the BindingOperation and PayLoadMessage.
at org.apache.camel.component.cxf.CxfEndpoint$CamelCxfClientImpl.setParameters(CxfEndpoint.java:1171)
Tried creating CxfPayload and then sent that to producerTeamplate while invoking the WS, but still the same Exception,
Finally I'm able to invoke WS using dataFormat as payload.
created CxfPayload object and added SOAP Headers and Body to it.
But still i was getting the same exception "The PayLoad elements cannot fit with the message parts of the BindingOperation"
Then I added defaultOperationName & defaultOperationNamespace headers while invoking the webservice as shown below.
<to uri="cxf:bean:camel_MQ_MQ-ws?dataFormat=PAYLOAD&defaultOperationName=TestService&defaultOperationNamespace=http://www.camel_MQ.org/interface&loggingFeatureEnabled=true" />
hope this helps ;-)
I'd like to configure a simple RabbitMQ message producer using Spring Integration constructs. The requirement is very basic: just a simple fire-and-forget, sending an event message to a queue, no response required.
I've configured the connection factory, RabbitTemplate and outbound channel adapter (see below), but missing the last piece: the code that actually sends the message out to the channel.
Thanks in advance.
<rabbit:connection-factory id="producerRabbitConnectionFactory"
channel-cache-size="${amqp.channel.cache.size}"
host="${amqp.hostname}"
port="${amqp.port}"
virtual-host="${amqp.vhost}"
username="${amqp.username}"
password="${amqp.password}"
requested-heartbeat="${amqp.heartbeat}"
/>
<bean id="producerRabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="producerRabbitConnectionFactory" />
<property name="exchange" value="${amqp.exchange.event}" />
<property name="routingKey" value="${amqp.routingKey.event}" />
</bean>
<int:channel id="outboundAmqpChannel" />
<int-amqp:outbound-channel-adapter id="outboundAmqpChannelAdapter"
channel="outboundAmqpChannel"
amqp-template="producerRabbitTemplate"
default-delivery-mode="NON_PERSISTENT"
lazy-connect="true"/>
The simplest is a Messaging Gateway. That way your code doesn't know your talking to an integration flow.
public interface Foo {
void bar(String foo);
}
<int:gateway service-interface="foo.Foo" default-request-channel="outboundAmqpChannel" />
Inject a Foo into your code and call it.
Is it possible to read mule configuration file path (file endpoints), smtp host /user/password (smtp endpoints) from database.We finally want to provide a User Interface , where the user can edit the properties through the screen.The normal properties file approach (key/Value) pairs was used earlier but needs to change to read these properties from the database.Any help on this will be greatly appreciated.
Yes, you can use a custom properties provider.
Its configuration would look like this:
<spring:bean class="org.mule.DatabasePropertiesProvider" id="DatabasePropertiesProvider"/>
<spring:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<spring:property name="properties">
<spring:bean factory-bean="DatabasePropertiesProvider" factory-method="getProperties" />
</spring:property>
</spring:bean>
And the code for DatabasePropertiesProvider is as simple as this:
public class DatabasePropertiesProvider {
public Properties getProperties() throws Exception {
Properties properties = new Properties();
// get properties from the database
return properties;
}
}
I need help with this question.
I'm using the camel-http component as shown here but I'm having trouble because the body I'm sending has unescaped ampersands. This is causing the query string on the receiving server to break the post into multiple post parameters.
I know I could create compiled routes in java, but I must use the spring xml dialect so that new routes may be create/changed in the config files without a recompile.
So, in short, I'd like to URL Encode the ${body} property on my route using the spring dialect as shown in the (obviously invalid) pseudocode below.
<setBody inheritErrorHandler="true" id="setBody2">
<simple>name=<urlencode>${body}</urlencode></simple>
</setBody>
Ok, I bit the bullet. I created a java POJO
package com.wufoo.camel;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.log4j.Logger;
public class PayloadEncoder {
public String getEncodedBody(String body) throws UnsupportedEncodingException {
Logger mylogger = Logger.getLogger("log4j.logger.org.apache.camel");
mylogger.info("Appending payload and URL Encoding");
String encodedBody = new StringBuffer()
.append("payload=")
.append(URLEncoder.encode(body, "UTF-8")).toString();
return encodedBody;
}
}
Then injected it into the context
<bean id="payloadEncoder" class="com.wufoo.camel.PayloadEncoder" />
And finally used a transform to encode the body
<transform>
<method bean="payloadEncoder" method="getEncodedBody"/>
</transform>
That works. If anyone can tell me what's wrong with this approach, please let me know.
You can also use groovy language, like this:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="file:camel/input"/>
<log message="Moving ${file:name} to the output directory"/>
<setBody>
<groovy>
"name=" + URLEncoder.encode(request.getBody(String.class));
</groovy>
</setBody>
<to uri="file:camel/output"/>
</route>
</camelContext>
</blueprint>