Thursday, 15 December 2016

Subclassing Custom Java Class in Clojure

Here are the ways to subclass a your custom Java class from Clojure. I have been fiddling to get this to work with lein for some time now and SO to the rescue of course. In your lein project say you have Java source at src/java. You want to extend a class from clojure, call super class method etc. There are two ways to extend, proxy and gen-class.
It's important to note that you need to have a package structure for the Java classes. Naked classes don't work because it's getting qualified to java.lang namespace when you try to run giving a ClassNotFoundException.
// src/java/com/example/
package com.example;

public class BaseClass {
    public String greet() {
        return "Hello from BaseClass";
You need to specify the Java source location using :java-source-paths in project.clj. If you are using :gen-class to extend, then you need to aot compile your clojure file. When running project with :java-source-paths added under Windows, I am getting the following error even if JAVA_HOME is set to JDK location and bin is in path.
Java compiler not found; Be sure to use java from a JDK
rather than a JRE by modifying PATH or setting JAVA_CMD.
Adding JAVA_CMD env variable to point to java_home-path\bin\java works.
; project.clj
(defproject subclass "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :java-source-paths ["src/java"]
  :main subclass.core
  :target-path "target/%s"
  :aot [subclass.core]
  :profiles {:uberjar {:aot :all}})
Extending using :gen-class use :extends keyword. If you want to call a super class method, use :expose-methods to specify an alias under which that method will be available locally. Here the greet from BaseClass is locally referred as pgreet
; src/subclass/core.clj
(ns subclass.core
   :extends com.example.BaseClass
   :exposes-methods {greet pgreet}))

;; greet method override. gen-class prefixes generated classes with '-' by default.
(defn -greet [this]
  ;this arg is the object of this class, i.e., subclass.core.
  (.pgreet this) ;calls super class' greet()
  (println "hi from clj"))

(defn -main [& args]
  (.greet (subclass.core.)))
Running the above gives
$ lein run
subclass.core=> (-main)
Hello from BaseClass
hi from clj
Extending using proxy.
; src/subclass/core.clj
(ns subclass.core
  (:import com.example.BaseClass))

(defn my-greet [] 
  (proxy [BaseClass] [] ;the class to extend
    ;override greet()
    (greet []
      (proxy-super greet) ;call super class method
      (println "hi from my-greet"))))

(defn -main [& args]
  (.greet (my-greet))) ;calling my-greet returns a proxy object. Invoke greet method on it.
Running the proxy version gives
$ lein repl
subclass.core=> (-main)
Hello from BaseClass
hi from my-greet
Using proxy or gen-class for extending depends on your use case.

Thursday, 8 December 2016

Slideshare Upload Failing With 307 Internal Redirect

If you try to upload files to slideshare from Chome and the upload fails with a status code of 307 Internal Redirect (in Network tab of dev console) with Non-Authoritative-Reason: Delegate as response header and you get XMLHttpRequest cannot load Response for preflight is invalid (redirect) error, means that your browser is redirecting an http request to https and it is failing. In my case it was because of the HTTPS Everywhere chrome extension. You want to uncheck Amazon Web Services from the extension for the upload to work. Also, if you have PrivacyBadger, then adjust it for the site if login fails.

Use .gitattributes to Override Language Detection in Github

Github uses linguist library to detect the languages used in a repository. It ignores library codes from counting towards the language percentage. So if you have files that are not part of the main code which are not in the vender specific path and you wanted to ignore them from the statistics, you can tell github to not count them by specifying it in .gitattributes file. Say I have a public folder which contains documentation or support files which I wanted to ignore, then the .gitattributes file will look like below.
public/js/libs/* linguist-vendored
public/* linguist-documentation
You can add linguist-vendored or linguist-documentation to ignore the files from counting. You can see the difference in the language statistics bar of your repository.

PrivacyBadger - Commenting on Blogger using Google Account

If you are using PrivacyBadger, your comments at blogger using Google account won't work. Also you cannot preview the comments as this extension blocks cookies. You need to enable cookie from in PrivacyBadger for this to work.

Wednesday, 7 December 2016

Configuring Leiningen and Git to Work Behind Proxy Under Windows

If you are behind a corporate proxy, you need to add the proxy details before lein or git can work.

Add the following environment variable name with value as given.
# env name
# env value
# env name
# env value
After setting the above, lein will be able to connect through the proxy.

Add the following in a file called .gitconfig under C:\Users\username.
    proxy = http://username:password@proxyurl:port
    proxy = http://username:password@proxyurl:port
If the ssh protocol and port is blocked, you can use the https version to clone and push changes to github. Give the github username and password when prompted. However if you have 2FA enabled, you need to first create an access token. Go to Personal access token under github settings and generate one. Under permissions, only repo needs to be enabled to push the changes to github. When you push, a github popup will prompt for authentication, where you enter your github credentials. Next the git bash will prompt for authentication where you should enter the generated access token for your password. It should get through the proxy.

Sunday, 4 December 2016

Download All Your Emails The Easy Way

You can have offline copy of all your emails with attachments using just Thunderbird.
1. Connect to your webmail account using IMAP (so that the server retains a copy of your mail).
2. Enable "Allow remote content in messages" under Privacy section of the Preference if you want images in the email to be downloaded as well. (See note below).
3. Make sure that the you have the following settings under "Synchronization & Storage" of your Account Settings.
    3.1 Enable "Keep messages for this account on this computer".
    3.2 Under "Advanced" enable all the folders that needs to be synchronized.
    3.3 Under "Disk Space" choose "Synchronize all messages locally regardless of age".
    3.4 Enable "Don't delete any messages".
4. Now click the computer icon on the lower bottom corner of Thunderbird or choose File->Offline->Work Offline. A popup will be displayed. Choose "Download Now".
Thunderbird will now download all your emails and attachments offline.

NB: The only caveat is that Thunderbird won't download remote contents like images from unknown senders even if you enable remote content (step 2). The sender has to be present in your address book or you need to whitelist the domains. See instructions here. This is for privacy reasons.

1. It's better to have full disk encryption (LUKS) and home directory encryption enabled or at least the later to keep your mails safer.
2. Enable master password in Thunderbird. Install StartupMaster extension. It fixes the multiple password prompt if you have multiple email accounts added.

Lisp for High Performance Transaction Processing

Lisp for High Performance Transaction Processing is a talk given by Daniel Weinreb on Aug 2009. The original video is not available now as it was later set as private and now taken down. You can watch or download the talk here or read the excerpt.
It's mainly about the use of Lisp at ITA which now is a part of Google. ITA's products are QPX which is an airline fare search program and res which is the airline reservation system. They use Common Lisp and Clozure CL for their business layer. The presentation is in Java and Oracle for their database. C++ is used for bit manipulation, cryptography, and other performance critical areas. Couple of Python, Perl, Shell etc. He just mentions that computational linguistics algorithm, rule based expert system and diversity algorithms are being used. He also talks about the future direction of Lisp especially with Clojure towards the end of the talk. In general, it's an interesting talk.
Watch or download.

Thursday, 1 December 2016

Chrome App Identity Authentication Not Working Issue

When you try to authenticate the user using OAuth 2.0, to access Google APIs from your chrome app and if it loads a URL like chrome://chrome-signin/?access_point=5&reason=0 means that there is issue with validating your client_id and your extension id. When you are developing chrome app, the extension id of the loaded unpacked extension might not be the same as the id generated by the chrome webstore. So when you create a chrome app oauth 2.0 client id from Google APIs console, instead of giving the chrome webstore URL id which it suggests, give your local extension id. Then use the obtained client_id in your manifest. Next time when you call the chrome identity API, it should show the OAuth permission popup. Basically, your chrome extension id should match the id used for generating the oauth credentials. Before publishing, use a different client id generated from the chrome webstore URL.

Tuesday, 22 November 2016

Sync Android Events, Tasks and Contacts with Yandex Account

Syncing Events
You can sync your Android calendar events with Yandex account. Follow the instructions at support page. You have to install a CalDAV sync app as Android doesn't come with CalDAV sync feature. I am using the CalDAV-Sync app and it works great. (It's a paid app but free version is also available). You have to install a patch app if the app notifies about it. The CalDAV configuration for default as well as domain accounts is as below:
Server name :
Use SSL: true
Username: or
Password: your password. For 2FA with authenticator app, generate app specific password for calendar from yandex passport.
Once you tap on next, it will connect and show you the default calendar to sync. In my case it is Мои события (My Events). You can enable two-way or one-way sync. Proceed next and complete the setup. Now when you add new event, choose the yandex calendar and it will sync to the account. The following fields from the S Planner app will sync with the account - title, location, start date, end date, recurring, participants, description, attending.

Syncing Tasks
Install the free OpenTasks app which is by the same developer of the above app. Yandex mail has tasks feature. OpenTasks will sync with the yandex task if you have the above app. You can add the task from the yandex web or from the app, it will sync both ways but tasks created via web is very limited in feature. The task account will get listed in the CalDAV-Sync account. You can change the name of the default tasks list from the web. Note that the yandex task is very minimalist in nature. It does not have a description field. You can create plain todos with reminders in the interval of 30 mins only and that's it. A description field and choosing minutes would have made it more useful. On the contrary the app has many fields, but those fields won't sync. Also the tasks added via web does not produce any reminder notification on the app. Not very useful at the moment.

Syncing Contacts
You need CardDAV sync app, as Android doesn't have this feature by default. You can use CardDAV-Syc app. (It's paid, but free version is also available). You need to install the patch app if you are on Android 4.1 or using a Samsung device. Choose CardDAV from the list and use the below configuration:
Server name or URL:
SSL: true
Username: or
Password: your password. For 2FA enabled accounts, create a new app password under the contacts section from yandex passport.
Choose next and you can choose the address book where your contacts will be synced to. In the next step, choose import contacts, and choose the ones to sync, like phone book, linkedin, google contacts, skype etc. The list depends upon the accounts added to the phone. Yandex contacts doesn't have lot of additional fields, so the details like organisation, title etc added in your phone's contact app will not sync. To add additional details like organisation, address etc., use the notes field. This will be synced as comments.

As a side note, I have been using Yandex domain account for a couple of years now and I find it to be an awesome service. I moved from Google Apps. I had a free Google Apps account. I also use Yandex to host the DNS for my domain. Happy camper!

Sunday, 20 November 2016

Enabling 2FA Using Yandex.Key for Yandex.Mail App for Android

Yandex.Mail supports two-factor authentication that uses HOTP or TOTP based authenticator app. First you have to setup 2FA from the yandex passport for the web mail. For that you need to have Yandex.Key app installed in your mobile. It's similar to Google's 2FA process except that you have to enter an additional PIN before you can get the auth code from the app. After setting up the 2FA, you can login to the web mail using the code generated by the Yandex.Key.
Now to access the mail using the Yandex.Mail for Android app, you give the code from the Yandex.Key and not using app specific password setup. This is a key difference when you take other apps like Gmail for Android. With Gmail, you have to generate app specific password since it doesn't support verification codes. If you want to access Yandex.Mail using Thunderbird on a desktop, you have to generate app specific password.
Yandex.Mail supports 2FA for the default address as well as custom mail domains hosted at yandex.

Saturday, 22 October 2016

Set Environment Variables When Wildfly Is Run As A Service

If you are running wildfly server as a service under GNU/Linux, setting environment variables under profile.d, bash_rc etc won't be read. In order for your war to get the environment variable follow the below steps. Say the variable is APP_PROPERTIES and the path is /opt/config/
# Add the following line in /etc/default/wildfly.conf
# export the variable in /etc/init.d/wildfly

Saturday, 15 October 2016

Run a Ring App With Compojure and Boot

Previously I have discussed on how to run a ring app using boot. Here we will see how to run a ring app that uses compojure for routes using boot. We will be using jetty as the server. The difference is that in a pure ring based app, you will pass the ring handler function to the run-jetty method, but here you will pass (compojure.handler/site routes-method) to run-jetty method.
|   build.boot
(ns qlambda.server.core
  (:use compojure.core)
  (:require [ring.adapter.jetty :as jetty]
            [compojure.handler :as handler]))

(defn greet []
  "Hello from compojure")

(defroutes api-routes
  (GET "/" [] (greet)))

(defn run-server []
  (jetty/run-jetty (handler/site api-routes) {:port 8080}))
  :source-paths #{"src"}
  :dependencies '[[org.clojure/clojure "1.8.0"]
                  [ring "1.5.0"]
                  [compojure "1.5.1"]])

(require '[qlambda.server.core :as server])

(deftask run []
  (with-pre-wrap fileset (server/run-server) fileset))
From the project root, run the server using boot as
boot run
Open http://localhost:8080 to see the response.

Hot Code Reloading Using http-kit and Lein

You can use the wrap-reload from ring.middleware.reload package. This reload the namespaces of modified files before each request.
(ns com.qlambda.server.core
  (:use [compojure.core :only [defroutes GET]]
        [compojure.handler :only [site]]
        [ring.middleware.reload :only [wrap-reload]]

(defroutes app-routes
  (GET "/" [] "hello world"))

(defn -main []
  (run-server (wrap-reload (site #'app-routes)) {:port 8080})
  (println "Server started."))
(defproject server "1.0.0"
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [ring "1.5.0"]
                 [compojure "1.5.1"]
                 [http-kit "2.2.0"]]
  :main com.qlambda.server.core)
Now start the server using
lein run
Go to http://localhost:8080 and you will see "hello world". Modify the message in the core.clj, reload the browser page and you will see the new message. Note that the you need to pass the route function as a var to wrap-reload using the #' reader macro for this to work.

Run Clojure Ring App Using Boot

Boot is a Clojure build framework. Here is an example of a basic ring application that is run using boot. The project folder is shown below.
    |   build.boot
(ns qlambda.server.core
  (:require [ring.adapter.jetty :as jetty]))

(defn handler [req]
  {:status 200
   :body "Hello World"})

(defn -main [& args]
  (jetty/run-jetty handler {:port 8080}))
  :source-paths #{"src"}
  :dependencies '[[org.clojure/clojure "1.8.0"]
                  [ring "1.5.0"]])

(require '[qlambda.server.core :as server])

(deftask run []
  (with-pre-wrap fileset (server/-main) fileset))
To run go to the project root directory and execute
boot run
Go to http://localhost:8080 to see the response.

Monday, 19 September 2016

Permutation of String in JavaScript

A simple algorithm to find the permutation of a string in JavaScript.
 * Find the permutations of the given string.
 * @param s The input string
 * Algorithm:
 * 1. If the length of the string is 1, return the string as permutation of 'a' is 'a'
 * 2. Else for the given string find the last character char and the substring by stripping the last char
 *    2.1 Recurse with the substring
 * 3. For each permutation from step 2 add the char to each position of the substring and return
function permute(s) {
    var len = s.length, char, i = 0, j = 0, p = [], elem, pArr = [], plen = 0;
    if (len == 1) return s;
    char = s.charAt(len-1);
    pArr = permute(s.substring(0, len-1));
    plen = pArr.length;

    for (i = 0; i < plen; i++) {
        for (j = 0; j < len; j++) {
            elem = pArr[i].split("");
            elem.splice(j, 0, char);
    return [ Set(p)];
permute("abc"); // [ 'cba', 'bca', 'bac', 'cab', 'acb', 'abc' ]
Download from github. Adapted from python version by GodComplex2.

Sunday, 18 September 2016

Make a Copy of an Array in JavaScript

If you want to make a copy of an array in JavaScript, you can use Array#slice method. This way we will get a copy of the array. If we are simply assigning the array to another variable, it is not making a copy. So performing splice() operation on the array will affect the value stored in both variables.
var a = [1,2,3],
    b = a;
a.splice(1,1); // => a is [1,3] and b is also [1,3]
var a = [1,2,3],
    b = a.slice(0);
a.splice(1,1); // => a is [1,3] and b is [1,2,3]

Saturday, 3 September 2016

Override Properties File Using Classpath

Say you have a file containing various configuration details. This file is placed under the resources folder and loaded using classpath. Then you can override the file during runtime by specifying a different file to the classpath command line argument. Let's say that the main class is inside com.example.test package and the test.jar is our program. Use the following command to override the file.
java -classpath .;test.jar;; com.example.test.Main