Friday, 7 October 2016

Formatting or preety printing XML and JSON in Java

Background

XML as we know is a platform independent format that can be used to communicate between different apps. We have seen couple of things on XML like parsing, handling special characters, java library etc (See links in Related Links section below). In this post we will see how we can format or preety print xml in Java.


To the code... preety printing XML

Following code will do the trick -

    private static String beautifyXml(String unformattedXML) {
        Transformer transformer;
        String formattedXmlString = null;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            //initialize StreamResult with File object to save to file
            StreamResult result = new StreamResult(new StringWriter());
            transformer.transform(new StreamSource(new StringReader(unformattedXML)), result);
            formattedXmlString = result.getWriter().toString();
        } catch (TransformerFactoryConfigurationError | TransformerException e) {
            e.printStackTrace();
        }
        return formattedXmlString;
    }

you can test it by simple calling above method with your unformatted xml as input argument. I have created a small web app and have added the functionality of pretty printing xml there. You can see the relevant code (beautifyXml method)-
You can clone the webapp itself and see how it works for yourself. It all on github -

preety printing JSON

You can do similar stuff for Json too -

    public static String beautifyJson(String unformattedStringForm) {
        JsonParser parser = new JsonParser();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        
        try {
            String jsonString = unformattedStringForm;
            JsonElement el = parser.parse(jsonString);
            return gson.toJson(el);
        }
        catch(JsonSyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }


Again relevant code can be found in (beautifyJson method) -


Related Links

Thursday, 6 October 2016

Setup Tomcat on an Amazon EC2 instance to deploy a wep app

Background

Amazon EC2 module is a virtual server in cloud. It's a IaaS (Infrastructure as a service) when you have your own machine (virtual) with an operating system and you can deploy your stuff there. We have already discussed setting up an EC2 instance and connecting to it. If you do not recollect please refer -
 In this post we will see how to setup your tomcat and access it remotely. This post assumes you have an EC2 instance up and running and you can SSH to it remotely.

Setup Tomcat on an Amazon EC2 instance to deploy a wep app

Apache Tomcat, often referred to as Tomcat, is an open-source web server developed by the Apache Software Foundation (ASF). Tomcat implements several Java EE specifications including Java Servlet, JavaServer Pages (JSP), Java EL, and WebSocket, and provides a "pure Java" HTTP web server environment in which Java code can run.

There are two ways you can setup tomcat on your machine (EC2 instance) in this case. Either
  1. install it through apt-get
  2. Download the binary distribution from their official site and use
We are going to take simpler route here and install it via apt-get. First get an update -
  • sudo apt-get update


 Next install tomcat7 -
  • sudo apt-get install tomcat7



 Post installation you can see the default configuration file located at -
  • less /etc/default/tomcat7 


 You can change the configuration parameters here if you wish.

To start,stop and see current tomcat status you can use following commands -
  • sudo service tomcat7 start
  • sudo service tomcat7 stop
  • sudo service tomcat7 status



 Next you can deploy your webapps at /var/lib/tomcat7/webapps
  • CATALINA_HOME : /usr/share/tomcat7
  • CATALINA_BASE : /var/lib/tomcat7

 Now your tomcat installation is done. Tomcat by default runs on port 8080 that you need to open externally (for inbound traffic). Go to Security Groups and add a custom TCP rule to open port 8080 externally -




 Add the rule and save it. You should not be able to access your tomcat. If it shows you Its up! page you are all set!

I have also deployed a web app. If you have a web app just export it as war and put it in webapps directory
  • /var/lib/tomcat7/webapps 
And restart tomcat. Tomcat will automatically extract folder out of it and deploy. If you do not have a webapp and want to test you can checkout -
Import it in eclipse and export a war out of it. If you want to skip all that you can directly download the war from -
and put it in webapps folder. Note this war is compiled with java 8. So make sure you are using Java 8 for your tomcat. You can set it in file -
  • /etc/default/tomcat7
Eg. JAVA_HOME=/usr/lib/jvm/java-8-oracle

Once deployed you can hit following URL and see if it works. If it does not you need to check catalina.out logs for any deployment issues.




You should see a page something like above.

Related Links

Saturday, 24 September 2016

How to delete recently opened files history in ubuntu 14.04

Background

In this post we will see how we can delete recently opened files from Ubuntu's Unity dashboard. Why would I do that you ask? Answer is privacy but then again it largely depends on the usecase. There should not be such a need for a strictly persona computer but if it is shared it is better to delete history when you leave.


How to delete recently opened files history in ubuntu 14.04

Open security and privacy settings from unity dashbaord.




Then go to Files & Applications and select clear usage data and select the period you want to clear data from - 



Click Ok and you are done.




Related Links


Wednesday, 21 September 2016

Programmatically upload files to amazon (AWS) S3

Background

This post will show you how to programmatically upload files to your AWS S3 account using AWS S3 SDK. Lets start by creating access key.

This post hopes you already have a S3 account set up on your AWS console.



Credentials setup


Lets start be creating access key. Following screenshots show you how to create your access key using AWS IAM (Identity and access management).






Once you have create a user you need to give it access to S3. Follow next set of steps for that -



 This will give your new user access to S3.Now you can use these credentials in the code. Before we go to code there is one more step. You need to set up your credential file. It will be in
  • ~/.aws/credentials
If its not there create one and add credentials to it as show below from the user you had created from IAM console.


I have used some template here but replace it with your exact access key if and secret key. Lets go on to the code now.

Programmatically upload files to amazon s3

I am using eclipse and ivy dependency management. So your ivy file should look like following -

<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
        organisation="osfg"
        module="AwsS3Demo"
        status="integration">
    </info>
    
    <dependencies>
        <dependency org="com.amazonaws" name="aws-java-sdk-s3" rev="1.11.36"/>
    </dependencies>
</ivy-module>


Note the dependency we have used. It for aws s3 only.

Now lets head on to the code -

package com.osfg;

import java.io.File;
import java.io.IOException;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;

/**
 * 
 * @author athakur
 *
 */
public class AwsS3Demo {
    
    private static String AWS_BUCKET_NAME = "test-athakur";
    private static String AWS_KEY_NAME = "testData";
    private static String UPLOAD_FILE = "/Users/athakur/Desktop/data.txt";
    
    public static void main(String[] args) throws IOException {
        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        try {
            System.out.println("Uploading a new object to S3 from a file\n");
            File file = new File(UPLOAD_FILE);
            s3client.putObject(new PutObjectRequest(
                    AWS_BUCKET_NAME, AWS_KEY_NAME, file));

         } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which " +
                    "means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which " +
                    "means the client encountered " +
                    "an internal error while trying to " +
                    "communicate with S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

}

Just run the above code and it should upload your file to the AWS bucket. Make sure -
  1. Credentials are correct in ~/.aws/credentials file
  2. That credential has access to S3 module
  3. File is present on your machine
  4. bucket name is valid
NOTE : ProfileCredentialsProvider internally uses ~/.aws/credentials file for the credentials to authenticate against AWS S3.

You can also find above code snippet at -
  • http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html

IF all goes correctly file should get uploaded to the S3 -







Related Links

Saturday, 17 September 2016

Spring configuration files XML schema versions

Background

If you have previously worked on a Spring web project then you must have come across Spring configuration files. They are xmls which will have references to to the namespace they are using and the version they are pointing to. Pick up any one of the Spring post we have seen before -
You would see spring configurations files something like -

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


Notice the 2.5 version. I mean it's pretty old now. You can find all the schema versions here -
 But as your project improves who cares about updating these files right? You update the spring version(jars) perhaps as you go forward but these configuration files remain as is. Lets see how we can resolve this problem.

Spring configuration XML schema: with or without version?

So how do you resolve this you ask? Do not specify the version. Yes I repeat do not specify the version. It should be something like -

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

When you do not specify any version it will take that latest schema that you have in your classpath jar and that will upgrade with the version of jar you upgrade. Spring schema versions are in a file called spring.schemas in spring-beans jar file -



 From this file it will take the latest schema version without you need to worrying about it.



NOTE : You should try to move completely move away from Spring xml configurations. You should start using Java class based configurations and annotations.


Related Links

t> UA-39527780-1 back to top