/** * Gets the context path if we are on Servlet 2.5+, or else returns failure description string. */ private String getContextPath() { try { Method m = servletContext.getClass().getMethod("getContextPath", CollectionUtils.EMPTY_CLASS_ARRAY); return (String) m.invoke(servletContext, CollectionUtils.EMPTY_OBJECT_ARRAY); } catch (Throwable e) { return "[can't query before Serlvet 2.5]"; } }
@Test public void testServletContext() { Message m = createMessage(); MessageContext mc = new MessageContextImpl(m); ServletContext request = EasyMock.createMock(ServletContext.class); m.put(AbstractHTTPDestination.HTTP_CONTEXT, request); assertSame(request.getClass(), mc.getServletContext().getClass()); assertSame(request.getClass(), mc.getContext(ServletContext.class).getClass()); }
// Added in Servlet 2.5 public String getContextPath() { try { Method getContextPathMethod = servletContext.getClass().getMethod("getContextPath", (Class<?>[]) null); // $NON-NLS-1$ return (String) getContextPathMethod.invoke(servletContext, (Object[]) null) + proxyContext.getServletPath(); } catch (Exception e) { // ignore } return null; }