Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 340498 - ServletContextAdapter.getContextPath returns incorrect path
Summary: ServletContextAdapter.getContextPath returns incorrect path
Status: RESOLVED FIXED
Alias: None
Product: Equinox
Classification: Eclipse Project
Component: Server-Side (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.7 M7   Edit
Assignee: Simon Kaegi CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-19 15:20 EDT by Scott Lewis CLA
Modified: 2011-03-20 01:52 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Scott Lewis CLA 2011-03-19 15:20:27 EDT
In the this method:

org.eclipse.equinox.http.servlet.internal.ServletContextAdaptor.getContextPath()

is currently this code:

// Added in Servlet 2.5
public String getContextPath() {
  try {
    Method getContextPathMethod = servletContext.getClass().getMethod("getContextPath", null); //$NON-NLS-1$
    return (String) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath();
  } catch (Exception e) {
	// ignore
  }
  return null;
}

Under some conditions (i.e. when using the servletbridge, and calling servletContext.getContextPath() from within the init(ServletConfig config) method of a Servlet), the line with the following:

  return (String) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath();

Can return a bogus context path...because the reflective call to getContextPathMethod.invoke(servletContext,null) will return a non-null path...e.g. "/foo" and the call to proxyContext.getServletPath() will return *null*...meaning that the overall result will be (e.g.):

contextPath = "/foonull"

It seems that the string concat should probably be guarded by a null check for the value returned from proxyContext.getServletPath().
Comment 1 Simon Kaegi CLA 2011-03-19 20:59:18 EDT
Thanks Scott,

We can put a guard but maybe a bit deeper. proxyContext.getServletPath() should probably be returning the empty string instead of null and need to figure out why it isn't.
Comment 2 Scott Lewis CLA 2011-03-19 23:58:10 EDT
(In reply to comment #1)
> Thanks Scott,
> 
> We can put a guard but maybe a bit deeper. proxyContext.getServletPath() should
> probably be returning the empty string instead of null and need to figure out
> why it isn't.

Sounds good...thanks.
Comment 3 Simon Kaegi CLA 2011-03-20 01:52:40 EDT
Looking a bit deeper I can see what's going on. The reason is that until the first "service" call the proxy servlet does not know what the servlet path is. With that said we really want to avoid returning null so if not set we now return "" instead of a null value for servlet path. 

This fix will correctly handle the typical /* mapping case but more complex mappings will continue to run into this problem and should instead either defer using contextpath methods to service calls by using the http.registry lazy init servlet activation (or do something similar themselves). The other perhaps more intrusive alternative is to altogether avoid getServletPath in servlet.init calls but I understand that might not be viable across the board.