summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/task/CMSConfigCert.java
blob: 7d85d161fa341be83133ee94c082fc39b0f0836b (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
// --- 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.task;

import java.util.*;
import javax.swing.*;
import com.netscape.management.client.*;
import com.netscape.admin.certsrv.*;
import com.netscape.admin.certsrv.config.install.*;
import com.netscape.admin.certsrv.wizard.WizardBasePanel;
import com.netscape.certsrv.common.*;
import com.netscape.management.client.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.netscape.management.client.console.*;
import com.netscape.management.client.topology.*;
import com.netscape.management.client.comm.*;
import java.net.*;
import java.io.*;
import netscape.ldap.*;
import netscape.ldap.util.*;

/**
 * Perform certificate server configuration.
 *
 * @author Christine Ho
 * @version $Revision$, $Date$
 */
public class CMSConfigCert extends CGITask {

    /*==========================================================
     * variables
     *==========================================================*/
    private static final String PREFIX = "CMSCONFIGCERT";
    
    public static final String CONFIG_CERT_CGI = "Tasks/Operation/config-cert";
    
	//private boolean mSuccess = false; // status of last executed CGI
	//private Hashtable mCgiResponse = null; // holds parsed contents of CGI return
	private String mCgiTask = null; // CGI task to call
    private InstallWizardInfo mWizardInfo;
	
	/*==========================================================
     * constructors
     *==========================================================*/
	public CMSConfigCert() {
		super();
	}

    /*==========================================================
	 * public methods
     *==========================================================*/
    public void initialize(InstallWizardInfo info) {
        _consoleInfo = info.getAdminConsoleInfo();

        // the results coming back from the daemon will be added to the 
        // wizard information.
        mWizardInfo = info;

		setForceBasicAuth(true);
    }
    
    /**
     * Collect the data in name value pairs format and then send them to the 
     * cgi process.
	 */
	public boolean configCert(Hashtable data) {
        JFrame mActiveFrame = UtilConsoleGlobals.getActivatedFrame();
        if (_consoleInfo.get("AdminUsername") == null)
            _consoleInfo.put("AdminUsername", _consoleInfo.getAuthenticationDN());
        Debug.println("AdminUsername = " + _consoleInfo.get("AdminUsername"));

        if (_consoleInfo.get("AdminUserPassword") == null)
            _consoleInfo.put("AdminUserPassword",
                             _consoleInfo.getAuthenticationPassword());
        Debug.println("AdminUserPassword = " + _consoleInfo.get("AdminUserPassword")); 
        data.put("AdminUserPassword", _consoleInfo.getAuthenticationPassword());
        _consoleInfo.put("arguments", data);

		// Send Random value for RNG entropy          
        data.put(ConfigConstants.PR_CMS_SEED, new Long(WizardBasePanel.mSeed).toString());
          
	    boolean status = false; // return value
	    
        Cursor cursor = mActiveFrame.getCursor();
        int type = cursor.getType();
        cursor = new Cursor(Cursor.WAIT_CURSOR);
        mActiveFrame.setCursor(cursor);

		try {
			status = super.run(null, CONFIG_CERT_CGI);
		} catch (Exception e) {
			Debug.println("Unexpected Error"+e.toString());
			status = false;
		}
		Debug.println("CMSConfigCert: configCert() after run status=" +
					  status + " mSuccess=" + mSuccess);

        if (!mSuccess) {
        Debug.println("Show error dialog");
            String errorMsg = getErrorMessage();
/*
            if (errorMsg == null || errorMsg.equals(""))
                CMSAdminUtil.showMessageDialog(mActiveFrame, mResource, PREFIX,
                    "SYSTEMERROR", CMSAdminUtil.ERROR_MESSAGE);
            else
                JOptionPane.showMessageDialog(mActiveFrame, errorMsg,
                  "Error", CMSAdminUtil.ERROR_MESSAGE,
                  CMSAdminUtil.getImage(CMSAdminResources.IMAGE_ERROR_ICON));
*/
        }

        cursor = new Cursor(type);
        mActiveFrame.setCursor(cursor);

		return mSuccess;
	}
   
    /**
	 *	the operation is finished after we receive the http stream
	 */
    public void replyHandler(InputStream response, CommRecord cr) {
        mSuccess = false;
/*
		if (mCgiResponse != null)
			mCgiResponse.clear();
*/
			
        try {
			BufferedReader rspStream =
				new BufferedReader(new InputStreamReader(response, "UTF8"));
			String rspStr;

			Debug.println("CMSConfigCert: replyHandler() - start");
			while ((rspStr = rspStream.readLine()) != null)
			{
				Debug.println("ConfigCert: replyHandler() - read [" + rspStr + "]");
				// NMC_ messages are parsed, but not shown to the user
				/*
				if (_statusText != null && !rspStr.startsWith("NMC_")) {
					_statusText.append(rspStr + "\n");
					Thread.yield(); // allow graphics repaints
				}
				*/
				parse(rspStr);
			}
		} catch (Exception e) {
			Debug.println("ConfigCert.Exception : " + e.toString());
		}
		
		Debug.println("ConfigCert.replyHandler: finished, mSuccess=" +
					  mSuccess);

        finish();
    }
    
    /**
	 * return the value for the given keyword in the reply
	 */
	private void parse(String s) {
		String sName;
		String sValue;
		int iIndex;

		Debug.println("Parse input: " + s);

		if ((iIndex=s.indexOf(":")) != (-1))
		{
			sName = s.substring(0, iIndex).trim();
			sValue = s.substring(iIndex+1).trim();
			Debug.println("Parse input: name=" + sName + " value=" + sValue);
            mWizardInfo.put(sName, sValue);
/*
			if (mCgiResponse == null)
				mCgiResponse = new Hashtable();
			mCgiResponse.put(sName, sValue);
*/
			if (sName.equalsIgnoreCase("NMC_Status"))
			{
				int code = Integer.parseInt(sValue);
				mSuccess = (code == 0);
				Debug.println("Parse input: code=" + code + " mSuccess=" + mSuccess);
			} else if (sName.equalsIgnoreCase("NMC_ERRINFO"))
                mErrorMsg = sValue;
    			Debug.println("ErrorMsg : " + mErrorMsg);
		}

		Debug.println("Parse finished");
    }
}