Previous Next Contents Index


ICallerContext interface

The ICallerContext interface manages programmatic security from within a servlet. ICallerContext provides two methods that override equivalent methods in the standard javax.ejb.EJBContext interface.

From within EJBs, do not use ICallerContext. Use javax.ejb.EJBContext instead, to maintain the portability of the bean.

Typically ICallerContext is used together with the IServerContext interface.

Package
com.netscape.server

Methods
The ICallerContext interface is described as follows:

public interface ICallerContext 

{
public java.security.Identity getCallerIdentity();
public boolean isCallerInRole(java.security.Identity role);
}
getCallerIdentity( ) returns the Identity object that identifies the caller.

The isCallerInRole( ) method returns true if the caller has the given role, where the role parameter describes the java.security.Identity of the role to be tested.

Neither of the specifications for servlets and EJBs describe a standard way of creating an instance of java.security.Identity. To create an instance within a NAS 4.0 application, use the following method from the IServerContext interface:

public java.security.Identity createIdentityByString(
	String identity)
Examples
Example 1
The following sample code demonstrates the use of programmatic security from a servlet:

// from within a servlet 

com.netscape.server.IServerContext netscapeCtx;
ServletContext ctx = getServletContext();
netscapeCtx = (com.netscape.server.IServerContext) ctx;

com.netscape.server.ICallerContext callerCtx;
callerCtx = netscapeCtx.getCallerContext();

Identity id = callerCtx.getCallerIdentity();

if(id == null)
System.out.println("Servlet invoked by anonymous user");
else
System.out.println("Servlet invoked by user " + id.getName());
Example 2
The following sample code demonstrates the use of programmatic security from a servlet:

// from within a servlet

com.netscape.server.IServerContext netscapeCtx;
ServletContext ctx = getServletContext();
netscapeCtx = (com.netscape.server.IServerContxt) ctx;

com.netscape.server.ICallerContext callerCtx;
Identity managers = netscapeCtx.createIdentityByString("managers");

if(callerCtx.isCallerInRole(managers))
// caller is a member of the group managers
else
// caller is not a manager
Example 3
The following sample code demonstrates the use of programmatic security from a bean.

Both SessionContext and EntityContext inherit from javax.ejb.EJBContext. The container will invoke setSessionContext( ) or setEntityContext( ) on your bean and provide you with an instance before any business methods are executed.

// within a bean 


javax.ejb.SessionContext m_ctx;

// method on javax.ejb.EJBContext
Identity id = m_ctx.getCallerIdentity();
In addition, the isCallerInRole() method, available from ICallerContext and EJBContext, can be used to test for membership.

Related Topics
com.kivasoft.applogic.AppLogic class (deprecated),
com.kivasoft.dlm.GXContext class,
com.kivasoft.IContext interface,
com.netscape.server.IServerContext interface

javax.ejb.EJBContext interface

Writing Secure Applications

 

© Copyright 1999 Netscape Communications Corp.