2015年7月21日 星期二

Apache shiro ldap multiple OU

Problem:

Shiro provide the LDAP access, but the accessing links is only complete CN.
such as
ldapRealm.userDnTemplate = cn={0},ou=eee,dc=gp,dc=mycompany

If you want to access the different ou such as
ldapRealm.userDnTemplate = cn={0},ou=eee,dc=gp,dc=mycompany
ldapRealm.userDnTemplate = cn={0},ou=aaa,dc=gp,dc=mycompany

It will use the ou=aaa, how to I access the two ou in same ldap content ?


Solution:

I define the access multiple ou format in shiro and inheritance the JndiLdapRealm

a. Shiro format  in shiro.ini
  - Define handle class
     >  ldapRealm = package.LdapMultiOU
  - Using the piple as sepearte
     > ldapRealm.userDnTemplate = cn={0},[cn=aaa|ou=eee],dc=gp,dc=mycompany


b.  The code

public class LdapMultiOU extends JndiLdapRealm {

protected AuthenticationInfo queryForAuthenticationInfo(
AuthenticationToken token, LdapContextFactory ldapContextFactory)
throws NamingException {

Object principal = token.getPrincipal();
Object credentials = token.getCredentials();

// coding
AuthenticationInfo info = null;
NamingException e = null;

principal = getLdapPrincipal(token);
System.out.println("Before ldap cn was " + principal);
String[] CNs= principal.toString().split(",");
StringTokenizer OUs = new StringTokenizer(CNs[1].replaceAll("(\\[|\\])", ""), "|"); 
while (OUs.hasMoreTokens()) {
         principal = CNs[0] + "," + OUs.nextToken() + "," +  CNs[2] + "," + CNs[3];
System.out.println("After ldap cn was " + principal);
       LdapContext ctx = null;
    try {
ctx = ldapContextFactory.getLdapContext(principal, credentials);
// context was opened successfully, which means their credentials
// were valid. Return the AuthenticationInfo:
info = createAuthenticationInfo(token, principal, credentials, ctx);
} catch (NamingException eNam) {
e = eNam;
    } finally {
LdapUtils.closeContext(ctx);
}
    }

if (info != null)  return info;
else throw e;
}
}


Reference:
http://stackoverflow.com/questions/9273631/apache-shiro-ldap-multiple-ous
http://blog.stratio.com/supporting-service-based-multi-realm-authentication-and-authorization/

2015年7月19日 星期日

Javascript Regular Expression

Description

Javascript provides two  regular function.

exec():
- Goal: This method will testing the string and return the match pattern into array.
- Example:
var  tabName = /#(.*)/g.exec(window.location.href)[1];

test():
- Goal: only testing the string is having the pattern and return true or false




Reference:
Chinese introduction
More detail explanation





2015年7月8日 星期三

Jquery event handler

1. You can define your own event using trigger() method

 $(document).trigger('reveal.facebox').trigger('afterReveal.facebox')



2. Then to receive the event by bind() or on() method

 $(document).bind('reveal.facebox', function() {

}

2015年5月18日 星期一

Multiple CSS and jquery

Problem Description

How does the meaning in class="errorMessage active"
HTML Code
<span class="errorMessage active">Invalid</span>


CSS Code and why the .active after the .errorMesage
.errorMessage{
display: none;
color: red;
margin-left: 10px;
}
.errorMessage.active {
display: inline-block;
border: 1px solid #e6e6e6;
padding: 0 5px;
background: #ffdae0;
border-radius: 3px;
}

Jquery code
$('.IncludeSnRNA').on('change', function(){
    if($(this).is(":checked")){
    $('.errorMessage').addClass("active");
    $('.IncludeSnRNA').val('t');
    }else {
    $('.errorMessage').removeClass("active");
    $('.IncludeSnRNA').val('f');
    }
    });



Solution

Target an element that has all of multiple classes
and means the active class of errorMessage class is use as constraint to that the subject is errorMessage

2015年5月4日 星期一

Jquery Deffer method

Introduction Basic content

Ajax: Asynchronous Java script and XML
JSON: Using string representation for an object 
jqXHR object:  is jquery XML HTTP object which represent as $.ajax
JSONP: Give a strategy to access cross domain data
 
                    Subject of Deffer function
Goal: 
It will let programmer make a asyn-like calling mechanism
and often use in nested ajax calling to avoid to nested content.

Event trigger:
In the process, declare the deffer start .deffer().
Then, you should write deffer.resolve() mean it success, or deffer.reject() mean fail in handle process.
Finally, you return it by .promise();

Event Handle:
Client code can handle it by .then(callback1, [callback2]) method after it use the Event trigger method,


Ref:
ajax-deferreds tutorial
jquery deferred-object function
jquery-deferred-object chinese








2015年4月24日 星期五

JAVA script template Using Handlebars.js


1. What is Handlebars.js ?
It is using javascript to process the MVC pattern in view process.

You can Learn start from getting-started-with-handlebars-js
Official site api from Official sites
code From git

2015年4月12日 星期日

Abstract How browser work

This is a abstract content from how browsers work


Main structure

User interface 
   this includes the address bar, back/forward button, bookmarking menu etc.
 
Browser engine
   the interface for querying and manipulating the rendering engine.
 
Rendering engine
responsible for displaying the requested content. For example if the requested content is HTML, it is responsible for parsing the HTML and CSS and displaying the parsed content on the screen.

Networking
 used for network calls, like HTTP requests. It has platform independent interface and underneath implementations for each platform.

UI backend 
 used for drawing basic widgets like combo boxes and windows. It exposes a generic interface that is not platform specific. Underneath it uses the operating system user interface methods.

JavaScript interpreter. 
Used to parse and execute the JavaScript code.

Data storage.
his is a persistence layer. The browser needs to save all sorts of data on the hard disk, for examples, cookies.


The main flow


I Parse STEP

1. Parse HTML  to DOM tree

2. Parse CSS to css tree

3. Parse javascript.
  • javascript will execute when it encounter and this the html may be not all in standby.

II Build the Render tree

A render tree will combine the DOM NODE and CSS attribution

III  Layout 

Layout is output content the width and height in the screen, and show the color etc. The viewport mean the browser default position