Esempio n. 1
0
  /**
   * Return the URL to the resource that is mapped to a specified path. The path must begin with a
   * "/" and is interpreted as relative to the current context root.
   *
   * @param path The path to the desired resource
   * @exception MalformedURLException if the path is not given in the correct form
   */
  public URL getResource(String path) throws MalformedURLException {

    DirContext resources = context.getResources();
    if (resources != null) {
      String fullPath = context.getName() + path;

      // this is the problem. Host must not be null
      String hostName = context.getParent().getName();

      try {
        resources.lookup(path);
        if (System.getSecurityManager() != null) {
          try {
            PrivilegedGetResource dp = new PrivilegedGetResource(hostName, fullPath, resources);
            return (URL) AccessController.doPrivileged(dp);
          } catch (PrivilegedActionException pe) {
            throw pe.getException();
          }
        } else {
          return new URL(
              "jndi",
              null,
              0,
              getJNDIUri(hostName, fullPath),
              new DirContextURLStreamHandler(resources));
        }
      } catch (Exception e) {
        // e.printStackTrace();
      }
    }
    return (null);
  }
Esempio n. 2
0
  /**
   * Return a <code>ServletContext</code> object that corresponds to a specified URI on the server.
   * This method allows servlets to gain access to the context for various parts of the server, and
   * as needed obtain <code>RequestDispatcher</code> objects or resources from the context. The
   * given path must be absolute (beginning with a "/"), and is interpreted based on our virtual
   * host's document root.
   *
   * @param uri Absolute URI of a resource on the server
   */
  public ServletContext getContext(String uri) {

    // Validate the format of the specified argument
    if ((uri == null) || (!uri.startsWith("/"))) return (null);

    // Return the current context if requested
    String contextPath = context.getPath();
    if (!contextPath.endsWith("/")) contextPath = contextPath + "/";
    if ((contextPath.length() > 0) && (uri.startsWith(contextPath))) {
      return (this);
    }

    // Return other contexts only if allowed
    if (!context.getCrossContext()) return (null);
    try {
      Host host = (Host) context.getParent();
      Context child = host.map(uri);
      if (child != null) return (child.getServletContext());
      else return (null);
    } catch (Throwable t) {
      return (null);
    }
  }