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.


Friday 12 September 2014

Eclipse forgets my proxy credentials

This usually happens when you are using a different eclipse installation to access your old eclipse workspace. Its something to do with the new installation not being able to access the secure storage created by your old installation. Here's what I did to solve it:

Close eclipse completely. Now fire up terminal or command prompt or whatever and navigate to your home directory. In my case it is '/Users/manthan/' directory.

Now cd into .eclipse/org.eclipse.equinox.security directory.
cd .eclipse/org.eclipse.equinox.security


If you list files, you should see a file called secure_storage in it. Rename that to secure_storage.old or something like that. You can delete it if you want but I prefer to rename things, make sure it works and then delete the renamed file.

mv secure_storage secure_storage.old

Now making sure that the secure_storage file has been renamed successfully, open eclipse and now try saving your credentials. It should ask for your key store master password and other stuff. 

That's it! You're done.

Saturday 6 September 2014

Moving steam games to another drive

So I came across this recently and thought to share it with you guys.

First of all, exit steam completely. If you've closed it, check if it's in the system tray and if it is, exit from there as well. Now, fire up Windows Explorer and go to where steam is installed on your computer. In my case, it was in the default installation directory (C:\Program Files (x86)\Steam).

Once in the folder, delete everything EXCEPT the SteamApps folder and Steam.exe. Now, go UP a directory level and move the whole Steam folder to wherever you want it to be. In my case, I moved it to E:\Games folder.

When the move is complete, double-click on the steam.exe. It'll re-download steam client for you and will require you to login to your steam account again.

So that's it. You should have all your games available to you in your new drive location!

Original source: https://support.steampowered.com/kb_article.php?ref=7418-YUBN-8129

Friday 5 September 2014

Rejecting a pairing request from a bluetooth device permanently on your Mac

So I had this problem the other day where I kept on getting pairing requests from my friend's bluetooth keyboard. This was rather annoying as the pairing dialog kept on popping up every few minutes. So I googled around a bit and found the following solution which worked for me.

Turn your bluetooth off. This will disable your bluetooth mouse and keyboard and hence you will have to use the built-in ones.

The problem is that at some point the keyboard that is nagging you to connect would have connected to your laptop. Your laptop remembers this and hence accepts incoming pairing request prompting you to verify it.

Our aim is to make the machine forget that it was ever connected to this device. In order to do this, you will need to edit a file called com.apple.Bluetooth.plist. This file is located in /Library/Preferences and ~/Library/Preferences folder. This file is in binary so in order to be able to edit it, you will have to convert it to xml first. So, open up terminal and type:
sudo plutil -convert xml1 /Library/Preferences/com.apple.Bluetooth.plist

Now you can go on to finder or whatever you have and edit this file. Remove ... followed by ... tags which relate to the keyboard/device that you are trying to ban. Simple way is to find the name of the device in the file. This name will be in ... tags. Once you find the name, remove the whole container ... and preceding ... tags. Once you do this, save the file and convert it back to binary using the following terminal command:
sudo plutil -convert binary1 /Library/Preferences/com.apple.Bluetooth.plist

Do the same for the com.apple.Bluetooth.plist file located in ~/Library/Preferences folder as well. If that folder does not have this file, copy it over from /Library/Preferences folder.

Once you are done, turn the bluetooth on and now it shouldn't prompt you for pairing requests.

Saturday 7 December 2013

An investigation regarding the effects of NoSQL on the four inherent problems of Software Engineering

So as you all know, I am currently in my final year of BEng (Hons) Software Engineering. In my final year, one of the courses that I am studying is Programming Frameworks. This course introduces to the academic side of Software Engineering and covers topics like quality plans, quality control, advanced methodologies, planning, frameworks etc.

My coursework for this course was to prepare a quality plan for writing an academic paper and then write the academic paper. The main topic of the academic paper was to compare and contrast one of the three concepts with an academic paper "No Silver Bullet - Essence and Accidents of Software Engineering" written by Fred Brooks. I prepared the quality plan, wrote the academic paper and submitted the coursework last month. I received a really good mark for it - 88% and thus I thought that I should share the academic paper that I wrote in LaTeX. You can download the academic paper from here.
Let me know what you think about the paper in the comments below.