From 61ec5553e416733996be05fda16983d32252000c Mon Sep 17 00:00:00 2001 From: Christina Fu Date: Mon, 3 Mar 2014 15:52:55 -0800 Subject: trac ticket #862 - TPS rewrite: provide connector service for JAVA-based TPS subsystem --- .../cmscore/connector/HttpConnFactory.java | 46 +++++-- .../netscape/cmscore/connector/HttpConnection.java | 138 +++++++++++++++------ .../netscape/cmscore/connector/HttpConnector.java | 47 +++++-- .../netscape/cmscore/connector/LocalConnector.java | 6 + .../cmscore/connector/RemoteAuthority.java | 35 ++++++ 5 files changed, 215 insertions(+), 57 deletions(-) (limited to 'base/server/cmscore/src/com/netscape/cmscore/connector') diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java index a55a186e3..be727a54b 100644 --- a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java +++ b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java @@ -75,7 +75,7 @@ public class HttpConnFactory { * initialize parameters obtained from either constructor or * config store * - * @param minConns minimum number of connection handls to have available. + * @param minConns minimum number of connection handles to have available. * @param maxConns maximum total number of connections to ever have. * @param connInfo ldap connection info. * @param authInfo ldap authentication info. @@ -113,7 +113,7 @@ public class HttpConnFactory { CMS.debug("leaving HttpConnFactory init."); } - private IHttpConnection createConnection() throws EBaseException { + private IHttpConnection createConnection(String op) throws EBaseException { IHttpConnection retConn = null; @@ -122,10 +122,18 @@ public class HttpConnFactory { try { ISocketFactory tFactory = new JssSSLSocketFactory(mNickname); - if (mTimeout == 0) { - retConn = CMS.getHttpConnection(mDest, tFactory); + if (op == null) { + if (mTimeout == 0) { + retConn = CMS.getHttpConnection(mDest, tFactory); + } else { + retConn = CMS.getHttpConnection(mDest, tFactory, mTimeout); + } } else { - retConn = CMS.getHttpConnection(mDest, tFactory, mTimeout); + if (mTimeout == 0) { + retConn = CMS.getHttpConnection(mDest, tFactory, op); + } else { + retConn = CMS.getHttpConnection(mDest, tFactory, mTimeout, op); + } } } catch (Exception e) { @@ -142,7 +150,7 @@ public class HttpConnFactory { /** * makes the minumum number of connections */ - private void makeMinimum() throws EBaseException { + private void makeMinimum(String op) throws EBaseException { CMS.debug("In HttpConnFactory.makeMinimum."); int increment; @@ -157,7 +165,7 @@ public class HttpConnFactory { CMS.debug( "increasing minimum connections by " + increment); for (int i = increment - 1; i >= 0; i--) { - mConns[i] = createConnection(); + mConns[i] = createConnection(op); } mTotal += increment; mNumConns += increment; @@ -190,6 +198,15 @@ public class HttpConnFactory { return getConn(true); } + /* + * See getConn() above + * @param op operation to determine the receiving servlet (multi-uri support) + */ + public IHttpConnection getConn(String op) + throws EBaseException { + return getConn(true, op); + } + /** * Returns a Http connection - a clone of the master connection. * All connections should be returned to the factory using returnConn() @@ -214,11 +231,20 @@ public class HttpConnFactory { */ public synchronized IHttpConnection getConn(boolean waitForConn) throws EBaseException { + return getConn(waitForConn, null); + } + + /* + * See getConn() above + * @param op operation to determine the receiving servlet (multi-uri support) + */ + public synchronized IHttpConnection getConn(boolean waitForConn, String op) + throws EBaseException { boolean waited = false; CMS.debug("In HttpConnFactory.getConn"); if (mNumConns == 0) - makeMinimum(); + makeMinimum(op); if (mNumConns == 0) { if (!waitForConn) return null; @@ -251,7 +277,7 @@ public class HttpConnFactory { } /** - * Teturn connection to the factory. + * Return connection to the factory. * This is mandatory after a getConn(). * The best thing to do is to put returnConn in a finally clause so it * always gets called. For example, @@ -296,4 +322,4 @@ public class HttpConnFactory { "In Http (bound) connection pool to" + msg); } -} \ No newline at end of file +} diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java index 7a65c1760..30d70be1b 100644 --- a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java +++ b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java @@ -61,6 +61,13 @@ public class HttpConnection implements IHttpConnection { } public HttpConnection(IRemoteAuthority dest, ISocketFactory factory) { + this(dest, factory, null); + } + + /* + * @param op operation to determine the receiving servlet (multi-uri support) + */ + public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, String op) { mDest = dest; mReqEncoder = new HttpRequestEncoder(); mHttpClient = new HttpClient(factory); @@ -68,7 +75,18 @@ public class HttpConnection implements IHttpConnection { Debug.trace("Created HttpClient"); try { mHttpreq.setMethod("POST"); - mHttpreq.setURI(mDest.getURI()); + if (op == null) + mHttpreq.setURI(mDest.getURI()); + else { + mHttpreq.setURI(mDest.getURI(op)); + } + + String contentType = dest.getContentType(); + if (contentType != null) { + CMS.debug("HttpConnection: setting Content-Type"); + mHttpreq.setHeader("Content-Type", contentType ); + } + mHttpreq.setHeader("Connection", "Keep-Alive"); CMS.debug("HttpConnection: connecting to " + dest.getHost() + ":" + dest.getPort()); String host = dest.getHost(); @@ -94,13 +112,31 @@ public class HttpConnection implements IHttpConnection { // Inserted by beomsuk public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, int timeout) { + this(dest, factory, timeout, null); + } + + /* + * @param op operation to determine the receiving servlet (multi-uri support) + */ + public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, int timeout, String op) { mDest = dest; mReqEncoder = new HttpRequestEncoder(); mHttpClient = new HttpClient(factory); CMS.debug("HttpConn:Created HttpConnection: factory " + factory + "client " + mHttpClient); try { mHttpreq.setMethod("POST"); - mHttpreq.setURI(mDest.getURI()); + if (op == null) + mHttpreq.setURI(mDest.getURI()); + else { + mHttpreq.setURI(mDest.getURI(op)); + } + + String contentType = dest.getContentType(); + if (contentType != null) { + CMS.debug("HttpConnection: setting Content-Type"); + mHttpreq.setHeader("Content-Type", contentType ); + } + mHttpreq.setHeader("Connection", "Keep-Alive"); CMS.debug("HttpConnection: connecting to " + dest.getHost() + ":" + dest.getPort() + " timeout:" + timeout); mHttpClient.connect(dest.getHost(), dest.getPort(), timeout); @@ -122,6 +158,7 @@ public class HttpConnection implements IHttpConnection { public IPKIMessage send(IPKIMessage tomsg) throws EBaseException { IPKIMessage replymsg = null; + HttpResponse resp = null; CMS.debug("in HttpConnection.send " + this); if (Debug.ON) @@ -139,20 +176,61 @@ public class HttpConnection implements IHttpConnection { 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 reconnect = false; mHttpreq.setHeader("Content-Length", Integer.toString(content.length())); - if (Debug.ON) - Debug.trace("request encoded length " + content.length()); + CMS.debug("HttpConnection.doSend: with String content length: " + Integer.toString(content.length())); mHttpreq.setContent(content); - HttpResponse p = null; - try { if (!mHttpClient.connected()) { mHttpClient.connect(mDest.getHost(), mDest.getPort()); - CMS.debug("HttpConn:reconnected to " + mDest.getHost() + ":" + mDest.getPort()); + CMS.debug("HttpConnection.doSend: reconnected to " + mDest.getHost() + ":" + mDest.getPort()); reconnect = true; } } catch (IOException e) { @@ -167,33 +245,32 @@ public class HttpConnection implements IHttpConnection { } // if remote closed connection want to reconnect and resend. - while (p == null) { + while (resp == null) { try { - if (Debug.ON) - Debug.trace("sending request"); - p = mHttpClient.send(mHttpreq); + CMS.debug("HttpConnection.doSend: sending request"); + resp = mHttpClient.send(mHttpreq); } catch (IOException e) { CMS.debug("HttpConn: mHttpClient.send failed " + e.toString()); if (reconnect) { - CMS.debug("HttpConn:resend failed again. " + e); + CMS.debug("HttpConnection.doSend:resend failed again. " + e); throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED", "resend failed again. " + e)); } try { - CMS.debug("HttpConn: trying a reconnect "); + CMS.debug("HttpConnection.doSend: trying a reconnect "); mHttpClient.connect(mDest.getHost(), mDest.getPort()); } catch (IOException ex) { - CMS.debug("reconnect for resend failed. " + ex); + CMS.debug("HttpConnection.doSend: reconnect for resend failed. " + ex); throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED", "reconnect for resend failed." + ex)); } reconnect = true; } - } + } //while // got reply; check status - String statusStr = p.getStatusCode(); + String statusStr = resp.getStatusCode(); - CMS.debug("HttpConn:server returned status " + statusStr); + CMS.debug("HttpConnection.doSend:server returned status " + statusStr); int statuscode = -1; try { @@ -208,37 +285,18 @@ public class HttpConnection implements IHttpConnection { /* HttpServletResponse.SC_UNAUTHORIZED = 401 */ if (statuscode == 401) { // XXX what to do here. - String msg = "request no good " + statuscode + " " + p.getReasonPhrase(); + String msg = "request no good " + statuscode + " " + resp.getReasonPhrase(); - if (Debug.ON) - Debug.trace(msg); + CMS.debug(msg); throw new EBaseException(CMS.getUserMessage("CMS_BASE_AUTHENTICATE_FAILED", msg)); } else { // XXX what to do here. - String msg = "HttpConn:request no good " + statuscode + " " + p.getReasonPhrase(); + String msg = "HttpConn:request no good " + statuscode + " " + resp.getReasonPhrase(); CMS.debug(msg); throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", msg)); } } - - // decode reply. - // if reply is bad, error is thrown and request will be resent - String pcontent = p.getContent(); - - if (Debug.ON) { - Debug.trace("Server returned\n"); - Debug.trace("-------"); - Debug.trace(pcontent); - Debug.trace("-------"); - } - - 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; + return resp; } } diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java index 33b0d62b7..ff0ece148 100644 --- a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java +++ b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java @@ -34,6 +34,7 @@ import com.netscape.certsrv.request.RequestId; import com.netscape.certsrv.request.RequestStatus; import com.netscape.cmsutil.http.JssSSLSocketFactory; import com.netscape.cmsutil.net.ISocketFactory; +import com.netscape.cmsutil.http.HttpResponse; public class HttpConnector implements IConnector { protected IAuthority mSource = null; @@ -71,7 +72,9 @@ public class HttpConnector implements IConnector { // mConn = CMS.getHttpConnection(dest, mFactory); // this will start resending past requests in parallel. - mResender = CMS.getResender(mSource, nickName, dest, resendInterval); + if (resendInterval >= 0) { + mResender = CMS.getResender(mSource, nickName, dest, resendInterval); + } } // Inserted by beomsuk @@ -95,11 +98,32 @@ public class HttpConnector implements IConnector { } // this will start resending past requests in parallel. - mResender = CMS.getResender(mSource, nickName, dest, resendInterval); + if (resendInterval >= 0) { + mResender = CMS.getResender(mSource, nickName, dest, resendInterval); + } } // Insert end + // cfu + public HttpResponse send(String op, String msg) + throws EBaseException { + CMS.debug("HttpConnector: send(): cfu"); + HttpResponse resp = null; + IHttpConnection curConn = null; + try { + curConn = mConnFactory.getConn(op); + resp = curConn.send(msg); + } catch (EBaseException e) { + CMS.debug("HttpConnector: send():"+ e.toString()); + } finally { + if (curConn != null) { + mConnFactory.returnConn(curConn); + } + } + return resp; + } + public boolean send(IRequest r) throws EBaseException { IHttpConnection curConn = null; @@ -148,9 +172,12 @@ public class HttpConnector implements IConnector { 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); + */ + if (mResender != null) + mResender.addRequest(r); return false; } @@ -181,12 +208,16 @@ public class HttpConnector implements IConnector { } catch (EBaseException e) { CMS.debug("HttpConn: inside EBaseException " + e.toString()); - if (!r.getRequestType().equals(IRequest.GETREVOCATIONINFO_REQUEST)) - mResender.addRequest(r); + if (!r.getRequestType().equals(IRequest.GETREVOCATIONINFO_REQUEST)) { + if (mResender != null) + 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; @@ -200,12 +231,14 @@ public class HttpConnector implements IConnector { public void start() { CMS.debug("Starting HttpConnector resender thread"); - mResender.start("HttpConnector"); + if (mResender != null) + mResender.start("HttpConnector"); } public void stop() { CMS.debug("Stopping HttpConnector resender thread"); - mResender.stop(); + if (mResender != null) + mResender.stop(); } } diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/LocalConnector.java b/base/server/cmscore/src/com/netscape/cmscore/connector/LocalConnector.java index ba2db83a1..6c12b2729 100644 --- a/base/server/cmscore/src/com/netscape/cmscore/connector/LocalConnector.java +++ b/base/server/cmscore/src/com/netscape/cmscore/connector/LocalConnector.java @@ -34,6 +34,7 @@ import com.netscape.certsrv.request.IRequestQueue; import com.netscape.certsrv.request.RequestId; import com.netscape.certsrv.request.RequestStatus; import com.netscape.cmscore.util.Debug; +import com.netscape.cmsutil.http.HttpResponse; public class LocalConnector implements IConnector { ICertAuthority mSource = null; @@ -122,6 +123,11 @@ public class LocalConnector implements IConnector { } } + public HttpResponse send(String op, String r) throws EBaseException { + CMS.debug("LocalConnector send() with String. Should not get here."); + return null; + } + public class LocalConnListener implements IRequestListener { public void init(ISubsystem sys, IConfigStore config) diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/RemoteAuthority.java b/base/server/cmscore/src/com/netscape/cmscore/connector/RemoteAuthority.java index 71d01579f..ff1e7e9e4 100644 --- a/base/server/cmscore/src/com/netscape/cmscore/connector/RemoteAuthority.java +++ b/base/server/cmscore/src/com/netscape/cmscore/connector/RemoteAuthority.java @@ -17,6 +17,8 @@ // --- END COPYRIGHT BLOCK --- package com.netscape.cmscore.connector; +import java.util.Hashtable; + import com.netscape.certsrv.base.EBaseException; import com.netscape.certsrv.base.IConfigStore; import com.netscape.certsrv.connector.IRemoteAuthority; @@ -25,6 +27,8 @@ public class RemoteAuthority implements IRemoteAuthority { String mHost = null; int mPort = -1; String mURI = null; + Hashtable mURIs = new Hashtable(); + String mContentType = null; int mTimeout = 0; /** @@ -40,9 +44,28 @@ public class RemoteAuthority implements IRemoteAuthority { mTimeout = timeout; } + public RemoteAuthority(String host, int port, Hashtableuris, int timeout) { + mHost = host; + mPort = port; + mURIs = uris; + mTimeout = timeout; + } + + public RemoteAuthority(String host, int port, Hashtableuris, int timeout, String contentType) { + mHost = host; + mPort = port; + mURIs = uris; + mTimeout = timeout; + if (contentType.equals("")) + mContentType = null; + else + mContentType = contentType; + } + public RemoteAuthority() { } +/*cfu what TODO?*/ public void init(IConfigStore c) throws EBaseException { mHost = c.getString("host"); @@ -63,7 +86,19 @@ public class RemoteAuthority implements IRemoteAuthority { return mURI; } + public String getURI(String name) { + return mURIs.get(name); + } + + public Hashtable getURIs() { + return mURIs; + } + public int getTimeout() { return mTimeout; } + + public String getContentType() { + return mContentType; + } } -- cgit