示例#1
1
  /** Fetch the classloader for the given ApplicationID. */
  static URLClassLoader getUrlClassLoader(ApplicationID appId, Map input) {
    NCube cpCube = getCube(appId, CLASSPATH_CUBE);
    if (cpCube
        == null) { // No sys.classpath cube exists, just create regular GroovyClassLoader with no
                   // URLs set into it.
      // Scope the GroovyClassLoader per ApplicationID
      return getLocalClassloader(appId);
    }

    final String envLevel = SystemUtilities.getExternalVariable("ENV_LEVEL");
    if (!input.containsKey("env")
        && StringUtilities.hasContent(
            envLevel)) { // Add in the 'ENV_LEVEL" environment variable when looking up sys.* cubes,
      // if there was not already an entry for it.
      input.put("env", envLevel);
    }
    if (!input.containsKey("username")) { // same as ENV_LEVEL, add it in if not already there.
      input.put("username", System.getProperty("user.name"));
    }
    Object urlCpLoader = cpCube.getCell(input);

    if (urlCpLoader instanceof URLClassLoader) {
      return (URLClassLoader) urlCpLoader;
    }

    throw new IllegalStateException(
        "If the sys.classpath cube exists it must return a URLClassLoader.");
  }
示例#2
0
 public static String getUserAgent() {
   String localAgent = userAgent.get();
   if (StringUtilities.hasContent(localAgent)) {
     return localAgent;
   }
   return globalUserAgent;
 }
示例#3
0
 public static String getReferrer() {
   String localReferrer = referrer.get();
   if (StringUtilities.hasContent(localReferrer)) {
     return localReferrer;
   }
   return globalReferrer;
 }
示例#4
0
  /**
   * Gets a connection from a url. All getConnection calls should go through this code.
   *
   * @param inCookies Supply cookie Map (received from prior setCookies calls from server)
   * @param input boolean indicating whether this connection will be used for input
   * @param output boolean indicating whether this connection will be used for output
   * @param cache boolean allow caching (be careful setting this to true for non-static retrievals).
   * @return URLConnection established URL connection.
   */
  public static URLConnection getConnection(
      URL url, Map inCookies, boolean input, boolean output, boolean cache, boolean allowAllCerts)
      throws IOException {
    URLConnection c = url.openConnection();
    c.setRequestProperty("Accept-Encoding", "gzip, deflate");
    c.setAllowUserInteraction(false);
    c.setDoOutput(output);
    c.setDoInput(input);
    c.setUseCaches(cache);
    c.setReadTimeout(220000);
    c.setConnectTimeout(45000);

    String ref = getReferrer();
    if (StringUtilities.hasContent(ref)) {
      c.setRequestProperty("Referer", ref);
    }
    String agent = getUserAgent();
    if (StringUtilities.hasContent(agent)) {
      c.setRequestProperty("User-Agent", agent);
    }

    if (c
        instanceof
        HttpURLConnection) { // setFollowRedirects is a static (global) method / setting - resetting
      // it in case other code changed it?
      HttpURLConnection.setFollowRedirects(true);
    }

    if (c instanceof HttpsURLConnection && allowAllCerts) {
      try {
        setNaiveSSLSocketFactory((HttpsURLConnection) c);
      } catch (Exception e) {
        LOG.warn("Could not access '" + url.toString() + "'", e);
      }
    }

    // Set cookies in the HTTP header
    if (inCookies != null) { // [optional] place cookies (JSESSIONID) into HTTP headers
      setCookies(c, inCookies);
    }
    return c;
  }
示例#5
0
  public static Map<String, Object> getSystemParams() {
    final ConcurrentMap<String, Object> params = systemParams;

    if (params != null) {
      return params;
    }

    synchronized (NCubeManager.class) {
      if (systemParams == null) {
        String jsonParams = SystemUtilities.getExternalVariable(NCUBE_PARAMS);
        systemParams = new ConcurrentHashMap<>();

        if (StringUtilities.hasContent(jsonParams)) {
          try {
            systemParams = new ConcurrentHashMap<>(JsonReader.jsonToMaps(jsonParams));
          } catch (Exception e) {
            LOG.warn("Parsing of NCUBE_PARAMS failed.  " + jsonParams);
          }
        }
      }
    }
    return systemParams;
  }