Results 1 to 3 of 3

Thread: How to find out the containing webapp url

  1. #1
    Join Date
    Oct 2009
    Posts
    5

    Default How to find out the containing webapp url

    Hello,

    I have a webapp that have plain servlets as front end and spring for middle tier. Is there a way to find out from spring which is the url where the web app runs?

    Thanks,
    Stefan.

  2. #2
    Join Date
    Jul 2009
    Location
    PUNE
    Posts
    43

    Cool RE:How to find out the containing webapp url

    Hello Stefan,

    You can use RequestContextHolder class to fetch RequestAttributes in your business objects to find out the currently requested URL. You can also configure following interceptor in xxxx-servlet.xml which will expose the request/session attributes via a ThreadLocal.

    RequestContextInterceptor.java
    Code:
    /*------------------------------------------------------------------------------
     * PACKAGE  : org.freeware.bizapps.core.security
     * FILE     : RequestContextInterceptor.java
     * CREATED  : Mar 10, 2009 7:39:03 PM
     * COPYRIGHT: Copyright (c) 2008, Prasad P. Khandekar
     *----------------------------------------------------------------------------*/
    packageorg.freeware.bizapps.core.security;
    import java.io.IOException;
    import java.sql.Timestamp;
    import java.util.ArrayList;
    import java.util.List;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    /**
     * This intercepter is responsible for populating information from HTTPRequest and HTTPSession
     * @author Prasad P. Khandekar
     * @verion $Id$
     * @since 1.0
     */
    public final class RequestContextInterceptor 
    extends HandlerInterceptorAdapter
    {
     private static final Log _log = LogFactory.getLog(RequestContextManagerInterceptor.class);
     private List<String> _lstRequestParams  = new ArrayList<String>(10);
     private List<String> _lstSessionParams  = new ArrayList<String>(10);
     public void setRequestParameterKeys(String[] keys)
     {
      if (null == keys) return;
      _lstRequestParams.clear();
      for (String key : keys)
       _lstRequestParams.add(key);
     }
     public void setSessionParameterKeys(String[] keys)
     {
      if (null == keys) return;
      _lstSessionParams.clear();
      for (String key : keys)
       _lstSessionParams.add(key);
     }
     /* (non-Javadoc)
      * @see HandlerInterceptorAdapter#postHandle(HttpServletRequest, HttpServletResponse, 
      *            Object, ModelAndView)
      */
     @Override
     public void postHandle(HttpServletRequest request,
         HttpServletResponse response, Object handler,
         ModelAndView modelAndView) throws Exception
     {
      super.postHandle(request, response, handler, modelAndView);
     }
     /* (non-Javadoc)
      * @see HandlerInterceptorAdapter#preHandle(HttpServletRequest, HttpServletResponse, 
      *            Object)
      */
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
            Object handler) throws Exception
     {
      Object tmp = null;
      HttpSession ses = null;
      RequestContext ctx = null;
      if (_log.isInfoEnabled())
       _log.info("Checking for session!");
      ses = request.getSession(false);
      ctx = new RequestContext();
      RequestContextHolder.setRequestContext(ctx);
      populateRequestParams(ctx, request);
      if (ses != null)
       populateSessionParams(ctx, ses);
      if (_log.isInfoEnabled())
       _log.info("Setting request context reference in Context Holder!");
      _log.debug(ctx);
      _log.debug("Client Code: " + ctx.getClient());
      _log.debug(RequestContextHolder.getRequestContext());
      return true;
     }
     private void populateSessionParams(RequestContext ctx, HttpSession ses)
     {
      if (_lstSessionParams == null) return;
      for (String key : _lstSessionParams)
       ctx.addReference(key, ses.getAttribute(key));
     }
     private void populateRequestParams(RequestContext ctx,
         HttpServletRequest request)
     {
      if (_lstRequestParams == null) return;
      for (String key : _lstRequestParams)
       ctx.addReference(key, request.getAttribute(key));
     }
    }
    RequestContextHolder.java
    Code:
    /*------------------------------------------------------------------------------
     * PACKAGE  : org.freeware.bizapps.core.security
     * FILE     : RequestContextHolder.java
     * CREATED  : Mar 4, 2009 1:32:56 PM
     * COPYRIGHT: Copyright (c) 2008, Prasad P. Khandekar
     *----------------------------------------------------------------------------*/
    package org.freeware.bizapps.core.security;
    /**
     * The RequestContextHolder is responsible for storing request level information
     * in a manner which enables all business objects to have access to it irrespective
     * of from where the request has originated.  
     * @author Prasad P. Khandekar
     * @verion $Id$
     * @since 1.0
     */
    public final class RequestContextHolder
    {
     private static ThreadLocal<RequestContext> _ctxtHolder = new ThreadLocal<RequestContext>();
        /**
         * Private constructor to prevent instantiation.
         */
        private RequestContextHolder()
        {
        }
        /**
         * @param context the processing context to set
         */
        public static void setRequestContext(RequestContext context)
        {
            _ctxtHolder.set(context);
        }
        /**
         * @return the processing context
         */
        public static RequestContext getRequestContext()
        {
            return _ctxtHolder.get();
        }
    }
    RequestContext.java
    Code:
    /*------------------------------------------------------------------------------
     * PACKAGE  : org.freeware.bizapps.core.security
     * FILE     : RequestContext.java
     * CREATED  : Mar 10, 2009 7:50:10 PM
     * COPYRIGHT: Copyright (c) 2008, Prasad P. Khandekar
     *----------------------------------------------------------------------------*/
    package org.freeware.bizapps.core.security;
    import java.sql.Timestamp;
    import java.util.Date;
    import java.util.LinkedHashMap;
    import java.util.Map;
    /**
     * The request context class instance makes the HTTP request and session parameters
     * available to business objects in transparent manner.
     * @author Prasad P. Khandekar
     * @verion $Id$
     * @since 1.0
     */
    public final class RequestContext implements IRequestContext
    {
     private Timestamp _tsStamp = null;
     private Map<String, Object> _mapParams = null;
     public RequestContext()
     {
      _tsStamp = new Timestamp(System.currentTimeMillis());
     }
     public void addReference(String key, Object value)
     {
      if (null == _mapParams) _mapParams = new LinkedHashMap<String, Object>(10);
      _mapParams.put(key, value);
     }
     public Object getReference(String key)
     {
      if (null == _mapParams)
       return null;
      return _mapParams.get(key);
     }
     public Timestamp getRequestStamp()
     {
      return _tsStamp;
     }
    }
    Regards,
    Prasad P. Khandekar
    Last edited by KPrasadKhan; Oct 6th, 2009 at 03:32 PM. Reason: Added code samples

  3. #3
    Join Date
    Oct 2009
    Posts
    5

    Default

    The original problem is like this. I need to override an url when spring container starts depending on where the app is hosted. So the solution with taking it from request fails, because it's before having any request to it. What about when initializing context? You may find out some property from Servlet's application context called "org.apache.catalina.resources" which contains host and webapp name. but no port(and yes specific to tomcat not to jetty or something else). Can someone help on this?

    Cheers,
    Stefan.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •