summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cmscore/connector/HttpConnector.java
blob: f947164d96c999893f953c6af8f34962c8927beb (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
// --- 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.util.Vector;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.authority.IAuthority;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.connector.IConnector;
import com.netscape.certsrv.connector.IHttpConnection;
import com.netscape.certsrv.connector.IHttpPKIMessage;
import com.netscape.certsrv.connector.IRemoteAuthority;
import com.netscape.certsrv.connector.IResender;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.request.RequestId;
import com.netscape.certsrv.request.RequestStatus;
import com.netscape.cmsutil.http.JssSSLSocketFactory;
import com.netscape.cmsutil.net.ISocketFactory;

public class HttpConnector implements IConnector {
    protected IAuthority mSource = null;
    protected IRemoteAuthority mDest = null;
    protected ISocketFactory mFactory = null;

    // XXX todo make this a pool.
    // XXX use CMMF in the future.
    protected IHttpConnection mConn = null;
    private Thread mResendThread = null;
    private IResender mResender = null;
    @SuppressWarnings("unused")
    private int mTimeout;

    private HttpConnFactory mConnFactory = null;

    public HttpConnector(IAuthority source, String nickName,
            IRemoteAuthority dest, int resendInterval, IConfigStore config) throws EBaseException {

        mTimeout = 0;
        mSource = source;
        mDest = dest;
        mFactory = new JssSSLSocketFactory(nickName);

        int minConns = config.getInteger("minHttpConns", 1);
        int maxConns = config.getInteger("maxHttpConns", 15);

        CMS.debug("HttpConn: min " + minConns);
        CMS.debug("HttpConn: max " + maxConns);

        try {
            mConnFactory = new HttpConnFactory(minConns, maxConns, source, dest, nickName, 0);
        } catch (EBaseException e) {
            CMS.debug("can't create new HttpConnFactory " + e.toString());
        }

        //        mConn = CMS.getHttpConnection(dest, mFactory);
        // this will start resending past requests in parallel.
        mResender = CMS.getResender(mSource, nickName, dest, resendInterval);
        mResendThread = new Thread(mResender, "HttpConnector");
    }

    // Inserted by beomsuk
    public HttpConnector(IAuthority source, String nickName,
            IRemoteAuthority dest, int resendInterval, IConfigStore config, int timeout) throws EBaseException {
        mSource = source;
        mDest = dest;
        mTimeout = timeout;
        mFactory = new JssSSLSocketFactory(nickName);

        int minConns = config.getInteger("minHttpConns", 1);
        int maxConns = config.getInteger("maxHttpConns", 15);

        CMS.debug("HttpConn: min " + minConns);
        CMS.debug("HttpConn: max " + maxConns);

        try {
            mConnFactory = new HttpConnFactory(minConns, maxConns, source, dest, nickName, timeout);
        } catch (EBaseException e) {
            CMS.debug("can't create new HttpConnFactory");
        }

        // this will start resending past requests in parallel.
        mResender = CMS.getResender(mSource, nickName, dest, resendInterval);
        mResendThread = new Thread(mResender, "HttpConnector");
    }

    // Insert end

    public boolean send(IRequest r)
            throws EBaseException {
        IHttpConnection curConn = null;

        try {
            IHttpPKIMessage tomsg = (IHttpPKIMessage) CMS.getHttpPKIMessage();
            HttpPKIMessage replymsg = null;

            tomsg.fromRequest(r);
            CMS.debug("Before synch");

            curConn = mConnFactory.getConn();

            CMS.debug("HttpConnector.send " + curConn);

            replymsg = (HttpPKIMessage) curConn.send(tomsg);

            if (replymsg == null) {
                CMS.debug("HttpConncter. replymsg is null");
                return false;
            }

            CMS.debug("HttpConncter.send has been called");

            RequestStatus replyStatus;
            RequestId replyRequestId;

            replyStatus = RequestStatus.fromString(replymsg.reqStatus);
            int index = replymsg.reqId.lastIndexOf(':');

            replyRequestId = new RequestId(replymsg.reqId.substring(index + 1));
            CMS.debug("reply request id " + replyRequestId);
            r.setExtData(IRequest.REMOTE_REQID, replyRequestId.toString());

            CMS.debug("reply request type " + r.getRequestType());
            CMS.debug("reply status " + replyStatus);

            // non terminal states.
            // XXX hack: don't resend get revocation info requests since
            // resent results are ignored.
            if ((!r.getRequestType().equals(
                        IRequest.GETREVOCATIONINFO_REQUEST)) &&
                    (replyStatus == RequestStatus.BEGIN ||
                            replyStatus == RequestStatus.PENDING ||
                            replyStatus == RequestStatus.SVC_PENDING ||
                    replyStatus == RequestStatus.APPROVED)) {
                CMS.debug("HttpConn:  remote request id still pending " +
                        r.getRequestId() + " state " + replyStatus);
                mSource.log(ILogger.LL_INFO,
                        CMS.getLogMessage("CMSCORE_CONNECTOR_REQUEST_NOT_COMPLETED", r.getRequestId().toString()));
                mResender.addRequest(r);
                return false;
            }

            // request was completed.
            replymsg.toRequest(r); // this only copies contents.

            // terminal states other than completed
            if (replyStatus == RequestStatus.REJECTED ||
                    replyStatus == RequestStatus.CANCELED) {
                CMS.debug(
                        "remote request id " + r.getRequestId() +
                                " was rejected or cancelled.");
                r.setExtData(IRequest.REMOTE_STATUS, replyStatus.toString());
                r.setExtData(IRequest.RESULT, IRequest.RES_ERROR);
                r.setExtData(IRequest.ERROR,
                        new EBaseException(CMS.getUserMessage("CMS_BASE_REMOTE_AUTHORITY_ERROR")));
                // XXX overload svcerrors for now.
                Vector<String> policyErrors = r.getExtDataInStringVector(IRequest.ERRORS);

                if (policyErrors != null && policyErrors.size() > 0) {
                    r.setExtData(IRequest.SVCERRORS, policyErrors);
                }
            }

            CMS.debug(
                    "remote request id " + r.getRequestId() + " was completed");
            return true;
        } catch (EBaseException e) {
            CMS.debug("HttpConn: inside EBaseException " + e.toString());

            if (!r.getRequestType().equals(IRequest.GETREVOCATIONINFO_REQUEST))
                mResender.addRequest(r);

            CMS.debug("HttpConn:  error sending request to cert " + e.toString());
            mSource.log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_CONNECTOR_SEND_REQUEST", r.getRequestId()
                    .toString(), mDest.getHost(), Integer.toString(mDest.getPort())));
            // mSource.log(ILogger.LL_INFO,
            //    "Queing " + r.getRequestId() + " for resend.");
            return false;
        } finally {

            if (curConn != null) {
                mConnFactory.returnConn(curConn);
            }
        }
    }

    public void start() {
        mResendThread.start();
    }

}