コード例 #1
0
  @Override
  public boolean retryRequest(IOException e, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b);

    if (executionCount > maxRetries) {
      retry = false;
    } else if (isInList(exceptionWhiteList, e)) {
      retry = true;
    } else if (isInList(exceptionBlackList, e)) {
      retry = false;
    } else if (!sent) {
      retry = true;
    }

    if (retry) {
      HttpUriRequest request = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
      if (request == null) {
        return false;
      }
    }

    if (retry) {
      SystemClock.sleep(retrySleepTimeMS);
    } else {
      e.printStackTrace();
    }

    return retry;
  }
コード例 #2
0
  static void testProtectedResource() throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpget, localContext);

    AuthState proxyAuthState =
        (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);

    System.out.println("Proxy auth scope: " + proxyAuthState.getAuthScope());
    System.out.println("Proxy auth scheme: " + proxyAuthState.getAuthScheme());
    System.out.println("Proxy auth credentials: " + proxyAuthState.getCredentials());

    AuthState targetAuthState =
        (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
    System.out.println("Target auth scope: " + targetAuthState.getAuthScope());
    System.out.println("Target auth scheme: " + targetAuthState.getAuthScheme());
    System.out.println("Target auth credentials: " + targetAuthState.getCredentials());

    HttpEntity entity = response.getEntity();
    String charset = HttpclientTutorial.getResponseCharset(response);
    System.out.println("charset:" + charset);
    HttpclientTutorial.output(entity, charset);

    EntityUtils.consume(entity);
  }
コード例 #3
0
  @Override
  public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
      // 尝试次数超过用户定义的测试,默认5次
      retry = false;
    } else if (exceptionBlacklist.contains(exception.getClass())) {
      // 线程被用户中断,则不继续尝试
      retry = false;
    } else if (exceptionWhitelist.contains(exception.getClass())) {
      retry = true;
    } else if (!sent) {
      retry = true;
    }

    if (retry) {
      HttpUriRequest currentReq =
          (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
      retry = currentReq != null && !"POST".equals(currentReq.getMethod());
    }

    if (retry) {
      // 休眠1秒钟后再继续尝试
      SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
      exception.printStackTrace();
    }

    return retry;
  }
コード例 #4
0
    public void process(HttpRequest request, HttpContext context)
        throws HttpException, IOException {

      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

      if (request.getFirstHeader("Authorization") != null) {
        // Using OAuth, leave as is
      } else if (request.getFirstHeader("X-No-Auth") != null) {
        // No auth required, leave as is
      } else {
        // If no auth scheme avaialble yet, try to initialize it preemptively
        if (authState.getAuthScheme() == null) {
          AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
          CredentialsProvider credsProvider =
              (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
          HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
          if (authScheme != null) {
            Credentials creds =
                credsProvider.getCredentials(
                    new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
              throw new HttpException("No credentials for preemptive authentication");
            } else {
              authState.setAuthScheme(authScheme);
              authState.setCredentials(creds);
            }
          }
        }
      }
    }
コード例 #5
0
    public void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {

      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

      if (authState.getAuthScheme() != null || authState.hasAuthOptions()) {
        return;
      }

      // If no authState has been established and this is a PUT or POST request, add preemptive
      // authorisation
      String requestMethod = request.getRequestLine().getMethod();
      if (requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) {
        CredentialsProvider credentialsProvider =
            (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        Credentials credentials =
            credentialsProvider.getCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (credentials == null) {
          throw new HttpException("No credentials for preemptive authentication");
        }
        authState.update(authScheme, credentials);
      }
    }
コード例 #6
0
ファイル: HttpUtils.java プロジェクト: Zamite/fcrepo
    public void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {
      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
      if (authState.getAuthScheme() != null) {
        return;
      }

      AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
      if (authScheme == null) {
        return;
      }

      CredentialsProvider credsProvider =
          (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
      HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

      Credentials creds =
          credsProvider.getCredentials(
              new AuthScope(targetHost.getHostName(), targetHost.getPort()));
      if (creds == null) {
        return;
      }

      authState.setAuthScheme(authScheme);
      authState.setCredentials(creds);
    }
コード例 #7
0
 private static String getUrlAfterRedirects(HttpContext context) {
   String lastRedirectUrl = (String) context.getAttribute(LAST_REDIRECT_URL);
   if (lastRedirectUrl != null) return lastRedirectUrl;
   else {
     HttpUriRequest currentReq =
         (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
     HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
     String currentUrl =
         (currentReq.getURI().isAbsolute())
             ? currentReq.getURI().toString()
             : (currentHost.toURI() + currentReq.getURI());
     return currentUrl;
   }
 }
コード例 #8
0
ファイル: Network.java プロジェクト: ndong211/bo
        public void process(final HttpRequest request, final HttpContext context)
            throws HttpException, IOException {
          Locale locale = Locale.getDefault();
          request.setHeader(
              "Accept-Language", (locale.getLanguage() + "-" + locale.getCountry()).toLowerCase());
          request.setHeader("Accept-Encoding", "gzip");
          AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
          // HttpHost targetHost = (HttpHost)
          // context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
          if (true /*(INFO.HOST_IP.equals(targetHost.getHostName())) && (INFO.HOST_PORT == targetHost.getPort())*/) {
            authState.setAuthScheme(new BasicScheme());

            /* username = "******";
            password = "******";*/
            if (username == null) {
              //            		username = appContext.getSharedPreferences(BaseActivity.SETTING_INFOS,
              // Context.MODE_PRIVATE).getString(BaseActivity.NAME, null);
              //            		password = appContext.getSharedPreferences(BaseActivity.SETTING_INFOS,
              // Context.MODE_PRIVATE).getString(BaseActivity.PASSWORD, null);
            }
            // INFO.Log("username", username+"");
            if (username != null) {
              authState.setCredentials(new UsernamePasswordCredentials(username, password));
            }
          }
        }
コード例 #9
0
        @Override
        public boolean retryRequest(
            IOException exception, int executionCount, HttpContext context) {
          // 设置恢复策略,在发生异常时候将自动重试N次
          if (executionCount >= RETRIED_TIME) {
            // Do not retry if over max retry count
            Logger.getLogger().e("Do not retry if over max retry count:" + executionCount);
            return false;
          }

          if (exception instanceof NoHttpResponseException) {
            // Retry if the server dropped connection on us
            Logger.getLogger()
                .i(
                    "Retry if the server dropped connection on us:exception instanceof NoHttpResponseException");
            return true;
          }

          if (exception instanceof SSLHandshakeException) {
            // Do not retry on SSL handshake exception
            Logger.getLogger().e("Do not retry on SSL handshake SSLHandshakeException ");
            return false;
          }

          HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

          boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
          if (!idempotent) {
            // Retry if the request is considered idempotent
            Logger.getLogger().i("Retry if the request is considered idempotent");
            return true;
          }

          return false;
        }
コード例 #10
0
 @Override
 public void process(HttpResponse response, HttpContext context)
     throws HttpException, IOException {
   CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
   for (Header header : response.getAllHeaders()) {
     if (!header.getName().equalsIgnoreCase("Set-Cookie")) {
       continue;
     }
     Matcher matcher = pattern.matcher(header.getValue());
     if (!matcher.find()) {
       continue;
     }
     for (Cookie cookie : cookieStore.getCookies()) {
       if (cookie.getName().equalsIgnoreCase(matcher.group(1))) {
         if (cookie instanceof BasicClientCookie) {
           ((BasicClientCookie) cookie).setValue('"' + cookie.getValue() + '"');
         } else if (cookie instanceof BasicClientCookie2) {
           ((BasicClientCookie2) cookie).setValue('"' + cookie.getValue() + '"');
         } else {
           Log.w(TAG, "unhandled cookie implementation " + cookie.getClass().getName());
         }
         break;
       }
     }
   }
 }
コード例 #11
0
  @Override
  public void process(final HttpRequest request, final HttpContext context)
      throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);

    if (authState.getAuthScheme() == null) {
      Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword);
      authState.update(new BasicScheme(), creds);
    }
  }
コード例 #12
0
ファイル: UrlUtils.java プロジェクト: arowser/androidnews
 public static String getRedirectedUrl(String url) {
   DefaultHttpClient client = new DefaultHttpClient();
   HttpGet get = new HttpGet(url);
   try {
     HttpContext context = new BasicHttpContext();
     HttpResponse response = client.execute(get, context);
     if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
       throw new IOException(response.getStatusLine().toString());
     HttpUriRequest currentReq =
         (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
     HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
     return currentHost.toURI() + currentReq.getURI();
   } catch (ClientProtocolException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return url;
 }
コード例 #13
0
    public void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {

      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
      CredentialsProvider credsProvider =
          (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
      HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

      // If not auth scheme has been initialized yet
      if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
          authState.setAuthScheme(new BasicScheme());
          authState.setCredentials(creds);
        }
      }
    }
コード例 #14
0
  public void process(final HttpRequest request, final HttpContext context)
      throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
      CredentialsProvider credsProvider =
          (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
      HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
      Credentials creds =
          credsProvider.getCredentials(
              new AuthScope(targetHost.getHostName(), targetHost.getPort()));
      if (creds == null) {
        throw new HttpException("No credentials for preemptive authentication");
      }
      authState.setAuthScheme(authScheme);
      authState.setCredentials(creds);
    }
  }
コード例 #15
0
 public void handle(
     final HttpRequest request, final HttpResponse response, final HttpContext context)
     throws HttpException, IOException {
   String creds = (String) context.getAttribute("creds");
   if (creds == null || !creds.equals("test:test")) {
     response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
   } else {
     response.setStatusCode(HttpStatus.SC_OK);
     StringEntity entity = new StringEntity("success", Consts.ASCII);
     response.setEntity(entity);
   }
 }
コード例 #16
0
 public void process(final HttpResponse response, final HttpContext context)
     throws HttpException, IOException {
   AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
   if (authState != null) {
     AuthScheme authScheme = authState.getAuthScheme();
     // Stick the auth scheme to the local context, so
     // we could try to authenticate subsequent requests
     // preemptively
     if (authScheme instanceof DigestScheme) {
       context.setAttribute("preemptive-auth", authScheme);
     }
   }
 }
コード例 #17
0
    public void process(HttpRequest request, HttpContext context)
        throws HttpException, IOException {
      HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

      UserPasswordAuthentication creds;
      synchronized (this) {
        creds = credentials.get(targetHost);
      }

      if (creds != null) {
        AuthScope scope = new AuthScope(targetHost);

        BasicCredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(
            scope, new UsernamePasswordCredentials(creds.getUsername(), creds.getPassword()));
        context.setAttribute(ClientContext.CREDS_PROVIDER, credProvider);
      }
    }
コード例 #18
0
        @Override
        public void process(HttpRequest request, HttpContext context)
            throws HttpException, IOException {
          BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
          cookie.setVersion(0);
          cookie.setPath("/");
          cookie.setDomain(jetty.getBaseUrl().getHost());

          CookieStore cookieStore = new BasicCookieStore();
          CookieSpecRegistry registry =
              (CookieSpecRegistry) context.getAttribute(ClientContext.COOKIESPEC_REGISTRY);
          String policy = HttpClientParams.getCookiePolicy(request.getParams());
          CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
          // Add the cookies to the request
          List<Header> headers = cookieSpec.formatCookies(Collections.singletonList(cookie));
          for (Header header : headers) {
            request.addHeader(header);
          }
          context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
          context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec);
        }
コード例 #19
0
 @Override
 public boolean isRedirected(HttpRequest request, HttpResponse res, HttpContext context)
     throws ProtocolException {
   Header header = res.getFirstHeader("Location");
   // HttpClientUtil.printResponseHeaders(res) ;O
   if (header != null) {
     String location = header.getValue();
     if (location.indexOf("://") > 0) {
       String site = (String) context.getAttribute("crawler.site");
       int idx = location.indexOf("://");
       String hostName = location.substring(idx + 3);
       idx = hostName.indexOf('/');
       if (idx > 0) {
         hostName = hostName.substring(0, idx);
       }
       context.setAttribute("url.redirect", location);
       if (!hostName.endsWith(site)) return false;
     }
   }
   return super.isRedirected(request, res, context);
 }
コード例 #20
0
ファイル: Spider.java プロジェクト: jwdstef/addison-jweb
  // 收集文章
  public List<Topic> collect(String url) {

    // 创建HttpClient
    this.httpclient = new DefaultHttpClient();
    this.context = new BasicHttpContext();
    this.url = url;

    // 设置网络代理
    //		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
    //				new HttpHost("192.168.1.1", 808));

    // 执行收集过程
    execute();

    httpclient.getConnectionManager().shutdown();

    // 获取收集到的文章
    List<Topic> articles = (List<Topic>) context.getAttribute("articles");

    // 返回文章列表
    return articles;
  }
コード例 #21
0
ファイル: FinalHttp.java プロジェクト: newisso/afinal
 public boolean retryRequest(
     IOException exception, int executionCount, HttpContext context) {
   if (executionCount >= 3) {
     // Do not retry if over max retry count
     return false;
   }
   if (exception instanceof NoHttpResponseException) {
     // Retry if the server dropped connection on us
     return true;
   }
   if (exception instanceof SSLHandshakeException) {
     // Do not retry on SSL handshake exception
     return false;
   }
   HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
   boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
   if (!idempotent) {
     // Retry if the request is considered idempotent
     return true;
   }
   return false;
 }
コード例 #22
0
  @Override
  protected int execute() {
    FileDownloadWithContextTask task =
        new FileDownloadWithContextTask(mContext, apiClient.getDownloadUri("books", mId), bookUri);
    task.setDownloadProgressListener(
        new FileDownloadTask.DownloadProgressListener() {
          @Override
          public void onProgressUpdate(int progress) {
            updateProgress(progress);
          }
        });

    int status = getDownloadStatus(task.execute());
    if (status == SUCCESS) {
      HttpContext httpContext = task.getHttpContext();
      HttpUriRequest request =
          (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);

      Uri uri = Uri.parse(request.getURI().toString());
      updateFileName(uri.getLastPathSegment());
    }
    return status;
  }
コード例 #23
0
ファイル: ApiServer.java プロジェクト: ngtuna/cloudstack-gre
  // NOTE: handle() only handles over the wire (OTW) requests from integration.api.port 8096
  // If integration api port is not configured, actual OTW requests will be received by ApiServlet
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  public void handle(HttpRequest request, HttpResponse response, HttpContext context)
      throws HttpException, IOException {

    // Create StringBuffer to log information in access log
    StringBuffer sb = new StringBuffer();
    HttpServerConnection connObj = (HttpServerConnection) context.getAttribute("http.connection");
    if (connObj instanceof SocketHttpServerConnection) {
      InetAddress remoteAddr = ((SocketHttpServerConnection) connObj).getRemoteAddress();
      sb.append(remoteAddr.toString() + " -- ");
    }
    sb.append(StringUtils.cleanString(request.getRequestLine().toString()));

    try {
      List<NameValuePair> paramList = null;
      try {
        paramList = URLEncodedUtils.parse(new URI(request.getRequestLine().getUri()), "UTF-8");
      } catch (URISyntaxException e) {
        s_logger.error("Error parsing url request", e);
      }

      // Use Multimap as the parameter map should be in the form (name=String, value=String[])
      // So parameter values are stored in a list for the same name key
      // APITODO: Use Guava's (import com.google.common.collect.Multimap;)
      // (Immutable)Multimap<String, String> paramMultiMap = HashMultimap.create();
      // Map<String, Collection<String>> parameterMap = paramMultiMap.asMap();
      Map parameterMap = new HashMap<String, String[]>();
      String responseType = BaseCmd.RESPONSE_TYPE_XML;
      for (NameValuePair param : paramList) {
        if (param.getName().equalsIgnoreCase("response")) {
          responseType = param.getValue();
          continue;
        }
        parameterMap.put(param.getName(), new String[] {param.getValue()});
      }

      // Get the type of http method being used.
      parameterMap.put("httpmethod", new String[] {request.getRequestLine().getMethod()});

      // Check responseType, if not among valid types, fallback to JSON
      if (!(responseType.equals(BaseCmd.RESPONSE_TYPE_JSON)
          || responseType.equals(BaseCmd.RESPONSE_TYPE_XML))) {
        responseType = BaseCmd.RESPONSE_TYPE_XML;
      }

      try {
        // always trust commands from API port, user context will always be
        // UID_SYSTEM/ACCOUNT_ID_SYSTEM
        CallContext.register(_accountMgr.getSystemUser(), _accountMgr.getSystemAccount());
        sb.insert(
            0,
            "(userId="
                + User.UID_SYSTEM
                + " accountId="
                + Account.ACCOUNT_ID_SYSTEM
                + " sessionId="
                + null
                + ") ");
        String responseText = handleRequest(parameterMap, responseType, sb);
        sb.append(" 200 " + ((responseText == null) ? 0 : responseText.length()));

        writeResponse(response, responseText, HttpStatus.SC_OK, responseType, null);
      } catch (ServerApiException se) {
        String responseText = getSerializedApiError(se, parameterMap, responseType);
        writeResponse(
            response,
            responseText,
            se.getErrorCode().getHttpCode(),
            responseType,
            se.getDescription());
        sb.append(" " + se.getErrorCode() + " " + se.getDescription());
      } catch (RuntimeException e) {
        // log runtime exception like NullPointerException to help identify the source easier
        s_logger.error("Unhandled exception, ", e);
        throw e;
      }
    } finally {
      s_accessLogger.info(sb.toString());
      CallContext.unregister();
    }
  }
コード例 #24
0
  public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);

      // special case to handle WSDLs
      if (protocolState == ProtocolState.WSDL_RESPONSE_DONE) {
        // we need to shut down if the shutdown flag is set
        HttpContext context = conn.getContext();
        ContentOutputBuffer outBuf =
            (ContentOutputBuffer) context.getAttribute("synapse.response-source-buffer");
        int bytesWritten = outBuf.produceContent(encoder);
        if (metrics != null && bytesWritten > 0) {
          metrics.incrementBytesSent(bytesWritten);
        }

        conn.requestInput();
        if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) {
          sourceConfiguration.getSourceConnections().releaseConnection(conn);
        }

        return;
      }

      if (protocolState != ProtocolState.RESPONSE_HEAD
          && protocolState != ProtocolState.RESPONSE_BODY) {
        log.warn(
            "Illegal incoming connection state: "
                + protocolState
                + " . Possibly two send backs "
                + "are happening for the same request");

        handleInvalidState(conn, "Trying to write response body");
        return;
      }

      SourceContext.updateState(conn, ProtocolState.RESPONSE_BODY);

      SourceResponse response = SourceContext.getResponse(conn);

      int bytesSent = response.write(conn, encoder);

      if (encoder.isCompleted()) {
        HttpContext context = conn.getContext();
        if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null
            && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null
            && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) {

          if (latencyView != null) {
            latencyView.notifyTimes(
                (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME),
                (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME),
                (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME),
                System.currentTimeMillis());
          } else if (s2sLatencyView != null) {
            s2sLatencyView.notifyTimes(
                (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME),
                (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME),
                (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME),
                System.currentTimeMillis());
          }
        }

        context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME);
        context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME);
        context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME);
      }

      metrics.incrementBytesSent(bytesSent);
    } catch (IOException e) {
      logIOException(conn, e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
コード例 #25
0
ファイル: PushRemoteWrapper.java プロジェクト: shailua/gsn
  public DataField[] registerAndGetStructure() throws IOException, ClassNotFoundException {
    // Create the POST request
    HttpPost httpPost =
        new HttpPost(initParams.getRemoteContactPointEncoded(lastReceivedTimestamp));
    // Add the POST parameters
    httpPost.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8));
    //
    httpPost.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
    // Create local execution context
    HttpContext localContext = new BasicHttpContext();
    //
    NotificationRegistry.getInstance().addNotification(uid, this);
    int tries = 0;
    AuthState authState = null;
    //
    while (tries < 2) {
      tries++;
      HttpResponse response = null;
      try {
        // Execute the POST request
        response = httpclient.execute(httpPost, localContext);
        //
        int sc = response.getStatusLine().getStatusCode();
        //
        if (sc == HttpStatus.SC_OK) {
          logger.debug(
              new StringBuilder()
                  .append("Wants to consume the structure packet from ")
                  .append(initParams.getRemoteContactPoint())
                  .toString());
          structure = (DataField[]) XSTREAM.fromXML(response.getEntity().getContent());
          logger.debug("Connection established for: " + initParams.getRemoteContactPoint());
          break;
        } else {
          if (sc == HttpStatus.SC_UNAUTHORIZED)
            authState =
                (AuthState)
                    localContext.getAttribute(
                        ClientContext.TARGET_AUTH_STATE); // Target host authentication required
          else if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED)
            authState =
                (AuthState)
                    localContext.getAttribute(
                        ClientContext.PROXY_AUTH_STATE); // Proxy authentication required
          else {
            logger.error(
                new StringBuilder()
                    .append("Unexpected POST status code returned: ")
                    .append(sc)
                    .append("\nreason: ")
                    .append(response.getStatusLine().getReasonPhrase())
                    .toString());
          }
          if (authState != null) {
            if (initParams.getUsername() == null
                || (tries > 1 && initParams.getUsername() != null)) {
              logger.error(
                  "A valid username/password required to connect to the remote host: "
                      + initParams.getRemoteContactPoint());
            } else {

              AuthScope authScope = authState.getAuthScope();
              logger.warn(
                  new StringBuilder()
                      .append("Setting Credentials for host: ")
                      .append(authScope.getHost())
                      .append(":")
                      .append(authScope.getPort())
                      .toString());
              Credentials creds =
                  new UsernamePasswordCredentials(
                      initParams.getUsername(), initParams.getPassword());
              httpclient.getCredentialsProvider().setCredentials(authScope, creds);
            }
          }
        }
      } catch (RuntimeException ex) {
        // In case of an unexpected exception you may want to abort
        // the HTTP request in order to shut down the underlying
        // connection and release it back to the connection manager.
        logger.warn("Aborting the HTTP POST request.");
        httpPost.abort();
        throw ex;
      } finally {
        if (response != null && response.getEntity() != null) {
          response.getEntity().consumeContent();
        }
      }
    }

    if (structure == null) throw new RuntimeException("Cannot connect to the remote host.");

    return structure;
  }
コード例 #26
0
  @Override
  public Header authenticate(
      final Credentials credentials, final HttpRequest request, final HttpContext context)
      throws AuthenticationException {
    Args.notNull(request, "HTTP request");
    switch (state) {
      case UNINITIATED:
        throw new AuthenticationException(
            getSchemeName() + " authentication has not been initiated");
      case FAILED:
        throw new AuthenticationException(getSchemeName() + " authentication has failed");
      case CHALLENGE_RECEIVED:
        try {
          final HttpRoute route = (HttpRoute) context.getAttribute(HttpClientContext.HTTP_ROUTE);
          if (route == null) {
            throw new AuthenticationException("Connection route is not available");
          }
          HttpHost host;
          if (isProxy()) {
            host = route.getProxyHost();
            if (host == null) {
              host = route.getTargetHost();
            }
          } else {
            host = route.getTargetHost();
          }
          final String authServer;
          String hostname = host.getHostName();

          if (this.useCanonicalHostname) {
            try {
              // TODO: uncomment this statement and delete the resolveCanonicalHostname,
              // TODO: as soon canonical hostname resolving is implemented in the
              // SystemDefaultDnsResolver
              // final DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
              // hostname = dnsResolver.resolveCanonicalHostname(host.getHostName());
              hostname = resolveCanonicalHostname(hostname);
            } catch (UnknownHostException ignore) {
            }
          }
          if (this.stripPort) { // || host.getPort()==80 || host.getPort()==443) {
            authServer = hostname;
          } else {
            authServer = hostname + ":" + host.getPort();
          }

          if (log.isDebugEnabled()) {
            log.debug("init " + authServer);
          }
          token = generateToken(token, authServer, credentials);
          state = State.TOKEN_GENERATED;
        } catch (final GSSException gsse) {
          state = State.FAILED;
          if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL
              || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED) {
            throw new InvalidCredentialsException(gsse.getMessage(), gsse);
          }
          if (gsse.getMajor() == GSSException.NO_CRED) {
            throw new InvalidCredentialsException(gsse.getMessage(), gsse);
          }
          if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN
              || gsse.getMajor() == GSSException.DUPLICATE_TOKEN
              || gsse.getMajor() == GSSException.OLD_TOKEN) {
            throw new AuthenticationException(gsse.getMessage(), gsse);
          }
          // other error
          throw new AuthenticationException(gsse.getMessage());
        }
      case TOKEN_GENERATED:
        final String tokenstr = new String(base64codec.encode(token));
        if (log.isDebugEnabled()) {
          log.debug("Sending response '" + tokenstr + "' back to the auth server");
        }
        final CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (isProxy()) {
          buffer.append(AUTH.PROXY_AUTH_RESP);
        } else {
          buffer.append(AUTH.WWW_AUTH_RESP);
        }
        buffer.append(": Negotiate ");
        buffer.append(tokenstr);
        return new BufferedHeader(buffer);
      default:
        throw new IllegalStateException("Illegal state: " + state);
    }
  }
コード例 #27
0
  // non-javadoc, see interface ClientRequestDirector
  public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
      throws HttpException, IOException {

    HttpRequest orig = request;
    RequestWrapper origWrapper = wrapRequest(orig);
    origWrapper.setParams(params);
    HttpRoute origRoute = determineRoute(target, origWrapper, context);

    RoutedRequest roureq = new RoutedRequest(origWrapper, origRoute);

    long timeout = ConnManagerParams.getTimeout(params);

    int execCount = 0;

    boolean reuse = false;
    HttpResponse response = null;
    boolean done = false;
    try {
      while (!done) {
        // In this loop, the RoutedRequest may be replaced by a
        // followup request and route. The request and route passed
        // in the method arguments will be replaced. The original
        // request is still available in 'orig'.

        RequestWrapper wrapper = roureq.getRequest();
        HttpRoute route = roureq.getRoute();

        // See if we have a user token bound to the execution context
        Object userToken = context.getAttribute(ClientContext.USER_TOKEN);

        // Allocate connection if needed
        if (managedConn == null) {
          ClientConnectionRequest connRequest = connManager.requestConnection(route, userToken);
          if (orig instanceof AbortableHttpRequest) {
            ((AbortableHttpRequest) orig).setConnectionRequest(connRequest);
          }

          try {
            managedConn = connRequest.getConnection(timeout, TimeUnit.MILLISECONDS);
          } catch (InterruptedException interrupted) {
            InterruptedIOException iox = new InterruptedIOException();
            iox.initCause(interrupted);
            throw iox;
          }

          if (HttpConnectionParams.isStaleCheckingEnabled(params)) {
            // validate connection
            this.log.debug("Stale connection check");
            if (managedConn.isStale()) {
              this.log.debug("Stale connection detected");
              // BEGIN android-changed
              try {
                managedConn.close();
              } catch (IOException ignored) {
                // SSLSocket's will throw IOException
                // because they can't send a "close
                // notify" protocol message to the
                // server. Just supresss any
                // exceptions related to closing the
                // stale connection.
              }
              // END android-changed
            }
          }
        }

        if (orig instanceof AbortableHttpRequest) {
          ((AbortableHttpRequest) orig).setReleaseTrigger(managedConn);
        }

        // Reopen connection if needed
        if (!managedConn.isOpen()) {
          managedConn.open(route, context, params);
        }
        // BEGIN android-added
        else {
          // b/3241899 set the per request timeout parameter on reused connections
          managedConn.setSocketTimeout(HttpConnectionParams.getSoTimeout(params));
        }
        // END android-added

        try {
          establishRoute(route, context);
        } catch (TunnelRefusedException ex) {
          if (this.log.isDebugEnabled()) {
            this.log.debug(ex.getMessage());
          }
          response = ex.getResponse();
          break;
        }

        // Reset headers on the request wrapper
        wrapper.resetHeaders();

        // Re-write request URI if needed
        rewriteRequestURI(wrapper, route);

        // Use virtual host if set
        target = (HttpHost) wrapper.getParams().getParameter(ClientPNames.VIRTUAL_HOST);

        if (target == null) {
          target = route.getTargetHost();
        }

        HttpHost proxy = route.getProxyHost();

        // Populate the execution context
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
        context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
        context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn);
        context.setAttribute(ClientContext.TARGET_AUTH_STATE, targetAuthState);
        context.setAttribute(ClientContext.PROXY_AUTH_STATE, proxyAuthState);

        // Run request protocol interceptors
        requestExec.preProcess(wrapper, httpProcessor, context);

        context.setAttribute(ExecutionContext.HTTP_REQUEST, wrapper);

        boolean retrying = true;
        while (retrying) {
          // Increment total exec count (with redirects)
          execCount++;
          // Increment exec count for this particular request
          wrapper.incrementExecCount();
          if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) {
            throw new NonRepeatableRequestException(
                "Cannot retry request " + "with a non-repeatable request entity");
          }

          try {
            if (this.log.isDebugEnabled()) {
              this.log.debug("Attempt " + execCount + " to execute request");
            }
            response = requestExec.execute(wrapper, managedConn, context);
            retrying = false;

          } catch (IOException ex) {
            this.log.debug("Closing the connection.");
            managedConn.close();
            if (retryHandler.retryRequest(ex, execCount, context)) {
              if (this.log.isInfoEnabled()) {
                this.log.info(
                    "I/O exception ("
                        + ex.getClass().getName()
                        + ") caught when processing request: "
                        + ex.getMessage());
              }
              if (this.log.isDebugEnabled()) {
                this.log.debug(ex.getMessage(), ex);
              }
              this.log.info("Retrying request");
            } else {
              throw ex;
            }

            // If we have a direct route to the target host
            // just re-open connection and re-try the request
            if (route.getHopCount() == 1) {
              this.log.debug("Reopening the direct connection.");
              managedConn.open(route, context, params);
            } else {
              // otherwise give up
              throw ex;
            }
          }
        }

        // Run response protocol interceptors
        response.setParams(params);
        requestExec.postProcess(response, httpProcessor, context);

        // The connection is in or can be brought to a re-usable state.
        reuse = reuseStrategy.keepAlive(response, context);
        if (reuse) {
          // Set the idle duration of this connection
          long duration = keepAliveStrategy.getKeepAliveDuration(response, context);
          managedConn.setIdleDuration(duration, TimeUnit.MILLISECONDS);
        }

        RoutedRequest followup = handleResponse(roureq, response, context);
        if (followup == null) {
          done = true;
        } else {
          if (reuse) {
            this.log.debug("Connection kept alive");
            // Make sure the response body is fully consumed, if present
            HttpEntity entity = response.getEntity();
            if (entity != null) {
              entity.consumeContent();
            }
            // entity consumed above is not an auto-release entity,
            // need to mark the connection re-usable explicitly
            managedConn.markReusable();
          } else {
            managedConn.close();
          }
          // check if we can use the same connection for the followup
          if (!followup.getRoute().equals(roureq.getRoute())) {
            releaseConnection();
          }
          roureq = followup;
        }

        userToken = this.userTokenHandler.getUserToken(context);
        context.setAttribute(ClientContext.USER_TOKEN, userToken);
        if (managedConn != null) {
          managedConn.setState(userToken);
        }
      } // while not done

      // check for entity, release connection if possible
      if ((response == null)
          || (response.getEntity() == null)
          || !response.getEntity().isStreaming()) {
        // connection not needed and (assumed to be) in re-usable state
        if (reuse) managedConn.markReusable();
        releaseConnection();
      } else {
        // install an auto-release entity
        HttpEntity entity = response.getEntity();
        entity = new BasicManagedEntity(entity, managedConn, reuse);
        response.setEntity(entity);
      }

      return response;

    } catch (HttpException ex) {
      abortConnection();
      throw ex;
    } catch (IOException ex) {
      abortConnection();
      throw ex;
    } catch (RuntimeException ex) {
      abortConnection();
      throw ex;
    }
  } // execute
コード例 #28
0
  /**
   * Creates a tunnel to the target server. The connection must be established to the (last) proxy.
   * A CONNECT request for tunnelling through the proxy will be created and sent, the response
   * received and checked. This method does <i>not</i> update the connection with information about
   * the tunnel, that is left to the caller.
   *
   * @param route the route to establish
   * @param context the context for request execution
   * @return <code>true</code> if the tunnelled route is secure, <code>false</code> otherwise. The
   *     implementation here always returns <code>false</code>, but derived classes may override.
   * @throws HttpException in case of a problem
   * @throws IOException in case of an IO problem
   */
  protected boolean createTunnelToTarget(HttpRoute route, HttpContext context)
      throws HttpException, IOException {

    HttpHost proxy = route.getProxyHost();
    HttpHost target = route.getTargetHost();
    HttpResponse response = null;

    boolean done = false;
    while (!done) {

      done = true;

      if (!this.managedConn.isOpen()) {
        this.managedConn.open(route, context, this.params);
      }

      HttpRequest connect = createConnectRequest(route, context);

      String agent = HttpProtocolParams.getUserAgent(params);
      if (agent != null) {
        connect.addHeader(HTTP.USER_AGENT, agent);
      }
      connect.addHeader(HTTP.TARGET_HOST, target.toHostString());

      AuthScheme authScheme = this.proxyAuthState.getAuthScheme();
      AuthScope authScope = this.proxyAuthState.getAuthScope();
      Credentials creds = this.proxyAuthState.getCredentials();
      if (creds != null) {
        if (authScope != null || !authScheme.isConnectionBased()) {
          try {
            connect.addHeader(authScheme.authenticate(creds, connect));
          } catch (AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
              this.log.error("Proxy authentication error: " + ex.getMessage());
            }
          }
        }
      }

      response = requestExec.execute(connect, this.managedConn, context);

      int status = response.getStatusLine().getStatusCode();
      if (status < 200) {
        throw new HttpException(
            "Unexpected response to CONNECT request: " + response.getStatusLine());
      }

      CredentialsProvider credsProvider =
          (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);

      if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
        if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {

          this.log.debug("Proxy requested authentication");
          Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
          try {
            processChallenges(
                challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
          } catch (AuthenticationException ex) {
            if (this.log.isWarnEnabled()) {
              this.log.warn("Authentication error: " + ex.getMessage());
              break;
            }
          }
          updateAuthState(this.proxyAuthState, proxy, credsProvider);

          if (this.proxyAuthState.getCredentials() != null) {
            done = false;

            // Retry request
            if (this.reuseStrategy.keepAlive(response, context)) {
              this.log.debug("Connection kept alive");
              // Consume response content
              HttpEntity entity = response.getEntity();
              if (entity != null) {
                entity.consumeContent();
              }
            } else {
              this.managedConn.close();
            }
          }

        } else {
          // Reset proxy auth scope
          this.proxyAuthState.setAuthScope(null);
        }
      }
    }

    int status = response.getStatusLine().getStatusCode();

    if (status > 299) {

      // Buffer response content
      HttpEntity entity = response.getEntity();
      if (entity != null) {
        response.setEntity(new BufferedHttpEntity(entity));
      }

      this.managedConn.close();
      throw new TunnelRefusedException(
          "CONNECT refused by proxy: " + response.getStatusLine(), response);
    }

    this.managedConn.markReusable();

    // How to decide on security of the tunnelled connection?
    // The socket factory knows only about the segment to the proxy.
    // Even if that is secure, the hop to the target may be insecure.
    // Leave it to derived classes, consider insecure by default here.
    return false;
  } // createTunnelToTarget
コード例 #29
0
  @Override
  public URI getLocationURI(final HttpResponse response, final HttpContext context)
      throws ProtocolException {
    if (response == null) {
      throw new IllegalArgumentException("HTTP response may not be null");
    }
    // get the location header to find out where to redirect to
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader == null) {
      // got a redirect response, but no location header
      throw new ProtocolException(
          "Received redirect response " + response.getStatusLine() + " but no location header");
    }
    // HERE IS THE MODIFIED LINE OF CODE
    String location = locationHeader.getValue().replaceAll(" ", "%20");

    URI uri;
    try {
      uri = new URI(location);
    } catch (URISyntaxException ex) {
      throw new ProtocolException("Invalid redirect URI: " + location, ex);
    }

    HttpParams params = response.getParams();
    // rfc2616 demands the location value be a complete URI
    // Location       = "Location" ":" absoluteURI
    if (!uri.isAbsolute()) {
      if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
        throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
      }
      // Adjust location URI
      HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
      if (target == null) {
        throw new IllegalStateException("Target host not available " + "in the HTTP context");
      }

      HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

      try {
        URI requestURI = new URI(request.getRequestLine().getUri());
        URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
        uri = URIUtils.resolve(absoluteRequestURI, uri);
      } catch (URISyntaxException ex) {
        throw new ProtocolException(ex.getMessage(), ex);
      }
    }

    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {

      RedirectLocations redirectLocations =
          (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);

      if (redirectLocations == null) {
        redirectLocations = new RedirectLocations();
        context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
      }

      URI redirectURI;
      if (uri.getFragment() != null) {
        try {
          HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
          redirectURI = URIUtils.rewriteURI(uri, target, true);
        } catch (URISyntaxException ex) {
          throw new ProtocolException(ex.getMessage(), ex);
        }
      } else {
        redirectURI = uri;
      }

      if (redirectLocations.contains(redirectURI)) {
        throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
      } else {
        redirectLocations.add(redirectURI);
      }
    }

    return uri;
  }
コード例 #30
0
  /**
   * Analyzes a response to check need for a followup.
   *
   * @param roureq the request and route.
   * @param response the response to analayze
   * @param context the context used for the current request execution
   * @return the followup request and route if there is a followup, or <code>null</code> if the
   *     response should be returned as is
   * @throws HttpException in case of a problem
   * @throws IOException in case of an IO problem
   */
  protected RoutedRequest handleResponse(
      RoutedRequest roureq, HttpResponse response, HttpContext context)
      throws HttpException, IOException {

    HttpRoute route = roureq.getRoute();
    HttpHost proxy = route.getProxyHost();
    RequestWrapper request = roureq.getRequest();

    HttpParams params = request.getParams();
    if (HttpClientParams.isRedirecting(params)
        && this.redirectHandler.isRedirectRequested(response, context)) {

      if (redirectCount >= maxRedirects) {
        throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
      }
      redirectCount++;

      URI uri = this.redirectHandler.getLocationURI(response, context);

      HttpHost newTarget = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());

      HttpGet redirect = new HttpGet(uri);

      HttpRequest orig = request.getOriginal();
      redirect.setHeaders(orig.getAllHeaders());

      RequestWrapper wrapper = new RequestWrapper(redirect);
      wrapper.setParams(params);

      HttpRoute newRoute = determineRoute(newTarget, wrapper, context);
      RoutedRequest newRequest = new RoutedRequest(wrapper, newRoute);

      if (this.log.isDebugEnabled()) {
        this.log.debug("Redirecting to '" + uri + "' via " + newRoute);
      }

      return newRequest;
    }

    CredentialsProvider credsProvider =
        (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);

    if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {

      if (this.targetAuthHandler.isAuthenticationRequested(response, context)) {

        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
          target = route.getTargetHost();
        }

        this.log.debug("Target requested authentication");
        Map<String, Header> challenges = this.targetAuthHandler.getChallenges(response, context);
        try {
          processChallenges(
              challenges, this.targetAuthState, this.targetAuthHandler, response, context);
        } catch (AuthenticationException ex) {
          if (this.log.isWarnEnabled()) {
            this.log.warn("Authentication error: " + ex.getMessage());
            return null;
          }
        }
        updateAuthState(this.targetAuthState, target, credsProvider);

        if (this.targetAuthState.getCredentials() != null) {
          // Re-try the same request via the same route
          return roureq;
        } else {
          return null;
        }
      } else {
        // Reset target auth scope
        this.targetAuthState.setAuthScope(null);
      }

      if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {

        this.log.debug("Proxy requested authentication");
        Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
        try {
          processChallenges(
              challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
        } catch (AuthenticationException ex) {
          if (this.log.isWarnEnabled()) {
            this.log.warn("Authentication error: " + ex.getMessage());
            return null;
          }
        }
        updateAuthState(this.proxyAuthState, proxy, credsProvider);

        if (this.proxyAuthState.getCredentials() != null) {
          // Re-try the same request via the same route
          return roureq;
        } else {
          return null;
        }
      } else {
        // Reset proxy auth scope
        this.proxyAuthState.setAuthScope(null);
      }
    }
    return null;
  } // handleResponse