cancel
Showing results for 
Search instead for 
Did you mean: 

PCD Filter - get IP Address of client

Former Member
0 Kudos

Hi there,

we are implementing a PCD Filter like described <a href="https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/ep/code-samples/Filtering%20Role%20and%20Workset%20Content.htm">here</a>

What we would like to do is make an iView available depending on the IP Address of the user.

Following code is used for the filter in the example of the link above. How can we retrieve the user's IP address? Can the request be accessed?


package com.acme.portal.demo;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.spi.ObjectFactory;

import com.sap.portal.directory.Constants;
import com.sap.security.api.IUser;
import com.sapportals.portal.pcd.gl.IPcdContext;


/**
 * Filter factory that filters returns a filter string filtering for the
 * attribute "com.sap.portal.demo.Country"
 */
public class CountryFilterFactory implements ObjectFactory
{

	/**
	 * @see javax.naming.spi.ObjectFactory#getObjectInstance(Object, Name, Context, Hashtable)
	 */
	public Object getObjectInstance(Object arg0, Name arg1, Context arg2, Hashtable env)
	throws Exception
	{
        // do not filter by default
        String filterExpression = "";  
        
        // only filter for the navigation aspect
        if (Constants.ASPECT_NAVIGATION.equals(env.get(Constants.REQUESTED_ASPECT)))
        {         
            IUser user = (IUser) env.get(IPcdContext.SECURITY_PRINCIPAL);
            if (user != null)
            {
                String country = user.getCountry();    
                                   
                if ((country != null) && (!country.equals("")))
                {        
                    filterExpression = "(" + CountryFilterService.COUNTRY_ATTRIBUTE_KEY + "=" + country + ")";                   		
                }
            }
        }
        return filterExpression;
	}

}

Many thanks,

Kevin

Accepted Solutions (0)

Answers (1)

Answers (1)

detlev_beutner
Active Contributor
0 Kudos

Hi Kevin,

I don't have a working example at hand and no time to create one. But: Debug the implementation. There are not many places from where you could, if possible at all, access the request (as far as I remember, it's not accessible, but it was a colleague who implemented it and also this is 1.5 years ago).

When debugging, you can set a break point into getObjectInstance and check out all passed parameters within your debugging NWDS.

Hope it helps

Detlev

Former Member
0 Kudos

Hi Detlev,

thanks for the response. I have used the NW Developer Studio's debugging. Unfortunately request doesn't seem to be available.

I have found some code where they retrieve the IP address based on the CallbackHandler. Don' think that would be an option here, is it? There doesn't seem to be a way of retrieving the handler.

<a href="http://help.sap.com/saphelp_nw04/helpdata/en/d0/5954e9f2aa47e6b91fc3ebf18d5de5/frameset.htm">Advanced Authentication Example</a>

Thanks again for the help,

Kevin

Former Member
0 Kudos

You should be able to use a standard java api for this --- InetAdress.getLocalHost();

One example is here:

import java.net.*;
import java.io.*;
import java.applet.*;

public class GetClientIP extends Applet {
  public void init() {
    try {
     InetAddress thisIp =
        InetAddress.getLocalHost();
     System.out.println("IP:"+thisIp.getHostAddress());
     }
    catch(Exception e) {
     e.printStackTrace();
     }
    }
}

Former Member
0 Kudos

Hi Eric,

thanks for the response. This however returns the server- side IP Adress, not the client's IP.

Best regards,

Kev

Former Member
0 Kudos

Ah, okay.

Thinking of one possible (and maybe not the best) workaround...

What if you get the ip address in javascript instead, since that whould be running on the client's machine?

Then, based on that value, redirect to the correct iview instead of/in combination with the pcd filter?