summaryrefslogtreecommitdiffstats
path: root/base/console/src/com/netscape/admin/certsrv/wizard/WizardBasePanel.java
blob: c31767d992e41394fe04da78a0b22fd83cd10161 (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
// --- 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.wizard;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.*;
import javax.swing.*;
import com.netscape.admin.certsrv.*;
import com.netscape.management.client.util.*;
import javax.swing.border.*;
import java.net.*;
import java.io.*;

/**
 * Wizard Base Panel
 *
 * @author Jack Pan-Chen
 * @version $Revision$, $Date$
 * @see com.netscape.admin.certsrv.config
 */
public class WizardBasePanel extends CMSBasePanel implements MouseMotionListener,
  ConfigServlet {

    /*==========================================================
     * variables
     *==========================================================*/
    protected String mTitle;
    protected String mErrorString;
    protected String mNextString =
      mResource.getString("GENERALWIZARD_LABEL_NEXT_LABEL"); 
    protected String mPanelName;
    public static long mSeed;

	/*==========================================================
     * constructors
     *==========================================================*/
    public WizardBasePanel(String name) {
      super(name);
      mTitle = mResource.getString(name+"_TITLE");
      mPanelName = name;
        addMouseMotionListener(this);
    }

    public WizardBasePanel(String name, ResourceBundle rb) {
        super(name, rb);
        mPanelName = name;
        try {
            mTitle = mResource.getString(name+"_TITLE");
        } catch (MissingResourceException e) {
            mTitle = "Missing Title";
        }
        addMouseMotionListener(this);
    }

    protected void init() {

/*
        GridBagConstraints gbc = new GridBagConstraints();
      
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.SOUTHWEST;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.gridheight = gbc.REMAINDER;
        gbc.insets = new Insets(COMPONENT_SPACE,COMPONENT_SPACE,
          COMPONENT_SPACE,COMPONENT_SPACE);
        JLabel nextLabel = new JLabel(mNextString);
        add(nextLabel, gbc);

        setBorder(makeTitledBorder(mPanelName));
*/
    }

    protected JTextArea createTextArea(String str, int row, int col) {
        JTextArea desc = new JTextArea(str, row, col);
        desc.setBackground(getBackground());
        desc.setEditable(false);
        desc.setCaretColor(getBackground());

        return desc;
    }

    protected JTextArea createTextArea(String str) {
        JTextArea desc = new JTextArea(str);
        desc.setBackground(getBackground());
        desc.setEditable(false);
        desc.setCaretColor(getBackground());
        desc.setLineWrap(true);
        desc.setWrapStyleWord(true);

        return desc;
    }

    /*==========================================================
	 * public methods
     *==========================================================*/
    /**
     * Returns the title of the tab
     * @return string representation of the title
     */
    public String getTitle() {
		return mTitle;
	}

	/**
	 * Returns the error string
	 */
    public String getErrorMessage() {
        return  mErrorString;
    }

    /**
     * Set error string
     */
    public void setErrorMessage(String keyword) {
        try {
            String err = mResource.getString(mPanelName+"_DIALOG_"+keyword+"_MESSAGE");
            mErrorString = err;
        } catch (MissingResourceException e) {
            mErrorString = keyword;
        }
    }

    public void cleanUpWizardInfo(WizardInfo wizardInfo) {
        wizardInfo.remove("NMC_WARNINFO");
        wizardInfo.remove("NMC_ERRINFO");
        wizardInfo.remove("NMC_STATUS");
    }
    
    public String getErrorMessage(WizardInfo wizardInfo) {
        String value = (String)wizardInfo.get("NMC_ERRINFO");
        if (value != null || value.trim().length() == 0)
            return value;
        value = (String)wizardInfo.get("NMC_WARNINFO");
        if (value != null || value.trim().length() == 0)
            return value;
      
        return null;
    }

    public boolean send(String host, int port, String servlet, String rawData, 
      WizardInfo wizardInfo) {
        try {
            Socket socket = new Socket(host, port);
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            InputStream is = socket.getInputStream();
            String spost = "POST "+servlet+" HTTP/1.0\r\n";
            byte[] b = rawData.getBytes();
            dos.writeBytes(spost);
            dos.writeBytes("User-Agent: HTTPTool/1.0\r\n");
            dos.writeBytes("Content-length: " + b.length + "\r\n");
            dos.writeBytes("Content-Type: application/x-www-form-urlencoded\r\n");
            dos.writeBytes("\r\n");
            dos.write(b);
            dos.writeBytes("\r\n");
            dos.flush();

            ByteArrayOutputStream bstream = new ByteArrayOutputStream(10000);
            while (true)
            {
                int r = is.read();
                if (r == -1)
                    break;
                bstream.write(r);
            }

            socket.close();
            String test = bstream.toString();

            StringTokenizer tokenizer = new StringTokenizer(test, "\r\n");
            while (tokenizer.hasMoreTokens()) {
                String nvalue = tokenizer.nextToken();
                System.out.println("tokenizer="+nvalue);
                StringTokenizer tokenizer1 = new StringTokenizer(nvalue, ":");
                int numTokens = tokenizer1.countTokens();
                if (numTokens == 2) {
                    String name = tokenizer1.nextToken().trim();
                    String value = tokenizer1.nextToken().trim();
                    wizardInfo.put(name, value);
                }
            }
            bstream.close();
            String sendStatus = (String)wizardInfo.get("NMC_STATUS");
            if (sendStatus.equals("0")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) { 
        }

        return false;
    }

    public boolean send(String rawData, WizardInfo wizardInfo) {
        try {
            Socket socket = new Socket("droopy-linux.sfbay.redhat.com", 1924);
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            InputStream is = socket.getInputStream();
            String servlet = "/config/configSubsystem";
            String spost = "POST "+servlet+" HTTP/1.0\r\n";
            byte[] b = rawData.getBytes();
            dos.writeBytes(spost);
            dos.writeBytes("User-Agent: HTTPTool/1.0\r\n");
            dos.writeBytes("Content-length: " + b.length + "\r\n");
            dos.writeBytes("Content-Type: application/x-www-form-urlencoded\r\n");
            dos.writeBytes("\r\n");
            dos.write(b);
            dos.writeBytes("\r\n");
            dos.flush();

            ByteArrayOutputStream bstream = new ByteArrayOutputStream(10000);
            while (true)
            {
                int r = is.read();
                if (r == -1)
                    break;
                bstream.write(r);
            }

            socket.close();
            String test = bstream.toString();

            StringTokenizer tokenizer = new StringTokenizer(test, "\r\n");
            while (tokenizer.hasMoreTokens()) {
                String nvalue = tokenizer.nextToken();
                System.out.println("tokenizer="+nvalue);
                StringTokenizer tokenizer1 = new StringTokenizer(nvalue, ":");
                int numTokens = tokenizer1.countTokens();
                if (numTokens == 2) {
                    String name = tokenizer1.nextToken().trim();
                    String value = tokenizer1.nextToken().trim();
                    wizardInfo.put(name, value);                        
                }
            }
            bstream.close();
            String sendStatus = (String)wizardInfo.get("NMC_STATUS");
            if (sendStatus.equals("0")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
        }

        return false;
    }

    /*==========================================================
	 * EVENT HANDLER METHODS
     *==========================================================*/

	/*
	 * mouselistener events - for JPanel
	 */

	/**
	 * This lets us know when someone move the mouse, so we can
	 * keep coordidate of mouse posion and use this value as a random seed
	 */
	public void mouseDragged(MouseEvent e) {
		// Do nothing for this
	}

	public void mouseMoved(MouseEvent e) {
		// Keep tracking coordinate values
		long x = e.getX();
		long y = e.getY();
		
		long top = mSeed >> 62;
		mSeed = ((mSeed << 2) ^ top ^ (x<<8) ^ (y)) % Long.MAX_VALUE;
	}
}