org.eclipse.mylyn.commons.repositories.http.core_0.9.1.v20120425-0100

16:46:28.825 INFO  jd.cli.Main - Decompiling org.eclipse.mylyn.commons.repositories.http.core_0.9.1.v20120425-0100.jar
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import org.apache.http.client.methods.HttpRequestBase;
import org.eclipse.core.runtime.Assert;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;

class DefaultHttpOperation<T>
  extends CommonHttpOperation<T>
{
  private final HttpRequestProcessor<T> processor;
  private final HttpRequestBase request;
  
  public DefaultHttpOperation(CommonHttpClient client, HttpRequestBase request, HttpRequestProcessor<T> processor)
  {
    super(client);
    Assert.isNotNull(processor);
    this.request = request;
    this.processor = processor;
  }
  
  public T run(IOperationMonitor monitor)
    throws IOException
  {
    CommonHttpResponse response = execute(request, monitor);
    return (T)(processor.autoRelease() ? processAndRelease(response, monitor) : process(response, monitor));
  }
  
  protected T doProcess(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {
    return (T)processor.doProcess(response, monitor);
  }
  
  protected void doValidate(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {
    processor.doValidate(response, monitor);
  }
  
  protected T process(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {
    try
    {
      doValidate(response, monitor);
      return (T)doProcess(response, monitor);
    }
    catch (IOException e)
    {
      response.release(monitor);
      throw e;
    }
    catch (RuntimeException e)
    {
      response.release(monitor);
      throw e;
    }
  }
  
  protected T processAndRelease(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {
    try
    {
      doValidate(response, monitor);
      return (T)doProcess(response, monitor);
    }
    finally
    {
      response.release(monitor);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.DefaultHttpOperation
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.ContentEncodingHttpClient;

class CommonHttpClient$1
  extends ContentEncodingHttpClient
{
  CommonHttpClient$1(CommonHttpClient paramCommonHttpClient) {}
  
  protected ClientConnectionManager createClientConnectionManager()
  {
    return this$0.createHttpClientConnectionManager();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Locale;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.impl.EnglishReasonPhraseCatalog;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.CoreUtil;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.core.net.AuthenticatedProxy;
import org.eclipse.mylyn.commons.core.net.NetUtil;
import org.eclipse.mylyn.commons.core.operations.MonitoredOperation;
import org.eclipse.mylyn.commons.core.operations.Operation;
import org.eclipse.mylyn.commons.core.operations.OperationUtil;
import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType;
import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials;
import org.eclipse.mylyn.internal.commons.repositories.http.core.IdleConnectionMonitorThread;
import org.eclipse.mylyn.internal.commons.repositories.http.core.PollingProtocolSocketFactory;
import org.eclipse.mylyn.internal.commons.repositories.http.core.PollingSslProtocolSocketFactory;

public class HttpUtil
{
  private static final int BUFFER_SIZE = 4096;
  private static final long CLOSE_TIMEOUT = -1L;
  private static final int CONNECTION_TIMEOUT_INTERVAL = 30000;
  private static final int CONNNECT_TIMEOUT = 60000;
  private static final int HTTP_PORT = 80;
  private static final int HTTPS_PORT = 443;
  private static final int POLL_INTERVAL = 500;
  private static final int SOCKET_TIMEOUT = 180000;
  private static final int POLL_ATTEMPTS = 360;
  private static SchemeSocketFactory socketFactory = new PollingProtocolSocketFactory();
  private static SchemeSocketFactory sslSocketFactory = new PollingSslProtocolSocketFactory();
  static final String ID_PLUGIN = "org.eclipse.mylyn.commons.repositories.http";
  private static ThreadSafeClientConnManager connectionManager;
  
  static {}
  
  public static void configureClient(AbstractHttpClient client, String userAgent)
  {
    HttpClientParams.setCookiePolicy(client.getParams(), "best-match");
    
    HttpProtocolParams.setUserAgent(client.getParams(), userAgent);
    HttpProtocolParams.setUseExpectContinue(client.getParams(), true);
    client.getParams().setBooleanParameter("http.protocol.allow-circular-redirects", true);
    
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 60000);
    HttpConnectionParams.setSoTimeout(client.getParams(), 180000);
  }
  
  public static void configureAuthentication(AbstractHttpClient client, RepositoryLocation location)
  {
    UserCredentials credentials = (UserCredentials)location.getCredentials(AuthenticationType.HTTP);
    if (credentials != null) {
      configureAuthentication(client, location, credentials);
    }
  }
  
  public static void configureAuthentication(AbstractHttpClient client, RepositoryLocation location, UserCredentials credentials)
  {
    Assert.isNotNull(client);
    Assert.isNotNull(location);
    Assert.isNotNull(credentials);
    String url = location.getUrl();
    Assert.isNotNull("The location url must not be null", url);
    
    String host = NetUtil.getHost(url);
    int port = NetUtil.getPort(url);
    
    NTCredentials ntlmCredentials = getNtCredentials(credentials, null);
    if (ntlmCredentials != null)
    {
      AuthScope authScopeNtlm = new AuthScope(host, port, AuthScope.ANY_REALM, "NTLM");
      client.getCredentialsProvider().setCredentials(authScopeNtlm, ntlmCredentials);
    }
    UsernamePasswordCredentials usernamePasswordCredentials = getUserNamePasswordCredentials(credentials);
    AuthScope authScopeAny = new AuthScope(host, port, AuthScope.ANY_REALM);
    client.getCredentialsProvider().setCredentials(authScopeAny, usernamePasswordCredentials);
  }
  
  public static HttpHost createHost(HttpRequestBase method)
  {
    URI uri = method.getURI();
    return new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
  }
  
  public static HttpResponse execute(AbstractHttpClient client, HttpContext context, HttpRequestBase method, IProgressMonitor monitor)
    throws IOException
  {
    return execute(client, createHost(method), context, method, monitor);
  }
  
  public static HttpResponse execute(final AbstractHttpClient client, final HttpHost host, final HttpContext context, final HttpRequestBase method, IProgressMonitor monitor)
    throws IOException
  {
    Assert.isNotNull(client);
    Assert.isNotNull(method);
    
    monitor = OperationUtil.convert(monitor);
    
    MonitoredOperation<HttpResponse> executor = new MonitoredOperation(monitor)
    {
      public void abort()
      {
        super.abort();
        method.abort();
      }
      
      public HttpResponse execute()
        throws Exception
      {
        return client.execute(host, method, context);
      }
    };
    return (HttpResponse)executeInternal(monitor, executor);
  }
  
  public static NTCredentials getNtCredentials(UserCredentials credentials, String workstation)
  {
    String username = credentials.getUserName();
    int i = username.indexOf("\\");
    if ((i > 0) && (i < username.length() - 1)) {
      return new NTCredentials(username.substring(i + 1), credentials.getPassword(), workstation, 
        username.substring(0, i));
    }
    return null;
  }
  
  public static UsernamePasswordCredentials getUserNamePasswordCredentials(UserCredentials credentials)
  {
    return new UsernamePasswordCredentials(credentials.getUserName(), credentials.getPassword());
  }
  
  public static InputStream getResponseBodyAsStream(HttpEntity entity, IProgressMonitor monitor)
    throws IOException
  {
    monitor = OperationUtil.convert(monitor);
    return entity.getContent();
  }
  
  public static SchemeRegistry getSchemeRegistry()
  {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, socketFactory));
    schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));
    return schemeRegistry;
  }
  
  public static void configureProxy(AbstractHttpClient client, RepositoryLocation location)
  {
    Assert.isNotNull(client);
    Assert.isNotNull(location);
    String url = location.getUrl();
    Assert.isNotNull("The location url must not be null", url);
    
    String host = NetUtil.getHost(url);
    Proxy proxy;
    Proxy proxy;
    if (NetUtil.isUrlHttps(url)) {
      proxy = location.getProxyForHost(host, "HTTPS");
    } else {
      proxy = location.getProxyForHost(host, "HTTP");
    }
    if ((proxy != null) && (!Proxy.NO_PROXY.equals(proxy)))
    {
      InetSocketAddress address = (InetSocketAddress)proxy.address();
      
      client.getParams().setParameter("http.route.default-proxy", 
        new HttpHost(address.getHostName(), address.getPort()));
      if ((proxy instanceof AuthenticatedProxy))
      {
        AuthenticatedProxy authProxy = (AuthenticatedProxy)proxy;
        Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(), 
          address.getAddress(), false);
        if ((credentials instanceof NTCredentials))
        {
          AuthScope proxyAuthScopeNTLM = new AuthScope(address.getHostName(), address.getPort(), 
            AuthScope.ANY_REALM, "NTLM");
          client.getCredentialsProvider().setCredentials(proxyAuthScopeNTLM, credentials);
          
          AuthScope proxyAuthScopeAny = new AuthScope(address.getHostName(), address.getPort(), 
            AuthScope.ANY_REALM);
          Credentials usernamePasswordCredentials = getCredentials(authProxy.getUserName(), 
            authProxy.getPassword(), address.getAddress(), true);
          client.getCredentialsProvider().setCredentials(proxyAuthScopeAny, usernamePasswordCredentials);
        }
        else
        {
          AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(), 
            AuthScope.ANY_REALM);
          client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials);
        }
      }
    }
    else
    {
      client.getParams().setParameter("http.route.default-proxy", null);
    }
  }
  
  private static <T> T executeInternal(IProgressMonitor monitor, Operation<?> request)
    throws IOException
  {
    try
    {
      return (T)OperationUtil.execute(monitor, request);
    }
    catch (IOException e)
    {
      throw e;
    }
    catch (RuntimeException e)
    {
      throw e;
    }
    catch (Error e)
    {
      throw e;
    }
    catch (Throwable e)
    {
      throw new RuntimeException(e);
    }
  }
  
  static Credentials getCredentials(String username, String password, InetAddress address, boolean forceUserNamePassword)
  {
    int i = username.indexOf("\\");
    if ((i > 0) && (i < username.length() - 1) && (address != null) && (!forceUserNamePassword))
    {
      String hostName = address.getHostName();
      try
      {
        InetAddress localHost = InetAddress.getLocalHost();
        if (localHost != null) {
          hostName = localHost.getHostName();
        }
      }
      catch (UnknownHostException e)
      {
        StatusHandler.log(new Status(4, "org.eclipse.mylyn.commons.repositories.http", 
          "Unable to get hostname.  Defaulting to servers host.", e));
      }
      if (hostName == null) {
        hostName = address.getHostName();
      }
      return new NTCredentials(username.substring(i + 1), password, hostName, username.substring(0, i));
    }
    return new UsernamePasswordCredentials(username, password);
  }
  
  public static synchronized ThreadSafeClientConnManager getConnectionManager()
  {
    if (connectionManager == null)
    {
      connectionManager = new ThreadSafeClientConnManager(getSchemeRegistry());
      if (CoreUtil.TEST_MODE)
      {
        connectionManager.setDefaultMaxPerRoute(2);
      }
      else
      {
        connectionManager.setDefaultMaxPerRoute(100);
        connectionManager.setMaxTotal(1000);
      }
      IdleConnectionMonitorThread thread = new IdleConnectionMonitorThread(30000L);
      thread.setTimeout(60000L);
      thread.addConnectionManager(connectionManager);
      thread.start();
    }
    return connectionManager;
  }
  
  public static String getStatusText(int statusCode)
  {
    return EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.getDefault());
  }
  
  public static void release(HttpRequest request, HttpResponse response, IProgressMonitor monitor)
  {
    Assert.isNotNull(request);
    Assert.isNotNull(response);
    if ((monitor != null) && (monitor.isCanceled()) && ((request instanceof HttpUriRequest))) {
      try
      {
        ((HttpUriRequest)request).abort();
      }
      catch (UnsupportedOperationException localUnsupportedOperationException)
      {
        try
        {
          EntityUtils.consume(response.getEntity());
        }
        catch (IOException localIOException1) {}catch (NullPointerException localNullPointerException1) {}
      }
    } else {
      try
      {
        EntityUtils.consume(response.getEntity());
      }
      catch (IOException localIOException2) {}catch (NullPointerException localNullPointerException2) {}
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.HttpUtil
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.core.operations.OperationUtil;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationCredentials;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationException;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationRequest;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType;

public abstract class CommonHttpOperation<T>
{
  private final CommonHttpClient client;
  
  public CommonHttpOperation(CommonHttpClient client)
  {
    this.client = client;
  }
  
  public CommonHttpOperation(CommonHttpClient client, HttpRequestBase request)
  {
    this.client = client;
  }
  
  protected void authenticate(IOperationMonitor monitor)
    throws IOException
  {
    client.authenticate(monitor);
  }
  
  protected HttpGet createGetRequest(String requestPath)
  {
    return new HttpGet(requestPath);
  }
  
  protected HttpHead createHeadRequest(String requestPath)
  {
    return new HttpHead(requestPath);
  }
  
  protected HttpPost createPostRequest(String requestPath)
  {
    return new HttpPost(requestPath);
  }
  
  public CommonHttpResponse execute(HttpRequestBase request, IOperationMonitor monitor)
    throws IOException
  {
    monitor = OperationUtil.convert(monitor);
    try
    {
      return executeOnce(request, monitor);
    }
    catch (AuthenticationException e)
    {
      boolean requestCredentials = !e.shouldRetry();
      
      handleAuthenticationError(request, e, monitor, requestCredentials);
      try
      {
        return executeOnce(request, monitor);
      }
      catch (AuthenticationException e)
      {
        if (requestCredentials)
        {
          invalidateAuthentication(e, monitor);
          throw e;
        }
        handleAuthenticationError(request, e, monitor, true);
      }
    }
    return executeOnce(request, monitor);
  }
  
  private void handleAuthenticationError(HttpRequestBase request, AuthenticationException e, IOperationMonitor monitor, boolean requestCredentials)
    throws AuthenticationException
  {
    invalidateAuthentication(e, monitor);
    if (!isRepeatable()) {
      throw e;
    }
    if (requestCredentials) {
      try
      {
        requestCredentials(e.getRequest(), monitor);
      }
      catch (UnsupportedOperationException localUnsupportedOperationException)
      {
        throw e;
      }
    }
  }
  
  protected boolean isRepeatable()
  {
    return true;
  }
  
  protected CommonHttpResponse executeOnce(HttpRequestBase request, IOperationMonitor monitor)
    throws IOException
  {
    if (needsAuthentication()) {
      authenticate(monitor);
    }
    HttpResponse response = client.execute(request, monitor);
    try
    {
      validate(response, monitor);
      
      return new CommonHttpResponse(request, response);
    }
    catch (IOException e)
    {
      HttpUtil.release(request, response, monitor);
      throw e;
    }
    catch (RuntimeException e)
    {
      HttpUtil.release(request, response, monitor);
      throw e;
    }
  }
  
  protected final CommonHttpClient getClient()
  {
    return client;
  }
  
  protected boolean needsAuthentication()
  {
    return client.needsAuthentication();
  }
  
  protected <T extends AuthenticationCredentials> T requestCredentials(AuthenticationRequest<AuthenticationType<T>> request, IOperationMonitor monitor)
  {
    return client.requestCredentials(request, monitor);
  }
  
  protected void invalidateAuthentication(AuthenticationException e, IOperationMonitor monitor)
  {
    client.setAuthenticated(false);
  }
  
  protected void validate(HttpResponse response, IOperationMonitor monitor)
    throws AuthenticationException
  {
    client.validate(response, monitor);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.CommonHttpOperation
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;

public abstract class HttpRequestProcessor<T>
{
  public static final HttpRequestProcessor<CommonHttpResponse> DEFAULT = new HttpRequestProcessor()
  {
    protected CommonHttpResponse doProcess(CommonHttpResponse response, IOperationMonitor monitor)
      throws IOException
    {
      return response;
    }
  };
  private final boolean autoRelease;
  
  public HttpRequestProcessor()
  {
    this(true);
  }
  
  public HttpRequestProcessor(boolean autoRelease)
  {
    this.autoRelease = autoRelease;
  }
  
  public boolean autoRelease()
  {
    return autoRelease;
  }
  
  protected T doProcess(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {
    return null;
  }
  
  protected void doValidate(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {}
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.HttpRequestProcessor
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;

public class CommonHttpResponse
{
  private final HttpRequest request;
  private final HttpResponse response;
  
  public CommonHttpResponse(HttpRequest request, HttpResponse response)
  {
    Assert.isNotNull(request);
    Assert.isNotNull(response);
    this.request = request;
    this.response = response;
  }
  
  public HttpRequest getRequest()
  {
    return request;
  }
  
  public HttpResponse getResponse()
  {
    return response;
  }
  
  public int getStatusCode()
  {
    return response.getStatusLine().getStatusCode();
  }
  
  public InputStream getResponseEntityAsStream(IProgressMonitor monitor)
    throws IOException
  {
    HttpEntity entity = response.getEntity();
    if (entity == null) {
      throw new IOException("Expected entity");
    }
    return HttpUtil.getResponseBodyAsStream(entity, monitor);
  }
  
  public void release(IProgressMonitor monitor)
  {
    HttpUtil.release(request, response, monitor);
  }
  
  public String getRequestPath()
  {
    if ((request instanceof HttpUriRequest)) {
      return ((HttpUriRequest)request).getURI().getPath();
    }
    return null;
  }
  
  public String getResponseCharSet()
  {
    return EntityUtils.getContentCharSet(response.getEntity());
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;

class HttpRequestProcessor$1
  extends HttpRequestProcessor<CommonHttpResponse>
{
  protected CommonHttpResponse doProcess(CommonHttpResponse response, IOperationMonitor monitor)
    throws IOException
  {
    return response;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.HttpRequestProcessor.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import javax.net.ssl.TrustManager;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.ContentEncodingHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.SyncBasicHttpContext;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.commons.core.net.SslSupport;
import org.eclipse.mylyn.commons.core.net.TrustAllTrustManager;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationCredentials;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationException;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationRequest;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType;
import org.eclipse.mylyn.commons.repositories.core.auth.CertificateCredentials;
import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials;

public class CommonHttpClient
{
  private boolean preemptiveAuthenticationEnabled;
  private boolean authenticated;
  private final SyncBasicHttpContext context;
  private AuthenticationType<UserCredentials> httpAuthenticationType;
  private AbstractHttpClient httpClient;
  private final RepositoryLocation location;
  
  public CommonHttpClient(RepositoryLocation location)
  {
    this.location = location;
    context = new SyncBasicHttpContext(null);
    httpAuthenticationType = AuthenticationType.HTTP;
  }
  
  public <T> T executeGet(String requestPath, IOperationMonitor monitor, HttpRequestProcessor<T> processor)
    throws IOException
  {
    HttpGet request = new HttpGet(location.getUrl() + requestPath);
    DefaultHttpOperation<T> op = new DefaultHttpOperation(this, request, processor);
    return (T)op.run(monitor);
  }
  
  public HttpResponse execute(HttpRequestBase request, IOperationMonitor monitor)
    throws IOException
  {
    prepareRequest(request, monitor);
    return HttpUtil.execute(getHttpClient(), HttpUtil.createHost(request), context, request, monitor);
  }
  
  public HttpContext getContext()
  {
    return context;
  }
  
  public AuthenticationType<UserCredentials> getHttpAuthenticationType()
  {
    return httpAuthenticationType;
  }
  
  public synchronized AbstractHttpClient getHttpClient()
  {
    if (httpClient == null) {
      httpClient = createHttpClient(null);
    }
    return httpClient;
  }
  
  public RepositoryLocation getLocation()
  {
    return location;
  }
  
  public boolean isAuthenticated()
  {
    return authenticated;
  }
  
  public boolean isPreemptiveAuthenticationEnabled()
  {
    return preemptiveAuthenticationEnabled;
  }
  
  public boolean needsAuthentication()
  {
    return (!isAuthenticated()) && (getLocation().getCredentials(AuthenticationType.REPOSITORY, false) != null);
  }
  
  public void setAuthenticated(boolean authenticated)
  {
    this.authenticated = authenticated;
  }
  
  public void setHttpAuthenticationType(AuthenticationType<UserCredentials> httpAuthenticationType)
  {
    this.httpAuthenticationType = httpAuthenticationType;
  }
  
  public void setPreemptiveAuthenticationEnabled(boolean preemptiveAuthenticationEnabled)
  {
    this.preemptiveAuthenticationEnabled = preemptiveAuthenticationEnabled;
  }
  
  private void prepareRequest(HttpRequestBase request, IOperationMonitor monitor)
  {
    UserCredentials httpCredentials = (UserCredentials)location.getCredentials(httpAuthenticationType);
    if (httpCredentials != null)
    {
      HttpUtil.configureAuthentication(getHttpClient(), location, httpCredentials);
      if (isPreemptiveAuthenticationEnabled())
      {
        HttpHost host = HttpUtil.createHost(request);
        Object authCache = context.getAttribute("http.auth.auth-cache");
        if (authCache == null)
        {
          authCache = new BasicAuthCache();
          context.setAttribute("http.auth.auth-cache", authCache);
        }
        if (((authCache instanceof BasicAuthCache)) && 
          (((BasicAuthCache)authCache).get(host) == null)) {
          ((BasicAuthCache)authCache).put(host, new BasicScheme());
        }
      }
    }
    HttpUtil.configureProxy(getHttpClient(), location);
    
    CertificateCredentials socketCredentials = (CertificateCredentials)location.getCredentials(AuthenticationType.CERTIFICATE);
    if (socketCredentials != null)
    {
      SslSupport support = new SslSupport(new TrustManager[] { new TrustAllTrustManager() }, 
        socketCredentials.getKeyStoreFileName(), socketCredentials.getPassword(), 
        socketCredentials.getKeyStoreType());
      request.getParams().setParameter(SslSupport.class.getName(), support);
    }
    else
    {
      context.removeAttribute("http.user-token");
    }
  }
  
  protected void authenticate(IOperationMonitor monitor)
    throws IOException
  {}
  
  protected AbstractHttpClient createHttpClient(String userAgent)
  {
    AbstractHttpClient client = new ContentEncodingHttpClient()
    {
      protected ClientConnectionManager createClientConnectionManager()
      {
        return createHttpClientConnectionManager();
      }
    };
    HttpUtil.configureClient(client, userAgent);
    return client;
  }
  
  protected ClientConnectionManager createHttpClientConnectionManager()
  {
    return HttpUtil.getConnectionManager();
  }
  
  protected <T extends AuthenticationCredentials> T requestCredentials(AuthenticationRequest<AuthenticationType<T>> request, IProgressMonitor monitor)
  {
    return location.requestCredentials(request, monitor);
  }
  
  protected void validate(HttpResponse response, IProgressMonitor monitor)
    throws AuthenticationException
  {
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 401)
    {
      AuthenticationRequest<AuthenticationType<UserCredentials>> request = new AuthenticationRequest(
        getLocation(), httpAuthenticationType);
      throw new AuthenticationException(HttpUtil.getStatusText(statusCode), request);
    }
    if (statusCode == 407)
    {
      AuthenticationRequest<AuthenticationType<UserCredentials>> request = new AuthenticationRequest(
        getLocation(), AuthenticationType.PROXY);
      throw new AuthenticationException(HttpUtil.getStatusText(statusCode), request);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.commons.repositories.http.core;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.protocol.HttpContext;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.commons.core.operations.MonitoredOperation;

class HttpUtil$1
  extends MonitoredOperation<HttpResponse>
{
  HttpUtil$1(IProgressMonitor $anonymous0, HttpRequestBase paramHttpRequestBase, AbstractHttpClient paramAbstractHttpClient, HttpHost paramHttpHost, HttpContext paramHttpContext)
  {
    super($anonymous0);
  }
  
  public void abort()
  {
    super.abort();
    val$method.abort();
  }
  
  public HttpResponse execute()
    throws Exception
  {
    return val$client.execute(val$host, val$method, val$context);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.commons.repositories.http.core;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import org.apache.http.conn.ClientConnectionManager;

public class IdleConnectionMonitorThread
  extends Thread
{
  private final List<ClientConnectionManager> connectionManagers;
  private long pollingInterval;
  private volatile boolean shutdown;
  private long timeout;
  
  public IdleConnectionMonitorThread(long pollingInterval)
  {
    this.pollingInterval = pollingInterval;
    connectionManagers = new CopyOnWriteArrayList();
    setDaemon(true);
  }
  
  public void addConnectionManager(ClientConnectionManager manager)
  {
    connectionManagers.add(manager);
  }
  
  public long getPollingInterval()
  {
    return pollingInterval;
  }
  
  public long getTimeout()
  {
    return timeout;
  }
  
  public void removeConnectionManager(ClientConnectionManager manager)
  {
    connectionManagers.remove(manager);
  }
  
  public void run()
  {
    try
    {
      while (!shutdown)
      {
        for (ClientConnectionManager connectionManager : connectionManagers)
        {
          connectionManager.closeExpiredConnections();
          if (timeout > 0L) {
            connectionManager.closeIdleConnections(timeout, TimeUnit.MILLISECONDS);
          }
        }
        synchronized (this)
        {
          wait(pollingInterval);
        }
      }
    }
    catch (InterruptedException localInterruptedException) {}
  }
  
  public void setPollingInterval(long pollingInterval)
  {
    this.pollingInterval = pollingInterval;
  }
  
  public void setTimeout(long timeout)
  {
    this.timeout = timeout;
  }
  
  public void shutdown()
  {
    shutdown = true;
    synchronized (this)
    {
      notifyAll();
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.commons.repositories.http.core.IdleConnectionMonitorThread
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.commons.repositories.http.core;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.eclipse.mylyn.commons.core.net.NetUtil;
import org.eclipse.mylyn.commons.core.operations.MonitoredOperation;

public class PollingProtocolSocketFactory
  implements SchemeSocketFactory
{
  private static final SocketFactory factory = ;
  
  public Socket createSocket(HttpParams params)
    throws IOException
  {
    return NetUtil.configureSocket(factory.createSocket());
  }
  
  public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpParams params)
    throws IOException, UnknownHostException, ConnectTimeoutException
  {
    if (params == null) {
      throw new IllegalArgumentException("Parameters may not be null");
    }
    Socket socket = sock != null ? sock : NetUtil.configureSocket(factory.createSocket());
    
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    
    socket.bind(localAddress);
    NetUtil.connect(socket, remoteAddress, connTimeout, MonitoredOperation.getCurrentOperation());
    return socket;
  }
  
  public boolean isSecure(Socket sock)
    throws IllegalArgumentException
  {
    return false;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.commons.repositories.http.core.PollingProtocolSocketFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.commons.repositories.http.core;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.LayeredSchemeSocketFactory;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.eclipse.core.runtime.Assert;
import org.eclipse.mylyn.commons.core.net.NetUtil;
import org.eclipse.mylyn.commons.core.net.SslSupport;
import org.eclipse.mylyn.commons.core.net.TrustAllTrustManager;
import org.eclipse.mylyn.commons.core.operations.MonitoredOperation;

public class PollingSslProtocolSocketFactory
  implements LayeredSchemeSocketFactory
{
  private final SslSupport defaultSslSupport;
  
  public PollingSslProtocolSocketFactory()
  {
    this(new SslSupport(new TrustManager[] { new TrustAllTrustManager() }));
  }
  
  public PollingSslProtocolSocketFactory(SslSupport sslSupport)
  {
    defaultSslSupport = sslSupport;
  }
  
  public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpParams params)
    throws IOException, UnknownHostException, ConnectTimeoutException
  {
    Assert.isNotNull(params);
    Socket socket = sock != null ? sock : createSocket(params);
    if (localAddress != null)
    {
      socket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
      socket.bind(localAddress);
    }
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    NetUtil.connect(socket, remoteAddress, connTimeout, MonitoredOperation.getCurrentOperation());
    if ((socket instanceof SSLSocket)) {
      return socket;
    }
    return getSslSupport(params).getSocketFactory().createSocket(socket, remoteAddress.getHostName(), 
      remoteAddress.getPort(), true);
  }
  
  public Socket createLayeredSocket(Socket socket, String target, int port, boolean autoClose)
    throws IOException, UnknownHostException
  {
    return NetUtil.configureSocket(getDefaultSocketFactory().createSocket(socket, target, port, autoClose));
  }
  
  public Socket createSocket(HttpParams params)
    throws IOException
  {
    Assert.isNotNull(params);
    return NetUtil.configureSocket(getSslSupport(params).getSocketFactory().createSocket());
  }
  
  public boolean equals(Object obj)
  {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    PollingSslProtocolSocketFactory other = (PollingSslProtocolSocketFactory)obj;
    if (defaultSslSupport == null)
    {
      if (defaultSslSupport != null) {
        return false;
      }
    }
    else if (!defaultSslSupport.equals(defaultSslSupport)) {
      return false;
    }
    return true;
  }
  
  public SSLSocketFactory getDefaultSocketFactory()
    throws IOException
  {
    return defaultSslSupport.getSocketFactory();
  }
  
  public int hashCode()
  {
    int result = 1;
    result = 31 * result + (defaultSslSupport == null ? 0 : defaultSslSupport.hashCode());
    return result;
  }
  
  public boolean isSecure(Socket socket)
    throws IllegalArgumentException
  {
    Assert.isNotNull(socket);
    if (!(socket instanceof SSLSocket)) {
      throw new IllegalArgumentException("Socket is not secure: " + socket.getClass());
    }
    if (socket.isClosed()) {
      throw new IllegalArgumentException("Socket is closed");
    }
    return true;
  }
  
  private SslSupport getSslSupport(HttpParams params)
  {
    SslSupport sslSupport = (SslSupport)params.getParameter(SslSupport.class.getName());
    return sslSupport != null ? sslSupport : defaultSslSupport;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.commons.repositories.http.core.PollingSslProtocolSocketFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
1

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd