From a4682ceae6774956461edd03b2485bbacea445f4 Mon Sep 17 00:00:00 2001 From: mharmsen Date: Tue, 4 Oct 2011 01:17:41 +0000 Subject: Bugzilla Bug #688225 - (dogtagIPAv2.1) TRACKER: of the Dogtag fixes for freeIPA 2.1 git-svn-id: svn+ssh://svn.fedorahosted.org/svn/pki/tags/IPA_v2_RHEL_6_2_20111003@2252 c9f7a03b-bd48-0410-a16d-cbbf54688b0b --- .../netscape/cmscore/connector/HttpConnector.java | 198 +++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 pki/base/common/src/com/netscape/cmscore/connector/HttpConnector.java (limited to 'pki/base/common/src/com/netscape/cmscore/connector/HttpConnector.java') diff --git a/pki/base/common/src/com/netscape/cmscore/connector/HttpConnector.java b/pki/base/common/src/com/netscape/cmscore/connector/HttpConnector.java new file mode 100644 index 000000000..4ce3ed848 --- /dev/null +++ b/pki/base/common/src/com/netscape/cmscore/connector/HttpConnector.java @@ -0,0 +1,198 @@ +// --- 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 com.netscape.certsrv.apps.*; +import com.netscape.certsrv.request.*; +import com.netscape.certsrv.base.*; +import com.netscape.certsrv.connector.*; +import com.netscape.certsrv.authority.*; +import com.netscape.certsrv.logging.*; +import com.netscape.cmsutil.http.*; +import com.netscape.cmsutil.net.*; +import java.util.*; + + +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; + 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 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(); + } + +} -- cgit