Monday, December 17, 2018

Best node.js rest client for post request

I was trying to make a post request from my server to a rest API which records data for auditing,
I found requestify as very clean package for node.js

Get Implementation below:
     var requestify = require('requestify');
     requestify.get('https://reqres.in/api/users?page=2')
               .then(function(response) {
                   // Get the response body (JSON parsed - JSON response or jQuery object in case of XML response)
                   console.log(response.getBody());

                   // Get the response raw body
                   console.log(response.body);
                });

POST Implementation


      requestify.post('https://reqres.in/api/users', {
             "name": "Asker",
             "job": "SSE"
          }).then(function(response) {
             // Get the response body
             console.log(response.getBody());
             console.log(response.body);
          });


Monday, July 2, 2018

How to set Max File Size in Spring Boot

Once you try to upload a file with size more than 10 MB. You will face the following error.

[org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: 

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.]

The solution I Applied is is as follows.

@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setMaxFileSize("124MB");
    factory.setMaxRequestSize("124MB");
    return factory.createMultipartConfig();
}
Ref: https://www.experts-exchange.com/questions/28990849/How-to-increase-Spring-boot-Tomcat-max-file-upload-size.html

Wednesday, June 27, 2018

How to run ANT with a lower Java Version than installed

ANT Refer to the Java version mentioned in JAVA_HOME path in system Environment Variables.

In a Scenario where you have to run ANT with JDK 1.7 while Java 8 is installed in the system.

Update JAVA_HOME to the JDK1.7 Path and execute the ANT Command.

Saturday, May 12, 2018

Collection Framework in Java Script -

A Collection Framework class contains all basic classes like Stack, Queue, HashMap, ArrayList, Set Download Here
define(['require','CONSTANT'], 
    function ( require, CONSTANT ) {
        /**
		 * A Collection Framework class contains all basic classes like Stack, Queue, HashMap, ArrayList, Set
		 * @name Collections
		 * @class Collections
		 * @example require('Collections')
		 * @exports Collections
		 * @version 1.0
		 * @module Collections
		 * @augments CONSTANT
		 **/
		var Collections = {
			
			 /**
			 * Object types provided by Collections 
			 * @name TYPE
			 * @class TYPE
			 * @example require('Collections').TYPE.STACK
			 * @example require('Collections').TYPE.ARRAYLIST
			 * @example require('Collections').TYPE.HASHMAP
			 * @example require('Collections').TYPE.SET
			 * @example require('Collections').TYPE.QUEUE
			 * @example require('Collections').TYPE.TREE
			 * @exports TYPE
			 * @version 1.0
			 * @module Collections
			 * @memberOf Collections
			 * @field
			 **/
			TYPE:{
				/** @constant*/
				STACK:"Stack",
				ARRAYLIST:"ArrayList",
				HASHMAP:"HashMap",
				SET:"Set",
				QUEUE:"Queue",
				TREE:"Tree"
			},
	        /**
			 * A Stack is a Collection that contains duplicate values. Last In First Out. 
			 * @name Stack
			 * @class Stack
			 * @example require('Collections').Stack()
			 * @exports Stack
			 * @version 1.0
			 * @module Collections
			 * @memberOf Collections
			 **/
			Stack: 	function () {
				var stac = [];
				var stackObj = {
					/**
					 * @name pop
					 * @memberOf Collections.Stack
					 * @method 
					 * @description removes the top item from teh stacks
					 * @return {Object} item 
					 * @function
					 */
					pop : function() {
						return stac.pop();
					},
					/**
					 * @name push
					 * @memberOf Collections.Stack
					 * @method 
					 * @description removes the top item from teh stacks
					 * @param {Object} item
					 * @return {null}  
					 * @function
					 */
					push : function(item) {
						stac.push(item);
					},
					/**
					 * @name isEmpty
					 * @memberOf Collections.Stack
					 * @method 
					 * @description returns true if teh Stack is empty.
					 * @return {boolean}  
					 * @function
					 */
					isEmpty : function() {
						return stac.length == 0;
					},
					/**
					 * @name objType
					 * @memberOf Collections.Stack
					 * @method 
					 * @description returns teh Object type as "Stack"
					 * @return {String} ObjectType  
					 * @function
					 */
					objType : function(){
						return "Stack";
					},
					/**
					 * @name getElements
					 * @memberOf Collections.Queue
					 * @method 
					 * @description returns teh Elemnts in the Queue
					 * @return {Object[]} itemArray  
					 * @function
					 */
					getElements : function(){
						return stac.clone();
					},
					/**
					 * @name toString
					 * @memberOf Collections.Stack
					 * @method 
					 * @description returns the JSON data of teh Object
					 * @return {String} ObjectType  
					 * @function
					 */
					toString : function(){
						return "["+stac.join()+"]";
					},
					/**
					 * @name clone
					 * @memberOf Collections.Stack
					 * @method 
					 * @description returns Collection.Stack a new Stack with the cloned object of the items.
					 * @return {Collections.Stack}
					 * @function
					 */
					clone:function(){
						var Collection = require('Collections');
						var cloneStack = Collection.Stack();
						var elements = stac;
						for(var i=0;i=0; i--){
							if(elements[i].clone)
								cloneQueue.add(elements[i].clone());
							else
								cloneQueue.add(elements[i]);
						}
						return cloneQueue;
					}
				};
				return queueObj;
			},
			/**
			 * An ArrayList is a Collection that contains duplicate values. 
			 * @name ArrayList
			 * @class ArrayList
			 * @example require('Collections').ArrayList()
			 * @exports ArrayList
			 * @version 1.0
			 * @module ArrayList
			 * @memberOf Collections
			 **/
			ArrayList : function() {
				var arr = [];
				var array = {
					/**
					 * @name add
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description insert an Item to the ArrayList, it adds to the last of the array, It allows duplicate values in the list.
					 * @param {Object} item
					 * @return {null} 
					 * @function
					 */
					add : function(item) {
						arr.push(item);
					},
					/**
					 * @name addAll
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description insert an array of Items to the ArrayList, it adds to the last of the array, It allows duplicate values in the list.
					 * @param {Object[]} itemArray
					 * @return {null} 
					 * @function
					 */
					addAll : function(items) {
						for(var i=0; i< items.length; i++)
							this.add(items[i]);
					},
					/**
					 * @name indexOf
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns teh index of teh item in the ArrayList, if item not found it returns -1.
					 * @param {Object} item
					 * @return {integer} 
					 * @function
					 */
					indexOf : function(item) {
						return arr.indexOf(item);
					},
					/**
					 * @name contains
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns whether the item present in the ArrayList.
					 * @param {Object} item
					 * @return {boolean} 
					 * @function
					 */
					contains:function(item){
						return arr.indexOf(item) != -1;
					},
					/**
					 * @name remove
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description remove an item from the array, then the remaining items will be shifted left, and their index will be reduced by 1.
					 * @param {Object} item
					 * @return {null} 
					 * @function
					 */
					remove : function(item) {
						if (arr.indexOf(item) >= 0)
							arr.splice(arr.indexOf(item), 1);
					},
					/**
					 * @name size
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns the number of the Elements present in the ArrayList.
					 * @return {integer} 
					 * @function
					 */
					size:function(){
						return arr.length;
					},
					/**
					 * @name get
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns the object at the prvided index.
					 * @param {integer} index
					 * @return {Object} item 
					 * @function
					 */
					get : function(index){
						return arr[index];
					},
					/**
					 * @name set
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description set an item at the specified location
					 * @param {integer} index
					 * @param {Object} Item
					 * @return {null}  
					 * @function
					 */
					set:function(index, item){
						if(index<0) throw "Index out of bound exception";
						arr[index]=item;
					},
					/**
					 * @name getElements
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns teh Elemnts in the ArrayList
					 * @return {Object[]} itemArray  
					 * @function
					 */
					getElements : function(){
						return arr.clone();
					},
					/**
					 * @name objType
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns teh Object type as "ArrayList"
					 * @return {String} ObjectType  
					 * @function
					 */
					objType:function(){
						return "ArrayList";
					},
					/**
					 * @name toString
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns the JSON data of teh Object
					 * @return {String} ObjectType  
					 * @function
					 */
					toString : function(){
						return "["+arr.join()+"]";
					},
					/**
					 * @name json
					 * @memberOf Collections.ArrayList
					 * @method 
					 * @description returns the JSON string
					 * @return {String} json  
					 * @function
					 */
					json :function(){
						var jsonArr=[];
						for(var i=0; i

Thursday, May 10, 2018

How to set the max size of upload file in Spring Boot embedded Apache Tomcat

Error in spring boot : org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field uploadfile exceeds its maximum permitted size of 1048576 bytes. I found the solution at Expert Exchange, which worked well.
@Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("124MB");
        factory.setMaxRequestSize("124MB");
        return factory.createMultipartConfig();
    }
Expert Exchange

Wednesday, November 8, 2017

Error while connecting Hive

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hive/service/rpc/thrift/TCLIService$Iface
 at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:107)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at io.saagie.example.hive.Main.main(Main.java:39)
Caused by: java.lang.ClassNotFoundException: org.apache.hive.service.rpc.thrift.TCLIService$Iface
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 ... 4 more

Thursday, October 26, 2017

Execute Shell script in Java Program


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;

import org.apache.log4j.Logger;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

public class ExecuteScriptInServer {
 private static final Logger logger = Logger.getLogger(ExecuteScriptInServer.class);

 public static void main(String[] args) {
  JSch jsch = new JSch();
  Session session = null;
  try {
   InetAddress addr = InetAddress.getLocalHost();
   logger.info("Local HostAddress: " + addr.getHostAddress());
   String hostname = addr.getHostName();
   logger.info("Local host name: " + hostname);
   session = jsch.getSession("root", "10.1.11.4", 22);//UserName, Pasword, Port
   jsch.setKnownHosts(new FileInputStream(new File("known_hosts.txt")));// This file can be generated using ssh-keygen -t rsa Command in the Unix Machine.
   UserInfo ui = new MyUserInfo();
   session.setUserInfo(ui);
   logger.info("Connecting...");
   session.setPassword("password".getBytes());
   session.connect();
   System.out.println("Connected");
   executeCommand(session, "pwd");
   session.disconnect();

   logger.info("Connected");
  } catch (JSchException e) {
   logger.info(e.getMessage());
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
   logger.info(e.getMessage());
  } finally {
   session.disconnect();
   logger.info("Disconnected....");
  }
 }

 private static void executeCommand(Session session, String command) throws JSchException, IOException {
  Channel channel = session.openChannel("exec");
  ((ChannelExec) channel).setCommand(command);
  channel.setInputStream(null);
  ((ChannelExec) channel).setErrStream(System.err);
  InputStream in = channel.getInputStream();

  channel.connect();

  byte[] tmp = new byte[1024];
  while (true) {
   while (in.available() > 0) {
    int i = in.read(tmp, 0, 1024);
    if (i < 0)
     break;
    System.out.print(new String(tmp, 0, i));
   }
   if (channel.isClosed()) {
    System.out.println("exit-status: " + channel.getExitStatus());
    break;
   }
   try {
    Thread.sleep(1000);
   } catch (Exception ee) {
   }
  }
  channel.disconnect();
 }

 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {

  @Override
  public String getPassphrase() {
   return null;
  }

  @Override
  public String getPassword() {
   return null;
  }

  @Override
  public boolean promptPassphrase(String arg0) {
   return false;
  }

  @Override
  public boolean promptPassword(String arg0) {
   return false;
  }

  @Override
  public boolean promptYesNo(String arg0) {
   return false;
  }

  @Override
  public void showMessage(String arg0) {
  }

  @Override
  public String[] promptKeyboardInteractive(String arg0, String arg1, String arg2, String[] arg3,
    boolean[] arg4) {
   return null;
  }
 }

}