Welcome to Techy Tunes !

Follow Me
With the introduction of the new 3.x drivers mongodb team introduced new methods for good reasons. This includes db authentication too. If you are writing a new application it is always better to use the latest apis. Also, it is always good to upgrade for the latest api for the sake of maintainability.

Lot's of articles explain how to connect to mongo db with java driver with deprecared methods in 2.x api. Here I explain how to do it with mongodb 3.x. If you already have mongo installed you can directly jump here

Install Mongo

Follow this link to install mongo db in ubuntu 16.04


Check mongo status

check whether the mongo service is running by command
service mongod status
Note the Active: active (running) in the output


Creating Users

  • To start with, make sure that the authentication is disabled in mongodb service. 
  • Generally, the mongo conf lies in /etc/mongod.conf
  • Check whether the following tag is present. If present make it disabled. If its absent, mongo is already in disabled authentication mode
 security: 
     authorization: disabled

After disabling authentication, restart the server
sudo service mongod restart

  • Login to mongo by just typing mongo in the command prompt
  • create user isuru in mydb database.
use mydb;
db.createUser(
  {
    user: "isuru",
    pwd: "mypwd",
    roles: [ { role: "dbOwner", db: "mydb" } ]
  })
  • Change "authorization" to "enabled" in the /etc/mongod.conf to enable authentication. 
  • Restart mongo

Login to Mongo with username and password

  • Login to mongo db with the new user
mongo  -u "isuru" -p "mypwd" --authenticationDatabase "mydb"


  • make sure to issue the command "use mydb" before you query any collection

Working with mongo db Java 3.x driver

How to authenticate mongodb with mongo java driver 3.x

  • This is how to connect with mongodb using username and password with java 3.x driver. Note that dbUser, dbName, dbPassword I get from configs
MongoDatabase getMongoDatabase() {
        MongoCredential credential = MongoCredential.createCredential(dbUser, dbName, dbPassword);
        ServerAddress serverAddress = new ServerAddress(dbHost);
        List credentialList = new ArrayList<>();
        credentialList.add(credential);

        MongoClient mongoClient = new MongoClient(serverAddress, credentialList);

        return mongoClient.getDatabase(dbName);
    }

There's some other tricky and interesting stuff on writing and fetching documents with mongodb. I'll update it here. Subscribe to keep you posted

As a developer you might want to work with different development environments. As an example, I had to work with Java 1.4, 1.5, 1.7 and 1.8 :D . When JDK changes all other stuff such as maven will also have to change.

One thing you can do is have everything in you bashrc or bash_profile and comment and uncomment the required environment variables. Believe me, I did that for a long time. duh...

Or else, you can have scripts to set your environment !!! (yay).  One sh file would look like this

#!/bin/sh
export JAVA_HOME=/home/isuru/installs/jdk/jdk1.8.0_101
export PATH=$JAVA_HOME/bin:$PATH

and the other would look like this

#!/bin/sh
export JAVA_HOME=/home/isuru/installs/jdk/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH

But we can't memorize where this files are to locate them always. That's where alias comes in.

Create .bash_aliases file in your home directory. Check whether this file is being sourced inside your bashrc or bash_profile.

Add aliases to the ~/.bash_aliases as follows
alias java8='. /home/isuru/stuff/scripts/java8.sh'
alias java7='. /home/isuru/stuff/scripts/java7.sh'
In my machine both the script files are located under the directory /home/isuru/stuff/scripts. Make sure the sh files have execute permissions.

Now we are done here. Open a new shell and type in java8. and then java  -version. Type in java7 and the java -version. See the magic :P

If you are following a routine everyday executing shell scripts, or some other stuff, put them in a single sh file and do register it as an alias. Will make your life easier.

If opening a new terminal really hurts, you can source the ~/.bashrc or ~/.bash_profile file. You can stay where you are in the current terminal and yet get the benefits of alias. See below to uncover a secret ;)

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

When we open a new terminal the ~/.bashrc or ~/.bash_profile automatically sources :D :D

Below are the steps to follow. I found these steps from a forum (which I cannot find the link now) and they have missed one important thing. So, this is how I got my huawei dongle working in Ubuntu 16.04


  • Create a mobile broadband connection first. You can do it by Edit Connections option.
  • In the terminal type lsusb having the dongle plugged in. Below is what I got

  • Note the line "Bus 001 Device 007: ID 12d1:1446 Huawei Technologies Co., Ltd. Broadband stick". This is my dongle
  • Note these, you need these up ahead. 12d1 is the vendor id, 1446 is the product id. These might change according to your dongle type (this was what they have missed ;))
sudo vi /lib/udev/rules.d/40-usb_modeswitch.rules
  • Add following line before "LABEL="modeswitch_rules_end"

ATTR{idVendor}=="12d1", ATTR{idProduct}=="1446", RUN+="usb_modeswitch '%b/%k'"


  • save the file and type the following. Make sure 12d1 replaces your vendor id and 1446 replaces your product id as per your dongle
sudo touch /etc/usb_modeswitch.d/12d1:1446.
  • Last step as follows. Same rules apply here for the vendor id and the product id. Additionaly add the prefix "0x" for the vendor id and product id
sudo usb_modeswitch -J -v 0x12d1 -p 0x1446 
.
  • I got the below screen and everything was good. 

  • I had to issue the command "sudo usb_modeswitch -J -v 0x12d1 -p 0x1446 " always when I restart the machine, but I always believe that having something is better than nothing ;)

What is Cron ?


  • Cron is a unix based job scheduler tool. 
  • Can be configured to execute tasks at a specified time.
  • We can use our java programs to make use of the Cron tool by configuring cron expressions. 
  • Here we focus on writing cron expressions which is compatible with the Spring's CronTrigger. 
  • Configuring these cron expressions with Spring is coming soon !!!

Cron Expression Format


Field NameMandatoryAllowed ValuesAllowed Special Characters
SecondsYES0-59, - * /
MinutesYES0-59, - * /
HoursYES0-23, - * /
Day of monthYES1/31/2015, - * ? / L W
MonthYES1-12 or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, - * /
Day of weekYES1-7 or SUN, MON, TUE, WED, THU, FRI, SAT, - * ? / L #
YearNOempty, 1970-2099, - * /

Here's an old school trick to help you to memorize this ;) If this is too dumb for you, make your own one :D :D

Sexy          Minions,    Our      DOMbAsses,    Mouthed   Dow's              Yearbook  :D :D :D

Seconds    Minutes     Hours   Day Of Month  Month      Day of Week    Year

  • Anyway, Spring's CronTrigger accept only 6 fields starting with Seconds, up to Day of Week.
  • So, a cron format could be "5/10 10-30 10 ? * MON,TUE". Once caught, this is really easy.

Special Characters


  • * - all values. In the above example ,the star corresponds to every month.
  • - - a range of values. In the above example, 0-15 means, it should start in 10th minute and end in 30th minute.
  • x/y - starts at value x. Run again when another y number of time units passed. In the above example, the cron starts at the 5th second, and thereafter run when each 10 seconds passed. means, it runs in 15, 25, 35, 45, 55 th seconds. 
  • , - specifies individual values. In the above example, MON,TUE means, this cron runs in Monday and Tuesday only
  • With above four characters we can write something like this "0-5,10,15,20/10  * * ? * SAT,MON,SUN". The section "0-5,10,15,20/10 " means, the cron runs from 0th seconds to 5th second, then at 10th and 15th seconds, then each 10 seconds after starting from 20th second. 
  • ? - Allowed only in Day of Month and Day of Week fields. There's a small catch here. In above example;
    • Say, we put 15 to the Day of Month's position and the value MON to the Day of Week's position. Then it is a huge ambiguity. 
    • Putting 15 implies that the cron will be run in every 15th day of the month. Putting WED implies that cron will be run on every Monday of the week. 
    • So,  will this cron fire on 15th of the month if it falls on a Monday, Or should it fire on both the 15th day of the month and every Monday. (drunk)
    • Here's the solution. You cannot put two values there. That's where "?" comes to play
    • If you specify the Day of month, you put "?" to the Day of week and vice versa. That's it , no more questions. ;)

Executing Cron Jobs with Java


  • To make java programs to execute these cron jobs we use Spring's org.springframework.scheduling.support.CronTrigger and it seems to support for Vixie Cron, where the special characters L, W and # are not supported. 
  • We can use org.springframework.scheduling.quartz.CronTriggerBean to support the above mentioned characters, but that is not our focus here.
  • How to do this ? I will post next and will update here. 

<3 With Love <3



Prism is a really cool, simple and lightweight syntax highlighter. Prism supports bunch of languages and 6 different themes by today. First, see how prism looks.

public class Main {
    public static void main(String[] args) {
        System.out.println("This is how prism looks");
    }
}

Download Prism JS

  • Hop into prismjs official website to download the required stuff
  • Go to Download page
  • You can download the development or minified version. Since we are going to use it straight away, we need the minified version
  • Select a theme from Themes section. You can see a live preview at the bottom of the page
  • In the Languages section , select all the languages you need to be included in your syntax highlighter
  • There are bunch of Plugins, select whatever you need, as per your requirement.
  • Finally, use DOWNLOAD CSS and DOWNLOAD JS buttons to download both .css and .js files.

Host Prism JS

  • Now we have to add these js and css files to blogger template. To do this, blogger needs to read these files through a cdn (Content Delivery Network). 
  • Simply, CDN makes available web content to the users, in our case its our js and css files. The downloaded files should be available online, where blogger can read them from the hosted location.
  • We can use dropbox to host our js and css files and its free !!!
  • Create a dropbox account and upload prism files to the Public folder. I have created a separate folder named CDN, and uploaded the two prism files
  • A default CDN for prism js is available here, but here you cannot get the advantage of the customizations provided by prism. That is the reason of hosting your own prism in dropbox

Add Prism JS to Blogger

  • Now we are ready to add Prism to blogger. Go to your blog's Template section. Click on the Backup / Restore button to download the full working template. By doing this, you can restore the template if something goes wrong.
  • Now click on Edit Html button. This will take you to the blogger template editor. 
  • Locate the </head> tag and add following lines immediate before the </head> tag

<link href='https://dl.dropboxusercontent.com/u/55229039/CDN/prism_custom.css' rel='stylesheet' type='text/css'/>

<script src='https://dl.dropboxusercontent.com/u/55229039/CDN/prism_custom.js' type='text/javascript'/>

  • These two links should get from the files we just uploaded to dropbox . 
  • Go to your file hosted location in dropbox and click on each of the file and click "Copy public link…" option to get their public link. Add them to your <link> and <script> tags.

  • Now save the template and preview it to be sure everything works fine and same as earlier
  • If everything goes well, we are done with adding prism to Blogger. Now we simply have to use it in our code.

Using Prism JS to highlight syntax

  • Cannot think of a simpler way to highlight syntax. Its just the below code

<pre><code class="language-xxx">### Your code here ###</code></pre>

  • The "xxx" is language-xxx should be replaced with the relevant language alias. If it is css

<pre><code class="language-css">p { color: red }</code></pre>

  • When you are pasting the code make sure it is encoded properly. If you paste the code to the blog post editor directly when it is in the "Compose" mode, it will encode the code automatically. Or else you can use a tool like this to encode your code.

Prism JS language aliases

  • I have mentioned below, all the language aliases available as per today. You can find them here too. 
  • But, keep in mind, to get the language support, you should select that specific language when you are downloading prism libraries.
  • Now, we are done. Make your blog a beauty with Prism !!! I would like to hear from you.

  • Markup - markup
  • CSS - css
  • C-like - clike
  • JavaScript - javascript
  • ActionScript - actionscript
  • Apache Configuration - apacheconf
  • AppleScript - applescript
  • ASP.NET (C#) - aspnet
  • AutoHotkey - autohotkey
  • Bash - bash
  • C - c
  • C# - csharp
  • C++ - cpp
  • CoffeeScript - coffeescript
  • CSS Extras - css-extras
  • Dart - dart
  • Eiffel - eiffel
  • Erlang - erlang
  • F# - fsharp
  • Fortran - fortran
  • Gherkin - gherkin
  • Git - git
  • Go - go
  • Groovy - groovy
  • Haml - haml
  • Handlebars - handlebars
  • Haskell - haskell
  • HTTP - http
  • Ini - ini
  • Jade - jade
  • Java - java
  • Julia - julia
  • LaTeX - latex
  • Less - less
  • LOLCODE - lolcode
  • Markdown - markdown
  • MATLAB - matlab
  • NASM - nasm
  • NSIS - nsis
  • Objective-C - objectivec
  • Pascal - pascal
  • Perl - perl
  • PHP - php
  • PHP Extras - php-extras
  • PowerShell - powershell
  • Python - python
  • R - r
  • React JSX - jsx
  • reST (reStructuredText) - rest
  • Rip - rip
  • Ruby - ruby
  • Rust - rust
  • SAS - sas
  • Sass (Scss) - scss
  • Scala - scala
  • Scheme - scheme
  • Smalltalk - smalltalk
  • Smarty - smarty
  • SQL - sql
  • Stylus - stylus
  • Swift - swift
  • Twig - twig
  • TypeScript - typescript
  • Wiki markup - wiki
  • YAML - yaml
There are couple of other ways to copy resources. but I find this is the easiest method.

maven-resources-plugin

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
    <execution>
        <id>copy-libs</id>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${basedir}/target/my-project/WEB-INF/lib</outputDirectory>
            <resources>
                <resource>
                    <directory>${deps.home}/lib</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
    <execution>
        <id>copy-xml</id>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${basedir}/target/classes/com/blogspot/techytunes/my-project/dao</outputDirectory>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/java/com/blogspot/techytunes/my-project/dao/</directory>
                    <excludes>
                        <exclude>*.java</exclude>
                    </excludes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>
</plugin>

  • We need multiple executions ( We need to copy from different sources to different destinations) . That's why we need the <executions> tag. Inside this tag we can define several executions using <execution> tag.
  • First, I have to copy some libs. I give an id to that process as <id>copy-libs</id>
  • Maven build life cycle has several phases. We need to use "process-resources" phase with the goal "copy-resources"
  • When packaging the web-app, maven takes the project folder from the target directory and creates the war file. In my case, it takes the my-project folder and packages its content to a war file. This happens in the package phase.
  • We add the basic configuration in the <configuration> tag. <outputDirectory> specifies the destination to copy.
  • In the <resources> tag we can add multiple <resource> elements. Everything we put in here, will be copied to the above destination.
  • To copy to another destination, we simply need another execution.
  • The second execution copy-xml is slightly different from copying resources. Some xml files needed to be copied to the destination of classes. 
  • After the process-resources phase there comes compile phase where maven compile all the classes to the target/classes directory. So we take the required xml files and copy them to the relevant destination in the target/classes/... 
  • we pick the xml files from src/main/java/com/blogspot/techytunes/my-project/dao and copy them to target/classes/com/blogspot/techytunes/my-project/dao
  • In the source directory (src/main/java/com/blogspot/techytunes/my-project/dao) we have .java files along with the xml. We use <excludes> tag to exclude .java files being copied to the destination.
  • When maven is in his compile phase, he will compile the classes and put it to the target/classes directory
Thats it. Good or bad, comments are always welcome :D
I had developed a simple json api. There I used
When I do a curl as "curl http://my-domain/my-app/url" it worked fine and produced me desired results.

Then I developed the front-end of the app and hosted it in a separate server. Now, my api and the client app are hosted in two different servers. I used jQuery to access the api and get the required data from it. But when I do it , this is all I got :(

No 'Access-Control-Allow-Origin' header is present on the requested resource
 Same as you, I googled :D found out that this happens because of the same origin policy. In a nutshell, same origin policy prevents JavaScript from making requests across domains. In my case , the api and the app were in two separate domains and there you have it :)

To avoid this add this lines to <CATALINA_HOME>/conf/web.xml

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

We do this because we only use Apache2 to redirect request purpose.

Restart Tomcat then you are good to go !!!
While I was developing a Web Client module I got this error. This happens because of a dependency issue.

I had used the dependency


<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>3.0.2</version>
</dependency>


This got me the error continuously. The one you need is


 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.2</version><</dependency>


Instead of simple, you have to use jaxws. That's it :) this saved me. (Though it is so simple)
I was developing some web apps with REST calls using Amazon Web Services. I did the initial stuff, such as registering and creating instances. But then , I was lost. I had some war files to deploy, so I installed Tomcat 7 and JDK to my host. Started the tomcat and called the url with the public ip that AWS gave me with the port 8080 in the browser (That's what we locally do right ? ;) ) . Sadly (and dumbly), Nothing happened :(

Setting Up AWS Firewall

First thing to do here is opening up the port 80 for your EC2 instance in AWS cloud.





Click on the the Security Groups of your instance. Mine its launch-wizard-1.

In next page, click on the Inbound tab and click Edit in the bottom of the page














Clicking on Edit will get you here. We have to add a a new rule here by clicking on Add Rule button below. We are able to ssh to the cloud since the SSH port 22 is opened for us by default.









Once you click the Add Rule button, select HTTP as the Type. Every other thing will be automatically filled.
















Click on Save. That's it. We have opened up the 80 port !!! But wait, the job is halfly done. We have to do some other stuff also ...

Setting up Apache2 and Tomcat

Install Apache2 and Apache Tomcat 7 to your cloud. For Ubuntu ;

  • Apache 2 : 
    • sudo apt-get install apache2
  • Apache Tomcat 7 : 
    • wget http://apache.mirrors.hoobly.com/tomcat/tomcat-7/v7.0.55/bin/apache-tomcat-7.0.55.tar.gz ( you can wget from any download location, it's fine :) )
We are going to put up apache2 to listen to the requests that comes to port 80 . And then we will redirect those to tomcat accordingly. Apache2 will be listening to the port 80 and Tomcat will be listening to the port 8080 by default.

Apache2 Configurations

Enable proxy and proxy_http first with following.

a2enmod proxy
a2enmod proxy_http
Open the file /etc/apache2/apache2.conf. This is the new place for apache2 configurations instead of httpd.conf

Add the ProxyPass to redirect the request to tomcat

ProxyPass    /the_calling_url    http://the_redirected_url
Ex :

ProxyPass    /get-all-users    http://localhost:8080/my-app/get-all-users-listener

Things here are as follows

/get-all-users is the url or the path that apache2 will receive. Say your domain name is test-aws-services.com. Then, if you invoke the request as this, " http://test-aws-services.com/get-all-users ".Then the above ProxyPass will work and it will pass that request to http://localhost:8080/my-app/get-all-users-listener

get-all-users-listener is a servlet that I have configured in my web application.

If you want to redirect all the requests that apache receives, you can omit the path. Simply add / to the path.

Ex :

ProxyPass    /    http://localhost:8080/my-app/get-all-users-listener
Same as this, we can add reverse proxy also. This will send everything out from Tomcat to port 80.

ProxyPassReverse / http://localhost:8080/

Tomcat Configuration

In Tomcat, open the <CATALINA_HOME>/conf/server.xml file and modify the following block as follows. Add proxyPort="80". This is used for Reverse Proxy. This tells Tomcat to send all the responses to the Port 80.

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
                proxyPort="80"
        />
So, that's it folks. This is how I got it work. There might be some variations or dependencies with your deployment on AWS. Hope this helps. Cheers !!!!