Friday, 12 August 2016

Textual description of firstImageUrl

Download WebMaster Tool Data using Casper JS

This is a simple casper js script to automate your process of downloading webmaster tools reports .


Google Webmaster does not provide any api to download the data .

This simple casperjs script will allow you to download the data and automate the process .

please change the google account credentials and also the time period for which you want to download the report .
var page = require('webpage').create();
var xpath = require('casper').selectXPath;
var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         loadImages:  false,         // The WebPage instance used by Casper will
         loadPlugins: false,         // use these settings
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'https://accounts.google.com/ServiceLogin';

casper.start(url, function() {
   // search for 'casperjs' from google form
   console.log("page loaded");
   this.fill('form#gaia_loginform', { 
        Email:'test', 
        Passwd:'test'
    }, true);
});

casper.thenEvaluate(function(){
   console.log("Page Title " + document.title);
});
var url = 'https://www.google.com/webmasters/tools/top-search-queries?hl=en&siteUrl=http://www.javaroots.com/&de=20150315&db=20150314&more=true&type=queries';
casper.thenOpen(url ,function (status) {
    /*var js = page.evaluate(function () {
        return document;
    });
    console.log(JSON.stringify(js)); */
 console.log(" trrsadsadsssssss");
});

casper.then(function() {
 console.log(" clickingggg first time ");
 this.click('#tsq-table-download > div:nth-child(1)');
 //this.click(xpath('//*[@class="goog-inline-block jfk-button jfk-button-standard"]'));
});


//casper.waitForSelector(xpath('/html/body/div[11]/div[3]/button[1]'), function() {
 casper.waitForSelector('.goog-buttonset-default', function() {

casper.then(function(){
  this.capture('abc.png', undefined, {
             format: 'png',
            quality: 100
     });
});
});

casper.then(function() {
 console.log(" clickingggg");
 //this.click(xpath('/html/body/div[11]/div[3]'));
 this.click('.goog-buttonset-default');
});

casper.on('resource.received', function(resource) {
 if(resource.url)
 {
  if(resource.url.indexOf("/top-search-queries-dl") > -1)
  {
   console.log(" resource url is " + resource.url);
   console.log("Downloading csv file");
   this.download(resource.url, 'ExportData.csv');
  }
 }
 
});
casper.run();
Post Comments and suggestions !!!!

Thursday, 4 August 2016

Textual description of firstImageUrl

How to migrate from SVN to GIT repository

This post describes step by step guide to migrate your svn repository to git repository.
First , lets have a look as why git is better than svn .

1) GIT is distributed, SVN is not.
 2) GIT stores content as metadata, SVN stores just files.
3) GIT branches are not the same as SVN branches.
4) GIT does not have a global revision no. like SVN do.
5) GIT’s content integrity is better than SVN.

Now we see the different structures of both these .
SVN :


->Trunk
->Branches
->Tags
->Extra DIR


GIT:


->Single Working Directory
.git directory stores binary format of branches , tags and other details.

Steps:

1. Download Subgit from above link
2. Make Sure you have java version >= 1.7
3. Note Down the SVN URL & Git Remotes
4. Extract the Downloaded archive.
5. Execute the below Commands
6. ~/Softwares/subgit-3.2.0/bin/subgit configure --layout auto https://svn.xysys.net/svn/dynamic-feed-server/projects/dsc dsc.git
7. The above command will detect the latest revision , history , branches, tags and configure authors.
8.  Once Done execute
9.  ~/Softwares/subgit-3.2.0/bin/subgit install dsc.git/  This would take some time depending on the revisions and the no of time file got modified to convert revision to git commits. Have Patience.
10. git clone dsc.git
11. This Would create your dsc git project with all tags , branches etc…
12. cd dsc && git branch –ar would list all the branches. You can also use git log to see the revisions converted to GIT Commits.
13. IMP:  git remote –v would saw you locat path which need to be changed to GIT Remote.
14. In mycase : In file .git/config , url = /Users/mohantysan/workspace/GitMigration/dsc.git 
We need to Change it to Git URL else if you make any commits and push it will push to SVN REPO.
Change the url to something like this :
url = git@github.com/ae/dsc.git
Now Your SVN Repo is Converted and ready to be published.
 git push origin –all # will push your code with all svn converted revision to GIT and you can use it as usual.
For Publishing Branches and Tags checkout branches and tags locally and push 
Happy Migration , Thank You

This post is created by Santosh Mohanty . You can contact him for any questions at santa.jyp@gmail.com or message him on his linkedIn profile .


Friday, 8 July 2016

Textual description of firstImageUrl

Jav 8 FlatMap Explained Stream API

In this short example , i will explain where we can use flatMap function of java stream api .

Suppose you have a multi level list where many objects are stored , and you want to convert it to a single list of objects . We can use flatmap which will flatten the list easily .

FlatMap basically first applies the transformation , and then then flatten the result in a single collection .

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by abhishek.somani on 8/7/16.
 */
public class JavarootsFlatMapExample {

    public static void main(String[] args) {
        String[] arr = {"abhishekl","somani"};
        String[] arr2 = {"java","example","1.8","stream"};
        List<List<String>> list = new ArrayList<>();
        list.add(Arrays.asList(arr));
        list.add(Arrays.asList(arr2));
        final List<String> strings = list.stream().flatMap(l -> l.stream()).collect(Collectors.toList());
        System.out.println(strings);
    }

}
Post Comments And Suggestions !!!

Tuesday, 5 April 2016

Textual description of firstImageUrl

Mule HttpListener Example

Mule Runtime 3.6 and later version have deprecated old Http Connector with , new Http Listener Config . Let's take an example to see how we can use it .This example also demonstrates how to get a particular xml element without using JAXB .

This example app will listen to a particular host and port , for post method , and parses the request xml using xpath . Based on the xml , we return the response based on the input xml received in request .

This is our mule config xml :
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
 xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.6.1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<http:listener-config port="8081" host="0.0.0.0" name="myRequestConfig" >
 <http:worker-threading-profile maxThreadsActive="15"/>
</http:listener-config>

<flow name="SimpleTest" >
<http:listener path="/abc" config-ref="myRequestConfig" allowedMethods="PATCH"/>
<logger category="d" level="ERROR" message="xpath: #[xpath3('/isCancelled')]"/>

<choice >
<when expression="#[xpath3('/isCancelled') == 'true']">
 <set-payload value="SUCCESSS"></set-payload>
</when>
<otherwise>
 <set-payload value="FAIL"></set-payload>
</otherwise>
</choice>
</flow>

</mule>
Now we can see that traditional http inbound endpoint and http connector is replaced with Http Listener and Http Listener Config respectively . The major difference is that we can not use service overrides in Http listener config like we used to do in http connector , also http listener does not return MULE_SESSION in response headers .If we use http listener , all outbound property will automatically be converted in http response headers . You can use allowedMethods attribute to configure different Http methods . We can have multiple http listener , referencing to same listener config . Multiple flows with same path but different methods is also allowed .

Post Comments And Suggestions .!!!



Wednesday, 9 December 2015

Textual description of firstImageUrl

How to Generate Barcode 128 In Java

For generating Barcodes , we can use Barcode4j library , which is opensource and free library . Let's look at some java code to generate barcode as image in java .

First of all , enter following dependency in your pom.xml.
<dependency>
 <groupId>net.sf.barcode4j</groupId>
 <artifactId>barcode4j</artifactId>
 <version>2.1</version>
   
   </dependency>

You can also download the jar from here if you are using it in a standalone java program .

Now here is the sample java program which generates the barcode 128 with codeset B and save it as a png file.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code128.Code128Constants;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;


public class MainClass2 {
  
  public static void main(String[] args) throws IOException {
    
    String barcodeString = "4888575";
    Code128Bean barcode128Bean = new Code128Bean();
    
    
    barcode128Bean.setCodeset(Code128Constants.CODESET_B);
    final int dpi = 100;

  //Configure the barcode generator
    //adjust barcode width here
  barcode128Bean.setModuleWidth(UnitConv.in2mm(5.0f / dpi)); 
  barcode128Bean.doQuietZone(false);

  //Open output file
  File outputFile = new File("G:/barcode.png");
  OutputStream out = new FileOutputStream(outputFile);
  try 
  {
      BitmapCanvasProvider canvasProvider = new BitmapCanvasProvider(
              out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

      barcode128Bean.generateBarcode(canvasProvider,barcodeString);

      canvasProvider.finish();
  } 
  finally 
  {
      out.close();
  }
    
  }

}

Barcode4J supports other formats like EAN-128, GS1-128 , Code 39 etc as well .


Provide comments and suggestions !!!



Sunday, 22 November 2015

Textual description of firstImageUrl

HOW TO SETUP NFS MOUNT IN LINUX

NFS mount is used to share a directory between several servers, It saves the disk Space and is Useful for a Infrastructure where a application shares a common directory and is hosted in multiple instances.

Lets think we have a Rails application where we upload a csv file and save it a directory in server with file name to DB .

On the other hand a Rake Task/ Cron Job runs every two minutes that polls DB for file name and proccess the CSV file to get the desired response.

It works fine when we have a single instance hosted in server, think if we have two instances of Rails in different Server and is managed by a Load balancer to server request.

When a File Upload Request comes to ELB , it directs to instance1 with file1 and the second request to instance 2 with file2, The cron job in instance 2 picks the file name file1 from DB and doesnot found any matching file and Marks FILE NOT FOUND IN DB , where as file is present in instance1.

To avoid this type of issues we can use S3 which we can get py paying few amount, the Opensource way is Using NFS Mount, Lets Check how to Configure NFS.

Lets Say we have two instances with IP:
10.23.225.16
10.23.225.17

Lets Configure
NFS-Master :  10.23.225.16
NFS - Client :  10.23.225.17

MASTER SETUP :
Execute the following Commands:
sudo su 
yum install nfs-utils nfs-utils-lib
Startup Scripts :

chkconfig nfs on 
service rpcbind start 
service nfs start

EXPORT the Shared Directory :

Suppose we want the directory /home/rails/upload_dir , to be shared between the instances.

Open vi /etc/exports
Add the following lines at EOF
/home/rails/upload_dir 10.23.225.17(rw,sync)
then execute :
exportfs -a
service nfs restart
CLIENT SETUP :
SSH to server 10.23.225.17

Execute following commands:

yum install nfs-utils nfs-utils-lib
#Assuming the upload_dir path is same as instance1
mkdir -p /home/rails/upload_dir
mount 10.23.225.26:/home/rails/upload_dir /home/rails/upload_dir

You Can verify it by typing:
df –h
Filesystem            Size  Used Avail Use% Mounted on /dev/sda               20G  783M   18G   5% / 10.23.225.16:/home/rails/upload_dir 20G  785M   18G   5% /home/rails/upload_dir
TO ENSURE IT IS ALWAYS MOUNTED MAKE CHANGES IN FS TAB:
nano /etc/fstab
Add the following line:
10.23.225.16:/home/rails/upload_dir  /home/rails/upload_dir nfs      defaults,_nfs_shared 0 0
Try creating a file in instace1 of shared dir and check it should be populated in instance2 of shared dir(/home/rails/upload_dir)

Keep in Mind Once the NFS Mount is unmounted all the data will be lost from the mounted instance , but data will be safe in Master instance.

This post is created by Santosh Mohanty . You can contact him for any questions at santa.jyp@gmail.com or message him on his linkedIn profile .


Tuesday, 17 November 2015

Textual description of firstImageUrl

Search And Replace String in ByteBuffer

This is a simple java program which will search and replace a particular String in ByteBuffer
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;


public class ByteBufferTest {
  
  public static void main(String[] args)
  {
    String fullText = "javarootsbestwebsiteoftheyear";
    String search = "website";
    String replace = "blog";
    ByteBuffer input = ByteBuffer.wrap(fullText.getBytes());
    byte[] toSearch = search.getBytes();
    int index = getIndex(input, toSearch);
    
    byte[] first = new byte[index];
    input.get(first);
    
    byte[] second = replace.getBytes();
    
    input.position(input.position()+search.length());
    
    ByteBuffer finalOne = ByteBuffer.allocate(first.length+second.length+input.remaining());
    finalOne.put(first);
    finalOne.put(second);
    finalOne.put(input);
    finalOne.flip();
    
    CharBuffer result = Charset.forName("UTF-8").decode(finalOne);
    String s = result.toString();
    System.out.println(s);
    
    
  }
  
  public static int getIndex(ByteBuffer input , byte[] toSearch)
  {
    int index = -1 ;
    while(input.hasRemaining() && index==-1)
    {
      boolean found = true ;
      for(byte b : toSearch)
      {
        if(input.get()!=b)
        {
          found = false ;
          break ; 
        }
      }
      if(found)
      {
        index= input.position()-toSearch.length;
      }
    }
    input.rewind();
    return index ;
  }

}

Post Comments And Suggestions !!!