code: public static URLConnection prepareForAuthentication( final URLConnection connection )
      {
          NullArgumentException.validateNotNull( connection, "url connection cannot be null" );
          if( connection.getURL().getUserInfo() != null )
          {
              // Need to decode username/password because it may contain encoded characters (http://www.w3schools.com/tags/ref_urlencode.asp)
              // A common encoding is to provide a username as an email address like user%40domain.org
              String decodedUserInfo = decode( connection.getURL().getUserInfo() );
              
              String base64Encoded = Base64Encoder.encode( decodedUserInfo );
              // sun bug 6459815: Long passwords cause Basic Auth to fail with a java.net.Authenticator
              base64Encoded = base64Encoded.replaceAll( "\n", "" );
              connection.setRequestProperty( "Authorization", "Basic " + base64Encoded );
          }
          return connection;
      }
docs: Prepares an url connection for authentication if necessary.
  
  @param connection the connection to be prepared
  
  @return the prepared conection