/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.nuget.server.feed.reader.impl;

import jetbrains.buildServer.version.ServerVersionHolder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.ContentEncodingHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
 * Date: 11.08.11 16:24
 */
public class FeedHttpClientHolder implements FeedClient {
  private final HttpClient myClient;

  public FeedHttpClientHolder() {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    HttpParams ps = new BasicHttpParams();
    DefaultHttpClient.setDefaultHttpParams(ps);

    HttpConnectionParams.setConnectionTimeout(ps, 300 * 1000);
    HttpConnectionParams.setSoTimeout(ps, 300 * 1000);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final DefaultHttpClient client = new ContentEncodingHttpClient(new ThreadSafeClientConnManager(), ps);
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = client;
  }

  @NotNull
  public HttpResponse execute(@NotNull HttpUriRequest request) throws IOException {
    return myClient.execute(request);
  }

  public void dispose() {
    myClient.getConnectionManager().shutdown();
  }

}

@@ -897,45 +897,110 @@
 ttp.
-impl.
 client.
-ContentEncodingHttpC
+protocol.RequestAcceptEncoding;%0D%0Aimport org.apache.http.c
 lient
+.protocol.ResponseContentEncoding
 ;%0D%0Ai
@@ -1117,24 +1117,85 @@
 ryHandler;%0D%0A
+import org.apache.http.impl.conn.ProxySelectorRoutePlanner;%0D%0A
 import org.a
@@ -1519,16 +1519,48 @@
 eption;%0D
+%0Aimport java.net.ProxySelector;%0D
 %0A%0D%0A/**%0D%0A
@@ -1872,24 +1872,30 @@
 n();%0D%0A%0D%0A    
+final 
 HttpParams p
@@ -1918,24 +1918,26 @@
 pParams();%0D%0A
+%0D%0A
     DefaultH
@@ -1969,26 +1969,24 @@
 arams(ps);%0D%0A
-%0D%0A
     HttpConn
@@ -2180,22 +2180,16 @@
 %0D%0A%0D%0A    
-final 
 DefaultH
@@ -2198,16 +2198,20 @@
 pClient 
+http
 client =
@@ -2215,31 +2215,23 @@
 t = new 
-ContentEncoding
+Default
 HttpClie
@@ -2274,20 +2274,339 @@
 , ps);%0D%0A
+%0D%0A
     
+httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(%0D%0A            httpclient.getConnectionManager().getSchemeRegistry(),%0D%0A            ProxySelector.getDefault()));%0D%0A    httpclient.addRequestInterceptor(new RequestAcceptEncoding());%0D%0A    httpclient.addResponseInterceptor(new ResponseContentEncoding());%0D%0A    http
 client.s
@@ -2695,16 +2695,20 @@
 lient = 
+http
 client;%0D

enable default proxy in NuGet commandline fetcher