Hi Ron,

I think this is safer:

import org.apache.jetspeed.request.RequestContext;

RequestContext rc = (RequestContext) 
renderRequest.getAttribute(RequestContext.REQUEST_PORTALENV);
String remoteAddr = rc.getRequest().getRemoteAddr();

The property path you used, 
"requestContext.servletRequest.request.request.remoteAddr", could be dangerous 
as well, depending on servlet containers or some other factors.

Regards,

Woonsan


--- On Mon, 6/29/09, Ron McNulty <rmcnu...@xtra.co.nz> wrote:

> From: Ron McNulty <rmcnu...@xtra.co.nz>
> Subject: Re: Getting the client IP address - Solved
> To: "Jetspeed Users List" <jetspeed-user@portals.apache.org>
> Date: Monday, June 29, 2009, 6:50 AM
> Hi All
> 
> The following code gets the client IP address using
> reflection. Not very pretty, but it works fine unless you
> install a security manager. It will also break if the
> Jetspeed implementation undergoes a major revision. But for
> now, in the absence of anything more elegant, it works.
> 
> public static void printRemoteAddr(RenderRequest request)
> {
>  try {
>     String remoteAddr = (String)
> findField(request,
> "requestContext.servletRequest.request.request.remoteAddr");
>     System.out.println("Remote addr: " +
> remoteAddr);
>  } catch (Exception e) {
>     e.printStackTrace();
>  }
> }
> 
> /**
>  * Find the value of a field that you normally do not have
> access to using reflection.
>  * The field may have a protected, package or private
> access modifier.
>  *
>  * @param obj The object instance to start with
>  * @param fieldName The name of the field in
> "request.remoteAddr" format
>  * @return The field value
>  * @throws IllegalArgumentException Field does not exist
>  * @throws IllegalAccessException Security manager threw
> you out
>  */
> public static Object findField(Object obj, String
> fieldName)
>  throws IllegalArgumentException, IllegalAccessException {
>    // Step through the path to the field
>   String[] tokens = fieldName.split("\\.");
>    for (String token : tokens) {
>       // Iterate up the inheritance
> hierarchy
>       boolean found = false;
>       Class<?> clazz =
> obj.getClass();
>       while (clazz != null &&
> !found) {
>       try {
>          Field field =
> clazz.getDeclaredField(token);
>          found = true;
>      
>    field.setAccessible(true);
>          obj =
> field.get(obj);
>      } catch (NoSuchFieldException
> nsfex) {
>        clazz =
> clazz.getSuperclass();
>      }
>    }
>    if (!found)
>     throw new IllegalArgumentException(token + "
> is not a field in class " + obj.getClass().getName());
>  }
>   return obj;
> }
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jetspeed-user-unsubscr...@portals.apache.org
> For additional commands, e-mail: jetspeed-user-h...@portals.apache.org
> 
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscr...@portals.apache.org
For additional commands, e-mail: jetspeed-user-h...@portals.apache.org

Reply via email to