Skip to Content.
Sympa Menu

notify-dpml - svn commit: r2656 - in development/main/transit/core/handler/src: main/net/dpml/transit/manager test/net/dpml/transit/manager

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: mcconnell AT dpml.net
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r2656 - in development/main/transit/core/handler/src: main/net/dpml/transit/manager test/net/dpml/transit/manager
  • Date: Mon, 23 May 2005 07:20:37 +0000

Author: mcconnell AT dpml.net
Date: Mon May 23 07:20:35 2005
New Revision: 2656

Added:

development/main/transit/core/handler/src/main/net/dpml/transit/manager/CredentialsHelper.java
Modified:

development/main/transit/core/handler/src/main/net/dpml/transit/manager/ProxyManager.java

development/main/transit/core/handler/src/test/net/dpml/transit/manager/ProxyManagerTestCase.java
Log:
Updates dealing with proxy password handling.

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/manager/CredentialsHelper.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/manager/CredentialsHelper.java
Mon May 23 07:20:35 2005
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2005 Stephen J. McConnell
+ *
+ * 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 net.dpml.transit.manager;
+
+import java.net.PasswordAuthentication;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+
+import net.dpml.io.CascadingIOException;
+
+
+/**
+ * Helper class used to convert credentials to and from byte arrays.
+ *
+ * @author <a href="mailto:dev-dpml AT lists.ibiblio.org";>The Digital Product
Meta Library</a>
+ * @version $Revision: 1.2 $ $Date: 2004/03/17 10:30:09 $
+ */
+class CredentialsHelper
+{
+ private CredentialsHelper()
+ {
+ // static utility class
+ }
+
+ public static byte[] exportCredentials( PasswordAuthentication auth )
throws IOException
+ {
+ CrendentialsHolder holder = new CrendentialsHolder( auth );
+ return toByteArray( holder );
+ }
+
+ public static PasswordAuthentication importCredentials( byte[] bytes )
throws IOException
+ {
+ try
+ {
+ ByteArrayInputStream input = new ByteArrayInputStream( bytes );
+ ObjectInputStream stream = new ObjectInputStream( input );
+ CrendentialsHolder holder = (CrendentialsHolder)
stream.readObject();
+ return holder.getPasswordAuthentication();
+ }
+ catch( IOException e )
+ {
+ throw e;
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Unexpected error while attempting to load object input
stream.";
+ throw new CascadingIOException( error, e );
+ }
+ }
+
+ public static byte[] toByteArray( Serializable object ) throws
IOException
+ {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ ObjectOutputStream output = null;
+ try
+ {
+ output = new ObjectOutputStream( stream );
+ output.writeObject( object );
+ return stream.toByteArray();
+ }
+ catch( IOException ioe )
+ {
+ throw ioe;
+ }
+ catch( Throwable e )
+ {
+ final String error =
+ "Unexpected error while attempting to write object to a byte
array."
+ + "\nclass: " + object.getClass().getName()
+ + "\nreason: " + e.toString();
+ throw new CascadingIOException( error, e );
+ }
+ finally
+ {
+ closeStream( output );
+ }
+ }
+
+ private static void closeStream( OutputStream out )
+ {
+ if( null != out )
+ {
+ try
+ {
+ out.close();
+ }
+ catch( IOException ioe )
+ {
+ boolean ignoreMe = true;
+ }
+ }
+ }
+
+ private static class CrendentialsHolder implements Serializable
+ {
+ static final long serialVersionUID = 1L;
+
+ private final String m_username;
+ private final char[] m_password;
+
+ CrendentialsHolder( PasswordAuthentication auth )
+ {
+ m_username = auth.getUserName();
+ m_password = auth.getPassword();
+ }
+
+ public PasswordAuthentication getPasswordAuthentication()
+ {
+ return new PasswordAuthentication( m_username, m_password );
+ }
+
+ public boolean equals( Object other )
+ {
+ if( null == other )
+ {
+ return false;
+ }
+ if( false == ( other instanceof CrendentialsHolder ) )
+ {
+ return false;
+ }
+ CrendentialsHolder holder = (CrendentialsHolder) other;
+ if( false == equals( m_username, holder.m_username ) )
+ {
+ return false;
+ }
+ if( false == equals( m_password , holder.m_password ) )
+ {
+ return false;
+ }
+ return true;
+ }
+
+ public int hashCode()
+ {
+ int hash = m_username.hashCode();
+ hash ^= new String( m_password ).hashCode();
+ return hash;
+ }
+
+ private boolean equals( String s1, String s2 )
+ {
+ if( null == s1 )
+ {
+ return null == s2;
+ }
+ else
+ {
+ return s1.equals( s2 );
+ }
+ }
+
+ private boolean equals( char[] s1, char[] s2 )
+ {
+ if( null == s1 )
+ {
+ return null == s2;
+ }
+ else if( null == s2 )
+ {
+ return null == s1;
+ }
+ else if( s1.length != s2.length )
+ {
+ return false;
+ }
+ else
+ {
+ for( int i=0; i<s1.length; i++ )
+ {
+ char c = s1[i];
+ if( c != s2[i] )
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+
+ }
+}

Modified:
development/main/transit/core/handler/src/main/net/dpml/transit/manager/ProxyManager.java
==============================================================================
---
development/main/transit/core/handler/src/main/net/dpml/transit/manager/ProxyManager.java
(original)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/manager/ProxyManager.java
Mon May 23 07:20:35 2005
@@ -19,6 +19,7 @@
package net.dpml.transit.manager;

import java.io.File;
+import java.io.IOException;
import java.util.prefs.Preferences;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.PreferenceChangeEvent;
@@ -116,6 +117,18 @@
}
}

+ /**
+ * Set the proxy authentication.
+ * @param the password authenticatoion to use for proxy sign-on
+ */
+ public void setProxyAuthentication( PasswordAuthentication auth ) throws
IOException
+ {
+ m_authentication = auth;
+ Preferences prefs = getPreferences();
+ byte[] bytes = CredentialsHelper.exportCredentials( m_authentication
);
+ prefs.putByteArray( "authentication", bytes );
+ }
+
public void addExclude( String key )
{
Preferences excludes = getExcludesPreferences();
@@ -217,18 +230,28 @@

private void setupProxyConfiguration( Preferences prefs, boolean notify )
{
- m_excludes = getProxyExcludesFromPrefs();
+ m_excludes = getProxyExcludesFromPrefs();
+ try
+ {
+ m_authentication = resolveProxyPasswordAuthentication( prefs );
+ }
+ catch( IOException e )
+ {
+ final String error =
+ "Unexpected error while resolving proxy credentials.";
+ getLogger().log( Level.SEVERE, error, e );
+ m_authentication = NULL_AUTHENTICATION;
+ }
+
if( null != prefs.get( "host", null ) )
{
try
{
RequestIdentifier identifier = resolveRequestIdentifier(
prefs );
- PasswordAuthentication authentication =
resolveProxyPasswordAuthentication( prefs );
String excludes = resolveProxyExcludes( m_excludes );
synchronized( this )
{
m_identifier = identifier;
- m_authentication = authentication;
m_excludesString = excludes;
}
}
@@ -242,7 +265,6 @@
else
{
m_identifier = null;
- m_authentication = null;
m_excludesString = null;
}
if( notify )
@@ -300,17 +322,17 @@
dispose();
}

- private PasswordAuthentication resolveProxyPasswordAuthentication(
Preferences prefs )
+ private PasswordAuthentication resolveProxyPasswordAuthentication(
Preferences prefs )
+ throws IOException
{
- String username = prefs.get( "username", null );
- if( null != username )
+ byte[] bytes = prefs.getByteArray( "authentication", new byte[0] );
+ if( bytes.length == 0 )
{
- String password = prefs.get( "password", "" );
- return new PasswordAuthentication( username,
password.toCharArray() );
+ return NULL_AUTHENTICATION;
}
else
{
- return null;
+ return CredentialsHelper.importCredentials( bytes );
}
}

@@ -446,4 +468,8 @@
ProxyExcludesEvent event = new ProxyExcludesEvent( this, m_excludes
);
enqueueEvent( event );
}
+
+ private static final PasswordAuthentication NULL_AUTHENTICATION =
+ new PasswordAuthentication( "", new char[0] );
+
}

Modified:
development/main/transit/core/handler/src/test/net/dpml/transit/manager/ProxyManagerTestCase.java
==============================================================================
---
development/main/transit/core/handler/src/test/net/dpml/transit/manager/ProxyManagerTestCase.java
(original)
+++
development/main/transit/core/handler/src/test/net/dpml/transit/manager/ProxyManagerTestCase.java
Mon May 23 07:20:35 2005
@@ -96,18 +96,19 @@

public void testProxyAuthentication() throws Exception
{
+ String username = "peter";
+ String password = "rabbit";
+ PasswordAuthentication auth =
+ new PasswordAuthentication( username, password.toCharArray() );
Preferences prefs = m_prefs.node( "proxy" );
- TestProxyAuthListener listener = new TestProxyAuthListener();
+ TestProxyAuthListener listener = new TestProxyAuthListener( auth );
m_manager.getProxyModel().addProxyListener( listener );
try
{
- String username = "peter";
- String password = "rabbit";
prefs.put( "host", "http://proxy.dpml.net"; );
prefs.put( "scheme", "a-scheme" );
prefs.put( "prompt", "a-prompt" );
- prefs.put( "username", username );
- prefs.put( "password", password );
+ m_manager.getProxyManager().setProxyAuthentication( auth );
prefs.putLong( "modified", new Date().getTime() );
prefs.flush();
}
@@ -173,14 +174,21 @@

public class TestProxyAuthListener implements ProxyListener
{
+ private PasswordAuthentication m_auth;
+ public TestProxyAuthListener( PasswordAuthentication auth )
+ {
+ m_auth = auth;
+ }
+
public void proxySettingsChanged( ProxyChangeEvent event )
{
event.getProxyModel().removeProxyListener( this );
PasswordAuthentication auth = event.getPasswordAuthentication();
assertNotNull( "auth", auth );
- assertEquals( "username", "peter", auth.getUserName() );
- String pwd = new String( auth.getPassword() );
- assertEquals( "password", "rabbit", pwd );
+ assertEquals( "username", m_auth.getUserName(),
auth.getUserName() );
+ assertEquals( "password",
+ new String( m_auth.getPassword() ),
+ new String( auth.getPassword() ) );
}

public void proxyExcludesChanged( ProxyExcludesEvent event )



  • svn commit: r2656 - in development/main/transit/core/handler/src: main/net/dpml/transit/manager test/net/dpml/transit/manager, mcconnell, 05/23/2005

Archive powered by MHonArc 2.6.24.

Top of Page