Showing posts with label Apache Camel. Show all posts
Showing posts with label Apache Camel. Show all posts

Friday, 7 October 2016

Apache Camel - https Rest

Problem:

Hi Guys I have assigned an task on adding https for Rest in Apache Camel. I spent more time on this to get ti working , finally achieved. I thought of sharing with community  for easy to go.

Solution:


  1. We need to add SSL Context Parameters
  2. Add "CamelContextId" to SSLContextParameters Section.
  3. Add "Id" to SSLContextParameters which it later can be linked to where ever we need in the Camel Context defined.

   

 <sslcontextparameters camelcontextid="RestLab" id="sslContext_rest" xmlns="http://camel.apache.org/schema/spring">
  <keymanagers keypassword="keypassword">
   <keystore password="keystorePassword" resource="yourKeystore.jks">
   </keystore>
  </keymanagers>
  <serverparameters clientauthentication="WANT">
   <clientparameters>
    <ciphersuitesfilter>
     <include>.* </include>
    </ciphersuitesfilter>
   </clientparameters>
  </serverparameters>
 </sslcontextparameters>

 <camelcontext id="RestLab">
  <restconfiguration bindingmode="auto" component="restlet" enablecors="true" port="8080" scheme="https">

   <endpointproperty key="sslContextParameters" value="#sslContext_rest">
    <corsheaders key="Access-Control-Allow-Origin" value="*">
     <corsheaders key="Access-Control-Allow-Headers" value="Origin, Accept, X-Requested-With,
                                Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers">
      <corsheaders key="Access-Control-Allow-Methods" value="PUT,POST,GET,DELETE,OPTIONS">
      </corsheaders>
     </corsheaders>
    </corsheaders>
   </endpointproperty>
  </restconfiguration>
 </camelcontext>
 



Thursday, 2 June 2016

Camel - Properties Loading using Java And Dynamic Loading of Routes XML



How to read properties from file in Camel Context and use it in anywhere ?

Solution:

I faced difficulty to get this working with camel context. That's why i wanna share with you.

Java version:

1.Load the Properties file and convert as Properties object in java.

2.
Properties props = new Properties();

try (InputStream propBody = IOUtils.toInputStream(body)){

props.loadFromXML(propBody);

} catch (Exception e) {

Logg the error here

}

3.
PropertiesComponent pcomp = new PropertiesComponent();
pc.setOverrideProperties(props);
exchange.getContext().addComponent("properties", pcomp);



Usage in Camel -Spring XML :

<log message="  TEST PROPERTY READ -  ${properties:serverURL}" loggingLevel="DEBUG"

logName="com.mycompnay.log" />





<enrich strategyRef="OrderPurifyStrategy">

<simple>${properties:serverURL}</simple>

</enrich>


Properties File:

serverURL= www.google.com


I used file component from camel to read the file for every change of content in file under "Test" Folder.

<route>
<from uri="file://Test?recursive=true&amp;noop=true&amp;delete=false&amp;runLoggingLevel=TRACE&amp;idempotent=true&amp;idempotentKey=${file:name}-${file:modified}" />
<bean ref="Someclass" method="updatePropertiesFile" />
</route>


Dynamic Loading of Routes Xml in Camel:

String body = body of the file.

try(InputStream is  =IOUtils.toInputStream(body)) {
                RoutesDefinition routes = exchange.getContext().loadRoutesDefinition(is);
                exchange.getContext().addRouteDefinitions(routes.getRoutes());
            } catch (Exception e) {
                logger.error("Error while routes ["+fileName+"]loading .....",e);
            }

you cannot use bean defintion in the same routes.xml, for this bean definition purpose we have to define new xml file with spring beans xml namespace.




Wednesday, 11 May 2016

CORS - Cross Domain-Resource Sharing , Camel Restlet


Hi guys, I have  come  with new one on CORS - HTTP Stuff

CORS means CROSS DOAMIN RESOURCE SHARING

If you develop REST Application and tried testing with Chrome REST or Firefox REST Plugin or addons , it would have worked. But When u try with HTML page using JavaScript it might return with HTP - 405 (Method Not Allowed)

In my case it is issue with Camel - RESTLET. I have the same issue as below :
INFO:         13:10:38        127.0.0.1        -        -        24366        OPTIONS        /ORDER/COMPUTER        -        405        487        0        2        http://127.0.0.1:24366        Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0        -

Solution:

Before going to issue you have to understand the CORS concept. As it name says , it is cross domain .

Your code is actually attempting to make a Cross-domain (CORS) request, not an ordinary POST.
That is: Modern browsers will only allow Ajax calls to services in the same domain as the HTML page.
Example: A page in http://www.server.com/index.html can only directly request services that are in http://www.server.com, like http://www.server.com/testservice/etc. If the service is in other domain, the browser won't make the direct call (as you'd expect). Instead, it will try to make a CORS request.
To put it shortly, to perform a CORS request, your browser:
  • Will first send an OPTION request to the target URL
  • And then only if the server response to that OPTION contains the adequate headers (Access-Control-Allow-Origin is one of them) to allow the CORS request, the browse will perform the call (almost exactly the way it would if the HTML page was at the same domain).
    • If the expected headers don't come, the browser simply gives up (like it did to you).


I enabled the CORS for RESTLET in camel.
And also upgraded to Camel 2.17.1, which works well with CORS.Bcoz it doesn't support in earlier version which is less than Camel 2.16.2 .

Reference:

Monday, 11 April 2016

Apache Camel Custome Annotated Beans Access



How to do Custom Bean Annotation Process in Apache Camel?
Sol:

In Camel we always have access to Camel Context. Using camel context we can access spring context as shown below. Once we have spring context we can access all features provided by Spring like getting annotated  beans .

Steps:
  1. Add Component scan to Spring for annotated beans.
  2. Annotate the class with  intended annotation
  3. As Code show below  
  4. Java code -   Link here   Java code