Monday 4 June 2012

Project URL generation in Websphere portal server 8.0

You can redirect request processing to a specific project by generating URLs with the ProjectIdentificationService API, the REST API, or the Enabler API. Request processing operates either completely within the scope of a project or completely outside the scope of a project. You cannot switch projects during request processing.

When a request originates from within a project, the request URL contains a project identifier for that project. The project information is included only in the URL and is not bound to the session. The project identifier can be an object ID (OID), as used by the portal, or a universally unique identifier (UUID), as used by Web Content Manager. To direct request processing to a specific project, you must generate a URL for the project and then render the URL.

Sample code:
public void init() throws PortletException{
          super.init();
          try {
                Context ctx = new InitialContext();
                projectService = (ProjectIdentificationService)ctx.lookup(ProjectIdentificationService.JNDI_NAME);
                PortletServiceHome serviceHome = (PortletServiceHome)ctx.lookup(JNDI_NAME);
                stateService = (PortletStateManagerService)serviceHome.getPortletService(PortletStateManagerService.class);

                } catch(NameNotFoundException ex) {
                      ex.printStackTrace();
                } catch (NamingException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                }
    }

public String createPageURL(PortletRequest request, PortletResponse response) throws Exception {
                URLFactory urlFct=null;
                try{
           // get the needed factories
                  // construct a server context for the project
              //projectService.getProjectID()
                String projid="1338867733125";
                ObjectID projoid=getObjectID(projid);
                  final ServerContext projectCtx = projectService.createServerContext(projoid, new MyServerContext());
                  // access the URL factory to create a URL
            PortletStateManager manager = stateService.getPortletStateManager(request, response);
            urlFct= (URLFactory) manager.getURLFactory(projectCtx);
                  // construct a URL to the current state
                  final EngineURL url = urlFct.newURL(Constants.SMART_COPY);
                  return url.writeDispose(new StringWriter()).toString();
                }
                finally
                {
                  if(null!=urlFct)urlFct.dispose();
                }
               }


      private ObjectID getObjectID(String uniqueNameStr){
          try {
            InitialContext ctx = new InitialContext();
            final Name uniqueName = new CompositeName("portal:uniquename");
            uniqueName.add(uniqueNameStr);
            ObjectID oidForUniqueName = (ObjectID) ctx.lookup(uniqueName);
            System.out.println("oidForUniqueName="+oidForUniqueName);
            return oidForUniqueName;
          } catch (Exception e) {
            e.printStackTrace(System.out);
          }
          return null;
      }

 Using CSA,we can obtain Project URL through javascript

<script>
// get the current nav state
var state = com.ibm.mashups.enabler.model.state;
var navState = state.NavigationStateModelFactory.getNavigationStateModel();

// get the URL generator
var urlGen = state.UrlGeneratorFactory.getURLGenerator();
urlGen.getURL(navState, function(url) { alert(url); }, { "project": "com.ibm.workplace.community.portal" } );
alert(urlGen);
</script>

Click here to download the code.

No comments:

Post a Comment