Monday, August 10, 2015

Amazon Web Services - Products overview

Following are the list of products Amazon web services offers. 


Compute
  • EC2: A service which allows user to rent servers with different configurations. You can choose the instance based on memory, diskspace, speed and other attributes and you pay on a per hour basis. You can shut down the instance at any time, and you pay for what you use. There are preconfigured machine images available that you can choose from different OS available ranging from Ubuntu, Windows and other OSs. 
  • ECS: EC2 container services make developer/network person life easy by making it easy to deploy, manage and scale Docker containers. 
  • Lamda: A compute service that executes developer’s code in response to events. The developer don’t need to have a developer environment, he can directly code in the AWS browser console or upload code. The events to respond can be “whenever an artifact has been uploaded into S3”. The main advantage is you donate have to worry about the infrastructure at all. You pay pay for the time taken for the code to get executed. Its a new approach to event driven computing. 
Storage
  • S3: S3 is storage solution, you pay for the storage and the bandwidth. S3 is very reliable and 99.99% available. With S3, you might want to store static content of a web site or data generated out of a project in a storage service so that it is available globally. S3 is often used with other AWS products such as EC2. Its very easy to use. Following are different ways by which you can store and retrieve data. 
      • Using Command line interface. 
      • using AWS console. 
      • Using AWS SDK. 
  • Elastic File system: is a new sharable file storage service that provides multiple EC2 instances to access a fully managed file system using NFSv4 protocol. You will be able to access using the CLI, console and AWS SDK. 
  • Glacier:  a low cost data archival storage for storing less frequently used data but for a much lower cost than S3. This service is effective for archiving. You have to use if you don’t want to access daily. The retrieval time is slow but cheaper. 
  • Cloudfront: is a content delivery service. You can use Cloudfront console / API to easily distribute content to end users with low latency, high data transfer speeds. You can use HTTP/HTTPS or RTMP to stream the content stored in cloud front. 

Network: 
  • VPC: This service lets you configure your instance in a virtual network that you define. You have complete control over your networking environment. You can control which port to open to which group and can leverage multiple layers of security. 
  • Direct Connect: This service helps you in establishing a dedicated connection from your office infrastructure to AWS environment. This can reduce the network cost and increase the bandwidth throughput. 
  • Route 54: This is a domain naming service from Amazon which helps you in mapping your domain name to the instance you have. 

Database
  • RDS: A service which helps in setting up and managing an RDBMS, scaling them in a easy and cost efficient way. 
  • DynamoDB: A no-sql database solution from Amazon which developers can use to store the content in a faster manner. Its a fully managed service which stores in document and key-value pair format. 
  • ElasticCache: An in memory caching engine that makes it easy to deploy, operate and scale an in memory cache in the cloud. It supports memcached and redis. 

Analytics
  • Redshift: a data warehouse solution from AWS to analyse all your data using existing BI tools. 
  • EMR: This is a managed hadoop environment which can be used out of the box to run hadoop jobs. With this service you don’t have to worry about the environment. You just have to upload the job file, input and output folders in S3. You can define the no. of instances you need - EMR will run the job - output the job content in the S3 file you define. EMR can also terminate the instances once the job is complete. 
  • Kinesis: Kinesis is a managed cloud service from Amazon to process real time data over large distributed data streams. The data can be captured from variety of stores such as web site click streams, social feeds, financial data transactions, sensor data, IT logs and location tracking events. 
  • Data pipeline: AWS Data Pipeline is a cloud-based data workflow service that helps you process and move data between different AWS services and on-premise data sources.
  • Amazon Machine learning: using this service, developers of any skill level can use the ML apis. It provides visualization tools and wizards that guides you through the process of creating ML models. This is highly scalable and is capable of predicting billions of predictions daily. 

Enterprise applications
  • Amazon Workspaces: a managed desktop computing service in the cloud. Using this the network engineers can easily provision cloud based desktops that the users can use their iPads, desktops, laptops to access the content. 
  • WorkMail: an email and calendering service from AWS for existing desktop and mobile email clients. 
  • Workdocs: A fully managed solution to store electronic documents with more features such as security, access provision, share and edit features. 

Application
  • SQS: A simple managed message queuing service that is fast reliable and scalable. It makes it simple and cost effective solution to send and receive messages to SQS. 
  • SWF: a workflow services which can be used to co-ordinate between application components. 
  • SES: a cost effective email sending service. The service can be used via AWS SDK that comes in different languages. 
  • SNS: with SNS you can send notifications to mobile phones - Apple APNS, GCIM for google, windows and other major mobile platforms. 
  • CloudSearch: With this service, you can quickly build a cost-effective solution to setup, manage and scale a search solution for your web site. 
  • AppStream: With this service, you can stream your existing application from the cloud, reaching more users without code modifications. 
Deployment & management
  • Elastic Beanstalk: A PAAS solution from AWS to help users deploy their application from their desktop in a easier way. There are Eclipse plugins available which you can use to move the artifact/scale up in AWS with a click of a button in the eclipse. 
  • OpsWorks: An application management service that helps you in deploy and operate applications of all shapes and sizes. With this you can automate server scaling, software configuration, database setups. This also provides operation to automatic instance scaling, instance monitoring etc., With this you create your entire application as stacks on top of layers. These layers are blueprints that helps you in deploying in a easy way. 
  • Cloud Formation: provides network engineers templates so that they can easily create and manage a collection of related AWS resources. 
  • Code Deploy: a service that automates code deploy to any instance. helps you rapidly release new resources. 
  • Code Pipeline:  a continuous delivery service for faster and reliable application updates. 
  • Code commit: a service that makes it easy for companies to host private GIT repositories. It eliminates the need to manage your own code versioning system. 

Security & Administration
  • CloudWatch: A monitoring tool for AWS products. 
  • Config: lets you have the full visibility of all AWS resources. 
  • CloudTrail: service that records AWS API calls you have made and provides the log files for analysis. It stores information such as IP address of the caller, request parameters etc., 
  • IAM: fine grained granular control of AWS services
  • Directory: A managed directory service. 
  • KMS: encryiption and key management service for the cloud. 


Sunday, August 9, 2015

AngularJS Basics

AngularJS is a Javascript framework which makes it incredibly easy to build web applications

Features of AngularJS: There are many features in AngularJS, primarily 2-way data binding, OO way of JS development, easy mapping between dom and JS model, Create custom tags, Dirty check on objects, implementing Routers.

Components:
  • RouteProvider
  • AngularJS templates (or) partials. 
  • Module
  • Controller

MVC: 
  • View holds the html fragments
  • Controller - holds the action to be performed
  • Model: holds data by getting from the REST services. 

Some attributes: 
  • ng-app: tell that this html page contains angularJS app
  • ng-model: bind the model to the div
  • ng-bind: used to bind the input tag to the model
  • ng-repeat: used to iterate through a list of model and display content. 
  • ng-controller: defines the controller that will be used for this div or section. 
  • ng-show | ng-hide: hide or show a div or section
  • ng-include: include another file in the html


AngularJS Directives: 
You can use directives in different ways:
  • As an attribute to a tag. 
  • As an element
  • As a class
  • As a comment
ng-init: used to initialise angularJS variables

You can also create custom directives: 

You have to use .directive method in the module to define a directive
myAppModule.directive('colorList', function () {
    return {
        restrict: 'AE',
        template:
              "<button ng-click='showHideColors()' type='button'>"
            + "{{isHidden ? 'Show Available Colors' : 'Hide Available Colors'}}"
            + "</button><div ng-hide='isHidden' id='colorContainer'>"
            + "</div>"

        }
});

AngularJS validation: 
  • $dirty
  • $valid
  • $invalid
  • $pristine

AngularJS Expressions: 
<p>My first expression: {{ 5 + 5 }}</p>
(or) 
<p>My first expression: {{ emp.getValue }}</p>
where emp is a angularJS model. 

AngularJS Filters: used to filter a list of values and display 
<div ng-app="myApp" ng-controller="personCtrl">
<p>The name is {{ lastName | uppercase }}</p>
</div>

You can also create custom filters in AngularJS:


Events:
  • ng-click
  • ng-double-click

Angular Services:
$http: 
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function(response) {$scope.names = response.records;});
});

var promise = $http({method: 'POST', url: 'memberservices/register', data: theData});

$window: represents browser window object. 
$location: gets information about the url and gives it to your application
$document: jqlite reference

You can also create custom services: 



—————————— Code Snippets ————————
Simplest AngularJS program
Enter your name : <input type="text" ng-model="ename">
Hello {{ ename }}
<script type="text/javascript" src="../js/lib/angular.min.js"></script>

Sample app code: 
<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
});
</script>

RouteProvider: used to define which AngularJS controller to call when you hit a URL
helloWorldApp.config(['$routeProvider', '$locationProvider', 
function($routeProvider, $locationProvider){ 
 $routeProvider
   when('/', { 
     templateUrl: 'partials/main.html', 
     controller: 'MainCtrl' }).
   when('/show', { 
     templateUrl: 'partials/show.html', 
     controller: 'ShowCtrl' 
}); 

AngularJS Templates: 
  • ng-view

AngularJS Controllers: 
  • Define methods which would inturn get the data from the server
      • use $http
var addonsControllers = 
  angular.module('addonsControllers', []);

addonsControllers.controller('AddonsCtrl', 
  ['$scope', 'checkCreds', '$location', 'AddonsList', '$http', 'getToken',
    function AddonsCtrl($scope, checkCreds, $location, AddonsList, 
      $http, getToken) {
        if (checkCreds() !== true) {
            $location.path('/loginForm');
        }

        $http.defaults.headers.common['Authorization'] = 
          'Basic ' + getToken();
        AddonsList.getList({},
           function success(response) { 
              console.log("Success:" + 
                     JSON.stringify(response));
                    $scope.addonsList = response;
           },
           function error(errorResponse) {
              console.log("Error:" + 
                     JSON.stringify(errorResponse));                   
           }
        );
        $scope.addonsActiveClass = "active";
}]);

ng-repeat
<li ng-repeat="phone in phones | filter:query">
          {{phone.name}}
          <p>{{phone.snippet}}</p>
        </li>


External controllers: 
<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>


<script src="personController.js"></script>

Spring security - different options

Spring provides options to perform authentication and authorisation. 

Authenticating using Spring: 
Spring provides the following features as part of authentication: 
  • Basic Authentication: process authentication based on user credentials present int he urls. 
  • Form based login: This is a default form which is provided as popup box to enter user credentials. 
  • Custom form: You can also create your own forms, but need to follow some rules so that Spring knows which is the form, its userid and password fields. 
  • Logout service: The user can logout using the logout url, the user object maintained in the session is removed. 
  • Remember me support. 

The data for authentication can reside in different places. Spring authentication provider provides support for user details to be 
  • hardcoded in memory files 
  <authentication-provider>
    <user-service>
        <user name="admin" password="secret" authorities="ROLE_ADMIN,ROLE_USER" />
        <user name="user1" password="1111" authorities="ROLE_USER" />
        <user name="user2" password="2222" disabled="true" authorities="ROLE_USER" />
    </user-service>
  </authentication-provider>
  • in the user SQL database. 
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery(
                        "SELECT username, password, 'true' as enabled FROM member WHERE username = ?")
                .authoritiesByUsernameQuery(
                        "SELECT member.username, member_role.role as authorities " +
                        "FROM member, member_role " +
                        "WHERE  member.username = ? AND member.id = member_role.member_id");
    }
  • Custom store - may be file or NOSQL storage. 
Create your own user details object and validate from wherever you want - DB, ftp etc., 
  • Identity store, you can retrieve using LDAP.
    <authentication-manager>
      <authentication-provider>
        <password-encoder hash="{sha}" />
        <ldap-user-service server-ref="ldapServer"
          user-search-filter="uid={0}" user-search-base="ou=people"
          group-search-filter="member={0}" group-search-base="ou=groups" />
      </authentication-provider>
    </authentication-manager>

    <ldap-server id="ldapServer"
        url="ldap://localhost:1389/dc=springrecipes,dc=com"
        manager-dn="cn=Directory Manager" manager-password="ldap" />

The configuration will also need to take care of what is the form page, what is the default successful url once logged in, the logout url, url signatures that need to be authenticated, userid and password field, action url of spring security. 

Securing URL Access

You can secure methods using different ways: 

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class ApplicationConfig { ... }

In the controller, 
  @Secured({"ROLE_USER", "ROLE_GUEST"})
    public List<Message> listMessages() {
        ...

    }

Implementing Data access layer using Spring - different options

This scrap note explains different ways by which you can implement Data access layer using Spring. Spring provides different ways for faster development of Data access layers. 

Using Direct JDBC: 
  • Creating the Datasource
In this you can configure the Datasource by providing the JDBC url, connection factory, user credentials string and create the DAO manually 
  • Creating DAO and accessing them
You can manually create the DAO and manually define methods for inserting, deleting, retrieving and modifying the entities. 

Using JDBC Template: 
In this you can use JDBCTemplate which simplifies the work for you, you have to create the JDBCTemplate on top of the data source you have defined. JDBCTemplate can be used to query and provide simple implementations to map to objects. 

Using Spring Data Modules: 
Spring data modules are the easiest way to implement DAO. 
In this you using data modules defined for each database type such as MongoDB, Cassandra or JPA etc., 

In Spring data modules you
  • Directly define the entities. 
  • The default CRUD implementation is provided for you. 
  • In case of custom implementation for DAO, you just provide the interface definition, the implementation is provided by the container. 

Its a very fast way of developing the Repository layer. 

There are lot of Spring data modules available such as
  • Spring data MongoDB
  • Spring data Cassandra
  • Spring data JPA 
  • Spring data REDIS

You can find more information about Spring data projects in http://projects.spring.io/spring-data/

When to use what: 
You can use Spring data modules for faster development, however it is not very flexible, but supports most of the day to day activities you perform with the DB, in case you want more control - you can go for JDBC Template. If you want full control, you can go for direct JDBC implementations, but you have to spend a lot of time in the mapping etc., 

Spring and Transaction management
There are 2 ways by which you can handle transactions
  • Programmatic way
  • Declarative way

Programmatic:
In the programmatic way, you have to manage your transaction which means you have to commit or rollback using the transaction manager API. 

Declarative: 
Declarative is the preferred way by most developers. 
  • Using AOP
  • Using @Transactional annotation. 

The @Transactional attribute supports the following attributes
  • REQUIRED
  • REQUIRES_NEW
  • SUPPORTS
  • NOT_SUPPORTED
  • MANDATORY
  • NEVER
  • NESTED

Isolation attributes
  • Dirty read
  • Non repeatable read
  • Phantom read
  • Lost updates. 

Isolation levels by Spring
  • DEFAULT
  • READ_UNCOMMITTED
  • READ_COMMITTED
  • REPEATABLE_READ
  • SERIALIZABLE


Spring MVC - Refreshing basics

Spring MVC is a way provided by Spring to handle and operate on web requests. 

The following steps are performed for any web requests. 
  • The request is sent to the Dispatcher servlet
  • The Dispatcher servlet passes the request to the handler mapping component. 
  • The handler mapping component returns the respective controller. 
  • The Controller receives the request along with the request attributes, performs the business logic and returns back the Model and view. 
  • The dispatcher servlet resolves the view name using the View resolver component.
  • The dispatcher servlet now knows which view to forward to, hence returns the respective view to the page. 

What should you do to get started using Spring mvc
  • Include required maven artifacts such as spring-webmvc and spring-web in your pom.xml
  • Add the component scanning class, just to tell container which classes to look out for. 
  • @EnableWebMvc to enable spring mvc in your application configuration file. 
  • Create Spring controllers: 
    • Tell the class that this is a controller class using @Controller
    • Define @RequestMapping annotation at the class level 
    • Define @RequestMapping annotation at the method level 
  • Create JSP views and provide view resolver mapping. 
  • Provide implementation in the controller method by accepting the input parameters, processing it and returning response. 

Signature of the controller method: 

The signature of the method of the controller is open ended, hence it can accept a variety of input parameters. Following are the possible parameters. 
  • The servlet request or servlet response
  • Request parameters annotated by @RequestParam annotation
  • Model attributes annotated by @ModelAttribute annotation
  • Cookie value in the incoming request using @CookieValue annotation
  • Map or model map for the user to had his own attributes. 
  • Errors or BindingResult for the user to add errors / validation result. 
and more options such as SessionStatus. 

@RequestMapping options: 
@RequestMapping("/member/add”)
@RequestMapping(value={"/member/remove","/member/delete"}, method=RequestMethod.GET)
@RequestMapping(value= "processUser", method =  RequestMethod.POST)

Note
  • You can use handler interceptor to provide implementation for pre / post processing of web requests. 
  • You can use resource bundle & message properties to implement internationalization. 
  • You can handle all the exceptions in one place using HandlerExceptionResolvers. 
  • You can publish data as rest services using @Requestmapping and @PathVariable. The return type can be XML or JSON depending upon the configuration 
  • You can produce JSON responses using Jackson mapping and @ResponseBody annotation. 
  • You can also access REST services using RESTTemplate class, there are multiple methods available to perform REST operation such as getForObject, put, delete, postForEntity etc., 
  • You can publish RSS feds using jdom. 


Spring basics

Spring - Basics (refreshing the basics)

This scrap note includes: 
  • Configuring beans using @Configuration
  • Creating beans and referencing them
  • Bean scopes

Configuring beans using @Configuration
You can configure actions such as configuring all the beans, reading resources from the property files, establishing connection to database in the @Configuration class.

@Configuration
@ComponentScan(basePackages = "com.deiveehan")
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig {

  @Inject
  Environment environment;

  @Bean
  public InternalResourceViewResolver configureInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
  }
}

Note: 
  • @Configuration is used to tell that this is a Bean configuration file. 
  • @ComponentScan is used to tell the container where to look for the class files. 
  • @PropertySource is used to define the properties file. 
  • environment.getProperty("host”) is used to get the property value. 
  • @Bean is used to define the beans. 

Creating beans and referencing them

You can create beans in 2 ways - using xml or using bean configuration files. 

Using annotation: 
@Bean is used to define a bean and 

Referencing beans: 
@Autowired is used to reference a bean
You can autowire a bean at the class level and at the method level. 

Example: 
@Configuration
@EnableMongoRepositories(basePackages = {
"com.deivee.framework.mongorepository",
“com.deivee.app.mongorepository" })
public class MongoDBConfig {

@Autowired
Environment environment;

@Bean
public Mongo mongo() throws Exception {
  return new Mongo(environment.getProperty("host"));
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
  return new MongoTemplate(mongo(),
  environment.getProperty("mongoDBName"));
}
}

and the MongoTemplate can be accessed in any bean class as below

@Autowired MongoTemplate. 

Bean scopes

There are 5 different types of bean scopes in Spring
  • singleton (single bean instance per container)
  • prototype (new bean each time when referred)
  • request (new bean for each request) 
  • session (available for the entire session)
  • globalsession (for a global http session, valid for portal application). 

For example you can create the scope for the bean when you create it. 
@Component
@Scope("prototype")
public class LoginAction {
Misc: 

  • You can use @PostConstruct to perform action after it is constructed and use @PreDestroy to perform action before it is destroyed. 
  • You can use @Profile to create profiles for different environment. 
  • You can detect the type of device that is sending the request using this String userAgent = request.getHeader("User-Agent”); the value of userAgent will be android, iPad, kindle etc., 
  • You can also use spring with other web frameworks such as JSF.