Accessing WCM component from outside JSP(Application) by passing authentication in URL

If you want access the WCM component from any outside application by just passing credential information as URL parameters like

http://localhost:10041/AnyWebAppContent/jsp/AccessWCMComponent.jsp?sitePath=library/site/Sitearea/SiteArea&username=wpsadmin&password=password

<%@page contentType="text/xml"%>
<%@ taglib uri="/WEB-INF/tld/wcm.tld" prefix="wcm" %>
<%@ page import="com.ibm.workplace.wcm.api.*" %>
<%@ page import="java.util.*" %>
<?xml version="1.0" encoding="UTF-8"?>
<%

String requestedURL= request.getRequestURL().toString();
String contextPath=request.getContextPath();
String baseURL="";
String compName = "Component Name";
String html = "";
String libraryName = "";

if( null!=requestedURL && 0!= requestedURL.trim().length()){
baseURL=requestedURL.substring(0,requestedURL.indexOf(contextPath));
baseURL+="/wps/wcm";
}
String userId=request.getParameter("username");
String password=request.getParameter("password");
String sitePath=request.getParameter("wcmPath");
if(null == userId || 0==userId.trim().length())userId=request.getParameter("userId");
if(null == password || 0==password.trim().length())password=request.getParameter("pwd");
if(null == sitePath || 0==sitePath.trim().length())sitePath=request.getParameter("sitePath");

if(null == userId || 0==userId.trim().length()||null == password || 0==password.trim().length()){
out.println("<h2><font color='red'><b>Invalid UserId and password,pass user name as \"username\" or \"userId\" and password as \"password\" or \"pwd\" </b></font></h2>");
}else if(null == sitePath || 0==sitePath.trim().length() || -1 == sitePath.indexOf("/")){
out.println("<h2><font color='red'><b>Invalid sitePath,pass sitePath as \"wcmPath\" or \"sitePath\"</b></font></h2>");
}else{
libraryName=sitePath.substring(0,sitePath.indexOf("/"));
Workspace workspace = WCM_API.getRepository().getWorkspace(userId, password);
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary(libraryName));
DocumentIdIterator docIds=workspace.findByName(DocumentTypes.LibraryComponent,compName);
if (docIds.hasNext()){
DocumentId did = (DocumentId)docIds.next();
LibraryComponent libComp = (LibraryComponent)workspace.getById(did);
RenderingContext rcjsp = (RenderingContext)request.getAttribute(Workspace.WCM_RENDERINGCONTEXT_KEY);
if(null == rcjsp)rcjsp = workspace.createRenderingContext(request,response,new HashMap(),baseURL,"connect");
rcjsp.setRenderedContent(sitePath);
html = workspace.render(rcjsp, libComp);
out.println(html);
}
} %>

5 comments:

  1. Could you please help me how to create new content using wcm api(in jsp) in WCM 8.0.

    ReplyDelete
    Replies
    1. You should be able to find sample code for creating content in lot of places. one simple way is

      <%@ page import="com.ibm.workplace.wcm.api.*"%>
      <%

      Repository repository = WCM_API.getRepository();
      Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
      //Workspace workspace = repository.getWorkspace("userId", "password");
      workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
      String authoringTemplateName = "AuthoringTemplate";
      String parentSiteAreaName = "SiteArea";
      String workflowName = "Workflow";

      DocumentId authoringTemplateId = null;
      DocumentId parentSiteAreaId = null;
      DocumentId workflowId = null;

      // find authoring template by name
      DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate, authoringTemplateName);
      if (authoringTemplateIterator.hasNext()) {
      authoringTemplateId = authoringTemplateIterator.nextId();
      }

      // find sitearea by name
      DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea, parentSiteAreaName);
      if (siteAreaIterator.hasNext()) {
      parentSiteAreaId = siteAreaIterator.nextId();
      }

      // find workflow by name
      DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow, workflowName);
      if (workflowIterator.hasNext()) {
      workflowId = workflowIterator.nextId();
      }

      if (null != authoringTemplateId && null != parentSiteAreaId && null != workflowId)) {
      // create new content
      //If the ChildPosition is ChildPosition.PREVIOUS or ChildPosition.NEXT, then the siblingId parameter must be specified.
      //If the ChildPosition is ChildPosition.START or ChildPosition.END then the siblingId parameter, then will be ignored.
      //So passing Null to siblingId.
      Content content = workspace.createContent(authoringTemplateId, parentSiteAreaId, null, ChildPosition.END);

      content.setName("TestContent");
      content.setTitle("Test Content");
      content.setDescription("Test Content Description");
      content.setWorkflowId(workflowId);
      String[] save = workspace.save((Document) content);
      if (0 == save.length) {
      out.println("Successfully Created test content.");
      }
      }
      %>

      Delete
  2. Is there a way to fetch the output of WCM component into the Portal 8, Theme jsps?
    Basically, trying to render WCM component inside the theme. Any tags? Can you please help? Thanks In Advance.

    ReplyDelete
    Replies
    1. Sorry for the late response, this can be achieved in so many ways like using the connect servlet url to wcm component directly or using the WCM rest calls or we can also use the WCM api calls

      Delete