Showing posts from 2017

Handle CORS problem using java (Servlet/Filter)

Handle CORS problem using java (Servlet/Filter)

1. Make a Filter to handle all CORS request

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet Filter implementation class CORSFilter
public class CORSFilter implements Filter {

    public CORSFilter() {
        // TODO Auto-generated constructor stub

    public void destroy() {
        // TODO Auto-generated method stub

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        HttpServletResponse resp = (HttpServletResponse) servletResponse;

Read Images from a xlsx file using Apache POI

This article is all about how to read and download images inside a file in xlsx format. We will use Apache POI using java.

1. Maven dependencies
commons-logging-1.2.jar commons-collections4-4.1.jaropenxml4j-1.0-betapoi-3.16.jarpoi-ooxml-3.16.jarpoi-ooxml-schemas-3.16.jarxmlbeans-2.6.0.jarservlet-api 2. Sharing java code

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWork…

Read Excel using Apache POI - Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:

Read Excel using Apache POI - Challenges ------------------------------------------------ 1) Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

Add following maven dependency
-- Use poi-ooxml from xlsx files while poi for xls <!-- POI : Excel library --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency>        < groupId>org.apache.poi</groupId>         <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency
2)Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException -- Reading excel using apache POI – Add library xml…

Create and write Excel file using Google App Engine

This article explains how you can export data in excel using Google App Engine.

1. Apache POI library -- Here is maven dependency
2. Download Servlet

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

     private static final String F…

Git Commands : How to remove all files deleted from index

Remove all deleted files from git index

git ls-files --deleted -z | xargs -0 git rm

Create java client from WSDL for Google App engine

This post will explain how to consume SOAP based web services on Google App Engine platform.

Keep your wsdl files in resources/wsdl folderMake these files entry in appengine-web.xml like  <static-files>
       <include path="/**.wsdl"/>
     <include path="/**.WSDL"/>
 </static-files> Now update POM file for wsdl to java plugin  <plugin>

Convert pfx file into jks using java

This is a post which shows how to convert pfx file into jks.

Prerequisites : Java 6+
Step 1: Use cmd with java in your classpath

keytool -importkeystore -srckeystore testKey.pfx -srcstoretype pkcs12 -srcalias <myAliasName>
 -destkeystore jksFile -deststoretype jks -deststorepass password -destalias <alias>

Java Client with certificate authentication to access SOAP Webservice

This is an example to explain  how to authenticate java stub client with certification and send request to SOAP WSDL.

public static void applyCertificateAuthentication(MyWsdlDataRequest wsdlClientRequest)
            throws Exception {

       String keyStoreLocation="resources/keys/testKey.jks";
       String password="key@12345";
        ClassLoader classLoader = this.class.getClassLoader();
        InputStream keyInputStream = classLoader.getResourceAsStream(keyStoreLocation);

        SSLContext sc = SSLContext.getInstance("SSLv3");
        KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(keyInputStream, password.toCharArray());
        factory.init(keyStore, password.toCharArray());
        sc.init(factory.getKeyManagers(), null, null);

        MyWsdlService serviceImpl = new MyWsdlService ();
       MyWsdlDataStub se…