Personal Project

Monday, May 30, 2016

How to set up Master Slave replication for REST server using Spring framework ?


MYSQL replication architecture is vital for building high available REST API servers.
In order to avoid singe-point of failure problem, it`s recommended that setting 2 application servers and  Master-Slave database in MYSQL. Of course, you have to set up the application servers behind the load balancer.

The following details how to setup  REST API server by using Spring framework of Java.
How to set up MYSQL Master/Slave replication ?
// Edit
vim /src/main/resources/application.properties

## Master and Slave
spring.datasource.url = jdbc:mysql:replication://localhost1:3306,localhost2:3306/userdb 

The source codes of REST server can be seen here.

Wednesday, May 25, 2016

How to sniff packets for REST API on Ubuntu ?


Since the REST API is based on Http protocol, it is necessary to sniff the http header
and messages to assure that your applications function well.  The more you understand
how the Http messages work and communicate, the faster you can make your job done.

The command shown below might be useful for you to dump Http header and message while developing REST API on Ubuntu Linux. Its feature is as same as whireshark on Windows.


tcpdump -A -vvv -i [network card] port [ number]
 ex:
tcpdump -A -vvv -i eth0 port 8080


Thursday, May 12, 2016

How to validate Store Receipt for Apple Store and Google Play using Unity and PHP ?

Get Started

This tutorial will teach you how to validate Store receipt for Google Play and Apple Store using PHP and Unity.  You have to upload the PHP (verifyReceipt.php) to your server and set up the Unity plugin and add the following sample codes.

Import Unity Prime31 Plugin

Import Prime31 plugins StoreKit for Apple Store and In-App-Billing for Google Play into Unity.

How to setup In-App-Purchase for IOS ?

There are two steps for this.
  • Setup Item`s Product Id
  • Setup Payment Success Event to Get Receipt

Setup Item`s Product Id

Open a demo scene in folder
  /Plugins/Prime31/Storekit/demo/StoreKitTestScene.
Edit StoreKitGUIManager.cs and modify Product Id.
 /Plugins/Prime31/Storekit/demo/ StoreKitGUIManager.cs

 // XXXX is the product Id, which must match what you have in iTunes.
 var productIdentifiers = new string[] { "XXXX" };
 StoreKitBinding.requestProductData( productIdentifiers );

Setup Payment Success Event to Get Receipt

The following function aims to capture the receipt from Apple when the payment becomes successful, and send the receipt to PHP to validate the receipt`s correctness.
Edit /Plugins/Prime31/Storekit/demo/ StoreKitEventListener.cs

void purchaseSuccessfulEvent( StoreKitTransaction transaction )
   {
       Debug.Log( "purchaseSuccessfulEvent: " + transaction );

       // Get iOS receipt 
       string receipt = transaction.base64EncodedTransactionReceipt;

       // Build POST form
       WWWForm form = new WWWForm ();
       form.AddField ("key", "1234");
       form.AddField ("receipt", receipt);
       form.AddField ("en", "prod") // dev, prod
       form.AddField ("os", "ios")  // ios, android

       // Server URL
       string url = "http://your server IP/verifyPayment.php";

       // Process respond
       StartCoroutine(this.DoWWW(new WWW(url, form), (www) => {
           Debug.Log("-------- Callback Success: " + www.text);
       }));
   }

How to setup In-App-Billing for Android ?

There are two steps for this.
  • Setup App Public Key
  • Setup Item`s Product Id
  • Setup Payment Success Event to Get Receipt

Setup App Public Key

Open a demo scene in folder
/Plugins/ InAppBillingAndroid /demo/IABTestScene.unity
Setup Public key
Edit /Plugins/ InAppBillingAndroid /demo/ IABUIManager.cs

// Setup Public key 
var key = "Your Public Key on Google Play";

GoogleIAB.init( key );

Setup Item`s Product Id

Edit /Plugins/ InAppBillingAndroid /demo/ IABUIManager.cs

// Setup Product ID 
private string[] skus = new string[] 
{
    "XXXXXX"  //  your Product Id here 
};

GoogleIAB.queryInventory( skus );

Setup Payment Success Event to Get Receipt

Edit /Plugins/ InAppBillingAndroid /demo/ GoogleIABEventListener.cs


void purchaseCompleteAwaitingVerificationEvent( string purchaseData, string signature )
{
    Debug.Log( "purchaseCompleteAwaitingVerificationEvent. purchaseData: " + purchaseData + ", signature: " + signature );
    Prime31.Utils.logObject (purchaseData);
    Prime31.Utils.logObject (signature);

    // Google receipt 
    string receipt = purchaseData;

    // Initil POST form
    WWWForm form = new WWWForm ();
    form.AddField ("key", "1234");
    form.AddField ("os", "android");
    form.AddField ("en", "prod");
    form.AddField ("receipt", receipt);
    form.AddField ( "sing", signature);

    // Server URL
    string url = "http://your server ip/veryPayment.php";
    // Process respond
    StartCoroutine (this.DoWWW (new WWW (url, form), (www) => 
    {
        Debug.Log("-------- Callback Success: " + www.text);
    }));
}

Tuesday, May 10, 2016

How to develop a secure REST server with high performance using OAuth, HIBERNATE, and MYSQL in Spring framework ?

I found it difficult to develop a secure REST API Server from scratch with features like OAuth, MYSQL, HIBERNATE, MYSQL Token Store, JDBCTemplate, and HTTPS. It will take lots of time to put all the features together and make them work properly. 

In order to reduce redundant work, I therefore created the REST API server template in Spring framework. With this server template, you can focus on developing REST API to meet your needs without being distracted by other technologies.

Get Started

This project is a very simple REST and OAuth server template with high performance. Since the authenticated token is stored in MYSQL, its easy to scale up your server to meet high user traffic.

Project

This project includes the following features.
MVN
Spring-Boot
REST
JPA
MYSQL + HIBERNATE 
MYSQL Token Store
JDBCTemplate
Https

Setup MYSQL Database

Import userdb.sql into your database.
import database/userdb.sql into MYSQL database

Enable SSL

// Create key store 
bash
keytool -genkey -alias tomcat -keyalg RSA

// Edit 
vim /src/main/resources/application.properties

// Uncomment the following lines and set up your key store path
## SSL
server.port=8443
server.ssl.key-store=./src/main/resources/your.jks
server.ssl.key-store-password=your store passowrd
server.ssl.key-password=your pass

Building

You need Java (1.7 or better) and Maven (3.0.5 or better):
$ mvn clean package
$ mvn package
$ java -jar target/*.jar
...

// Http
<app starts and listens on port 8080>

// Https
<app starts and listens on port 8443>

Here are some curl commands to use to get started:
// Get Token
curl -k -X POST -d 'grant_type=client_credentials' --user 'my-client-with-secret:secret' https://localhost:8443/oauth/token
{"access_token":"bf12a9c8-c341-44a6-9ce6-084a8ba86652","token_type":"bearer","expires_in":43199,"scope":"read"}

// hasUserId GET
curl -k -H "Authorization: 5470484a-148d-479f-988e-89dfce617bb1" https://localhost:8443/user/hasUserId?uid=336u594534
{"status":200, "userId": 336u594534}

// Twitter Login POST
curl -k -H "Authorization: Bearer b61db2dd-0af4-4e3c-b2b9-7c307a9d7c69" -X POST -H "Content-Type: application/json" -d "{\"twitterId\": \"0926841831\", \"deviceId\": \"2222\"}" https://localhost:8443/user/twitterLogin

How to optimize MYSQL connection pool ?

// Edit
vim /src/main/resources/application.properties

// Configure initial and maximal connections 
spring.datasource.initialSize= 15
spring.datasource.maxActive= 30 

Monday, April 11, 2016

How to cache UIWebView webpage for offline viewing ?

There is an issue when invoking UIWebView API on iOS that webpage won`t be displayed 
immediately.  It will take around 2 seconds to load the html page from the server and display
it properly. 
In order to optimize the UI, i suggest that  preloading the webpage in cache and it could be displayed for offline viewing at anytime.  

This solution has integrated UnityAds SDK for displaying video ad and the source codes can be seen at github.


Monday, March 7, 2016

Configuring Tomcat To Use SSL

Setting up SSL for Tomcat can be pided into two main tasks: creating a functional keystore, and configuring the Tomcat connectors and applications. Let's tackle them one at a time.  

Step 1 - Creating the Keystore

$JAVA_HOME/bin/keytool -genkey -alias 6waves -keyalg RSA -keystore /key/keystore

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA -keystore /key/keystore


 

Step 2- Creating the Certificate Signing Request


$JAVA_HOME/bin/keytool -certreq -keyalg RSA -alias 6waves -file 6wavesCerificate.csr -keystore /key/keystore
$JAVA_HOME/bin/keytool -certreq -keyalg RSA -alias [youralias] -file [yourcertificatname].csr -keystore [path/to/your/keystore]

 

Step 3
Download CA



 

Step 4

To import the Root Certificate -
keytool -import -alias root -keystore /key/keystore -trustcacerts -file GeoTr                                                      ust_Global_CA.cer
To import your new Certificate -
keytool -import -alias 6waves -keystore /key/keystore -file
keytool -import -alias [youralias] -keystore [path/to/your/keystore] -file [path/to/your_keystore]

Test URL
https://your server ip:8443/test.jsp

Enable root user password to login EC2 on Ubuntu


Edit your SSHd config to allow password based logins:


    sudo nano -w /etc/ssh/sshd_config

Add the following line to /etc/ssh/sshd_config:
    PasswordAuthentication yes

Copy your authorized SSH keys if you want to log in via keys as well:
    sudo mkdir /root/.ssh
    sudo cp /home/ubuntu/.ssh/authorized_keys /root/.ssh/

Reload your SSHd config:
    sudo reload ssh

Now you can login as root!

How to install Tomcat and Deploy war on Ubuntu ?

Install JDK

sudo apt-get install openjdk-7-*

Install tomcat

sudo apt-get install tomcat7*

Setup tomcat

vim /etc/tomcat7/tomcat-users.xml
sudo vim /etc/tomcat7/server.xml

Setup JAVA HOME

sudo vim /etc/profile JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin export PATH

Install tomcat7

sudo apt-get install tomcat7

Start tomcat7

sudo service tomcat7 start

Stop tomcat7

sudo service tomcat7 stop

Restart tomcat7

sudo service tomcat7 restart

Deploy war

Upload war to folder /var/lib/tomcat7/webapp

How to install LAMP on Ubuntu ?

1. Install Apache



2. Install MySQL



3. Install PHP


Restart Server



Check PHP

You can check your PHP by executing any PHP file in folder /var/www/html












Wednesday, March 2, 2016

Send Email in PHP with Japanese subject and body


   Before, we get started, you must have an SMTP server that can receive the emails from your machines 

   and send them to the recipient

    (i.e. your corporate exchange or Gmail).

 

    How to Setup Email on Linux Using Gmail and SMTP

  • Install ssmtp 

    sudo apt-get install ssmtp

    Edit ssmtp file

    sudo vim /etc/ssmtp/ssmtp.conf
  • root=username@gmail.com
    mailhub=smtp.gmail.com:465
    rewriteDomain=gmail.com
    AuthUser=username@gmail.com
    AuthPass=password
    FromLineOverride=YES
    UseTLS=YES

In order to make the default (root) “from” field be the server name, edit the/etc/ssmtp/revaliases file:
sudo vim /etc/ssmtp/revaliases
And add into it the desired translation which in our Gmail examples case will be:
root:username@gmail.com:smtp.gmail.com:465
Incredibly this is all you have to do to enable the ability. From now on, the machine will Email you when something is up.

Test Email Command

Lets test that our ssmtp setup was correct by sending an Email:

echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com

 

 

Use the following codes to send email with PHP

 

 

    //  $to  : receiver email address
    //  $subject :  Japanese subject
    //  $message : Japanese body
    function sendEmail($to, $subject, $message)
    {
        if(!isset($to) || !isset($subject) || !isset($message)) {
            return;
        }
   
         // Set up environment
        mb_language('Japanese');
        mb_internal_encoding('UTF-8');
   
        // Set up subject
        $headers ='From: '.mb_encode_mimeheader("ありがとうございます").' <test@123.jp>' . "\r\n";
                'Reply-To: test@123.jp' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
   
          // Set up body
        $message = mb_convert_encoding($message, "JIS", "UTF-8");
       
         //  Send email  
        if(mb_send_mail($to, $subject, $message, $headers))
        {
                error_log("#### Send mail OK ####");
   
        }else {
                 error_log("#### Send mail Error ####");
      }
       
    } // End Send Email