Skip to Content

Last week I have read a lot of questions about Webservices and Visual Composer and how to get the Portal roles in Visual Composer, that’s why I decided to write this blog. This blogs has two parts. The first one is about webservices which returns all Portal roles and the portal role of the user. I want to explain in the first part the Java coding of the webservice. In the second part I describe how to use the webservice in Visual Composer and how you can test your webservice with the webservice Navigator resp. in Visual Composer.
 

For this example you should be able to create a simple webservice with the SAP NetWeaver Developer Studio. I don’t explain how to do this, because there are several blogs on SDN available, e.g. from Baris Buyuktanir:

How-To Create Dynamic & Configurable Web Services Easily With Netweaver Development Components (Part I)

How-To Create Dynamic & Configurable Web Services Easily With Netweaver Development Components (Part II)

You have to add com.sap.security.api.jar to your project that includes UME classes. First you need a help class, which I have already mentioned it in this Visual Composer Problem with displaying array or list returned by a web service.

Then you have to implement the follwing code:

 

package getAllPortalRoles;

import java.io.Serializable;
public class roleModel implements Serializable {
private String Role;
private String RoleID;
private String Description;

/**
* @return
*/
public String getRole() {
return Role;
}

/**
* @return
*/
public String getRoleID() {
return RoleID;
}

/**
* @param string
*/
public void setRole(String string) {
Role = string;
}

/**
* @param string
*/
public void setRoleID(String string) {
RoleID = string;
}

/**
* @return
*/
public String getDescription() {
return Description;
}

/**
* @param string
*/
public void setDescription(String string) {
Description = string;
}
}

After implementing the help class you can implement the business method for getting all portal roles.

package getAllPortalRoles;
import java.util.ArrayList;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import com.sap.security.api.IRole;
import com.sap.security.api.IRoleFactory;
import com.sap.security.api.IRoleSearchFilter;
import com.sap.security.api.ISearchAttribute;
import com.sap.security.api.ISearchResult;
import com.sap.security.api.UMFactory;

/**
* @ejbHome <{getAllPortalRoles.GetAllPortalRolesHome}>
* @ejbLocal <{getAllPortalRoles.GetAllPortalRolesLocal}>
* @ejbLocalHome <{getAllPortalRoles.GetAllPortalRolesLocalHome}>
* @ejbRemote <{getAllPortalRoles.GetAllPortalRoles}>
* @stateless
* @transactionType Container
*/


public class GetAllPortalRolesBean implements SessionBean {

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

public void setSessionContext(SessionContext context) {
myContext = context;
}

private SessionContext myContext;
/**
* Create Method.
*/
public void ejbCreate() throws CreateException {
// TODO : Implement
}



/**
* Business Method.
*/
public roleModel[] getRoles() throws Exception{
// Create new array list for roles
ArrayList RoleList = new ArrayList();
try {
IRoleFactory rfact = UMFactory.getRoleFactory();
// define filter for portal roles; using * for all portal Roles
IRoleSearchFilter roleFilter = rfact.getRoleSearchFilter();
roleFilter.setUniqueName("*", ISearchAttribute.EQUALS_OPERATOR, false);
ISearchResult resultList = rfact.searchRoles(roleFilter);
// Passing the result list to the array list
if (resultList.getState() == ISearchResult.SEARCH_RESULT_OK) {
while (resultList.hasNext()) {
// read roleUid
String roleUid = (String) resultList.next();
// create roleObject for roleUid
IRole roleObject = rfact.getRole(roleUid);
// instantiate new role for array list
roleModel rolle = new roleModel();
// read DisplayName and UniqeID of the the portal role and set it to the portal role object
rolle.setRole(roleObject.getDisplayName());
rolle.setRoleID(roleObject.getUniqueID());
rolle.setDescription(roleObject.getDescription());
// add the role object to the array list
RoleList.add(rolle);
}
}
// create new array of the data type roleModel with the corresponding size
roleModel[] returnList = new roleModel[RoleList.size()];
// move ArrayList into the created array
RoleList.toArray(returnList);
// retrun all portal roles
return returnList;
// throw an exception if an error occurs
} catch (Exception e) {
throw new Exception(e);
}
}
}

After you have deployed it to your portal you can use it in Visual Composer. Now we add a further service, which returns the roles of a certain portal user.

package getRolesForUser;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import com.sap.security.api.IRole;
import com.sap.security.api.IRoleFactory;
import com.sap.security.api.IUser;
import com.sap.security.api.UMFactory;

import java.util.ArrayList;
import java.util.Iterator;

/**
* @ejbHome <{getRolesForUser.GetRolesForUserHome}>
* @ejbLocal <{getRolesForUser.GetRolesForUserLocal}>
* @ejbLocalHome <{getRolesForUser.GetRolesForUserLocalHome}>
* @ejbRemote <{getRolesForUser.GetRolesForUser}>
* @stateless
* @transactionType Container
*/
public class GetRolesForUserBean implements SessionBean {

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

public void setSessionContext(SessionContext context) {
myContext = context;
}

private SessionContext myContext;
/**
* Business Method.
*/
public String[] getPortalRolesForUser(String userGID) throws Exception{

// create array list
ArrayList RoleList = new ArrayList();
String UniqueID = new String();
try {
// get roles of user
Iterator rit = null;
IUser user = UMFactory.getUserFactory().getUserByLogonID(userGID);
rit = user.getRoles(true);
IRoleFactory rfact = UMFactory.getRoleFactory();
//adding roles of user to the array list
while (rit.hasNext()) {
String roleName = (String) rit.next();
IRole role = rfact.getRole(roleName);
UniqueID = role.getUniqueID();
RoleList.add(UniqueID);
}

// create new array of data type string with the corresponding size
String[] returnList = new String[RoleList.size()];
// move ArrayList into the created array
RoleList.toArray(returnList);
// return the portal roles of the user
return returnList;
}
// throw exception if an error occurs
catch (Exception e) {
throw new Exception(e);
}
}

/**
* Create Method.
*/
public void ejbCreate() throws CreateException {
// TODO : Implement
}

}

This was the first part of this weblog. In the next days I will show you how to use the webservices in Visual Composer.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Richard Hirsch
    Hi Marcel,

    Take a look at my old blog “Visual Composer Explorations: Using Portal Roles to influence UI Element Behavior” (The specified item was not found.) which explored similar themes.

    Thanks.

    Dick

    (0) 

Leave a Reply