Delete all the keys of ehcache - rest

I'm using Ehcache with Apache camel. I'm exposing a rest endpoint which should delete all the keys from Ehcache. For some reason, its not deleting the keys after calling the rest endpoint.
Following code is being used to achieve the same :
<restConfiguration component="servlet"
bindingMode="json" />
<rest path="/clear">
<get uri="/ehcache">
<to uri="direct:clear_ehcache" />
</get>
</rest>
<route id="clear_ehcache_001">
<from uri="direct:clear_ehcache" />
<setHeader headerName="CamelCacheOperation">
<constant>CamelCacheDeleteAll</constant>
</setHeader>
<setHeader headerName="CamelEhcacheAction">
<constant>REMOVE_ALL</constant>
</setHeader>
<to uri="ehcache://mycache" />
</route>
I've been stuck on this for a while now as there are no errors generated.

So it worked with the following piece of code instead :
<!-- Rest endpoint for clearing ehcache : Start -->
<restConfiguration component="servlet"
bindingMode="json" />
<rest path="/clear">
<get uri="/ehcache">
<to uri="direct:clear_ehcache" />
</get>
</rest>
<!-- Rest endpoint for clearing ehcache : End -->
<!-- Route for clearing ehcache : Start -->
<route id="clear_ehcache_001">
<from uri="direct:clear_ehcache" />
<log id="_logging_clear_ehcache"
message="Clearing Ehcache records.." />
<setHeader headerName="CamelEhcacheAction"
id="getCamelEhCacheAction003">
<constant>CLEAR</constant>
</setHeader>
<to id="_to4"
uri="ehcache://mycache?keyType=java.lang.String&valueType=java.lang.String" />
</route>
<!-- Route for clearing ehcache : End -->

Related

Configure different handlers for APIs based on HTTP method in WSO2 synapse file

I have a WSO2 synapse file to handle my API requests at gateway level. I have configured my GET and POST/PUT for an endpoint as below. I have a custom handler which deals with allowing a specific host to call these APIs. I have a requirement to allow GET from any host and POST/PUT only from a specific host. However, I cannot configure it in my synapse file because handlers which we define in the file are getting applied to all HTTP methods. Is there way to configure handlers based on HTTP method for the same endpoint/context. How I can have my custom handler (com.abc.wso2.handler.HostRestrictionHandler) applicable to only POST?PUT calls? Below is my configuration.
<api xmlns="http://ws.apache.org/ns/synapse" context="/user/notification/v3" version="v2" version-type="context">
<resource methods="GET" uri-template="/*">
<inSequence>
<class name="org.wso2.carbon.apimgt.gateway.mediators.TokenPasser"/>
<filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE">
<then>
<class name="com.abc.mediators.DispatchMediator">
<property name="url" value="http://api.{HOST_NAME}.com/user/notification/v3"/>
</class>
<send>
<endpoint name="Notification_API">
<address>
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>-1</errorCodes>
<initialDuration>0</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>-1</errorCodes>
</markForSuspension>
</address>
</endpoint>
</send>
</then>
<else>
<sequence key="_sandbox_key_error_"/>
</else>
</filter>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
<resource methods="POST PUT" uri-template="/*">
<inSequence>
<property name="X-Forwarded-Host" scope="transport" expression="$trp:Host"/>
<class name="org.wso2.carbon.apimgt.gateway.mediators.TokenPasser"/>
<filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE">
<then>
<class name="com.abc.mediators.DispatchMediator">
<property name="url" value="http://api.{HOST_NAME}.com/user/notification/v3"/>
</class>
<send>
<endpoint name="Notification_API_2">
<address>
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>-1</errorCodes>
<initialDuration>0</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>-1</errorCodes>
</markForSuspension>
</address>
</endpoint>
</send>
</then>
<else>
<sequence key="_sandbox_key_error_"/>
</else>
</filter>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
<handlers>
<handler class="com.abc.wso2.handler.HostRestrictionHandler">
<property name="allowedHosts" value="my.customhost.com" />
</handler>
</handlers>
</api>
You can use this(context.getProperty("api.ut.HTTP_METHOD")) to get the method resource and this(context.getProperty("api.ut.hostName")) to get the host. So you can implement a logic in your handler to check the host and the resource and then allow the request.

response body is empty after write log for body

I am testing fuse63 on spring dsl for rest service, I need to proxy a rest service on camel for back end system, here is excerpt:
<camelContext id="_camelContext1_rest" xmlns="http://camel.apache.org/schema/spring">
<restConfiguration component="spark-rest" port="9091"/>
<rest path="/say">
<post consumes="application/json" produces="text/plain" uri="/hellopost">
<to uri="direct:hellopost"/>
</post>
...
<route id="_route_saypost">
<from id="_from_saypost" uri="direct:hellopost"/>
<log id="_log_saypost" message="this is hellopost"/>
<to id="_file_saypost" uri="file:/tmp2/target/messages/hellopost?fileName=${header.fileName}"/>
<setHeader headerName="camel-test" id="_setHeader1">
<constant>this is camel</constant>
</setHeader>
<setHeader headerName="CamelHttpMethod" id="_setHeader2">
<constant>POST</constant>
</setHeader>
<log id="_log_saypost_before_rest" message="before rest"/>
<to id="_to1" uri="http4://192.168.56.11:8080/test/httpheader.jsp?bridgeEndpoint=true&synchronous=true"/>
<setHeader headerName="Content-Type" id="_setHeader2">
<constant>application/json</constant>
</setHeader>
</route>
This works fine, but if I add the logging action at the end of the route:
<log id="_log_saypost_after_rest3" message="${body}"/>
</route>
the response body from camel(localhost:9091/say/hellopost) will be empty, What's the reason for the response body being cleared after a logging action?
Best regards
Lan
Use Stream caching like
<route streamCache="true">
This http://camel.apache.org/why-is-my-message-body-empty.html answers to your question.
In fact you don't have to use directy
<log />
but you have to use
<to uri="log: "/>
https://camel.apache.org/components/3.15.x/log-component.html

Process cxf request with amq

I want to make such a solution:
cxf https soap service gets a request and sends it to activemq queue
1
service implementation gets message from queue 1, process it and
puts to queue 2
endpoint gets response from queue 2 and sends
response to a client
Now, I came with a kind of a solution but I'm not sure how to process response from activemq and send back as SOAP response. My camel blueprints below. Endpoint blueprint:
<?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"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:soap="http://cxf.apache.org/blueprint/bindings/soap"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
http://cxf.apache.org/blueprint/bindings/soap http://cxf.apache.org/schemas/configuration/blueprint/soap.xsd">
<bean id="cardServiceEndpoint" class="com.endpoint.card.CardEndpoint">
<argument>
<reference interface="com.card.CardService" />
</argument>
</bean>
<cxf:cxfEndpoint
id="cardEndpoint"
address="https://host:port/soa/card"
serviceClass="com.card.CardService">
<cxf:properties>
<entry key="schema-validation-enabled" value="true" />
</cxf:properties>
</cxf:cxfEndpoint>
<bean id="jaxB" class="org.apache.camel.model.dataformat.JaxbDataFormat">
<property name="prettyPrint" value="true" />
<property name="contextPath" value="com.type.card" />
</bean>
<camelContext id="endpoint-card-cxf" xmlns="http://camel.apache.org/schema/blueprint">
<route id="endpoint-soap-in">
<from uri="cxf:bean:cardEndpoint"/>
<transform>
<simple>${body[0]}</simple>
</transform>
<marshal ref="jaxB"/>
<setHeader headerName="JMSType">
<simple>${headers.operationName}</simple>
</setHeader>
<to uri="amq:q.in"/>
</route>
<route id="endpoint-soap-out">
<from uri="amq:q.out" />
<unmarshal ref="jaxB" />
<!-- STUCK HERE :( -->
</route>
</camelContext>
</blueprint>
Service processor blueprint:
<?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"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<bean id="cardService" class="com.card.impl.DefaultCardService">
<argument>
<reference interface="com.base.StashService"/>
</argument>
</bean>
<service interface="com.card.CardService" ref="cardService" />
<bean id="amqCardServiceEndpoint" class="com.card.endpoint.AmqCardEndpoint">
<argument ref="cardService" />
</bean>
<bean id="jaxB" class="org.apache.camel.model.dataformat.JaxbDataFormat">
<property name="prettyPrint" value="true" />
<property name="contextPath" value="com.type.base:com.type.card" />
</bean>
<camelContext id="service-card-cx" xmlns="http://camel.apache.org/schema/blueprint">
<route id="card-rq-broker">
<from uri="amq:queue:q.in?asyncConsumer=true" />
<unmarshal ref="jaxB" />
<doTry>
<bean ref="amqCardServiceEndpoint" method="invoke" />
<doCatch>
<exception>com.type.base.BaseException</exception>
<setBody>
<simple>${exception.getFaultInfo()}</simple>
</setBody>
</doCatch>
</doTry>
<marshal ref="jaxB" />
<to uri="amq:q.out" />
</route>
</camelContext>
</blueprint>
Any help or advice?
Use jmsReplyTo to specify the name of the reply queue (if you want a fixed queue name) that Camel should use for listening for the response. See more about request/reply on the Camel JMS documentation.
<to uri="amq:q.in?jmsReplyTo=q.out"/>
// continue here when reply is back
http://camel.apache.org/jms

WSO2 : Determining that a message contains a specific element or not

How can i determine that a message contains a specific element? i want to enrich the message if it does not contain specific element, but i do not know how to determine it?
thank you.
You can use the filter mediator to perform content based mediation. Following sample shows your usecase. (1) The filtering was done using the xpath matching. It searches for NOT occurrence of XPath //p:echoString/test, and performs enrich mediation based on that. Following soap body will match the filter. (2)
(1)
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="messageFilter" transports="http https" startOnLoad="true" trace="disable">
<target>
<inSequence>
<log level="full" separator=","/>
<filter xpath="not(//p:echoString/test)" xmlns:p="http://echo.services.core.carbon.wso2.org" >
<then>
<log separator=",">
<property name="XPath Matched" value="true"/>
</log>
<enrich>
<source clone="true" xpath="//p:echoString/in"/>
<target type="property" property="ORIGINAL_REQ"/>
</enrich>
<log separator=",">
<property name="ORIGINAL_REQ" expression="get-property('ORIGINAL_REQ')"/>
</log>
</then>
<else>
<log separator=",">
<property name="XPath Matched" value="false"/>
</log>
</else>
</filter>
<send>
<endpoint>
<address uri="http://localhost:9763/services/echo"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full" separator=",">
<property name="OUT-SEQUENCE" value="property_value"/>
</log>
<send/>
</outSequence>
<faultSequence/>
</target>
</proxy>
(2)
<body>
<p:echoString xmlns:p="http://echo.services.core.carbon.wso2.org">
<in>123</in>
<test>testing-node</test>
</p:echoString>
</body>

JBoss ESB XML MEP Behviour

I am using JBoss AS 5.1.0 and Jboss ESB 4.10
I am trying to Invoke a Service which has a single action. I have Set MEP = oneWay for the Service.
When I Invoke the Service Using the Below Method I do not get a reply but an Exception.
new ServiceInvoker("Chapter3Sample", "Chapter3Service").deliverSync(esbMessage, 10000);
WHen I change mep=RequestResponse : I am able to get the Reply
As per my understanding ESB Message has a ReplyTo field (Since I am invkoing a Sync Request) the Message should be returned back by the last Action which is not happening in my case. Please find below the ESB XML:
<?xml version="1.0"?>
<jbossesb parameterReloadSecs="5"
xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd">
<providers>
<jms-provider connection-factory="ConnectionFactory" name="JBossMQ">
<jms-bus busid="chapter3GwChannel">
<jms-message-filter dest-name="queue/chapter3_Request_gw" dest-type="QUEUE"/>
</jms-bus>
<jms-bus busid="chapter3EsbChannel">
<jms-message-filter dest-name="queue/chapter3_Request_esb" dest-type="QUEUE"/>
</jms-bus>
</jms-provider>
</providers>
<services>
<service category="Chapter3Sample"
description="A template for Chapter3" name="Chapter3Service">
<listeners>
<jms-listener busidref="chapter3GwChannel" is-gateway="true" name="Chapter3GwListener"/>
<jms-listener busidref="chapter3EsbChannel" name="Chapter3Listener"/>
</listeners>
<actions mep="OneWay">
<action class="org.jboss.soa.esb.samples.chapter3.MyAction"
name="BodyPrinter">
<property name="process" value="displayMessage"/>
<property name="symbol" value="*"/>
<property name="count" value="50"/>
<property name="propertyName">
<hierarchicalProperty attr="value">
<inner name="myName" random="randomValue"/>
</hierarchicalProperty>
</property>
<property name="exceptionMethod" value="processException"/>
<property name="okMethod" value="processSuccess"/>
</action>
</actions>
</service>
</services>
</jbossesb>
When your are invoking call as synchronus.
new ServiceInvoker("Chapter3Sample", "Chapter3Service").deliverSync(esbMessage, 10000).
set mep=RequestResponse.
when your are invoking call asynchronus.
new ServiceInvoker("Chapter3Sample", "Chapter3Service").deliverASync(esbMessage, 10000).
set mep=oneWay .