Jul 25, 2018

Implementing oAuth with Apigee






<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OAuthV2 async="false" continueOnError="false" enabled="true" name="OAuth-v20-1">
    <DisplayName>OAuth v2.0-1</DisplayName>
    <Properties/>
    <Attributes/>
    <ExternalAuthorization>false</ExternalAuthorization>
    <Operation>VerifyAccessToken</Operation>
    <SupportedGrantTypes/>
    <GenerateResponse enabled="true"/>
    <Tokens/>
</OAuthV2>


Now this is protected by oAuth on server so we need service to generate oAuth token. Create a product that exposes this API and App that has access to this Product. Note the Client id and Client secret of this Developer app and get the Base64 encoded value of string <client_id>:<client_secret>.
Set this value in Basic authorisation as shown.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OAuthV2 async="false" continueOnError="false" enabled="true" name="OAuth-v20-1">
    <DisplayName>OAuth v2.0-1</DisplayName>
    <ExpiresIn>180000</ExpiresIn>
    <Operation>GenerateAccessToken</Operation>
    <SupportedGrantTypes>
        <GrantType>client_credentials</GrantType>
    </SupportedGrantTypes>
    <GrantType>request.header.grant_type</GrantType>
    <GenerateResponse enabled="true"/>
</OAuthV2>

Now send a request for oAuth from postman as


Note down the token returned by this call and set it in header while sending request to actual API.


Congratulations...you were able to get the response from API that is protected by oAuth.

Jun 7, 2018

Cron Expressions for Spring Boot


List of few important cron expressions we can use with @Scheduled annotation.

Cron expression is made up of six characters
First ==> second :) interesting ;) its unit and not the position second
Second ==> minute
Third ==> hour
Fourth ==> date
Fifth ==> month
Sixth ==> year? hmm...no its for day of week. :)

Following are some handy expressions you may want to use for your scheduled jobs.

* * * * * *      ==> Every second any Minute, any Hour, any Hour, any Date, any Month, any Day

0 * * * * *      ==> When second is zero (i.e. Every minute) any Hour, any Date, any Month, any Day

*/5 * * * * *   ==> Every five seconds any Hour, any Date, any Month, any Day (i.e. 00:00:00, 00:00:05, 00:00:10 .... 23:59:55)

0-1 * * * * *   ==> Every zeroth and first second of minute any Hour, any Date, any Month, any Day (i.e. 00:00:00, 00:00:01, 00:01:00, 00:01:01 .... 23:59:00, 23:59:01)

5,10,15 * * * * *   ==> Every fifth, tenth and fifteenth seconds of minute any Hour, any Date, any Month, any Day (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

5,10,15 * * * * THU   ==> Every fifth, tenth and fifteenth seconds of minute of every Thursday any Hour, any Date, any Month (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

5,10,15 * * * * MON,TUE,THU   ==> Every fifth, tenth and fifteenth seconds of minute of every Monday, Tuesday and Thursday any Hour, any Date, any Month (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

5,10,15 * * * * MON-FRI   ==> Every fifth, tenth and fifteenth seconds of minute of every Monday to Friday any Hour, any Date, any Month (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

* 40 22 * * *  ==> Every second at 22:40 (i.e. 10:40 PM) any Hour, any Date, any Month, any Day

0 40 22 * * *  ==> Only once at 22:40:00 any Day, any Month, any Day

0 30 * * * *    ==> Only once at half past of each hour any Hour, any Date, any Month, any Day (i.e. at 00:30, 01:30 .... 23:30)

0 */10 * * * * ==> Once once at every ten minutes any Hour, any Date, any Month, any Day

0 */10 0 * * * ==> Once once at every ten minutes, zeroth Hour, any Date, any Month, any Day

0 0 1 * * * ==> When month changes (welcome salary)

0 */10 0 1 * * ==> Once once at every ten minutes, zeroth Hour, first of each month, any Month, any Day

Now some fun items...

0 0 0 15 8 * ==> Every year on 15th August midnight (to welcome independence day)

0 0 0 25 12 * ==> Welcome christmas

0 0 0 1 1 * ==> Happy new year

0 30 6 * * MON-FRI ==> Wake up on working days

0 30,35 6 * * MON-FRI ==> Wake up on working days with 5 minute snooze.

0 0 6 * * SAT,SUN ==> Weekend of traveller

0 30 7 * * SAT, SUN ==> Normal weekends




Apr 26, 2018

Springboot: Running Application on Https with Java keystore

Consider Source code available on my git repository https://github.com/mitanjos/sping4hibernate5.git. If I run this application with default profile, it will run on http port 8000



and can be accessed as url http://localhost:8000/api/v1/category




Now we want to ensure that this port is exposed as https service and will require client to have appropriate keystore to trust response sent by our service.

So first we generate keystore using java keygen tool as follows.

Run command

keytool -genkeypair -alias selfsignedcert -keypass password -keystore myapi.keystore -storepass password -validity 180

and provide required details as prompted.



For the sake of simplicity I have added generated file in resources.

Now we want to add this file in our server configuration. To achieve that you need to set following properties in spring boot startup configuration.


server.ssl.key-store = classpath:myapi.keystore
server.ssl.key-store-password = password
server.ssl.key-password = password



So we create a separate file (to dynamically chose between normal run and running application under https with keystore we have just created). Refer application-secure.properties here in the repository.

Now we run the same application with parameter -Dspring.profiles.active=secure so that system will pickup application-secure.properties and start listening to port 8443 on https protocol.



Now if you try to curl it normally it will give error as ssl handshake will fail.


In the next blog we will configure this keystore with Feign client to access this REST endpoint.

Apr 23, 2018

Maven configuration for Jenkins

Go to Jenkins home at http://localhost:8080/ and in right menu click on "Manage Jenkins" then click on "Global Tool Configuration"


Go to Maven section and click on "Maven Installation"



Select checkbox "Install Automatically"
Select option "Install from Apache"
Click on "Add Maven"


101 of Jenkins on Mac OS: First Job setup

Once we have completed Jenkins Installation on local machine as explained in previous blog. Go to http://localhost:8080/ and if no jobs are configured it will show you screen as follows.

Click on "Create New Job" --> Provide name for this Job and select type of project as "Free Project" and click on "Ok"


On next screen do as following

Under General tab provide description.
In "Source Code Management" select "Git" and provide "Repository URL" (here I have used my git project available at url https://github.com/mitanjos/sping4hibernate5.git (this is a SpringBoot project with H2 database and Maven as build tool) and branch as master.
Not selected any trigger for basic project.


Under build section select "Invoke top-level Maven targets"



Select Maven version and provide maven task to be executed.
If you do not have any option in dropdown and you want to create new configuration follow steps as explained here.

Once that is done click on "Save" button and it will create your first job.


Next click on "Build Now" to run a new build for this project and it will schedule a first build for this project. Click on the build number (in this case #1 and on the next page click on "Console output" and will show you build logs.

If you click on "Jenkins" icon (Home) it will show you "Dashboard" of all your projects (here only one project and only first build"


Thats all for this step.

Apr 17, 2018

BrainTree Integration with REST services

First Create your account on BrainTree sandbox.
Will send you an activation email --> Click on the activation link and activate your account.
Notedown following important information

  • merchantid
  • public key
  • private key
And configure in your project. In my case I have configured them as environment variable. In production ensure its encrypted.

Add following dependency in your project

Create a controller class

And a service class

List of nonce are available here

Perform few transactions like following
http://localhost:8000/v1/api/braintree/transaction?amt=130&nonce=fake-valid-visa-nonce
http://localhost:8000/v1/api/braintree/transaction?amt=34&nonce=fake-valid-dinersclub-nonce

On Braintree Transaction Summary page you can see these transactions reflected.

Or you can check transactions


Apr 11, 2018

101 of Jenkins on Mac OS: Setup


Environment Setup

  • Downloaded Jenkins for MacOS
  • Command to Start Jenkins: sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist
  • Jenkins installation directory is /Users/Shared/Jenkins/
  • Note that to get the admin password you need to use sudo su
  • Initial Admin password is at /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
  • Now hit the url http://localhost:8080/ and it will ask for initial admin password.  Provide initial password.  Will provide options to chose from
  • Once required plugins are installed it will show screen to create first admin user. Provide required details to create admin user

Next: Create your first Job




Jan 17, 2018

Notes on Microservice Architecture

What is microservice

  • Service that solves single business purpose. (S of SOLID principal). Keep slicing till you get the answer "nothing" for question "What else it is doing?"
  • Can be developed independently
  • Can be deployed independently.
  • Decoupled from rest of the world :)

Why microservice
  • Since developed independently development is faster.
  • Since deployed independently CI/CD is easy and so is agile and TDD etc.
  • In a way more secure as loophole in one functionality can not be exploited for other functionalities
  • Uptime is increased as problem is localised.
  • Scalability is better as scaling up or out is done for specific service that really requires it and not for entire monolith.
  • Greater flexibility to development teams in terms of choice of technology etc.
  • In terms of cloud more appropriate type of environment can be selected for deployment (say type of EC2 instance)

Features of MSA
  • High cohesion
  • Autonomous
  • Business Domain Centric
  • Resilient
  • Observable
  • Automation

Components involved (with example in Spring cloud)