2017年6月29日 星期四

Java LDAP SSL connection example

package com.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class LdapExample {

    public static final String JAVA_HOME = "C:\\Program Files\\Java\\jdk1.8.0_131";
   
    public static void main(String[] args)
    {
        final String ldapAdServer = "host.com:6368";
        final String ldapSearchBase = "dc=bam,dc=edu,dc=hk";

        final String ldapUsername = "uid=37b7fe15, dc=bam, dc=edu, dc=hk";
        final String ldapPassword = "bambam";
       
        final String ldapAccountToLookup = "(|(universityid=ddz044071))";
       
        LdapContext ctx = null;
        try
        {
            Hashtable env = new Hashtable();
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            if(ldapUsername != null)
            {
                env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
            }
            if(ldapPassword != null)
            {
                env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
            }

            env.put(Context.SECURITY_PROTOCOL, "ssl");
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
           
            if( "ssl".equals(env.get(Context.SECURITY_PROTOCOL)) )
            {
                env.put(Context.PROVIDER_URL, "ldaps://"+ldapAdServer);
            }
            else
            {
                env.put(Context.PROVIDER_URL, "ldap://"+ldapAdServer);
            }

            System.out.println("java home: "+System.getProperty("java.home"));
            System.setProperty("javax.net.ssl.keyStore", JAVA_HOME+"\\jre\\lib\\security\\cacerts");
            System.setProperty("javax.net.ssl.trustStore", JAVA_HOME+"\\jre\\lib\\security\\cacerts");
           
           
            //ensures that objectSID attribute values
            //will be returned as a byte[] instead of a String
            env.put("java.naming.ldap.attributes.binary", "objectSID");
           
            // the following is helpful in debugging errors
            //env.put("com.sun.jndi.ldap.trace.ber", System.err);
           
            SearchControls ctls = new SearchControls();
            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            ctls.setReturningAttributes(
                new String[] {
                    "ds-cfg-listen-port",
                    "objectclass"
                } );
           
            ctx = new InitialLdapContext( env, null );

            LdapExample ldap = new LdapExample();
           
            NamingEnumeration list = ldap.findAccount(ctx,
                    ldapSearchBase, ldapAccountToLookup);
           
            listResult( list );
           
           
            // modify attributes
            ModificationItem[] mods = new ModificationItem[1];

         Attribute mod0 = new BasicAttribute("visible", "yes");
         //Attribute mod1 = new BasicAttribute("number2", "AAA");

         mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
         //mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);

         ctx.modifyAttributes("uid=304f8d81-5bce11e7-80babd7d-ae3f753a,dc=cuhk,dc=edu,dc=hk", mods);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(ctx!=null)
                {
                    ctx.close();
                }
            }
            catch(Exception e)
            {
            }
        }
        System.out.println("LdapExample exit");
    }
   
    public NamingEnumeration findAccount(DirContext ctx,
            String ldapSearchBase, String searchFilter) throws NamingException {

        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        NamingEnumeration results = ctx.search(ldapSearchBase, searchFilter, searchControls);

        return results;
    }
   
    public static void listResult(NamingEnumeration results) throws NamingException
    {
        int i = 1;
        SearchResult searchResult = null;
        while(results.hasMoreElements())
        {
            searchResult = (SearchResult) results.nextElement();
            System.out.println("============ record "+i+" ============");
            System.out.println( searchResult.getNameInNamespace() );
            Attributes attrs = searchResult.getAttributes();
            NamingEnumeration enumer = attrs.getAll();
            Attribute attrObj;
            while( enumer.hasMore() )
            {
                attrObj = (Attribute)enumer.next();
                //System.out.println( "attr: "+attrObj.getClass().getName() );
                //System.out.println( "attr: "+attrObj );
               
                System.out.print( "name: "+attrObj.getID()+", " );
                System.out.println( "value: "+attrObj.get() );
                //System.out.println( "attr: "+(attrObj instanceof javax.naming.directory.Attribute) );
               
                //break;
            }
            i++;
        }
    }
}

2017年6月14日 星期三

[ServiceNow] Can we also publish dashboards? Just like for reports where you can publish and send the url?

Apparently, we cannot publish dashboards.

[ServiceNow] Can we create a report by joining 2 tables?

Yes, using database views.



如何計算非香港居民身份證驗證碼 (CHECK DIGIT)?

以身份證RA123456(2)為例,此身份證的CHECK DIGIT為2。

首先將身份證中的英文字母轉為數字,A=1, B=2, C=3, .... Z=26。
然後按以下公式計算︰

先計算

第1位數字 * 2 加
第2位數字 * 8 加
第3位數字 * 7 加
第4位數字 * 6 加
第5位數字 * 5 加
第6位數字 * 4 加
第7位數字 * 3 加
第8位數字 * 2

的總和;

以RA123456(2)為例

R * 2 = 18 * 8 = 144
A * 8 = 1 * 8 = 8
1 * 7 = 1 * 7 = 7
2 * 6 = 2 * 6 = 12
3 * 5 = 3 * 5 = 15
4 * 4 = 4 * 4 = 16
5 * 3 = 5 * 3 = 15
6 * 2 = 6 * 2 = 12

總和是229。

然後計算229除11的餘數,再用11減去剛才的餘數便是驗證碼 (CHECK DIGIT)。

229 除 11,餘數是9

11 - 9 = 2

因此驗證碼 (CHECK DIGIT)是 2。

如最後計算出來的驗證碼 (CHECK DIGIT)是10,以A表示;

謝謝

[ServiceNow] How can you create a homepage specific to the groups?

The homepage can be made available to particular roles and then you can assign those roles to the intended groups.

[ServiceNow] Can we provide link to our existing KB?

If the requirement is to provide the link inside the ServiceNow KB article, follow the below procedure
1) Navigate to the Knowledge article.
2) In the HTML editor, click the Insert/Edit link icon.
3) Enter the complete URL in the text box (including http:// or https://)
4) Choose Target: New window (_blank). 
5) Click OK.

However, if the requirement is that upon clicking the KB article it should be routed to the external KB without 

opening the article in ServiceNow then scripting would be required to achieve the same.

[ServiceNow] Can we change the "quantity" options to 10, 20, etc? (shopping cart)

Yes, the values can be changed following the below steps:

1) Navigate to System Definition > Choice Lists.
2) Filter the list for records with Table set to sc_cart_item and the Element set to 
    quantity. The existing quantity choices appear.
3) To increase the quantities available for catalog items, add quantity choices. 
     Model the additional choices after the existing ones.


Inactivate the existing ones if not needed and create new according to your requirements.

[ServiceNow] Can i start the workflow from point of failure?

For any reason some of the tasks are failed in workflow, Can i start the workflow from point of failure?

This would be achieved through scripting 

[ServiceNow] What is the difference between service catalog and service portal?

Service Portal framework that allows administrators to build a mobile-friendly self service experience for users. 

It interacts with parts of the ServiceNow platform, so users can access specific platform features using Service Portal.

Service Catalog application, create service catalogs that provide your customers with self-service opportunities. 


Portals can be customised so that customers can request catalog items such as service and product offerings.

[ServiceNow] Can we visit Knowledge Homepage without login?

Follow the procedure to achieve the requirement:

1) Navigate to Knowledge > Administration > Knowledge Bases.
2) Select the knowledge base you want to make public.
3) Go to the Can Read section or tab and remove any entries from the list. 
    Everyone gets read permission on all articles in that knowledge base.
4) In the Application Navigator's Filter navigator field, enter sys_public.list and press 
    enter or return.
5) In the Page column, look for page named kb_find.
6) For kb_find, change the value in the Active column to true.
7) In the Page column, look for page named kb_view.
8) For kb_view, change the value in the Active column to true.

9) As a public user, try <instance>/kb_find.do.

[ServiceNow] Where and how could we create a custom Schedule for SLA?

In the application navigator, navigate to System Scheduler > Schedules