summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/config/CMSNetworkPanel.java
blob: ef85e49869a363c334936ae5caea3735dbf6bcd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.admin.certsrv.config;

import com.netscape.admin.certsrv.*;
import com.netscape.admin.certsrv.connection.*;
import com.netscape.certsrv.common.*;
import com.netscape.management.client.util.*;
import com.netscape.management.client.console.ConsoleInfo;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import netscape.ldap.*;

/**
 * Network Connection Setting Tab to be displayed at the right hand side
 *
 * @author Christine Ho
 * @author Jack Pan-Chen
 * @version $Revision$, $Date$
 * @see com.netscape.admin.certsrv.config
 */
public class CMSNetworkPanel extends CMSBaseTab {

    /*==========================================================
     * variables
     *==========================================================*/
    private final static String PANEL_NAME = "NETWORK";
    private final static String DISABLED = "-1";

    private static final int MAX_PORT = 65535;
    private static final int MIN_PORT = 1;

    private Color mActiveColor; 

    // TextField for port
    private JTextField mAdminSSLPortText;
    private JTextField mAgentSSLPortText;
    private JTextField mGatewayPortText;
    private JTextField mGatewaySSLPortText;

    // TextField for Backlog
    private JTextField mAdminSSLBacklogText;
    private JTextField mAgentSSLBacklogText;
    private JTextField mGatewayBacklogText;
    private JTextField mGatewaySSLBacklogText;

    // Label for EE port
    private JLabel mPortLabel;
    private JLabel mEnableLabel;
    private JLabel mBacklogLabel;

    // Label for SSL EE port
    private JLabel mSSLPortLabel;
    private JLabel mSSLBacklogLabel;

    private JCheckBox mEnable;

    private CMSBaseResourceModel mModel;
    protected AdminConnection mAdmin;
    private boolean mBlankFieldError = false;
    private boolean mNumberError = false;
    private CMSTabPanel mParent;
    private static final String HELPINDEX = "configuration-system-network-help";

    /*==========================================================
     * constructors
     *==========================================================*/
    public CMSNetworkPanel(CMSTabPanel parent) {
        super(PANEL_NAME, parent);
        mParent = parent;
        mModel = parent.getResourceModel();
        mHelpToken = HELPINDEX;
    }
    
    /*==========================================================
	 * public methods
     *==========================================================*/

    /**
     * Actual Instanciation of the UI components
     */
    public void init() {
        Debug.println("NetworkPanel: init()");
        mAdmin = mModel.getServerInfo().getAdmin();
		GridBagLayout gb = new GridBagLayout();
	    GridBagConstraints gbc = new GridBagConstraints();
		CMSAdminUtil.resetGBC(gbc);
		mCenterPanel.setLayout(gb);

		// admin panel
        JPanel adminPanel = new JPanel();
        GridBagLayout gb1 = new GridBagLayout();
        adminPanel.setLayout(gb1);
        adminPanel.setBorder(makeTitledBorder("ADMIN"));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTH;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.weightx = 1.0;
        gb.setConstraints(adminPanel, gbc);
        mCenterPanel.add(adminPanel);

        CMSAdminUtil.resetGBC(gbc);
        JLabel adminSSLport = makeJLabel("ADMINSSLPORT");
        mAdminSSLPortText = makeJTextField(10);
        JLabel adminBacklog = makeJLabel("ADMINBACKLOG");
        mAdminSSLBacklogText = makeJTextField(10);
        JLabel dummy1 = new JLabel(" ");
        CMSAdminUtil.addEntryField(adminPanel, adminSSLport, 
          mAdminSSLPortText, adminBacklog, mAdminSSLBacklogText, dummy1, gbc);

        mActiveColor = mAdminSSLPortText.getBackground();

        // gateway panel
        JPanel agentPanel = new JPanel();
        GridBagLayout gb2 = new GridBagLayout();
        agentPanel.setLayout(gb2);
        agentPanel.setBorder(makeTitledBorder("AGENT"));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTH;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.weightx = 1.0;
        gb.setConstraints(agentPanel, gbc);
        mCenterPanel.add(agentPanel);
        
        CMSAdminUtil.resetGBC(gbc);
        JLabel agentPort = makeJLabel("AGENTSSLPORT");
        mAgentSSLPortText = makeJTextField(10);
        JLabel agentBacklog = makeJLabel("SECUREAGENTBACKLOG");
        mAgentSSLBacklogText = makeJTextField(10);
        JLabel dummy = new JLabel(" ");
        CMSAdminUtil.addEntryField(agentPanel, agentPort, mAgentSSLPortText,
          agentBacklog, mAgentSSLBacklogText, dummy, gbc);

        JPanel gatewayPanel = new JPanel();
        GridBagLayout gb3 = new GridBagLayout();
        gatewayPanel.setLayout(gb3);
        gatewayPanel.setBorder(makeTitledBorder("EE"));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTH;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.gridheight = gbc.REMAINDER;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gb.setConstraints(gatewayPanel, gbc);
        mCenterPanel.add(gatewayPanel);

        CMSAdminUtil.resetGBC(gbc);
        mPortLabel = makeJLabel("GATEWAYPORT");
        mGatewayPortText = makeJTextField(10);
        mBacklogLabel = makeJLabel("EEBACKLOG");
        mGatewayBacklogText = makeJTextField(10);
        mEnableLabel = makeJLabel("ENABLED");
        mEnable = makeJCheckBox();
        CMSAdminUtil.addEntryField(gatewayPanel, mPortLabel, mGatewayPortText, 
          mBacklogLabel, mGatewayBacklogText, mEnableLabel, mEnable, gbc);

        CMSAdminUtil.resetGBC(gbc);
        mSSLPortLabel = makeJLabel("GATEWAYSSLPORT");
        mGatewaySSLPortText = makeJTextField(10);
        mSSLBacklogLabel = makeJLabel("SECUREEEBACKLOG");
        mGatewaySSLBacklogText = makeJTextField(10);
        JLabel dummy2 = new JLabel(" ");
        CMSAdminUtil.addEntryField(gatewayPanel, mSSLPortLabel, 
          mGatewaySSLPortText, mSSLBacklogLabel, mGatewaySSLBacklogText,
          dummy2, gbc);
          
        refresh();
    }

    /**
     * Implementation for saving panel information
     * @return true if save successful; otherwise, false.
     */
    public boolean applyCallback() {
        String adminSSLPortStr = mAdminSSLPortText.getText().trim();
        String gatewayPortStr = mGatewayPortText.getText().trim();
        String gatewaySSLPortStr = mGatewaySSLPortText.getText().trim();
        String agentSSLPortStr = mAgentSSLPortText.getText().trim();
        String adminSSLBacklogStr = mAdminSSLBacklogText.getText().trim();
        String gatewayBacklogStr = mGatewayBacklogText.getText().trim();
        String gatewaySSLBacklogStr = mGatewaySSLBacklogText.getText().trim();
        String agentSSLBacklogStr = mAgentSSLBacklogText.getText().trim();
        //String docroot = mDocRootText.getText().trim();

        //check blank fields
        if (adminSSLPortStr.equals("") || 
          (gatewayPortStr.equals("") && mGatewayPortText.isEnabled()) ||
          (gatewaySSLPortStr.equals("") && mGatewaySSLPortText.isEnabled()) || 
          agentSSLPortStr.equals("") || 
          adminSSLBacklogStr.equals("") || 
          (gatewayBacklogStr.equals("") && mGatewayBacklogText.isEnabled()) ||
          (gatewaySSLBacklogStr.equals("") && mGatewaySSLBacklogText.isEnabled()) ||
          agentSSLBacklogStr.equals("") ) { 
            showMessageDialog("BLANKFIELD");
            return false;
        }

        //check format and range number
        int adminSSLPort;
        int gatewayPort;
        int gatewaySSLPort;
        int agentSSLPort;
        int adminSSLBacklog;
        int gatewayBacklog;
        int gatewaySSLBacklog;
        int agentSSLBacklog;

        try {
            adminSSLPort = Integer.parseInt(adminSSLPortStr);
            gatewayPort = Integer.parseInt(gatewayPortStr);
            gatewaySSLPort = Integer.parseInt(gatewaySSLPortStr);
            agentSSLPort = Integer.parseInt(agentSSLPortStr);
            adminSSLBacklog = Integer.parseInt(adminSSLBacklogStr);
            gatewayBacklog = Integer.parseInt(gatewayBacklogStr);
            gatewaySSLBacklog = Integer.parseInt(gatewaySSLBacklogStr);
            agentSSLBacklog = Integer.parseInt(agentSSLBacklogStr);
        } catch (NumberFormatException e) {
            showMessageDialog("NUMBERFORMAT");
            return false;
        }

        if (adminSSLBacklog <= 0 || gatewayBacklog <= 0 ||
          gatewaySSLBacklog <= 0 || agentSSLBacklog <= 0) {
            showMessageDialog("NEGATIVE");
            return false;
        }
        if ((adminSSLPort < MIN_PORT) || (adminSSLPort > MAX_PORT) ||
          (gatewayPort < MIN_PORT) || (gatewayPort > MAX_PORT) ||
          (agentSSLPort < MIN_PORT) || (agentSSLPort > MAX_PORT) ||
          (gatewaySSLPort < MIN_PORT) || (gatewaySSLPort > MAX_PORT)) {
            showMessageDialog("PORTRANGE");
            return false;
        } else {
            mModel.progressStart();
            NameValuePairs nvps = new NameValuePairs();
            nvps.add(Constants.PR_ADMIN_S_PORT, adminSSLPortStr);
            nvps.add(Constants.PR_GATEWAY_PORT, gatewayPortStr);
            nvps.add(Constants.PR_AGENT_S_PORT, agentSSLPortStr);

            if (mGatewaySSLPortText.isEnabled()) {
                nvps.add(Constants.PR_GATEWAY_S_PORT, gatewaySSLPortStr);
            }

            if (mGatewaySSLBacklogText.isEnabled()) {
                nvps.add(Constants.PR_GATEWAY_S_BACKLOG, gatewaySSLBacklogStr);
            }

            if (mEnable.isSelected()) {
                nvps.add(Constants.PR_GATEWAY_PORT_ENABLED, Constants.TRUE);
                nvps.add(Constants.PR_ADMIN_S_BACKLOG, adminSSLBacklogStr);
                nvps.add(Constants.PR_GATEWAY_BACKLOG, gatewayBacklogStr);
                nvps.add(Constants.PR_AGENT_S_BACKLOG, agentSSLBacklogStr);
            } else
                nvps.add(Constants.PR_GATEWAY_PORT_ENABLED, Constants.FALSE);

            try {
                mAdmin.modify(DestDef.DEST_SERVER_ADMIN, ScopeDef.SC_NETWORK,
                  Constants.RS_ID_CONFIG, nvps);
            } catch (EAdminException e) {
                showErrorDialog(e.toString());
                mModel.progressStop();
                return false;
            }

            ConsoleInfo consoleInfo = mModel.getConsoleInfo();
            LDAPConnection conn = consoleInfo.getLDAPConnection();
            try {
                LDAPAttribute attr = new LDAPAttribute("nsserverport", adminSSLPortStr);
                LDAPModification singleChange = new LDAPModification(LDAPModification.REPLACE,
                  attr);
                conn.modify(consoleInfo.getCurrentDN(), singleChange);
            } catch (Exception eee) {
            }
            mModel.progressStop();
        }

        clearDirtyFlag();
        return true;
    }

    /**
     * Implementation for reset values
     * @return true if save successful; otherwise, false.
     */
    public boolean resetCallback() {
        refresh();
        //clearDirtyFlag();
        return true;
    }

    /**
	 * refresh the panel and update data
	 */
    public void refresh() {
        mModel.progressStart();
        NameValuePairs nvps = new NameValuePairs();
        nvps.add(Constants.PR_ADMIN_S_PORT, "");
        nvps.add(Constants.PR_AGENT_S_PORT, "");
        nvps.add(Constants.PR_GATEWAY_S_PORT, "");
        nvps.add(Constants.PR_GATEWAY_PORT, "");
        nvps.add(Constants.PR_GATEWAY_PORT_ENABLED, "");

        try {
            NameValuePairs val = mAdmin.read(DestDef.DEST_SERVER_ADMIN,
              ScopeDef.SC_NETWORK, Constants.RS_ID_CONFIG, nvps);

            populate(val);
        } catch (EAdminException e) {
            showErrorDialog(e.toString());
            mModel.progressStop();
        }
        mModel.progressStop();
        clearDirtyFlag();
        mParent.setOKCancel();
    }

    public void actionPerformed(ActionEvent e) {
        super.actionPerformed(e);
        if (e.getSource().equals(mEnable)) {
            if (mEnable.isSelected()) {
                enableFields(true, mActiveColor);
            } else {
                enableFields(false, getBackground());
            }
        }
    }

    private void populate(NameValuePairs nvps) {
        for (int i=0; i<nvps.size(); i++) {
            NameValuePair nvp = nvps.elementAt(i);
            String name = nvp.getName();
            String str = nvp.getValue();
            
            if (name.equals(Constants.PR_GATEWAY_PORT_ENABLED)) {
                mEnable.setSelected(getBoolean(str));
            } else {
                if (!validate(str))
                    continue;

                if (name.equals(Constants.PR_AGENT_S_PORT)) {
                    mAgentSSLPortText.setText(str);
                } else if (name.equals(Constants.PR_ADMIN_S_PORT)) {
                    mAdminSSLPortText.setText(str);
                } else if (name.equals(Constants.PR_GATEWAY_S_PORT)) {
                    if (str.equals(DISABLED)) {
                        mGatewaySSLPortText.setText("");
                        enableFields(mSSLPortLabel, mGatewaySSLPortText, false,
                          getBackground());
                    } else {
                        mGatewaySSLPortText.setText(str);
                        enableFields(mSSLPortLabel, mGatewaySSLPortText, true,
                          mActiveColor);
                    }
                } else if (name.equals(Constants.PR_GATEWAY_PORT)) {
                    if (str.equals(DISABLED)) {
                        mGatewayPortText.setText("");
                        enableFields(mEnable, false);
                    } else {
                        mGatewayPortText.setText(str);
                        enableFields(mEnable, true);
                    }
                } else if (name.equals(Constants.PR_ADMIN_S_BACKLOG)) {
                    mAdminSSLBacklogText.setText(str);
                } else if (name.equals(Constants.PR_AGENT_S_BACKLOG)) {
                    mAgentSSLBacklogText.setText(str);
                } else if (name.equals(Constants.PR_GATEWAY_S_BACKLOG)) {
                    if (str.equals(DISABLED)) {
                        enableFields(mSSLBacklogLabel, mGatewaySSLBacklogText, 
                          false, getBackground());
                        mGatewaySSLBacklogText.setText("");
                    } else {
                        enableFields(mSSLBacklogLabel, mGatewaySSLBacklogText, 
                          true, mActiveColor);
                        mGatewaySSLBacklogText.setText(str);
                    }
                } else if (name.equals(Constants.PR_GATEWAY_BACKLOG)) {
                    if (str.equals(DISABLED)) {
                        enableFields(mEnable, false);
                        mGatewayBacklogText.setText("");
                    } else {
                        enableFields(mEnable, true);
                        mGatewayBacklogText.setText(str);
                    }
                }
            }
        }

        if (mEnable.isSelected())
            enableFields(true, mActiveColor);
        else
            enableFields(false, getBackground());
    }

    private boolean getBoolean(String val) {
        if (val.equals(Constants.TRUE))
            return true;
        return false;
    }

    private void enableFields(boolean enabled, Color color) {
        mGatewayPortText.setEnabled(enabled);
        mGatewayPortText.setEditable(enabled);
        mGatewayPortText.setBackground(color);
        mGatewayBacklogText.setEnabled(enabled);
        mGatewayBacklogText.setEditable(enabled);
        mGatewayBacklogText.setBackground(color);
        mPortLabel.setEnabled(enabled);
        mBacklogLabel.setEnabled(enabled);

        invalidate();
        validate();
        repaint(1);
    }

    private void enableFields(JLabel label, JTextComponent text, 
      boolean enabled, Color color) {
        label.setEnabled(enabled);
        text.setEnabled(enabled);
        text.setEditable(enabled);
        text.setBackground(color);
        CMSAdminUtil.repaintComp(label);
        CMSAdminUtil.repaintComp(text);
    }

    private void enableFields(JCheckBox comp, boolean enabled) {
        comp.setEnabled(enabled);
        CMSAdminUtil.repaintComp(comp);
    }

    private boolean validate(String str) {
        if (str.equals("")) {
            if (!mBlankFieldError) {
                showMessageDialog("BLANKFIELD");
                mBlankFieldError = true;
            }
            return false;
        }

        try {
            int sslPort = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            if (!mNumberError) {
                showMessageDialog("NUMBERFORMAT");
                mNumberError = true;
            }
            return false;
        }
        return true;
    }
}