Sunday, January 22, 2017

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986



If tomcat throws below exception:


java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

 
 Check below point….

Are you using tomcat 8 ? 


Because there is | and ‘{‘, ‘}’ character or some other character which is not supported by Tomcat 8. And it may through below exception.

Tomcat - User - Tomcat 8.0.39 and tomcat 8.5.8 fails handling request

Tomcat has increased the validation of incoming HTTP requests to reject the requests that are not specification compliant. 


For the query string or parameters, the RFC specific extracts from RFC 3986 are:

unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
query       = *( pchar / "/" / "?" )
sub-delims    = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"

Hence, '{' and '}' are rejected.




Note:

In my request, I am sending a filter JSON object to the server which includes ‘{‘and ‘}’ and is getting rejected.


Request:

 http://localhost:8080/App/eqp?filters={"filters":{ "column":"xxx","operator":"LIKE","value":"111”}


But If you uses Tomcat Version 7, its working fine!!!






Wednesday, January 18, 2017

How to control object creation in Spring ?And scopes in Spring.





Spring IOC container will create only one object of the Test class. If....





We have two scopes for simple beans :

  1. singleton

  2. prototype



If it is a web application then we have 3 scopes :

  1. request

  2. session

  3. context


So what IOC internally is doing after reading XML file document:

Whenever we try to call getBean of “test” it will try to search for references and if it will find “test” that “test” reference class object it will create and if its scope is singleton then it will create singleton test and you know what it is.

I we call it multiple time the same object will be return.

But if scope is prototype then it will create new object for every call.. ;)

So this is how we creates objects of pojo classes. We have created objects by using core container.

Advanced container :

ApplicationContext ap = new ClassPathXMLApplicationContext(“spring.xml”);
ap.getBean(“test”);
ap.getBean(“test”);


How to start Spring containers like we start Tomcat container by simply start and stop ?




We have interfaces in containers and corresponding classes to start to start these containers.as in screen below...

IOC Hierarchy (Factory, Classes and Interfaces)

DriverManager.getConnection() will give a Connection object, in same way WebApplicationContextUtil is a factory class which will give WebApplicationConext object.

HelloWorld (required below) 

1. Pojo
2. XML
3. Client class (Driver class) 

------------------------------------------------------------------


HelloWord Sample :



Test.java (Pojo class)
 ---------------------------------------------------------------------------------
/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println(" Hello World !!!");
    }
}



Spring XML :

spring.xml
----------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd>
   
   
    <beans:bean id="test" class="com.demo.Test">
   
</beans>




Client.java (run this class)
---------------------------------------------------------

 public class Client
{
    public static void main( String[] args )
    {
        Resource r = new ClassathResource("spring.xml");
        BeanFactory factory = new XMLBeanFactory(r);
        factory.getBean("test");
    }
}



Output:
---------------------------------------------------------

 Hello World !!!



What are the types of IOC containers in Spring ?



In spring we have two IOC containers and one Web Container is in Spring MVC:

1.       Core container   (BeanFactory)

2.       J2EE container    (ApplicationContext/ConfigurableApplicationContext)

3.    Web Container

This web container is prepared on top of IOC container.
IOC types Hierarchy


What container will do ? 

Below are few tasks of containers :
 
  1. read xml files (web.xml)

  2. create instances of xml beans classes/servlet...POJO

  3. manage lifecycle of bean classes/servlet...POJO

  4. it will supply dynamic parameters to bean classes/servlet..POJO as eg. init param and context param in servlet  ;)


Note: XMLs are used to give input to POJO classes and Associated references to make our layers loosely coupled.

Dependency Injection (DI) is the main asset of the IOC container.

This web container is prepared on top of IOC container.


What do you mean by Spring is loosely coupled?



Lets start with an example GSM mobile numbers are compatible with any network.We can change network on any Network Provide (like Airtel,BSNL etc.), sim cards portability compatibility refers to loosely couple. We don’t need any tightly relation or binding with any network.

The same type of compatibility we need to have in our applications.

 Our application should not have any dependency with other layers. Our application should also need not to have any dependency with any layer.


Eg. Like in our application we have view layer, controller  and other layers as below in the picture below..
JEE application Flow
           

What do you mean by Spring is light weight?



Few points to confirm Spring is Light Weight :

1. Like Swings are light weight , AWT are heavy weight. Same reason.

2. AWT will use OS libraries while swing  has no dependency on OS. It will use JDK libraries only.

3. Swings are not having any dependency on OS.It will directly use JDK libraries.

4. EJB’s are having dependency of Application Servers.

5. Spring don’t need application server.Spring jars are enough along with your JDK.



What is Spring ? Brief history. And Why Spring is hot in the market ?

Spring
Spring has been developed by Rod Johnson.
Earlier the oorganization name was Spring but now it has changed to Pivotal
In a very beginning, Spring/Pivotal used to call it Interface 21.
Why named as Spring ? No Reasons… ;)
EJBs are like winter season in US -20 degree temperature. After winter next season is Spring. Because in US Spring season is best around 5-10 degree temperature. NO SPECIAL REASON that’s why logo is a leaf. ;)


Why Spring is Hot in developers world ?

Below are few points to answer above query :
  1. Spring is better alternative of EJB. 

  2. By using EJB, applications become tightly coupled and heavy weight but using spring we can create loosely coupled and light weight application. 

  3. Reduce investment cost. No need for application servers or to buy services.

  4. Spring freely provide services so that we can use those service in our enterprise applications.

  5. EJBs are suitable for banking applications but for  if your investment cost is 1-2 lacks EJBs are not suitable .We need some free resources that is Spring.

Friday, January 13, 2017

How to avoid downloading same file again to your local machine from server by using MD5 algorithm?





How to avoid downloading same file again to your local machine from server by using MD5 algorithm?

Note: do have a checksum file on server with same extension of the files you are going 
to download.
Eg. You are going to download test.jar and Config.properties. 
You should create hash/checksum of these files on
Server with extension as test.jar.sh and Config.properties.sh, 
These file will have content as there check sum.



Below class will perform the checked download of the file you are going to download 
from the server.
Means, if the same file is already in your machine, it will compare the checksum 
of the server file which we will download at very first.

At a very first, if the checksum files folder is empty, it will download all the files from
 the server.

On second time, if there is any change in the file on server, then the checksum 
of that file will change and we will download that zip folder and extract on put local machine.

Then we will compare the local machine files checksum and newly downloaded
 checksum file’s folder. 

If there is difference, it will download the fresh copy of the file else not.


---------------------------------------------------------------------------------
Code:
 --------------------------------------------------------------------------------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Test {
       // server path from where the files will download
       static String serverSHFilesPath = "http://localhost:8080/examples/server/serverSH.zip";

       // the file which you want to download after checking checksum
       static String serverDownloadFilesPath = "http://localhost:8080/examples/server/";

       // location on local machine where the files will get download after
       // checksum or by default
       // on first time
       static String destPath = "C:/Users/UserName/AppData/Roaming/download/";
       // path where server checksum files will get download
       static String destPathServerSH = "C:/Users/UserName/AppData/Roaming/download/serverSH/";

       static String destPathLocalSH = "C:/Users/UserName/AppData/Roaming/download/localSH/";

       static String serverSHName = "serverSH.zip";

       // name of the file which will be downloading
       static String downloadFile = "Config.properties";

       public static void main(String[] args) throws MalformedURLException,
                     NoSuchAlgorithmException, IOException {
              createFilesFolderPath(destPathServerSH);
              downloadServerSHFiles(serverSHFilesPath,
                           Paths.get(destPathServerSH, serverSHName).toString());

              downloadingFiles(serverDownloadFilesPath, destPath, downloadFile,
                           destPathServerSH);

       }

       private static void downloadServerSHFiles(String serverPropsPath2,
                     String destPathServerSH2) throws MalformedURLException, IOException {

              InputStream openStream = new URL(serverPropsPath2).openStream();

              try (ReadableByteChannel in = Channels.newChannel(openStream);
                           FileChannel out = new FileOutputStream(destPathServerSH2)
                                         .getChannel()) {
                     out.transferFrom(in, 0, Long.MAX_VALUE);
              }
//unzip checksum folder on local
              unzip(destPathServerSH2, destPath);
//delete after unzipping
              deleteZipFolders(destPathServerSH2);
       }

       public static void downloadingFiles(String serverDownloadFilesPath,
                     String destPath, String downloadFile, String destPathServerSH)
                     throws MalformedURLException, IOException, NoSuchAlgorithmException {

              // copyFile(createFilesFolderPath, serverPropsPath);
              MessageDigest md5 = null;

              md5 = MessageDigest.getInstance("MD5");

              InputStream openStream = new URL(serverDownloadFilesPath + downloadFile)
                           .openStream();

              // careate path on local where you have intend to download the files
              createFilesFolderPath(destPath);

              StringBuilder sb = null;
              // logic to check the file you are going to download are already
              // downloaded and are not changed
              File fLocal = new File(Paths.get(destPathLocalSH,
                           downloadFile + ".sh.txt").toString());
              File fServer = new File(Paths.get(destPathServerSH,
                           downloadFile + ".sh.txt").toString());

              // if the checksum file exist on local machine then enter in if
              // condition
              if (fLocal.exists()) {
                     // sb = calculateHash(destPath, downloadFile, md5);
                     String localFileSH = new String(Files.readAllBytes(Paths.get(fLocal
                                  .getAbsolutePath())));

                     String serverFileSH = new String(Files.readAllBytes(Paths
                                  .get(fServer.getAbsolutePath())));
                     System.out.println("Hash value of file  : " + downloadFile + " : "
                                  + localFileSH);

                     //same hashcode then don’t download
                     if (serverFileSH.equals(localFileSH)) {
                           System.out
                                         .println(" hash code is same !!! no need to download the file ");
                     } else {
//different checksum, update local copy of checksum file and download file as well
                           BufferedWriter writer = new BufferedWriter(new FileWriter(fLocal));
                           writer.write(serverFileSH.toString());
                           writer.close();

                           try (ReadableByteChannel in = Channels.newChannel(openStream);
                                         FileChannel out = new FileOutputStream(destPath
                                                       + downloadFile).getChannel()) {
                                  out.transferFrom(in, 0, Long.MAX_VALUE);
                           }

                           System.out
                                         .println("Successfully re-downloaded  and verified checksum!"
                                                       + localFileSH);
                     }
              } else {

                     // unzip code
                     try (ReadableByteChannel in = Channels.newChannel(openStream);
                                  FileChannel out = new FileOutputStream(destPath
                                                + downloadFile).getChannel()) {
                           out.transferFrom(in, 0, Long.MAX_VALUE);
                     }
                     sb = calculateHash(destPath, downloadFile, md5);
                     BufferedWriter writer = new BufferedWriter(new FileWriter(fLocal));
                     writer.write(sb.toString());
                     writer.close();
                     String contents = new String(Files.readAllBytes(Paths.get(fLocal
                                  .getAbsolutePath())));
                     System.out
                                  .println("Successfully downloaded  and verified checksum!"
                                                + contents);
              }

       }

       public static String createFilesFolderPath(String zaraOpsLocalPath) {

              File file = new File(zaraOpsLocalPath);
              if (!file.exists()) {
                     if (file.mkdirs()) {
                           return zaraOpsLocalPath;

                     } else {
                           System.out.println("Failed to create directory!");
                     }
              }
              return zaraOpsLocalPath;
       }

       private static void copyFiles(File source, File dest, String downloadFile,
                     String destPath) throws IOException {
              if (downloadFile.contains("ConfigProp.zip")) {
                     File propDel = new File(destPath + "Config.properties");
                     propDel.delete();
              }
              if (downloadFile.contains(".zip")) {
                     String replace = downloadFile.replace(".zip", "");
                     File propDel = new File(destPath + replace);
                     boolean deleteDirectory = deleteDirectory(propDel);
                     if (deleteDirectory)
                           System.out.println(" file : " + deleteDirectory + " deleted.");
              }
              if (dest.exists())
                     dest.delete();
              Files.copy(source.toPath(), dest.toPath());
       }

       public static boolean deleteDirectory(File directory) {
              if (directory.exists()) {
                     File[] files = directory.listFiles();
                     if (null != files) {
                           for (int i = 0; i < files.length; i++) {
                                  if (files[i].isDirectory()) {
                                         deleteDirectory(files[i]);
                                  } else {
                                         files[i].delete();
                                  }
                           }
                     }
              }
              return (directory.delete());
       }

       private static StringBuilder calculateHash(String destPath,
                     String downloadFile, MessageDigest md5)
                     throws FileNotFoundException, IOException {
              StringBuilder sb = new StringBuilder();
              byte[] dataBytes = new byte[1024];

              int nread = 0;
              FileInputStream fis = new FileInputStream(destPath + downloadFile);

              while ((nread = fis.read(dataBytes)) != -1)
                     md5.update(dataBytes, 0, nread);
              fis.close();
              byte[] mdbytes = md5.digest();

              for (int i = 0; i < mdbytes.length; i++)
                     sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
                                  .substring(1));
              return sb;
       }

       private static void unzip(String zipFilePath, String destDir) {
              if (zipFilePath.contains(".zip")) {
                     try {
                           File dir = new File(destDir);
                           // create output directory if it doesn't exist
                           if (!dir.exists())
                                  dir.mkdirs();
                           // Open the zip file
                           ZipFile zipFile = new ZipFile(zipFilePath);
                           Enumeration<?> enu = zipFile.entries();
                           while (enu.hasMoreElements()) {
                                  ZipEntry zipEntry = (ZipEntry) enu.nextElement();

                                  String fileName = zipEntry.getName();

                                  // Do we need to create a directory ?
                                  File file = new File(destDir + File.separator + fileName);
                                  System.out.println("Unzipping to : "
                                                + file.getAbsolutePath());
                                  if (fileName.endsWith("/")) {
                                         file.mkdirs();
                                         continue;
                                  }

                                  File parent = file.getParentFile();
                                  if (parent != null) {
                                         parent.mkdirs();
                                  }

                                  // Extract the file
                                  InputStream is = zipFile.getInputStream(zipEntry);
                                  FileOutputStream fos = new FileOutputStream(file);
                                  byte[] bytes = new byte[1024];
                                  int length;
                                  while ((length = is.read(bytes)) >= 0) {
                                         fos.write(bytes, 0, length);
                                  }
                                  is.close();
                                  fos.close();

                           }
                           zipFile.close();
                     } catch (IOException e) {
                           e.printStackTrace();
                     }
              }
       }

       private static void deleteZipFolders(String zipFilePath) {
              // TODO Auto-generated method stub
              File file = new File(zipFilePath);
              if (zipFilePath.contains(".zip")) {
                     file.delete();
                     System.out.println("deleted : " + file.getName());
              }
       }
}




Output:
This hierarchy will get generated after executing above code.

 

Inside localSH folder..











 
       
Inside serverSH… you can iterate all the files to download and
check checksum..




 












   
On server, this can be created by creating a web service which will
 generate these files on server and stored as zip.


 




     
   Content of these files will be something like below screen shot :


 

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...