summaryrefslogtreecommitdiffstats
path: root/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java
blob: c4804784af837775e0817419859a1d8f1335f848 (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
// --- 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.cmscore.connector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.connector.IHttpConnection;
import com.netscape.certsrv.connector.IPKIMessage;
import com.netscape.certsrv.connector.IRemoteAuthority;
import com.netscape.certsrv.connector.IRequestEncoder;
import com.netscape.cmscore.util.Debug;
import com.netscape.cmsutil.http.HttpClient;
import com.netscape.cmsutil.http.HttpRequest;
import com.netscape.cmsutil.http.HttpResponse;
import com.netscape.cmsutil.net.ISocketFactory;

public class HttpConnection implements IHttpConnection {

    protected IRemoteAuthority mDest = null;
    protected HttpRequest mHttpreq = new HttpRequest();
    protected IRequestEncoder mReqEncoder = null;
    protected HttpClient mHttpClient = null;

    int timeout = 0;
    List<InetSocketAddress> targets;

    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory,
            int timeout // seconds
            ) {

        CMS.debug("HttpConnection: Creating HttpConnection with timeout=" + timeout);

        mDest = dest;
        mReqEncoder = new HttpRequestEncoder();
        mHttpClient = new HttpClient(factory);

        this.timeout = timeout;

        targets = parseTarget(dest.getHost(), dest.getPort());

        try {
            mHttpreq.setMethod("POST");

            // in case of multi-uri, uri will be set right before send
            //   by calling setRequestURI(uri)
            if (mDest.getURI() != null)
                mHttpreq.setURI(mDest.getURI());

            String contentType = dest.getContentType();
            if (contentType != null) {
                CMS.debug("HttpConnection: setting Content-Type");
                mHttpreq.setHeader("Content-Type", contentType );
            }

            mHttpreq.setHeader("Connection", "Keep-Alive");

            connect();

        } catch (IOException e) {
            // server's probably down. that's fine. try later.
            CMS.debug("HttpConnection: Unable to create connection: " + e);
        }
    }

    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory) {
        this(dest, factory, 0);
    }

    List<InetSocketAddress> parseTarget(String target, int port) {

        List<InetSocketAddress> results = new ArrayList<InetSocketAddress>();

        if (target == null || target.indexOf(' ') < 0) {
            // target is a single hostname

            // add hostname and the global port to the results
            results.add(new InetSocketAddress(target, port));
            return results;
        }

        // target is a list of hostname:port, for example:
        // "server1.example.com:8443 server2.example.com:8443"

        for (String hostnamePort : target.split(" ")) {

            // parse hostname and port, and ignore the global port
            String[] parts = hostnamePort.split(":");
            String hostname = parts[0];
            port = Integer.parseInt(parts[1]);

            // add hostname and port to the results
            results.add(new InetSocketAddress(hostname, port));
        }

        return results;
    }

    void connect() throws IOException {

        IOException exception = null;

        // try all targets
        for (InetSocketAddress target : targets) {

            String hostname = target.getHostString();
            int port = target.getPort();

            try {
                CMS.debug("HttpConnection: Connecting to " + hostname + ":" + port + " with timeout " + timeout + "s");

                mHttpClient.connect(hostname, port, timeout * 1000);

                CMS.debug("HttpConnection: Connected to " + hostname + ":" + port);
                return;

            } catch (IOException e) {
                exception = e;
                CMS.debug("HttpConnection: Unable to connect to " + hostname + ":" + port + ": " + e);
                // try the next target immediately
            }
        }

        // throw the last exception
        throw exception;
    }

    public void setRequestURI(String uri)
            throws EBaseException {
        mHttpreq.setURI(uri);
    }

    public String getRequestURI() {
        return mHttpreq.getURI();
    }
    /**
     * sends a request to remote RA/CA, returning the result.
     *
     * @throws EBaseException if request could not be encoded
     */
    public IPKIMessage send(IPKIMessage tomsg)
            throws EBaseException {
        IPKIMessage replymsg = null;
        HttpResponse resp = null;

        CMS.debug("in HttpConnection.send " + this);
        if (Debug.ON)
            Debug.trace("encoding request ");

        String content = null;

        try {
            content = mReqEncoder.encode(tomsg);
        } catch (IOException e) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "Could not encode request"));
        }
        if (Debug.ON) {
            Debug.trace("encoded request");
            Debug.trace("------ " + content.length() + "-----");
            Debug.trace(content);
            Debug.trace("--------------------------");
        }
        resp = doSend(content);

        // decode reply.
        // if reply is bad, error is thrown and request will be resent
        String pcontent = resp.getContent();

        if (Debug.ON) {
            Debug.trace("Server returned\n");
            Debug.trace("-------");
            Debug.trace(pcontent);
            Debug.trace("-------");
        }
        CMS.debug("HttpConnection.send response: " + pcontent);

        try {
            replymsg = (IPKIMessage) mReqEncoder.decode(pcontent);
        } catch (IOException e) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "Could not decode content"));
        }
        CMS.debug("HttpConn:decoded reply");
        return replymsg;
    }

    /**
     * sends a request to a remote authority, returning the result.
     * @author cfu (multi-uri support)
     * @throws EBaseException for any failure
     */
    public HttpResponse send(String content)
            throws EBaseException {
        HttpResponse resp = null;
        if ((content == null) || content.equals("")) {
            CMS.debug("HttpConnection.send: with String content: null or empty");
            throw new EBaseException("HttpConnection.send: with String content: null or empty");
        }

        CMS.debug("HttpConnection.send: with String content: " + content);

        resp = doSend(content);
        return resp;
    }

    private HttpResponse doSend(String content) throws EBaseException {

        HttpResponse resp = null;
        boolean reconnected = false;

        if (getRequestURI() == null) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "URI not set in HttpRequest"));
        }

        mHttpreq.setHeader("Content-Length",
                Integer.toString(content.length()));
        CMS.debug("HttpConnection.doSend: with String content length: " + Integer.toString(content.length()));
        mHttpreq.setContent(content);

        try {
            if (!mHttpClient.connected()) {
                connect();
                reconnected = true;
            }

        } catch (IOException e) {

            CMS.debug(e);

            if (e.getMessage().indexOf("Peer's certificate issuer has been marked as not trusted") != -1) {
                throw new EBaseException(
                        CMS.getUserMessage(
                                "CMS_BASE_CONN_FAILED",
                                "(This local authority cannot connect to the remote authority. The local authority's signing certificate must chain to a CA certificate trusted for client authentication in the certificate database. Use the certificate manager, or command line tool such as certutil to verify that the trust permissions of the local authority's issuer cert have 'CT' setting in the SSL client auth field.)"));
            }

            throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED", "Couldn't reconnect " + e));
        }

        // if remote closed connection want to reconnect and resend.
        while (resp == null) {
            try {
                CMS.debug("HttpConnection.doSend: sending request");
                resp = mHttpClient.send(mHttpreq);

            } catch (IOException e) {

                CMS.debug(e);

                if (reconnected) {
                    CMS.debug("HttpConnection.doSend: resend failed again.");
                    throw new EBaseException(
                            CMS.getUserMessage("CMS_BASE_CONN_FAILED", "resend failed again: " + e), e);
                }

                try {
                    CMS.debug("HttpConnection.doSend: trying a reconnect ");
                    connect();

                } catch (IOException ex) {
                    CMS.debug("HttpConnection.doSend: reconnect for resend failed. " + ex);
                    throw new EBaseException(
                            CMS.getUserMessage("CMS_BASE_CONN_FAILED", "reconnect for resend failed: " + ex), e);
                }

                reconnected = true;
            }
        } //while

        // got reply; check status
        String statusStr = resp.getStatusCode();

        CMS.debug("HttpConnection.doSend: server returned status " + statusStr);
        int statuscode = -1;

        try {
            statuscode = Integer.parseInt(statusStr);
        } catch (NumberFormatException e) {
            statuscode = -1;
        }

        /* HttpServletResponse.SC_OK = 200 */
        if (statuscode != 200) {

            /* HttpServletResponse.SC_UNAUTHORIZED = 401 */
            if (statuscode == 401) {
                // XXX what to do here.
                String msg = "request no good " + statuscode + " " + resp.getReasonPhrase();

                CMS.debug("HttpConnection: " + msg);
                throw new EBaseException(CMS.getUserMessage("CMS_BASE_AUTHENTICATE_FAILED", msg));

            } else {
                // XXX what to do here.
                String msg = "HttpConnection: request no good " + statuscode + " " + resp.getReasonPhrase();

                CMS.debug(msg);
                throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", msg));
            }
        }

        return resp;
    }
}