Wednesday, 11 March 2015

VMM High level view websphere portal

VMM:- Is virtual member managment everyone says about it what is it actually? For any user to login he need to register first those details will be stored in a database(LDAP or XYZ). Now we need some way to retrieve this information what user register later when user login in for business purpose. For this ibm has provide a way to write adapaters and connect to our required repository and get the information in a xml format such that websphere portal understands it(UserRegistery.xml). This all process happens in VMM. Portal connects to VMM via Puma api and retrieve the required information. Below picture explains the flow.




Monday, 9 February 2015

Best way to make PumaUtil component

Overview :- Create a puma class which can be used inside portlet,portal and even in a servlet.


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.ActionRequest;
import javax.portlet.PortletRequest;
import javax.servlet.ServletRequest;

import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.ibm.portal.um.Group;
import com.ibm.portal.um.Principal;
import com.ibm.portal.um.PumaController;
import com.ibm.portal.um.PumaHome;
import com.ibm.portal.um.PumaLocator;
import com.ibm.portal.um.PumaProfile;
import com.ibm.portal.um.User;
import com.ibm.portal.um.exceptions.PumaException;
import com.ibm.wps.logging.LogManager;
import com.ibm.wps.logging.Logger;


public class PumaUtil{

private static final Logger LOGGER = LogManager.getLogManager().getLogger(
PumaUtil.class);

/**
* The JNDI name of the portal PUMA SPI service.
*/
private static final String PORTAL_PUMA_SERVICE_JNDI_NAME = "portal:service/usermanagement/Puma";

/**
* The JNDI name of the portlet PUMA SPI service.
*/
private static final String PORTLET_PUMA_SERVICE_JNDI_NAME = "portletservice/com.ibm.portal.um.portletservice.PumaHome";

/**
* The portal puma service home object that is valid for the lifetime of the
* portal server.
*/
private static PumaHome portalPumaHome;

/**
* The portlet puma service home object that is valid for the lifetime of
* the portal server.
*/
private static com.ibm.portal.um.portletservice.PumaHome portletPumaHome;

static {
//If we are invoking in portal theme or in servlet
try {
Context context = new InitialContext();
portalPumaHome = (PumaHome) context
.lookup(PORTAL_PUMA_SERVICE_JNDI_NAME);
} catch (NamingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//If we are using it inside portal server
try {
Context context = new InitialContext();
PortletServiceHome portletServiceHome = (PortletServiceHome) context
.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
portletPumaHome = (com.ibm.portal.um.portletservice.PumaHome) portletServiceHome
.getPortletService(com.ibm.portal.um.portletservice.PumaHome.class);
} catch (NamingException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (PortletServiceUnavailableException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

/**
* The current servlet request.
*/
private ServletRequest servletRequest;

/**
* The current portlet request.
*/
private PortletRequest portletRequest;

/**
*
* @param request
*/
private PumaUtil(ServletRequest request) {
servletRequest = request;
}

/**
*
* @param request
*/
private PumaUtil(PortletRequest request) {
portletRequest = request;
}

/**
*
* @return
*/
private boolean isServletRequest() {
return servletRequest != null;
}

/**
*
* @return
*/
private boolean isPortletRequest() {
return portletRequest != null;
}

/**
*
* @param request
* @return
*/
public static PumaUtil getInstance(ServletRequest request) {
return new PumaUtil(request);
}

/**
*
* @param request
* @return
*/
public static PumaUtil getInstance(PortletRequest request) {
return new PumaUtil(request);
}

/**
*
* @return
*/
public PumaProfile getProfile() {
if (isServletRequest())
return portalPumaHome.getProfile();
else
return portletPumaHome.getProfile(portletRequest);
}

/**
*
* @return
*/
public PumaLocator getLocator() {
if (isServletRequest())
return portalPumaHome.getLocator();
else
return portletPumaHome.getLocator(portletRequest);
}

/**
*
* @return
*/
public PumaController getController() {
if (isServletRequest())
return portalPumaHome.getController();
else if (portletRequest instanceof ActionRequest)
return portletPumaHome
.getController((ActionRequest) portletRequest);
return null;
}

/**
*
* @return
*/
public User getUser() {
try {
if (isServletRequest())
return portalPumaHome.getProfile().getCurrentUser();
else
return portletPumaHome.getProfile(portletRequest)
.getCurrentUser();
} catch (PumaException e) {
e.printStackTrace();
return null;
}
}

/**
*
* @return
*/
public boolean isUserAuthenticated() {
if (getUser() != null) {
return true;
}
return false;
}

public boolean attributeExists(String name, String value) {
try {
List list = getLocator().findUsersByAttribute(name, value);
if (list.size() > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return false;
}

/**
*
* @param name
* @return
*/
public String getUserAttribute(String name) {
assert name != null;
try {
List list = new ArrayList(1);
list.add(name);
Map map = getProfile().getAttributes(getUser(), list);
return (String) map.get(name);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
*
* @param name
* @param value
*/
public void setUserAttribute(String name, String value) {
setAttribute(getUser(), name, value);
}

/**
*
* @param principal
* @param name
* @param value
*/
private void setAttribute(Principal principal, String name, String value) {
Map attributes = new HashMap();
attributes.put(name, value);
try {
getController().setAttributes(principal, attributes);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
*
* @param distinguishedName
* @return
*/
public boolean isUserMemberOfGroup(String distinguishedName) {
try {
List groups = getLocator().findGroupsByPrincipal(getUser(), false);
Iterator groupIterator = groups.iterator();
List attributes = new ArrayList();
attributes.add(USER_GROUP_ATTRIBUTE);
while (groupIterator.hasNext()) {
Group group = (Group) groupIterator.next();
Map groupAttributes = getProfile().getAttributes(group,
attributes);
String groupName = (String) groupAttributes
.get(USER_GROUP_ATTRIBUTE);
List list = new ArrayList(1);
list.add(getUser());
if (distinguishedName.equals(groupName)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

}

Wednesday, 28 January 2015

Data-structures,sorting & searching with java Part1

Overview :- We will start with sorting techniques. To start fast we will pick the easiest sorting technique like Bubble  sort



Java program :- 


public class BubbleSort {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        //plain logic for bubble sort
        int a [] = {4,30,-3,7,1,20,12,50,-9,-1,89,14,13};
        int count =0;
        for(int i=0;i<a.length;i++){
        count=count+1;
        for(int j=i+1;j<a.length;j++){
        count=count+1;
        if(a[i]>a[j]){
        int temp;
        temp =a[j];
        a[j] = a[i];
        a[i] = temp;      
        }
        }
        }
        System.out.println(" count via bubble sort first approach :- "+count);
        for(int i=0;i<a.length;i++){      
        System.out.println(" elements :- "+a[i]);
        }
        int b [] =  {4,30,-3,7,1,20,12,50,-9,-1,89,14,13};    
        fasterBubbleSort(b);
        int c [] =  {4,30,-3,7,1,20,12,50,-9,-1,89,14,13};  
        bubbleSorting(c);
}
        //alternate logic for bubble sort
public static void bubbleSorting(int[] a){
int count =0;
for(int i=0;i<a.length;i++){
        count=count+1;
        for(int j=0;j<(a.length-i-1);j++){
        count=count+1;
        if(a[j]>a[j+1]){
        int temp;
        temp =a[j+1];
        a[j+1] = a[j];
        a[j] = temp;      
        }
        }
       }
System.out.println("count via alternate bubble sort :-"+count);
}
       //smarter logic for bubble sort
public  static void fasterBubbleSort(int[] testArray) {
System.out.println("length :-"+testArray.length);
     boolean isSwapped = true;
     int i = 0;
     int count =0;
     while (isSwapped ) {
           isSwapped = false;
           i++;
           for (int j = 0; j < testArray.length - i; j++) {
            count=count+1;
                 if (testArray[j] > testArray[j + 1]) {                        
                       int temp = testArray[j];
                       testArray[j] = testArray[j + 1];
                       testArray[j + 1] = temp;
                       isSwapped = true;
                 }
           }              
     }
     System.out.println("count via faster bubble sort :-"+count);
}

}


Simple explanation :- Take first element and go to all other elements and check > or < based on condition{ascending/descending} swap it. Like that do it for every element that's it. Don't confuse your selves with time complexity . Its nothing but how much time it is taking to execute your code. And how it will work when inputs are big. To know the time complexity just count how many loops the program is executing.

Now time complexity :- Now about the time complexity  for the bubble sort by leaving smart approach. n+n-1 + n-2... = n(n-1)/2 its our old school formula. The same thing happens here because we will be taking each value in array and compare with all other elements that exactly follows the sequence i have mentioned above.so n^2 /2 - n/2. When n^2 is very big we can remove n/2. So now we have to complexity of n^2/2.  



Saturday, 10 January 2015

Invoking web-service to consume web methods(Basic Web-Services Part4)


1)Using SoapUI Tool  :- Create new project in tool then provide WSDL url and project name then click ok.


Now click on any methods on left hand side and then select any request to view output


2)Using Java Client :-  We need to write a java client to invoke the web-service

package com.krishna.webservice.basics;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class WebServiceClient {

public static void main(String args[]){

try {
URL url = new URL("http://127.0.0.1:9876/ws?wsdl");
QName qname = new QName("http://basics.webservice.krishna.com/","NewJoinesImplService");
Service service = Service.create(url, qname);
JoiningFormalities joiningFormalities= service.getPort(JoiningFormalities.class);
System.out.println("Message :-"+joiningFormalities.showWelcomeGreetings());
System.out.println("Role :-"+joiningFormalities.getEmployeeRole());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Output :- 


First Webservice program for a fresh Learner(Basic WebService Part3)

Web-service :- Its a service built and deployed in server. Which can be accessed by any type of application irrespective of language platform. For more information please refer to my previous tutorials.

Service end point interface :-  Create an interface which a blue print for your web-service. If you annotate your Interface with @WebService then your interface is eligible for web service. If you annotate your methods as @WebMethod then methods inside the interface are exposed as web-service methods.

package com.krishna.webservice.basics;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface JoiningFormalities {

@WebMethod
public String getEmployeeRole();

    @WebMethod
public String showWelcomeGreetings();
}

Service implementation bean :- We need to create implementation class for our service interface. If we annotate our class with @webservice and point it to our interface then it will become implementation of our service.

package com.krishna.webservice.basics;

import javax.jws.WebService;

@WebService(endpointInterface="com.krishna.webservice.basics.JoiningFormalities")
public class NewJoinesImpl implements JoiningFormalities {

@Override
public String getEmployeeRole() {  
return "Team Lead";
}

@Override
public String showWelcomeGreetings() {
return "Welcome Krishna";
}

}

Publishing web-service :- Generally we publish webservices on application servers but using Endpoint class we can publish to our personal computer. (127.0.0.1) is our computer loopback address. 

package com.krishna.webservice.basics;

import javax.xml.ws.Endpoint;

public class PublishWebservice {
public static void main(String args[]){
 
Endpoint.publish("http://127.0.0.1:9876/ws", new NewJoinesImpl());
}
}

Testing Webservice by opening a browser :- 


WSDL file :- 

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://basics.webservice.krishna.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://basics.webservice.krishna.com/" name="NewJoinesImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://basics.webservice.krishna.com/" schemaLocation="http://127.0.0.1:9876/ws?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="getEmployeeRole">
<part name="parameters" element="tns:getEmployeeRole"></part>
</message>
<message name="getEmployeeRoleResponse">
<part name="parameters" element="tns:getEmployeeRoleResponse"></part>
</message>
<message name="showWelcomeGreetings">
<part name="parameters" element="tns:showWelcomeGreetings"></part>
</message>
<message name="showWelcomeGreetingsResponse">
<part name="parameters" element="tns:showWelcomeGreetingsResponse"></part>
</message>
<portType name="JoiningFormalities">
<operation name="getEmployeeRole">
<input message="tns:getEmployeeRole"></input>
<output message="tns:getEmployeeRoleResponse"></output>
//List of operations under this webservice
</operation>
<operation name="showWelcomeGreetings">
<input message="tns:showWelcomeGreetings"></input>
<output message="tns:showWelcomeGreetingsResponse"></output>
</operation>
</portType>
<binding name="NewJoinesImplPortBinding" type="tns:JoiningFormalities">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="getEmployeeRole">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
<operation name="showWelcomeGreetings">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
//Service end point declaration
<service name="NewJoinesImplService">
<port name="NewJoinesImplPort" binding="tns:NewJoinesImplPortBinding">
<soap:address location="http://127.0.0.1:9876/ws"></soap:address>
</port>
</service>
</definitions>


Sunday, 4 January 2015

Working with MLS in web content managment

MLS :- Multilingual site contains information localized across different users and different languages. MLS is available out of the box from portal8.0.0.1 and for below versions. We need to download it from greenhouse.

Step1(Registering MLS to portal) :-  To register you can run below two tasks{Make sure WasPassword and PortalAdminPwd is set in wkplc file before running below tasks}

  • ConfigEngine.bat register-wcm-mls
  • ConfigEngine.bat deploy-wcm-mls

Step2(Registering to virtual portal ):-  If you have virtual portal you need run below extra config engine task.

ConfigEngine.bat import-wcm-mls-data  -DVirtualPortalContext=dpath

Step3(Configuring Meber fixer tool ) :-  Run below task for Member fixer tool.

ConfigEngine.bat run-wcm-admin-task-member-fixer -DPortalAdminId=wpsadmin -DPortalAdminPwd=wpsadmin -DWasUserId=wpsadmin -DWasPassword=wpsadmin -Dlibrary=MLConfiguration_v7 -Dfix=true -DinvalidDn=update -DmismatchedId=update -DaltDn=update

Step4 :- Now navigate to administration you can find MLS copy link under administration using which we can copy library's from one language to other.

Creating credential vault slot for syndication

Overview :- To create a syndicator using which we can get all libraries from configured system.

Step1 :- Navigate till portal server bin and save below xml as CreateSyndSlot.xml

<?xml version="1.0" encoding="UTF-8"?>
<request
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="PortalConfig_8.0.0.xsd"
    type="update" create-oids="true">

    <!-- Sample for creating a new credential vault slot. This script creates a     -->
    <!-- credential vault resource and a shared slot in the Default Admin Segment   -->
    <portal action="locate">
         <credential-segment action="locate" adapter-type="default" name="DefaultAdminSegment"
             user-mapped="false">
             <description>Default Admin Segment</description>
             <credential-slot action="update" active="false" name="syndication-slot"
                 resource="syndication-resource" secrettype="userid-password" system="true">
                 <localedata locale="en">
                     <description>used for syndicator and subscriber pair</description>
                 </localedata>
                 <password-secret action="create" external-id="padmin"
                     user="padmin">padmin</password-secret>
             </credential-slot>
         </credential-segment>
    </portal>
</request>

Step2 :- Execute below command
xmlaccess.bat -in CreateSyndSlot.xml -out slot-out.xml -url http://localhost:10039/wps/config -user wpsadmin -password wpsadmin

Step3 :- Now navigate to portal administration click on subscribers and click on subscriber then enter the syndicator url , select above credential slot click ok then select the library you want to syndicate. Then select allitems then click ok.

Step4 :- After completing all these click on refresh button syndication will become active when it will become idle syndication is done.


Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...