001         package com.croftsoft.apps.agoracast.c2p;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.io.*;
006         import java.util.*;
007         import javax.swing.*;
008    
009         import com.croftsoft.core.lang.NullArgumentException;
010         import com.croftsoft.core.net.news.NntpConstants;
011         import com.croftsoft.core.net.news.NntpLib;
012         import com.croftsoft.core.net.news.NntpSocket;
013         import com.croftsoft.core.net.news.UsenetMessage;
014         import com.croftsoft.core.text.DateFormatLib;
015         import com.croftsoft.core.util.log.Log;
016    
017         /*********************************************************************
018         *
019         * <p />
020         *
021         * @version
022         *   2001-08-02
023         * @since
024         *   2001-07-30
025         * @author
026         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
027         *********************************************************************/
028    
029         public final class  AgoracastSendPanel
030           extends JPanel
031           implements ActionListener, Log, Runnable
032         //////////////////////////////////////////////////////////////////////
033         //////////////////////////////////////////////////////////////////////
034         {
035    
036         private final AgoracastMediator    agoracastMediator;
037    
038         private final AgoracastPostPanel   agoracastPostPanel;
039    
040         private final JTextArea            jTextArea;
041    
042         private final JButton              cancelButton;
043    
044         //
045    
046         private UsenetMessage   usenetMessage;
047    
048         private NntpSocket      nntpSocket;
049    
050         //////////////////////////////////////////////////////////////////////
051         //////////////////////////////////////////////////////////////////////
052    
053         public  AgoracastSendPanel (
054           AgoracastMediator   agoracastMediator,
055           AgoracastPostPanel  agoracastPostPanel )
056         //////////////////////////////////////////////////////////////////////
057         {
058           super ( new BorderLayout ( ), true ); // isDoubleBuffered
059    
060           NullArgumentException.check (
061             this.agoracastMediator = agoracastMediator );
062    
063           NullArgumentException.check (
064             this.agoracastPostPanel = agoracastPostPanel );
065    
066           AgoracastLib.setColor ( this, agoracastMediator );
067    
068           add ( new JScrollPane ( jTextArea = new JTextArea ( ) ),
069             BorderLayout.CENTER );
070    
071           jTextArea.setEditable ( false );
072    
073           jTextArea.setFont ( AgoracastConstants.LOG_FONT );
074    
075           cancelButton = new JButton ( "Cancel" );
076    
077           cancelButton.addActionListener ( this );
078    
079           add ( cancelButton, BorderLayout.SOUTH );
080         }
081    
082         //////////////////////////////////////////////////////////////////////
083         //////////////////////////////////////////////////////////////////////
084    
085         public synchronized void  send ( UsenetMessage  usenetMessage )
086         //////////////////////////////////////////////////////////////////////
087         {
088           this.usenetMessage = usenetMessage;
089    
090           new Thread ( this ).start ( );
091         }
092    
093         public synchronized void  run ( )
094         //////////////////////////////////////////////////////////////////////
095         {
096           jTextArea.setText (
097             DateFormatLib.toIsoDateFormat ( new Date ( ) )
098             + " Sending message..." );
099    
100           cancelButton.setText ( "Cancel" );
101    
102           try
103           {
104             post ( );
105           }
106           catch ( Throwable  throwable )
107           {
108             record ( throwable );
109           }
110    
111           cancelButton.setText ( "Done" );  
112         }
113    
114         public void  actionPerformed ( ActionEvent  actionEvent )
115         //////////////////////////////////////////////////////////////////////
116         {
117           if ( nntpSocket != null )
118           {
119             try
120             {
121               nntpSocket.close ( );
122             }
123             catch ( IOException  ex )
124             {
125             }
126           }
127    
128           agoracastPostPanel.cancel ( );
129         }
130    
131         //////////////////////////////////////////////////////////////////////
132         //////////////////////////////////////////////////////////////////////
133    
134         public synchronized void  record ( String  message )
135         //////////////////////////////////////////////////////////////////////
136         {
137           jTextArea.append ( '\n' + message );
138    
139           trim ( );     
140         }
141    
142         public synchronized void  record ( Throwable  throwable )
143         //////////////////////////////////////////////////////////////////////
144         {
145           record ( '\n' + throwable.toString ( ) );
146         }
147    
148    
149         public synchronized void  record (
150           String  message, Throwable  throwable )
151         //////////////////////////////////////////////////////////////////////
152         {
153           record ( '\n' + message + '\n' + throwable.toString ( ) );
154         }
155    
156         //////////////////////////////////////////////////////////////////////
157         //////////////////////////////////////////////////////////////////////
158    
159         private synchronized void  trim ( )
160         //////////////////////////////////////////////////////////////////////
161         {
162           String  text = jTextArea.getText ( );
163    
164           int  textLength = text.length ( );
165    
166           if ( textLength > AgoracastConstants.LOG_TEXT_LENGTH_MAX )
167           {
168             jTextArea.setText ( text.substring (
169               textLength - AgoracastConstants.LOG_TEXT_LENGTH_MAX ) );
170           }
171         }
172    
173         private synchronized void  post ( )
174           throws IOException
175         //////////////////////////////////////////////////////////////////////
176         {
177           String  nntpServer = agoracastMediator.getServer ( );
178    
179           String  responseCode = null;
180    
181           try
182           {
183             nntpSocket = new NntpSocket ( nntpServer, this );
184    
185             responseCode = nntpSocket.getResponseCode ( );
186    
187             if ( !nntpSocket.getPostingAllowed ( ) )
188             {
189               throw new IOException ( responseCode );
190             }
191    
192             responseCode = nntpSocket.command ( NntpConstants.COMMAND_POST );
193    
194             if ( responseCode.startsWith ( "480" ) )
195             {
196               // 480 Authentication Required
197    
198               AgoracastLib.authenticate ( nntpSocket, agoracastMediator );
199    
200               // throws SecurityException if it fails
201    
202               responseCode = nntpSocket.command ( NntpConstants.COMMAND_POST );
203             }
204    
205             if ( !responseCode.startsWith ( "340" ) )
206             {
207               throw new IOException ( responseCode );
208             }
209    
210             responseCode = nntpSocket.command ( usenetMessage.toString ( ) );
211    
212             if ( !responseCode.startsWith ( "240" ) )
213             {
214               throw new IOException ( responseCode );
215             }
216    
217             responseCode = nntpSocket.command_QUIT ( );
218           }
219           finally
220           {
221             if ( nntpSocket != null )
222             {
223               nntpSocket.close ( );
224             }
225           }
226         }
227    
228         //////////////////////////////////////////////////////////////////////
229         //////////////////////////////////////////////////////////////////////
230         }