Exemplo n.º 1
0
  @Override
  public Resource getResource(String uriInContext) throws MalformedURLException {
    Resource resource = null;
    // Try to get regular resource
    //        replacer.getAutoconfigTempDirectory().getAbsolutePath()
    try {
      resource = getAutoconfigResource(uriInContext);
      if (resource == null) {
        //                resource = super.getResource(uriInContext);
        resource = getResourceFromMavenOrSuper(uriInContext);
      }
    } catch (Exception e) {
      logger.warn("find autoconfig resource error! " + uriInContext, e);
    }
    // If no regular resource exists check for access to /WEB-INF/lib or /WEB-INF/classes
    if ((resource == null || !resource.exists()) && uriInContext != null && webInfClasses != null) {
      String uri = URIUtil.canonicalPath(uriInContext);

      try {
        // Replace /WEB-INF/classes with real classes directory
        if (uri.startsWith(WEB_INF_CLASSES_PREFIX)) {
          Resource res = null;
          int i = 0;
          while (res == null && (i < webInfClasses.size())) {
            String newPath = uri.replace(WEB_INF_CLASSES_PREFIX, webInfClasses.get(i).getPath());
            res = Resource.newResource(newPath);
            if (!res.exists()) {
              res = null;
              i++;
            }
          }
          return res;
        }
        // Return the real jar file for all accesses to
        // /WEB-INF/lib/*.jar
        else if (uri.startsWith(WEB_INF_LIB_PREFIX)) {
          String jarName = uri.replace(WEB_INF_LIB_PREFIX, "");
          if (jarName.startsWith("/") || jarName.startsWith("\\")) jarName = jarName.substring(1);
          if (jarName.length() == 0) return null;
          File jarFile = webInfJarMap.get(jarName);
          if (jarFile != null) return Resource.newResource(jarFile.getPath());

          return null;
        }
      } catch (MalformedURLException e) {
        throw e;
      } catch (IOException e) {
        Log.ignore(e);
      }
    }
    return resource;
  }
  /* ------------------------------------------------------------ */
  private int checkNonce(String nonce, Request request) {
    try {
      byte[] n = B64Code.decode(nonce.toCharArray());
      if (n.length != 24) {
        return -1;
      }

      long ts = 0;
      long sk = _nonceSecret;
      byte[] n2 = new byte[16];
      System.arraycopy(n, 0, n2, 0, 8);
      for (int i = 0; i < 8; i++) {
        n2[8 + i] = (byte) (sk & 0xff);
        sk = sk >> 8;
        ts = (ts << 8) + (0xff & (long) n[7 - i]);
      }

      long age = request.getTimeStamp() - ts;
      if (Log.isDebugEnabled()) {
        Log.debug("age=" + age);
      }

      byte[] hash = null;
      try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(n2, 0, 16);
        hash = md.digest();
      } catch (Exception e) {
        Log.warn(e);
      }

      for (int i = 0; i < 16; i++) {
        if (n[i + 8] != hash[i]) {
          return -1;
        }
      }

      if (_maxNonceAge > 0 && (age < 0 || age > _maxNonceAge)) {
        return 0; // stale
      }

      return 1;
    } catch (Exception e) {
      Log.ignore(e);
    }
    return -1;
  }