Tuesday, October 22, 2013

Python

Python is an interpreted and Object oriented language.

Variable is simply defined as:
my_int=7
my_float=1.23
my_bool=True
my_string = "Always look on the bright side of life!"

Never use quotation marks (' or ") with booleans, and always capitalize the first letter! Python is case-sensitive (it cares about capitalization). 

In Python, statements are separated by whitespace (no ; at the end!).

Code needs to be indented by 4 spaces (or a tab).
Ex:
def spam():
    eggs = 12
    return eggs

Comments begin with # or use triple quotation marks for a multi-line comment.
Ex:
""" Hi there 
this is my comment

Long one indeed!"""

Arithmetic operators: +, -, *, /, % and ** (Exponentiation). The ** operator raises the first number, the base, to the power of the second number, theexponent.
Ex:
my_var = 10**2
Ans: 100

Backslash (\) escapes a string with quotes.
Ex:
'Help! Help! I\'m being repressed!'

So if you wanted "Y", you could just type "PYTHON"[1].

String methods: string methods are pre-built pieces of code that perform specific tasks for strings. They are: len(), lower(), upper() and str()
Ex:
print "The value of pi is around " + str(3.14)
+ works as string concat operator but first we need to convert 3.14 (a float) to string using str()

print "The %s who %s %s!" % ("Knights", "say", "Ni")

Getting user input on Console:
name = raw_input("What is your name?")


Using datetime:
from datetime import datetime

now = datetime.now()
print str(now.month) + "/" + str(now.day) + "/" + str(now.year)



Tuesday, October 15, 2013

implementing-rest - Exploring the implementation aspects of the REST architectural style. - Google Project Hosting

implementing-rest - Exploring the implementation aspects of the REST architectural style. - Google Project Hosting:

This is a place for exploring aspects of implementing applications using the REST architectural style. This may include statements about existing frameworks and libraries, general discussions about the nature of the style and how it may be expressed and/or encouraged via a programming framework, etc.

Featured Content

We've started a list of available REST Frameworks 

'via Blog this'

Code::Blocks IDE

Code::Blocks:


An open source IDE i started playing with today. Its fast, beautiful (neat syntax highlighting) and the best thing i liked so far is the code completion unlike i had ever seen before for C/C++ IDE. It also claims to be pretty fast in doing builds and debugging.

'via Blog this'

Sunday, October 13, 2013

Best Practices for Speeding Up Your Web Site

Best Practices for Speeding Up Your Web Site

Make Yahoo! Web Service REST Calls - YDN

Make Yahoo! Web Service REST Calls - YDN

RESTful API Design

Excerpted from: https://blog.apigee.com/detail/api_design_third_edition_video_slides


Source: http://www.blinksale.com/api/All response codes are included in the HTTP Status response header. Possible status codes include:
·         200: Success (upon a successful GET, PUT, or DELETE request)
·         201: Created (upon a successful POST request)
·         400: Resource Invalid (improperly formatted request)
·         401: Unauthorized (incorrect or missing authentication credentials)
·         404: Resource Not Found
·         405: Method Not Allowed (in case some HTTP method of GET/POST/PUT/DELETE is not supported for a given URI).
·         406: Not Acceptable

·         500: Application Error


Friday, October 11, 2013

SPDY Protocol - Road to HTTP/2.0

Its been 13 years since HTTP/1.1 version came into being (in 1999) and has wonderfully managed to keep up with the pace of development and growth in web usage and technologies.

SPDY Protocol (by Google) proposes to enhance HTTP 1.1 by bringing to the table these new core concepts to web delivery:

  • Multiplexing (allowing multiple requests to flow over a single connection)
  • Prioritization (providing the ability to indicate that one resource is more important than another and should hence jump to the head of the line)
  • Compression (making compression universal and extending it to headers)
  • Server Push (allowing the server to give content to a user-agent before it is asked for)
  • A strong recommendation for encryption (current implementations require it) - dropped from HTTP/2.0

IETF started HTTP/2.0 standards effort based on the proposals from SPDY protocol (as starting point). If everything goes well and according to the working group's stated goals, HTTP/2.0 will be faster, safer, and use fewer resources than HTTP/1.1.

HTTP/2.0 is still a ways away. The httpbis working group is targeting Fall of 2014 for a final draft of the protocol.

Understanding REST

The following is excerpted from: http://rest.elkstein.org
What is REST?
REST stands for Representational State Transfer. It relies on a stateless, client-server, cacheable communications protocol -- mostly, the HTTP protocol is used. REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines. REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations. Thus, REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.).

The notion of REST was created in the PhD dissertation of Roy T. Fielding.

Benefits:
REST architecture has all the benefits of Web Service:
  • Platform-independent (you don't care if the server is Unix, the client is a Mac, or anything else),
  • Language-independent (C# can talk to Java, etc.),
  • Standards-based (runs on top of HTTP), and
  • Can easily be used in the presence of firewalls.

Like Web Services, REST offers no built-in security features, encryption, session management, QoS guarantees, etc. But also as with Web Services, these can be added by building on top of HTTP (like use https and use of basic auth etc).

One thing that is not part of a good REST design is cookies: The "ST" in "REST" stands for "State Transfer", and indeed, in a good REST design operations are self-contained, and each request carries with it (transfers) all the information (state) that the server needs in order to complete it.

Example: 

To lookup a user by ID, REST URI is: 

http://www.acme.com/phonebook/UserDetails/12345?firstName=John&lastName=Doe

This URL is sent to the server using a simpler GET request, and the HTTP reply is the raw result data. If you need to pass long parameters, or binary ones, you'd normally use HTTP POST requests, and include the parameters in the POST body.

It is a common convention in REST design to use nouns rather than verbs to denote simple resources.

Real- world REST API docs:
1. Twitter - https://dev.twitter.com/docs/api - They use only GET and POST (used for create/update/delete).

  • For create - https://api.twitter.com/1.1/direct_messages/new.json
  • For Delete - https://api.twitter.com/1.1/direct_messages/destroy.json

2. Amazon S3 - http://docs.aws.amazon.com/AmazonS3/2006-03-01/API/APIRest.html
3. Google Glass' Mirror API - http://www.youtube.com/watch?feature=player_embedded&v=JpWmGX55a40
4. Facebook - https://developers.facebook.com/docs/reference/apis/
5. LinkedIn - http://developer.linkedin.com/apis



Components of REST Architecture:

  • Resources - identified by logical URLs. Resources are the key element of a true RESTful design, as opposed to "methods" or "services" used in RPC and SOAP Web Services, respectively. You do not issue a "getProductName" and then a "getProductPrice" RPC calls in REST; rather, you view the product data as a resource -- and this resource should contain all the required information (or links to it). Whenever relevant, a resource should contain links to additional information -- just as in web pages.
  • There is no connection state; interaction is stateless (although the servers and resources can of course be stateful). Each new request should carry all the information required to complete it, and must not rely on previous interactions with the same client.
  • Resources should be cachable whenever possible (with an expiration date/time). The protocol must allow the server to explicitly specify which resources may be cached, and for how long. The HTTP cache-control headers are used for this purpose.

Design Guidelines:

  1. Queries should not return an overload of data. If needed, provide a paging mechanism. For example, a "product list" GET request should return the first n products (e.g., the first 10), with next/prev links.
  2. GET access requests should never cause a state change. Anything that changes the server state should be a POST request (or other HTTP verbs, such as DELETE).
  3. Rather than letting clients construct URLs for additional actions, include the actual URLs with REST responses.
Another advantage of REST lies with performance: with better cache support, lightweight requests and responses, and easier response parsing, REST allows for nimbler clients and servers, and reduces network traffic, too.

Documenting REST Services:
With version 2.0, WSDL supports all HTTP verbs and it is now considered to be an acceptable method of documenting REST services (WSDL 1.0 only supported HTTP GET and POST verbs).
The second alternative is WADL, the Web Application Description Language which is light weight compared to WSDL and less verbose.


Here is a WADL specification, describing Yahoo "News Search" service:

 1 <?xml version="1.0"?> 
 2 <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3  xsi:schemaLocation="http://wadl.dev.java.net/2009/02 wadl.xsd" 
 4  xmlns:tns="urn:yahoo:yn" 
 5  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 6  xmlns:yn="urn:yahoo:yn" 
 7  xmlns:ya="urn:yahoo:api" 
 8  xmlns="http://wadl.dev.java.net/2009/02"> 
 9   <grammars> 
10     <include 
11       href="NewsSearchResponse.xsd"/> 
12     <include 
13       href="Error.xsd"/> 
14   </grammars> 
15 
16   <resources base="http://api.search.yahoo.com/NewsSearchService/V1/"> 
17     <resource path="newsSearch"> 
18       <method name="GET" id="search"> 
19         <request> 
20           <param name="appid" type="xsd:string" 
21             style="query" required="true"/> 
22           <param name="query" type="xsd:string" 
23             style="query" required="true"/> 
24           <param name="type" style="query" default="all"> 
25             <option value="all"/> 
26             <option value="any"/> 
27             <option value="phrase"/> 
28           </param> 
29           <param name="results" style="query" type="xsd:int" default="10"/> 
30           <param name="start" style="query" type="xsd:int" default="1"/> 
31           <param name="sort" style="query" default="rank"> 
32             <option value="rank"/> 
33             <option value="date"/> 
34           </param> 
35           <param name="language" style="query" type="xsd:string"/> 
36         </request> 
37         <response status="200"> 
38           <representation mediaType="application/xml" 
39             element="yn:ResultSet"/> 
40         </response> 
41         <response status="400"> 
42           <representation mediaType="application/xml" 
43             element="ya:Error"/>
44         </response> 
45       </method> 
46     </resource> 
47   </resources> 
48 
49 </application>
WADL uses type safety using XML schema types.
To author WADL, get the  http://www.w3.org/Submission/wadl/wadl.xsd and use a tool that can validate your WADL.xml against the WADL.xsd (one such tool is - XRay XML Editor or you can use http://www.utilities-online.info/xsdvalidation/). Apigee has created a WADL Builder - https://github.com/apigee/WADL-builder

Some REST advocates, however, find even the lightweight WADL to be an overkill. And indeed, most REST services are documented by no more than a textual description (a human-readable HTML file).

Using REST in Java


package rest.client;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * A Simple REST client using Java HttpURLConnection class.
 *
 * @author rwatsh
 */
public class RestClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        System.out.println(httpGet("http://localhost:8080/CustomerDB/webresources/entities.customer"));
    }

    public static String httpGet(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn =
                (HttpURLConnection) url.openConnection();

        if (conn.getResponseCode() != 200) {
            throw new IOException(conn.getResponseMessage());
        }

        // Buffer the result into a string
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();

        conn.disconnect();
        return sb.toString();
    }

    public static String httpPost(String urlStr, String[] paramName,
            String[] paramVal) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection conn =
                (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        // Create the form content
        OutputStream out = conn.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        for (int i = 0; i < paramName.length; i++) {
            writer.write(paramName[i]);
            writer.write("=");
            writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
            writer.write("&");
        }
        writer.close();
        out.close();

        if (conn.getResponseCode() != 200) {
            throw new IOException(conn.getResponseMessage());
        }

        // Buffer the result into a string
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();

        conn.disconnect();
        return sb.toString();
    }
}

Yahoo has a nice documentation of how to use Java for REST - http://developer.yahoo.com/java/howto-reqRestJava.html.

References:

XMPP

What is XMPP?
- XMPP is communication protocol for message-oriented middleware based on XML.
Originally called Jabber, developed for instant messaging, presence info and contact list maintenance.
XMPP specs published in RFC 6120/21/22.

  • RFC 6120: XMPP Core
  • RFC 6121: XMPP IM
  • RFC 6122: XMPP Address Format

Strengths:

  • Mature, Open Standards based technology
  • Secure (SASL and TLS)
  • Extensible – Several extensions exist today (http://xmpp.org/xmpp-protocols/xmpp-extensions/) 

Weaknesses:

  • Binary data transfer (e.g. file transfer) is inefficient (as it requires converting to Base64 encoded form).

Core includes:
  • XML streaming layer
  • Security: SASL and TLS
  • Presence: Built-in information about network availability
  • Rosters: Presence-enabled contact lists
XMPP defines 2 fundamental terms with respect to messaging:
  • Stream – Open XML envelope sent before more XML elements are exchanged between two entities.
  • Stanza – XML elements exchanged between 2 entities. 3 types:
    • Presence
    • Message
    • IQ – information or query
Client sends:
xml version="1.0"?>
<stream:stream to="example.com" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
Server acknowledges by sending:
"?>
 <stream:stream from="example.com" id="someid" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
... encryption, authentication, and resource binding ...
Client sends message:
xml:lang="en"> Art thou not Romeo, and a Montague?
Server forwards message to client:
xml:lang="en"> Neither, fair saint, if either thee dislike.
Client closes its stream/session:
</stream:stream>
Server closes its session:
</stream:stream>

Message – to/from = Jabber IDs. 
Wherefore art thou, Romeo?
IQ – Info/Query. 

  • Get/Set
  • Result
  • Error
  • Roster - Rosters are stored on the server so that the XMPP client may retrieve the same each time the user logs in. Rosters are a type of IQ Stanza. Example showing IQ message to request for roster.



Presence – the presence info of a user is broadcasted from server to all users in roster list of that user. This works using the PubSub messaging.

Bi-directional streams Over Synchronous HTTP (BOSH)


  • It is a long-lived 2 way communication over HTTP.
    • Significantly more bandwidth efficient over http (compared to other technologies – such as AJAX)
    • Applications requiring both “push” and “pull” communications
    • http://xmpp.org/extensions/xep-0124.html
  • Avoids http polling for more efficiency and low latency


PubSub

  • Each user is assigned a unique Jabber ID. 
  • XMPP server manages the messages published to a certain node (topic) and delivers the messages to all subscribers for that node.
  • Messages may contain payload.
  • Messages may be persistent.

XMPP Libraries for Application Programming

  • Smack API – Java API for XMPP clients.
  • Can be used to:
    • Connect to the XMPP server with user’s JID
    • Get the user’s roster from XMPP server
    • Sending and receiving different type of Stanzas (IQ, Message and Presence).
    • Publish messages to nodes 
    • Subscribe to nodes of interest.
    • File transfer/Group chat support also available.
  • http://www.igniterealtime.org/projects/smack/index.jsp 

Network Management over XMPP
  • Arista’s CloudVision framework Multi-Switch CLI extension uses XMPP to provide a shared message bus for managing and configuring switches. This is based on the idea from Internet of Things extension for XMPP (http://xmpp.org/extensions/xep-0324.html). 
  • Arista Switches run an XMPP client and respond to CLI commands. 
  • User can use any XMPP client (like iChat) on phone or PC to execute CLI on switches.
  • A single login to XMPP server gives secure access to all switches.
  • Switches can participate in group chats – meaning CLI can be executed on all participating switches in batch.
  • A Switch can be in more than one chat room at the same time. Command executed in a given chat room, is executed on all participating switches in that chat room.
  • Arista uses ejabberd XMPP server (included with fedora distro).
  • Arista uses SleekXMPP client.
  • If 2 switches are connected via XMPP, you can run CLI commands on switch A directly from switch B using xmpp send CLI command.
XMPP with Primefaces

  • Primefaces uses Atmosphere framework for implementing server push (PrimePush) - http://blog.primefaces.org/?p=2053.
  • Atmosphere comes with a plug-in for BOSH/XMPP (https://github.com/Atmosphere/atmosphere/wiki/Atmosphere-PlugIns-and-Extensions#xmpp)
References

  • Fun with XMPP and Google Talk - http://www.adarshr.com/papers/xmpp
  • Smack API - http://www.igniterealtime.org/projects/smack/index.jsp
  • XMPP Technology overview - http://xmpp.org/about-xmpp/technology-overview/
  • Arista’s Network Management over XMPP - https://eos.aristanetworks.com/2011/08/management-over-xmpp/

Friday, October 04, 2013

Ant build script for a simple Java Web App

<?xml version="1.0"?> 
<project name="MyWebApp" default="dist" basedir=".">
 <property name="build" location="bin" />
 <property name="src" location="src" />
 <property name="dist" location="dist" />
 <property name="web" location="web" />
 <!-- Set up the 'env' prefix for environment variables -->
 <property environment="env" />
 <property name="lib" location="lib" />
 <property name="version.num" value="1.0" />
 <path id="classpath">
  <fileset dir="${lib}">
   <include name="**/*.jar" />
  </fileset>
  <fileset dir="${env.TOMCAT_HOME}/lib">
   <include name="**/*.jar" />
  </fileset>
 </path>
 
 <!-- get the source compile classpath in a printable forms -->
 <pathconvert pathsep="${line.separator}|   |-- " property="echo.path.compile" refid="classpath">
 </pathconvert>
 
 <!-- 0. Abort the build if TOMCAT_HOME is not set -->
 <target name="checkTomcatHome" unless="env.TOMCAT_HOME">
  <fail message="TOMCAT_HOME must be set!" />
 </target>
 
 <!-- 0. PRINT DEFAULTS. -->
 <target name="print_default_properties">
  <echo message="os.name:          ${os.name}" />
  <echo message="basdir:           ${basedir}" />
  <echo message="ant.file:         ${ant.file}" />
  <echo message="ant.project.name: ${ant.project.name}" />
  <echo message="ant.java.version: ${ant.java.version}" />
  <echo message="tomcat_base:      ${env.TOMCAT_HOME}" />
  <echo message="|-- compile classpath" />
  <echo message="|   |" />
  <echo message="|   |-- ${echo.path.compile}" />
 </target>
 
 <!-- 1. CLEAN the build generated artifacts. -->
 <target name="clean">
  <delete dir="${build}" />
  <delete dir="${dist}" />
 </target>
 <!-- 2. INITIALIZE the build. -->
 <target name="init" depends="print_default_properties, checkTomcatHome">
  <mkdir dir="${build}" />
 </target>
 <!-- 3. COMPILE the source files. -->
 <target name="compile" depends="init">
  <!-- Compile the java code -->
  <javac srcdir="${src}" destdir="${build}" classpathref="classpath" debug="on" />
 </target>
 <!-- 4. Create the DISTRIBUTION. -->
 <target name="dist" depends="compile">
  <mkdir dir="${dist}" />
 
  <copy todir="${build}">
   <fileset dir="${basedir}">
    <include name="*.properties" />
    <include name="*.xml" />
    <include name="*.xsd" />
   </fileset>
  </copy>
 
  <war destfile="${dist}/${ant.project.name}.war" webxml="${web}/WEB-INF/web.xml">
   <manifest>
    <attribute name="Built-By" value="${user.name}" />
    <attribute name="Implementation-Version" value="${version.num}" />
    <attribute name="Built-Date" value="${TODAY}" />
   </manifest>
   <fileset dir="${web}" />
   <lib dir="${lib}">
    <include name="**/*.jar"/>
   </lib>
   <classes dir="${build}" />
  </war>
 </target>
 
 <!-- 5. DEPLOY the generated WAR to tomcat. -->
 <target name="deploy" depends="dist">
  <echo>Deploying to ${env.TOMCAT_HOME}</echo>
  <copy todir="${env.TOMCAT_HOME}/webapps">
   <fileset dir="${dist}">
    <include name="${ant.project.name}.war" />
   </fileset>
  </copy>
 </target>
 
 <!-- HELP -->
 <target name="help">
  <echo message="help         - Displays help menu">
  </echo>
  
  <echo message="compile      - (default) Builds Java classes">
  </echo>
  <echo message="dist         - Creates web applicaiton .war ">
  </echo>
  <echo message="clean        - Delete built .class and .war files, leave directories">
  </echo>
  <echo message="deploy       - Deploy to local tomcat installation">
  </echo>
 </target>
</project>
</pre>

Wednesday, October 02, 2013

jQuery treetable

jQuery treetable: A nice tree table component that can be customized to support line highlighting and drag and drop. Unfortunately it does not support column sorting.

'via Blog this'

Visualization: Bar Chart - Google Charts — Google Developers

Visualization: Pie Chart - Google Charts — Google Developers:

Google Charts are very easy to use charting components for your web application.

'via Blog this'

Clearcase error when trying to add jquery-x.x.x.min.js to source control

When you add the jquery-x.x.x.min.js to clearcase you have to specify 
the file type like this: 

cleartool mkelem -eltype compressed_file jquery-x.x.x.min.js 
cleartool ci -nc jquery-x.x.x.min.js 

A minified javascript file has all the whitespace characters removed to make it more efficient to load the javascript on the client browser. So the entire contents of the javascript source file is compressed into a single line (removing all whitespace). Clearcase still treats it as text file but it needs to be told explicitly if its a compressed text file. I figured there is no way on the UI (clearcase explorer) that lets us do so. So we need to go to command line cleartool and run the above command to make it work.

Popular micro services patterns

Here are some popular Microservice design patterns that a programmer should know: Service Registry  pattern provides a  central location  fo...