Monday, September 1, 2014

Apache PDFBox

Apache PDFBox open source.

I came across this open source, when i wanted to split PDF based on the number pages.

PDFBox is a easy to use opensource library for merging, spliting PDFs which is available for free under Apache license.

If you want to split the large PDF using the PDF box without writing any code.you can use the command line tools of the same library

http://pdfbox.apache.org/commandline/

Download the pdfbox libraries, and follow the instructions.
It can encrypt, decrypt, extrtact text from pdf, etc.

To split below is the command

 java -jar pdfbox-app-x.y.z.jar PDFSplit [OPTIONS] <PDF file>


To code in java for creating a blank pdf document, below is the code.
------------------------------------
import java.io.IOException;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class EntryClass {
public static void main(String args[])
{
try {
PDDocument document= new PDDocument();
PDPage blankPage = new PDPage();
document.addPage(blankPage);
document.save("FirstPage.pdf");
if(document!=null)
{
document.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (COSVisitorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
}

OUTPUT:
Creates a blank page
----------------------

Wednesday, June 4, 2014

Retrieving the URL parameters as name value in java - URLEncodedUtils class

Printing the URL parameter in name value pair

Requirement: 
If the complete URL is know probably this program would help in printing the url parameters in name-value pair using java.

I used it for the below reason:
In order to validate the security vulnerabilities like XSS, SQL Injection, etc. IBM APPSCAN tool was used, the report should be analyzed to verify the issue.
The report has the url that was used to get the response, most of the times the URL was too big and difficult to interpret so i was going thorough java docs to find a way to print the parameters of the URL so that it would be human readable format.

Below was the code i was using, it was useful

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

public class ReadURLNameValue {

static String urlVal = "http://www.domain.com/index.jsp?name=urname&city=&pincode=1231";
public static void main (String args[]) throws URISyntaxException
{
List<NameValuePair> params = URLEncodedUtils.parse(new URI(urlVal), "UTF-8");

for (NameValuePair param : params) {
System.out.println(param.getName() + " : " + param.getValue());
}
}
}

OUTPUT:
name : urname
city: null
pincode : 1231

Friday, March 14, 2014

Converting HTTP url web service to HTTPS (SSL) web service in Linux - tomcat application

To change the HTTP URL web-service to HTTPS URL.

Our web application was using a HTTP URL for invoking the web-service to fetch values based on the input there was a requirement to change this to HTTPS web-service (HTTP over SSL).

The HTTP web-service client was implemented in Axis 1.0. The web application developed using Spring-Struts framework in Java (1.7) and deployed in tomcat 6.0 hosted in Linux os server.

To implement the HTTPS web-service in the web application the below was followed:

In Linux, we tried to call the HTTPS URL directly using the curl command:

curl <wsdl https url>

we got certificate error stating could not handshake with the remote web-service.

curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). The default bundle is named curl-ca-bundle.crt; you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).

Since we had filtered access to the Linux server we have to validate that the WSDL HTTPS web-service is accessible via Linux. after using

curl -k <wsdl https url> 

we where able to access the wsdl, the option -k masks the handshake.

Obtaining the certificate.
After getting the certificate, in order to generate a keystore and include it the system we have to do the below.

To generate a keystore
1. The certificate (CA authorized)  was .cer file.
           - use java keytool command to create a key store. Example:
            /opt/java1.7/bin/keytool -import -alias <aliasName> -file <CertificateFileName.cer> -keystore <keyStorFileName.keystore>
To include in the tomcat CLASS path
2. include this key store in tomcat JAVA_OPTS path in setenv.bat or in case of Linux setenv.ksh. as
           - Djava.ssl.truststore=<path of the keystore>
3. Stop and Start the application. the HTTPS web-service will work without any issues.

Reference the below link (courtesy
http://javarushi.blogspot.com/2012/07/consuming-secure-web-services-or-web.html