summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java
diff options
context:
space:
mode:
Diffstat (limited to 'pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java')
-rw-r--r--pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java141
1 files changed, 80 insertions, 61 deletions
diff --git a/pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java b/pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java
index e8b03542..52639053 100644
--- a/pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java
+++ b/pki/base/common/src/com/netscape/cmscore/connector/HttpConnection.java
@@ -17,7 +17,6 @@
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.connector;
-
import java.io.IOException;
import java.util.StringTokenizer;
@@ -33,34 +32,32 @@ 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;
- protected boolean Connect(String host, HttpClient client)
- {
- StringTokenizer st = new StringTokenizer(host, " ");
- while (st.hasMoreTokens()) {
- String hp = st.nextToken(); // host:port
- StringTokenizer st1 = new StringTokenizer(hp, ":");
- try {
- String h = st1.nextToken();
- int p = Integer.parseInt(st1.nextToken());
- client.connect(h, p);
- return true;
- } catch (Exception e) {
- // may want to log the failure
- }
- try {
- Thread.sleep(5000); // 5 seconds
- } catch (Exception e) {
- }
-
- }
- return false;
+ protected boolean Connect(String host, HttpClient client) {
+ StringTokenizer st = new StringTokenizer(host, " ");
+ while (st.hasMoreTokens()) {
+ String hp = st.nextToken(); // host:port
+ StringTokenizer st1 = new StringTokenizer(hp, ":");
+ try {
+ String h = st1.nextToken();
+ int p = Integer.parseInt(st1.nextToken());
+ client.connect(h, p);
+ return true;
+ } catch (Exception e) {
+ // may want to log the failure
+ }
+ try {
+ Thread.sleep(5000); // 5 seconds
+ } catch (Exception e) {
+ }
+
+ }
+ return false;
}
public HttpConnection(IRemoteAuthority dest, ISocketFactory factory) {
@@ -73,56 +70,63 @@ public class HttpConnection implements IHttpConnection {
mHttpreq.setMethod("POST");
mHttpreq.setURI(mDest.getURI());
mHttpreq.setHeader("Connection", "Keep-Alive");
- CMS.debug("HttpConnection: connecting to " + dest.getHost() + ":" + dest.getPort());
+ CMS.debug("HttpConnection: connecting to " + dest.getHost() + ":"
+ + dest.getPort());
String host = dest.getHost();
// we could have a list of host names in the host parameters
- // the format is, for example,
+ // the format is, for example,
// "directory.knowledge.com:1050 people.catalog.com 199.254.1.2"
if (host != null && host.indexOf(' ') != -1) {
- // try to do client-side failover
- boolean connected = false;
- do {
- connected = Connect(host, mHttpClient);
- } while (!connected);
+ // try to do client-side failover
+ boolean connected = false;
+ do {
+ connected = Connect(host, mHttpClient);
+ } while (!connected);
} else {
- mHttpClient.connect(host, dest.getPort());
+ mHttpClient.connect(host, dest.getPort());
}
- CMS.debug("HttpConnection: connected to " + dest.getHost() + ":" + dest.getPort());
+ CMS.debug("HttpConnection: connected to " + dest.getHost() + ":"
+ + dest.getPort());
} catch (IOException e) {
// server's probably down. that's fine. try later.
- //System.out.println(
- //"Can't connect to server in connection creation");
+ // System.out.println(
+ // "Can't connect to server in connection creation");
}
}
// Inserted by beomsuk
- public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, int timeout) {
+ public HttpConnection(IRemoteAuthority dest, ISocketFactory factory,
+ int timeout) {
mDest = dest;
mReqEncoder = new HttpRequestEncoder();
mHttpClient = new HttpClient(factory);
- CMS.debug("HttpConn:Created HttpConnection: factory " + factory + "client " + mHttpClient);
+ CMS.debug("HttpConn:Created HttpConnection: factory " + factory
+ + "client " + mHttpClient);
try {
mHttpreq.setMethod("POST");
mHttpreq.setURI(mDest.getURI());
mHttpreq.setHeader("Connection", "Keep-Alive");
- CMS.debug("HttpConnection: connecting to " + dest.getHost() + ":" + dest.getPort() + " timeout:" + timeout);
+ CMS.debug("HttpConnection: connecting to " + dest.getHost() + ":"
+ + dest.getPort() + " timeout:" + timeout);
mHttpClient.connect(dest.getHost(), dest.getPort(), timeout);
- CMS.debug("HttpConnection: connected to " + dest.getHost() + ":" + dest.getPort() + " timeout:" + timeout);
+ CMS.debug("HttpConnection: connected to " + dest.getHost() + ":"
+ + dest.getPort() + " timeout:" + timeout);
} catch (IOException e) {
// server's probably down. that's fine. try later.
- //System.out.println(
- //"Can't connect to server in connection creation");
- CMS.debug("CMSConn:IOException in creating HttpConnection " + e.toString());
+ // System.out.println(
+ // "Can't connect to server in connection creation");
+ CMS.debug("CMSConn:IOException in creating HttpConnection "
+ + e.toString());
}
}
// Insert end
- /**
+ /**
* sends a request to remote RA/CA, returning the result.
- * @throws EBaseException if request could not be encoded
+ *
+ * @throws EBaseException if request could not be encoded
*/
- public IPKIMessage send(IPKIMessage tomsg)
- throws EBaseException {
+ public IPKIMessage send(IPKIMessage tomsg) throws EBaseException {
IPKIMessage replymsg = null;
CMS.debug("in HttpConnection.send " + this);
@@ -133,7 +137,8 @@ public class HttpConnection implements IHttpConnection {
try {
content = mReqEncoder.encode(tomsg);
} catch (IOException e) {
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "Could not encode request"));
+ throw new EBaseException(CMS.getUserMessage(
+ "CMS_BASE_INVALID_ATTRIBUTE", "Could not encode request"));
}
if (Debug.ON) {
Debug.trace("encoded request");
@@ -143,8 +148,7 @@ public class HttpConnection implements IHttpConnection {
}
boolean reconnect = false;
- mHttpreq.setHeader("Content-Length",
- Integer.toString(content.length()));
+ mHttpreq.setHeader("Content-Length", Integer.toString(content.length()));
if (Debug.ON)
Debug.trace("request encoded length " + content.length());
mHttpreq.setContent(content);
@@ -154,15 +158,21 @@ public class HttpConnection implements IHttpConnection {
try {
if (!mHttpClient.connected()) {
mHttpClient.connect(mDest.getHost(), mDest.getPort());
- CMS.debug("HttpConn:reconnected to " + mDest.getHost() + ":" + mDest.getPort());
+ CMS.debug("HttpConn:reconnected to " + mDest.getHost() + ":"
+ + mDest.getPort());
reconnect = true;
}
} catch (IOException 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.)"));
+ 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.)"));
}
CMS.debug("HttpConn:Couldn't reconnect " + e);
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED", "Couldn't reconnect " + e));
+ throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED",
+ "Couldn't reconnect " + e));
}
// if remote closed connection want to reconnect and resend.
@@ -175,14 +185,18 @@ public class HttpConnection implements IHttpConnection {
CMS.debug("HttpConn: mHttpClient.send failed " + e.toString());
if (reconnect) {
CMS.debug("HttpConn:resend failed again. " + e);
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED", "resend failed again. " + e));
+ throw new EBaseException(
+ CMS.getUserMessage("CMS_BASE_CONN_FAILED",
+ "resend failed again. " + e));
}
try {
CMS.debug("HttpConn: trying a reconnect ");
mHttpClient.connect(mDest.getHost(), mDest.getPort());
} catch (IOException ex) {
CMS.debug("reconnect for resend failed. " + ex);
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_CONN_FAILED", "reconnect for resend failed." + ex));
+ throw new EBaseException(CMS.getUserMessage(
+ "CMS_BASE_CONN_FAILED",
+ "reconnect for resend failed." + ex));
}
reconnect = true;
}
@@ -206,22 +220,26 @@ 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 + " "
+ + p.getReasonPhrase();
if (Debug.ON)
Debug.trace(msg);
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_AUTHENTICATE_FAILED", 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 + " "
+ + p.getReasonPhrase();
CMS.debug(msg);
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", 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
+ // decode reply.
+ // if reply is bad, error is thrown and request will be resent
String pcontent = p.getContent();
if (Debug.ON) {
@@ -234,7 +252,8 @@ public class HttpConnection implements IHttpConnection {
try {
replymsg = (IPKIMessage) mReqEncoder.decode(pcontent);
} catch (IOException e) {
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "Could not decode content"));
+ throw new EBaseException(CMS.getUserMessage(
+ "CMS_BASE_INVALID_ATTRIBUTE", "Could not decode content"));
}
CMS.debug("HttpConn:decoded reply");
return replymsg;