Monday 24 August 2015

Configuring Java HttpClient to use proxy

So, for various security reasons, at work, I have to go through proxy in order to access anything. I was doing some prototyping with sparkpost and none of my code worked as by default the code wasn't going through the business proxy.

Naturally, I thought that I'd set HTTP_PROXY and HTTPS_PROXY environment variables and then run my java code. I did and as it is with all things in Software Engineering industry, it didn't work.

After several hours of googling (or what felt like several hours) I finally found what was wrong with it. In Java, due to security reasons, all proxy variables are ignored unless they have been explicitly set in code. Here's how you set them:

        String proxyHost = System.getenv("HTTP_PROXY_HOST");
        String proxyPort = System.getenv("HTTP_PROXY_PORT");
        String proxyUser = System.getenv("HTTP_PROXY_USER");
        String proxyPassword = System.getenv("HTTP_PROXY_PWRD");

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)),
                new UsernamePasswordCredentials(proxyUser, proxyPassword));

        HttpHost proxyHostObject = new HttpHost(proxyHost, Integer.parseInt(proxyPort));

        HttpClient client = HttpClientBuilder.create().setProxy(proxyHostObject).setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()).setDefaultCredentialsProvider(credsProvider).build();

        HttpGet getRequest = new HttpGet(URL);
        getRequest.addHeader("Authorization", API_KEY);
        getRequest.addHeader("Content-Type", "application/json");
        try {
            System.out.println("Executing request...");
            HttpResponse response = client.execute(getRequest);
            System.out.println("Request successfully executed.");

            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            System.out.println(responseString);
        } catch (IOException e) {
            e.printStackTrace();
        }

The CredentialsProvider class allows one to save the proxy credentials while the HttpHost class allows storing the proxy host.

I personally am not a fan of this arrangement as the proxyHost and proxyPort is being duplicated twice in both classes. If you find a better arrangement, feel free to drop me a line.

Thanks!

Thursday 6 August 2015

How to get file sizes recursively in a directory and list them in ascending order

When cleaning up unused files from your linux computer, I find it useful to know which of my files use most of the space so that I can easily determine which ones to remove.

The most basic command to find file size of a given file is:
du -sm myfile.txt

If you are in a folder with multiple files and want to get file sizes of all the files, the command is:
du -sm *

The above command does not get file sizes recursively. However, if you are happy with it and want to sort the file sizes in ascending order:
du -sm * | sort -n -k 1

Now, this is little useful as it does not do recursive file size listing. To do that:
find . -type f -exec du -sm {} \; -print | sort -n -k 1

If you are looking for a particular type of file (eg. jar files):
find . -name "*.jar" -type f -exec du -sm {} \; -print | sort -n -k 1

Enjoy!

Monday 20 July 2015

Unix/Linux command to check if Apache Tomcat is running

So I've come across this problem quite a few times. Normal way to do this is:

ps -ef | grep tomcat

This works most of the times. If tomcat is running, it gives between 1 and 2 lines back but if not, it gives anywhere between 0 and 1 lines back. A much cleaner use of the above command would be with wc -l:

ps -ef | grep tomcat | wc -l

However, this doesn't solve the actual problem as along with the tomcat process, it also gives you the process of command "grep tomcat".

Here's the command to solve this problem. You can use either of the two below commands:

ps -ef | grep tomcat | grep -v "grep tomcat" | wc -l

ps -ef | grep tomca[t] | wc -l

The first command explicly says that once you get a list of all processes containing the word tomcat, ignore lines containing words "grep tomcat". And then the usual, pipe it to word count and output the number of lines.

The second one, however, tricks the grep into using a regular expression and ignoring itself. This is because the actual output containing "grep tomca[t]" will have the square brackets which obviously won't match the actual regular expression.