Beispiel #1
0
  /**
   * Handle authentication and Proxy'ing
   *
   * @param cb
   * @throws HTTPException
   */
  protected synchronized void setAuthenticationAndProxy(HttpClientBuilder cb) throws HTTPException {
    // First, setup the ssl factory
    cb.setSSLSocketFactory(globalsslfactory);

    // Second, Construct a CredentialsProvider that is
    // the union of the Proxy credentials plus
    // either the global local credentials; local overrides global
    // Unfortunately, we cannot either clone or extract the contents
    // of the client supplied provider, so we are forced (for now)
    // to modify the client supplied provider.

    // Look in the local authcreds for best scope match
    AuthScope bestMatch = HTTPAuthUtil.bestmatch(scope, localcreds.keySet());
    CredentialsProvider cp = null;
    if (bestMatch != null) {
      cp = localcreds.get(bestMatch);
    } else {
      bestMatch = HTTPAuthUtil.bestmatch(scope, globalcreds.keySet());
      if (bestMatch != null) cp = globalcreds.get(bestMatch);
    }
    // Build the proxy credentials and AuthScope
    Credentials proxycreds = null;
    AuthScope proxyscope = null;
    if (proxyuser != null && (httpproxy != null || httpsproxy != null)) {
      if (httpproxy != null) proxyscope = HTTPAuthUtil.hostToAuthScope(httpproxy);
      else // httpsproxy != null
      proxyscope = HTTPAuthUtil.hostToAuthScope(httpsproxy);
      proxycreds = new UsernamePasswordCredentials(proxyuser, proxypwd);
    }
    if (cp == null && proxycreds != null && proxyscope != null) {
      // If client provider is null and proxycreds are not,
      // then use proxycreds alone
      cp = new BasicCredentialsProvider();
      cp.setCredentials(proxyscope, proxycreds);
    } else if (cp != null && proxycreds != null && proxyscope != null) {
      // If client provider is not null and proxycreds are not,
      // then add proxycreds to the client provider
      cp.setCredentials(proxyscope, proxycreds);
    }
    if (cp != null) this.sessioncontext.setCredentialsProvider(cp);
  }
  public void postBatchToPipeline(List docs) throws Exception {
    int numDocs = docs.size();

    int requestId = requestCounter.incrementAndGet();
    ArrayList<String> mutable = null;
    synchronized (this) {
      mutable = new ArrayList<String>(sessions.keySet());
    }

    if (mutable.isEmpty()) {
      // completely hosed ... try to re-establish all sessions
      synchronized (this) {
        try {
          Thread.sleep(2000);
        } catch (InterruptedException ie) {
          Thread.interrupted();
        }

        sessions = establishSessions(originalEndpoints, fusionUser, fusionPass, fusionRealm);
        mutable = new ArrayList<String>(sessions.keySet());
      }
      if (mutable.isEmpty())
        throw new IllegalStateException(
            "No available endpoints! "
                + "Check log for previous errors as to why there are no more endpoints available. This is a fatal error.");
    }

    if (mutable.size() > 1) {
      Exception lastExc = null;

      // try all the endpoints until success is reached ... or we run out of endpoints to try ...
      while (!mutable.isEmpty()) {
        String endpoint = getLbEndpoint(mutable);
        if (endpoint == null) {
          // no more endpoints available ... fail
          if (lastExc != null) {
            log.error(
                "No more endpoints available to retry failed request ("
                    + requestId
                    + ")! raising last seen error: "
                    + lastExc);
            throw lastExc;
          } else {
            throw new RuntimeException(
                "No Fusion pipeline endpoints available to process request "
                    + requestId
                    + "! Check logs for previous errors.");
          }
        }

        if (log.isDebugEnabled())
          log.debug(
              "POSTing batch of "
                  + numDocs
                  + " input docs to "
                  + endpoint
                  + " as request "
                  + requestId);

        Exception retryAfterException =
            postJsonToPipelineWithRetry(endpoint, docs, mutable, lastExc, requestId);
        if (retryAfterException == null) {
          lastExc = null;
          break; // request succeeded ...
        }

        lastExc = retryAfterException; // try next endpoint (if available) after seeing an exception
      }

      if (lastExc != null) {
        // request failed and we exhausted the list of endpoints to try ...
        log.error("Failing request " + requestId + " due to: " + lastExc);
        throw lastExc;
      }

    } else {
      String endpoint = getLbEndpoint(mutable);
      if (log.isDebugEnabled())
        log.debug(
            "POSTing batch of "
                + numDocs
                + " input docs to "
                + endpoint
                + " as request "
                + requestId);

      Exception exc = postJsonToPipelineWithRetry(endpoint, docs, mutable, null, requestId);
      if (exc != null) throw exc;
    }
  }