private static String getContextPath(ServletContext context) {
   // cette m茅thode retourne le contextPath de la webapp
   // en utilisant ServletContext.getContextPath si servlet api 2.5
   // ou en se d茅brouillant sinon
   // (on n'a pas encore pour l'instant de request pour appeler HttpServletRequest.getContextPath)
   if (context.getMajorVersion() == 2 && context.getMinorVersion() >= 5
       || context.getMajorVersion() > 2) {
     // api servlet 2.5 (Java EE 5) minimum pour appeler ServletContext.getContextPath
     return context.getContextPath();
   }
   URL webXmlUrl;
   try {
     webXmlUrl = context.getResource("/WEB-INF/web.xml");
   } catch (final MalformedURLException e) {
     throw new IllegalStateException(e);
   }
   String contextPath = webXmlUrl.toExternalForm();
   contextPath = contextPath.substring(0, contextPath.indexOf("/WEB-INF/web.xml"));
   final int indexOfWar = contextPath.indexOf(".war");
   if (indexOfWar > 0) {
     contextPath = contextPath.substring(0, indexOfWar);
   }
   // tomcat peut renvoyer une url commen莽ant pas "jndi:/localhost"
   // (v5.5.28, webapp dans un r茅pertoire)
   if (contextPath.startsWith("jndi:/localhost")) {
     contextPath = contextPath.substring("jndi:/localhost".length());
   }
   final int lastIndexOfSlash = contextPath.lastIndexOf('/');
   if (lastIndexOfSlash != -1) {
     contextPath = contextPath.substring(lastIndexOfSlash);
   }
   return contextPath;
 }
Exemple #2
0
  /** @see javax.faces.context.ExternalContext#getContextName() */
  public String getContextName() {

    if (servletContext.getMajorVersion() >= 3
        || (servletContext.getMajorVersion() == 2 && servletContext.getMinorVersion() == 5)) {
      return this.servletContext.getServletContextName();
    } else {
      // for servlet 2.4 support
      return servletContext.getServletContextName();
    }
  }
  public void init(FilterConfig filterConfig) throws ServletException {
    boolean jetty_7_or_greater =
        "org.eclipse.jetty.servlet".equals(filterConfig.getClass().getPackage().getName());
    _context = filterConfig.getServletContext();

    String param = filterConfig.getInitParameter("debug");
    _debug = param != null && Boolean.parseBoolean(param);
    if (_debug) __debug = true;

    param = filterConfig.getInitParameter("jetty6");
    if (param == null) param = filterConfig.getInitParameter("partial");
    if (param != null) _jetty6 = Boolean.parseBoolean(param);
    else _jetty6 = ContinuationSupport.__jetty6 && !jetty_7_or_greater;

    param = filterConfig.getInitParameter("faux");
    if (param != null) _faux = Boolean.parseBoolean(param);
    else _faux = !(jetty_7_or_greater || _jetty6 || _context.getMajorVersion() >= 3);

    _filtered = _faux || _jetty6;
    if (_debug)
      _context.log(
          "ContinuationFilter "
              + " jetty="
              + jetty_7_or_greater
              + " jetty6="
              + _jetty6
              + " faux="
              + _faux
              + " filtered="
              + _filtered
              + " servlet3="
              + ContinuationSupport.__servlet3);
    _initialized = true;
  }
Exemple #4
0
 private static String getServletContextIdentifier(ServletContext context) {
   if (context.getMajorVersion() == 2 && context.getMinorVersion() < 5) {
     return context.getServletContextName();
   } else {
     return context.getContextPath();
   }
 }
  protected final void logServerInfo(@Nonnull final ServletContext aSC) {
    // Print Java and Server (e.g. Tomcat) info
    s_aLogger.info(
        "Java "
            + SystemProperties.getJavaVersion()
            + " running '"
            + aSC.getServletContextName()
            + "' on "
            + aSC.getServerInfo()
            + " with "
            + (Runtime.getRuntime().maxMemory() / CGlobal.BYTES_PER_MEGABYTE)
            + "MB max RAM and Servlet API "
            + aSC.getMajorVersion()
            + "."
            + aSC.getMinorVersion());

    // Tell them to use the server VM if possible:
    final EJVMVendor eJVMVendor = EJVMVendor.getCurrentVendor();
    if (eJVMVendor.isSun() && eJVMVendor != EJVMVendor.SUN_SERVER)
      s_aLogger.warn(
          "Consider using the Sun Server Runtime by specifiying '-server' on the commandline!");

    if (SystemProperties.getJavaVersion().startsWith("1.6.0_14"))
      s_aLogger.warn(
          "This Java version is bad for development - breakpoints don't work in the debugger!");

    if (getClass().desiredAssertionStatus())
      s_aLogger.warn("Java assertions are enabled - this should be disabled in production!");
  }
Exemple #6
0
  /** @return the name of this application */
  public String getServletContextName() {

    if (servletContext.getMajorVersion() == 2 && servletContext.getMinorVersion() <= 4) {
      return servletContext.getServletContextName();
    } else {
      return servletContext.getContextPath();
    }
  }
 @Test
 public void versions() {
   ServletContext context = new ServletContext1(new MockWebContext());
   assertEquals(3, context.getMajorVersion());
   assertEquals(0, context.getMinorVersion());
   assertEquals(3, context.getEffectiveMajorVersion());
   assertEquals(0, context.getEffectiveMinorVersion());
 }
  /** Initialisation. */
  @Before
  public void setUp() {
    Utils.initialize();
    try {
      final Field field = MonitoringFilter.class.getDeclaredField("instanceCreated");
      field.setAccessible(true);
      field.set(null, false);
    } catch (final IllegalAccessException e) {
      throw new IllegalStateException(e);
    } catch (final NoSuchFieldException e) {
      throw new IllegalStateException(e);
    }
    final ServletContext parametersContext = createNiceMock(ServletContext.class);
    expect(parametersContext.getMajorVersion()).andReturn(2).anyTimes();
    expect(parametersContext.getMinorVersion()).andReturn(5).anyTimes();
    expect(parametersContext.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
    expect(parametersContext.getServletContextName()).andReturn("test webapp").anyTimes();
    expect(parametersContext.getServerInfo()).andReturn("mock").anyTimes();
    replay(parametersContext);
    Parameters.initialize(parametersContext);
    verify(parametersContext);

    final ServletConfig config = createNiceMock(ServletConfig.class);
    final ServletContext context = createNiceMock(ServletContext.class);
    expect(config.getServletContext()).andReturn(context).anyTimes();
    // anyTimes sur getInitParameter car TestJdbcDriver a pu fixer la propriété système à false
    expect(
            context.getInitParameter(
                Parameters.PARAMETER_SYSTEM_PREFIX + Parameter.DISABLED.getCode()))
        .andReturn(null)
        .anyTimes();
    expect(config.getInitParameter(Parameter.DISABLED.getCode())).andReturn(null).anyTimes();
    expect(context.getMajorVersion()).andReturn(2).anyTimes();
    expect(context.getMinorVersion()).andReturn(5).anyTimes();
    expect(context.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
    expect(context.getAttribute(ReportServlet.FILTER_CONTEXT_KEY))
        .andReturn(new FilterContext())
        .anyTimes();
    reportServlet = new ReportServlet();
    replay(config);
    replay(context);
    reportServlet.init(config);
    verify(config);
    verify(context);
  }
Exemple #9
0
  private void logPushFilterWarning(ServletContext servletContext) {
    String message;

    if (servletContext.getMajorVersion() >= 3) {
      message =
          "PushFilter has been deprecated, you can remove its declaration in Servlets 3 environment";
    } else {
      message = "PushFilter has been deprecated, you should use PushServlet instead";
    }

    LOGGER.warn(message);
  }
Exemple #10
0
 /**
  * Prepare text.
  *
  * @param request The HTTP request
  * @return Builder of text
  */
 private StringBuilder text(final HttpServletRequest request) {
   final StringBuilder text = new StringBuilder();
   text.append(Logger.format("date: %s\n", new Date()));
   this.append(text, request, "code");
   this.append(text, request, "message");
   this.append(text, request, "exception_type");
   this.append(text, request, "request_uri");
   final ServletContext ctx = this.getServletContext();
   text.append(Logger.format("servlet context path: \"%s\"\n", request.getContextPath()));
   text.append(
       Logger.format(
           "requested: %s (%s) at %s:%d\n",
           request.getRequestURL().toString(),
           request.getMethod(),
           request.getServerName(),
           request.getServerPort()));
   text.append(
       Logger.format(
           "request: %s, %d bytes\n", request.getContentType(), request.getContentLength()));
   text.append(
       Logger.format(
           "remote: %s:%d (%s)\n",
           request.getRemoteAddr(), request.getRemotePort(), request.getRemoteHost()));
   text.append(
       Logger.format(
           "servlet: \"%s\" (API %d.%d) at \"%s\"\n",
           ctx.getServletContextName(),
           ctx.getMajorVersion(),
           ctx.getMinorVersion(),
           ctx.getServerInfo()));
   text.append("headers:\n").append(ExceptionTrap.headers(request)).append('\n');
   text.append(
       Logger.format(
           "exception: %[exception]s\n", request.getAttribute("javax.servlet.error.exception")));
   return text;
 }
Exemple #11
0
 /**
  * Parse the web.xml for the current application and scan for a FacesServlet entry, if found,
  * set the <code>facesServletPresent</code> property to true.
  *
  * @param context the ServletContext instance for this application
  */
 private void scanForFacesServlet(ServletContext context) {
   InputStream in = context.getResourceAsStream(WEB_XML_PATH);
   if (in == null) {
     if (context.getMajorVersion() < 3) {
       throw new ConfigurationException("no web.xml present");
     }
   }
   SAXParserFactory factory = getConfiguredFactory();
   if (in != null) {
     try {
       SAXParser parser = factory.newSAXParser();
       parser.parse(in, new WebXmlHandler());
     } catch (Exception e) {
       warnProcessingError(e, context);
       facesServletPresent = true;
       return;
     } finally {
       if (in != null) {
         try {
           in.close();
         } catch (Exception ioe) {
           // ignored;
         }
       }
     }
   }
   if (!facesServletPresent && context.getMajorVersion() >= 3) {
     ClassLoader cl = Util.getCurrentLoader(this);
     Enumeration<URL> urls;
     try {
       urls = cl.getResources(WEB_FRAGMENT_PATH);
     } catch (IOException ioe) {
       throw new ConfigurationException(ioe);
     }
     if (urls != null) {
       while (urls.hasMoreElements() && !facesServletPresent) {
         InputStream fragmentStream = null;
         try {
           URL url = urls.nextElement();
           URLConnection conn = url.openConnection();
           conn.setUseCaches(false);
           fragmentStream = conn.getInputStream();
           SAXParser parser = factory.newSAXParser();
           parser.parse(fragmentStream, new WebXmlHandler());
         } catch (Exception e) {
           warnProcessingError(e, context);
           facesServletPresent = true;
           return;
         } finally {
           if (fragmentStream != null) {
             try {
               fragmentStream.close();
             } catch (IOException ioe) {
               // ignore
             }
           }
         }
       }
     }
   }
 } // END scanForFacesServlet
 @Override
 public int getMajorVersion() {
   return proxy.getMajorVersion();
 }
 public void init(FilterConfig filterConfig) throws ServletException {
   servletContext = filterConfig.getServletContext();
   servletMajorVersion = servletContext.getMajorVersion();
 }
 public int getMajorVersion() {
   return delegate.getMajorVersion();
 }
Exemple #15
0
 public static boolean isGreaterThan(ServletContext context, int majorVersion, int minorVersion) {
   int contextMajorVersion = context.getMajorVersion();
   int contextMinorVersion = context.getMinorVersion();
   return (contextMajorVersion > majorVersion)
       || (contextMajorVersion == majorVersion && contextMinorVersion > minorVersion);
 }
 @Override
 public int getMajorVersion() {
   return sc.getMajorVersion();
 }
 public int getMajorVersion() {
   return servletContext.getMajorVersion();
 }