summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/security/Comm.java
blob: 1bbd022bf2f94a9e53de7697b5e297fb5639083b (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
// --- 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.security;

import java.io.*;
import java.net.*;
import java.util.*;

import com.netscape.management.client.comm.*;
import com.netscape.management.client.util.*;

/**
 *
 * Extends dt's comm package to do some communication with backend.
 * eventually this will be phase out, and key cert related tasks that
 * require cgi call will use AdmTask.java instead of this one.
 *
 * @version    1.0    98/07/10
 * @author     <A HREF="mailto:shihcm@netscape.com">shihcm@netscape.com</A>
 *
 */
class Comm implements CommClient, Runnable {
    public static final int DEFAULT_TIMEOUT_PERIOD = 30000; // 30 seconds
    public boolean finished = false;
    public String value = null;


    static String server_response = null;

    String url_cgi;
    Hashtable cgi_arg;
    boolean waitForResponse;

    String id = "Admin";
    String pw = "Admin";

    Exception error = null;

    public Comm(String url_cgi, Hashtable cgi_arg,
            boolean waitForResponse) {
        this.url_cgi = url_cgi;
        this.cgi_arg = cgi_arg;
        this.waitForResponse = waitForResponse;
    }


    public void setAuth(String userName, String password) {
        this.id = userName;
        this.pw = password;
    }

    public Exception getError() {
        return error;
    }

    public static String getData() {
        return server_response;
    }

    public void run() {
        HttpManager h = new HttpManager();

        try {
            ByteArrayInputStream value = HttpChannel.encode(cgi_arg);
            h.post(new URL(url_cgi), this, null, value,
                    value == null ? 0 : value.available(),
                    CommManager.FORCE_BASIC_AUTH);
            awaitValue();
        } catch (InterruptedIOException timeout) {
            error = timeout;
        }
        catch (ConnectException connectError) {
            error = connectError;
        }
        catch (IOException ioError) {
            error = ioError;
        }
        catch (Exception e) {
            error = e;
        }
    }

    public synchronized void awaitValue() {
        try {
            wait(DEFAULT_TIMEOUT_PERIOD);
        } catch (Exception e) {
            error = e;
        }
        if (value == null) {
            error = new InterruptedIOException("HTTP response timeout");
        }
    }


    public synchronized void finish() {
        finished = true;
        notifyAll();
    }

    public synchronized void setValue(String s) {
        value = s;

        server_response = s;

        notifyAll();
    }

    public void replyHandler(InputStream response, CommRecord cr) {
        try {
            InputStreamReader reader =
                    new InputStreamReader(response, "UTF8");
            int c = reader.read();

            if (c == 'S') {
                finish();
                return;
            }
            String s = (char) c + "";

            while ((c = reader.read()) != -1) {
                s += (char) c + "";
            }

            setValue(s);
        } catch (Exception e) {
            error = e;
        }
    }

    public void errorHandler(Exception exception, CommRecord cr) {
        error = exception;
        Debug.println("errorHandler: " + exception);
        finish();
    }

    public String username(Object auth, CommRecord cr) {
        return id;
    }

    public String password(Object auth, CommRecord cr) {
        return pw;
    }
}