Coverage Report - groovyx.net.http.AuthConfig
 
Classes in this File Line Coverage Branch Coverage Complexity
AuthConfig
53%
9/17
50%
1/2
1.5
 
 1  
 /*
 2  
  * Copyright 2003-2008 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * You are receiving this code free of charge, which represents many hours of
 17  
  * effort from other individuals and corporations.  As a responsible member 
 18  
  * of the community, you are asked (but not required) to donate any 
 19  
  * enhancements or improvements back to the community under a similar open 
 20  
  * source license.  Thank you. -TMN
 21  
  */
 22  
 package groovyx.net.http;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.net.MalformedURLException;
 27  
 import java.net.URL;
 28  
 import java.security.GeneralSecurityException;
 29  
 import java.security.KeyStore;
 30  
 
 31  
 import org.apache.http.auth.AuthScope;
 32  
 import org.apache.http.auth.UsernamePasswordCredentials;
 33  
 import org.apache.http.conn.scheme.Scheme;
 34  
 import org.apache.http.conn.ssl.SSLSocketFactory;
 35  
 
 36  
 /**
 37  
  * Encapsulates all configuration related to HTTP authentication methods.
 38  
  * @see HTTPBuilder#getAuth()
 39  
  * 
 40  
  * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
 41  
  */
 42  
 public class AuthConfig {
 43  
         protected HTTPBuilder builder;
 44  36
         public AuthConfig( HTTPBuilder builder ) {
 45  36
                 this.builder = builder;
 46  36
         }
 47  
         
 48  
         /**
 49  
          * Set authentication credentials to be used for the current 
 50  
          * {@link HTTPBuilder#getURL() default host}.  This method name is a bit of 
 51  
          * a misnomer, since these credentials will actually work for "digest" 
 52  
          * authentication as well.
 53  
          * @param user
 54  
          * @param pass
 55  
          */
 56  
         public void basic( String user, String pass ) {
 57  12
                 URL url = (URL)builder.getURL();
 58  12
                 if ( url == null ) throw new IllegalStateException( "a default URL must be set" );
 59  12
                 this.basic( url.getHost(), url.getPort(), user, pass );
 60  12
         }
 61  
         
 62  
         /**
 63  
          * Set authentication credentials to be used for the given host and port. 
 64  
          * @param host
 65  
          * @param port
 66  
          * @param user
 67  
          * @param pass
 68  
          */
 69  
         public void basic( String host, int port, String user, String pass ) {
 70  12
                 builder.getClient().getCredentialsProvider().setCredentials( 
 71  
                         new AuthScope( host, port ),
 72  
                         new UsernamePasswordCredentials( user, pass )
 73  
                 );
 74  12
         }
 75  
         
 76  
         /**
 77  
          * Sets a certificate to be used for SSL authentication.  
 78  
          * @param certURL URL to a JKS keystore where the certificate is stored
 79  
          * @param password password to decrypt the keystore
 80  
          */
 81  
         public void certificate( String certURL, String password ) 
 82  
                         throws GeneralSecurityException, MalformedURLException, IOException {
 83  
                 
 84  0
                 KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
 85  0
         InputStream jksStream = new URL(certURL).openStream();
 86  
         try {
 87  0
                 keyStore.load( jksStream, password.toCharArray() );
 88  0
         } finally { jksStream.close(); }
 89  
 
 90  0
         SSLSocketFactory ssl = new SSLSocketFactory(keyStore, password);
 91  0
         ssl.setHostnameVerifier( SSLSocketFactory.STRICT_HOSTNAME_VERIFIER );
 92  
         
 93  0
         builder.getClient().getConnectionManager().getSchemeRegistry()
 94  
                 .register( new Scheme("https", ssl, 443) );
 95  0
         }
 96  
 }