Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect property https.protocols #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
<version>4.5.2</version>
</dependency>
<dependency>
<!-- Only needed for JSON parsing -->
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/groovyx/net/http/AuthConfig.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,31 @@
*/
package groovyx.net.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Map;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthException;

import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Map;

/**
* Encapsulates all configuration related to HTTP authentication methods.
* @see HTTPBuilder#getAuth()
Expand Down Expand Up @@ -85,14 +80,19 @@ public void basic( String user, String pass ) {
* @param pass
*/
public void basic( String host, int port, String user, String pass ) {
final HttpClient client = builder.getClient();
if ( !(client instanceof AbstractHttpClient )) {
throw new IllegalStateException("client is not an AbstractHttpClient");
}
((AbstractHttpClient)client).getCredentialsProvider().setCredentials(
new AuthScope( host, port ),
new UsernamePasswordCredentials( user, pass )
);
AuthScope authScope = new AuthScope(host, port);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
final HttpClient client = builder.getClient();
if (client instanceof AbstractHttpClient) {
((AbstractHttpClient)client).getCredentialsProvider().setCredentials(
authScope,
credentials
);
} else if(client instanceof CloseableHttpClient){
builder.setCredentials(authScope, credentials);
} else {
throw new IllegalStateException("client is not an AbstractHttpClient");
}
}

/**
Expand Down
100 changes: 60 additions & 40 deletions src/main/java/groovyx/net/http/HTTPBuilder.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,16 @@
*/
package groovyx.net.http;

import static groovyx.net.http.URIBuilder.convertToURI;
import groovy.lang.Closure;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -60,18 +39,35 @@
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.cookie.params.CookieSpecPNames;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.*;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.MethodClosure;

import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Map;

import static groovyx.net.http.URIBuilder.convertToURI;

/** <p>
* Groovy DSL for easily making HTTP requests, and handling request and response
* data. This class adds a number of convenience mechanisms built on top of
Expand Down Expand Up @@ -175,7 +171,7 @@
*/
public class HTTPBuilder {

private HttpClient client;
private HttpClient client;
protected URIBuilder defaultURI = null;
protected AuthConfig auth = new AuthConfig( this );

Expand All @@ -192,8 +188,14 @@ public class HTTPBuilder {

protected EncoderRegistry encoders = new EncoderRegistry();
protected ParserRegistry parsers = new ParserRegistry();
public static final HttpParams DEFAULT_PARAMS = new BasicHttpParams();

/**
static{
DEFAULT_PARAMS.setParameter( CookieSpecPNames.DATE_PATTERNS,
Arrays.asList("EEE, dd-MMM-yyyy HH:mm:ss z", "EEE, dd MMM yyyy HH:mm:ss z") );
}

/**
* Creates a new instance with a <code>null</code> default URI.
*/
public HTTPBuilder() {
Expand Down Expand Up @@ -836,10 +838,7 @@ public Map<?,?> getHeaders() {
*/
public HttpClient getClient() {
if (client == null) {
HttpParams defaultParams = new BasicHttpParams();
defaultParams.setParameter( CookieSpecPNames.DATE_PATTERNS,
Arrays.asList("EEE, dd-MMM-yyyy HH:mm:ss z", "EEE, dd MMM yyyy HH:mm:ss z") );
client = createClient(defaultParams);
client = createClient(DEFAULT_PARAMS);
}
return client;
}
Expand All @@ -854,11 +853,27 @@ public void setClient(HttpClient client) {
* @param params
* @return
*/
protected HttpClient createClient( HttpParams params ) {
return new DefaultHttpClient(params);
protected HttpClient createClient( HttpParams params) {
if(StringUtils.isNotBlank(System.getProperty("https.protocols"))) {
HttpClientBuilder httpClientBuilder = getHttpClientBuilder();
return httpClientBuilder.build();
} else {
return new DefaultHttpClient(params);
}
}

/**
private HttpClientBuilder getHttpClientBuilder() {
String protocols = System.getProperty("https.protocols");
String[] protocolArray = protocols.split(",");
SSLContext sslContext = SSLContexts.createDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
protocolArray,
null,
new NoopHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(sslsf);
}

/**
* Used to access the {@link AuthConfig} handler used to configure common
* authentication mechanism. Example:
* <pre>builder.auth.basic( 'myUser', 'somePassword' )</pre>
Expand Down Expand Up @@ -950,9 +965,14 @@ public void shutdown() {
getClient().getConnectionManager().shutdown();
}

void setCredentials(AuthScope authScope, UsernamePasswordCredentials credentials) {
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(authScope,credentials);
client = getHttpClientBuilder().setDefaultCredentialsProvider(basicCredentialsProvider).build();
}


/**
/**
* <p>Encloses all properties and method calls used within the
* {@link HTTPBuilder#request(Object, Method, Object, Closure)} 'config'
* closure argument. That is, an instance of this class is set as the
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/groovyx/net/http/thirdparty/GAEClientConnection.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ of the License, or (at your option) any later version.

import java.io.*;
import java.net.*;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.http.*;
import org.apache.http.conn.*;
Expand All @@ -34,14 +35,16 @@ of the License, or (at your option) any later version.

import com.google.appengine.api.urlfetch.*;

class GAEClientConnection
implements ManagedClientConnection {
class GAEClientConnection implements ManagedClientConnection {

final String id;

public GAEClientConnection(ClientConnectionManager cm, HttpRoute route, Object state) {
this.connManager = cm;
this.route = route;
this.state = state;
this.closed = true;
id = UUID.randomUUID().toString();
}

// From interface ManagedClientConnection
Expand Down Expand Up @@ -281,6 +284,19 @@ public void abortConnection()
private HTTPRequest request;
private HTTPResponse response;
private boolean closed;
private Socket socket;

private static URLFetchService urlFS = URLFetchServiceFactory.getURLFetchService();

public String getId() {
return id;
}

public void bind(Socket socket) throws IOException {
this.socket = socket;
}

public Socket getSocket() {
return socket;
}
}
2 changes: 1 addition & 1 deletion src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HttpURLClientTest {
@Test public void testRedirect() {
def http = new HttpURLClient(followRedirects:false)

def params = [ url:'http://www.google.com/search',
def params = [ url:'https://www.google.com/search',
query:[q:'HTTPBuilder', btnI:"I'm Feeling Lucky"],
headers:['User-Agent':'Firefox'] ]
def resp = http.request( params )
Expand Down