001 package com.croftsoft.apps.agoracast.c2p;
002
003 import java.awt.*;
004 import java.io.*;
005 import javax.swing.*;
006
007 import com.croftsoft.core.gui.IdentifierDialog;
008 import com.croftsoft.core.lang.StringLib;
009 import com.croftsoft.core.net.news.NntpConstants;
010 import com.croftsoft.core.net.news.NntpLib;
011 import com.croftsoft.core.net.news.NntpSocket;
012 import com.croftsoft.core.security.Authentication;
013 import com.croftsoft.core.security.Identifier;
014
015 /*********************************************************************
016 *
017 * <p />
018 *
019 * @version
020 * 2001-09-12
021 * @since
022 * 2001-07-26
023 * @author
024 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
025 *********************************************************************/
026
027 public final class AgoracastLib
028 //////////////////////////////////////////////////////////////////////
029 //////////////////////////////////////////////////////////////////////
030 {
031
032 public static void authenticate (
033 NntpSocket nntpSocket,
034 AgoracastMediator agoracastMediator )
035 throws IOException
036 //////////////////////////////////////////////////////////////////////
037 {
038 boolean isAuthenticated = false;
039
040 while ( !isAuthenticated )
041 {
042 String username = agoracastMediator.getUsername ( );
043
044 String password = agoracastMediator.getPassword ( );
045
046 Authentication authentication = null;
047
048 if ( ( username != null )
049 && !"".equals ( username.trim ( ) )
050 && ( password != null )
051 && !"".equals ( password.trim ( ) ) )
052 {
053 authentication = new Authentication ( username, password );
054 }
055 else
056 {
057 Frame parentFrame = agoracastMediator.getParentFrame ( );
058
059 authentication
060 = promptForAuthentication ( agoracastMediator, parentFrame );
061
062 if ( authentication != null )
063 {
064 agoracastMediator.setUsername ( authentication.getUsername ( ) );
065
066 agoracastMediator.setPassword ( authentication.getPassword ( ) );
067 }
068 }
069
070 if ( authentication != null )
071 {
072 try
073 {
074 NntpLib.authenticate (
075 nntpSocket,
076 authentication.getUsername ( ),
077 authentication.getPassword ( ) );
078
079 isAuthenticated = true;
080 }
081 catch ( SecurityException ex )
082 {
083 agoracastMediator.setPassword ( null );
084 }
085 }
086 else
087 {
088 throw new SecurityException ( "authentication canceled" );
089 }
090 }
091 }
092
093 public static Authentication promptForAuthentication (
094 AgoracastMediator agoracastMediator,
095 Frame parentFrame )
096 //////////////////////////////////////////////////////////////////////
097 {
098 IdentifierDialog identifierDialog = new IdentifierDialog (
099 parentFrame,
100 AgoracastConstants.IDENTIFIER_DIALOG_TITLE,
101 agoracastMediator.getUsername ( ),
102 agoracastMediator.getPanelBackgroundColor ( ),
103 agoracastMediator.getTextFieldBackgroundColor ( ) );
104
105 Authentication authentication
106 = identifierDialog.getAuthentication ( );
107
108 identifierDialog.dispose ( );
109
110 return authentication;
111 }
112
113 public static void setColor (
114 JPanel jPanel,
115 AgoracastMediator agoracastMediator )
116 //////////////////////////////////////////////////////////////////////
117 {
118 Color panelBackgroundColor
119 = agoracastMediator.getPanelBackgroundColor ( );
120
121 if ( panelBackgroundColor != null )
122 {
123 jPanel.setBackground ( panelBackgroundColor );
124 }
125 }
126
127 public static void setColor (
128 JTextField jTextField,
129 AgoracastMediator agoracastMediator )
130 //////////////////////////////////////////////////////////////////////
131 {
132 Color textFieldBackgroundColor
133 = agoracastMediator.getTextFieldBackgroundColor ( );
134
135 if ( textFieldBackgroundColor != null )
136 {
137 jTextField.setBackground ( textFieldBackgroundColor );
138 }
139 }
140
141 //////////////////////////////////////////////////////////////////////
142 //////////////////////////////////////////////////////////////////////
143
144 private AgoracastLib ( ) { }
145
146 //////////////////////////////////////////////////////////////////////
147 //////////////////////////////////////////////////////////////////////
148 }