001: /*
002: * PingServiceRequest.java
003: *
004: * Created on May 21, 2003, 10:21 AM
005: */
006:
007: package com.sun.portal.util;
008:
009: import java.io.BufferedReader;
010: import java.io.DataInputStream;
011: import java.io.DataOutputStream;
012: import java.io.IOException;
013: import java.io.InputStreamReader;
014: import java.net.Socket;
015: import java.net.URL;
016: import java.util.logging.Level;
017: import java.util.logging.Logger;
018:
019: import sun.misc.BASE64Encoder;
020:
021: import com.sun.portal.log.common.PortalLogger;
022:
023: /**
024: * This is just a service ping request implementation. If proxy server is
025: * present it always uses the CONNECT command to check the service availability
026: * irrespective of the service protocol.
027: *
028: * TBD : Consider creating a cache for the socket that's getting created here.
029: * Also cache the currently active servers for better performance.
030: *
031: * @author Rajesh T
032: * @date 21-05-2003
033: */
034:
035: public class PingServiceRequest {
036:
037: /** Creates a new instance of PingService */
038: private String serviceHost = null;
039:
040: private int servicePort = -1;
041:
042: private String proxyHost = null;
043:
044: private int proxyPort = -1;
045:
046: private String proxyUser = null;
047:
048: private String proxyPassword = null;
049:
050: private boolean proxyAuthRequired = false;
051:
052: private static String retries = SystemProperties.get(
053: "gateway.sockretries", "2");
054:
055: // static Logger logger = Logger.getLogger("com.sun.portal.sra.rproxy");
056: private static Logger logger = PortalLogger
057: .getLogger(PingServiceRequest.class);
058:
059: public PingServiceRequest(URL serviceURL) {
060: serviceHost = serviceURL.getHost();
061: servicePort = serviceURL.getPort();
062: }
063:
064: public PingServiceRequest(URL serviceURL, String proxyHost,
065: int proxyPort) {
066: serviceHost = serviceURL.getHost();
067: servicePort = serviceURL.getPort();
068: this .proxyHost = proxyHost;
069: this .proxyPort = proxyPort;
070: }
071:
072: public PingServiceRequest(URL serviceURL, String proxyHost,
073: int proxyPort, String proxyUser, String proxyPassword) {
074: serviceHost = serviceURL.getHost();
075: servicePort = serviceURL.getPort();
076: this .proxyHost = proxyHost;
077: this .proxyPort = proxyPort;
078: this .proxyUser = proxyUser;
079: this .proxyPassword = proxyPassword;
080: proxyAuthRequired = true;
081: }
082:
083: public PingServiceRequest(String serviceHost, int servicePort,
084: String proxyHost, int proxyPort) {
085: this .serviceHost = serviceHost;
086: this .servicePort = servicePort;
087: this .proxyHost = proxyHost;
088: this .proxyPort = proxyPort;
089: }
090:
091: public PingServiceRequest(String serviceHost, int servicePort,
092: String proxyHost, int proxyPort, String proxyUser,
093: String proxyPassword) {
094: this .serviceHost = serviceHost;
095: this .servicePort = servicePort;
096: this .proxyHost = proxyHost;
097: this .proxyPort = proxyPort;
098: this .proxyUser = proxyUser;
099: this .proxyPassword = proxyPassword;
100: proxyAuthRequired = true;
101: }
102:
103: public boolean isServiceAlive() throws ProxyAuthNeededException,
104: ProxyAuthFailedException, ProxyUnreachableException {
105: int retry = Integer.parseInt(retries);
106: for (int i = 0; i < retry; i++) {
107: // logger.info("Retry Number for Server # " + i);
108: Object[] params0 = { i + "" };
109: logger.log(Level.INFO, "PSSR_CSPU087", params0);
110: boolean state = checkService();
111: if (state) {
112: return true;
113: }
114: try {
115: Thread.sleep(3000);
116: } catch (InterruptedException ie) {
117: // Ignoring all Exceptions
118: }
119: }
120: return false;
121: }
122:
123: private boolean checkService() throws ProxyAuthNeededException,
124: ProxyAuthFailedException, ProxyUnreachableException {
125: String message = new String(
126: "\n***************Check Proxy information ....**************\n");
127: message += "Proxy Host : " + proxyHost + "\n";
128: message += "Proxy Port : " + proxyPort + "\n";
129: message += "Proxy User : " + proxyUser + "\n";
130: String encodePassword = null;
131: if (proxyPassword != null) {
132: encodePassword = "isPresent";
133: }
134: message += "Proxy Password : " + encodePassword + "\n";
135: message += "Service Host : " + serviceHost + "\n";
136: message += "Service Port : " + servicePort + "\n";
137: message += "*********************************************************\n";
138: // logger.info("Checking Proxy Status:" + message.toString());
139: Object[] params1 = { message.toString() };
140: logger.log(Level.INFO, "PSSR_CSPU088", params1);
141:
142: if (proxyHost == null || proxyHost.equals("null")
143: || proxyHost.trim().length() < 1) {
144: return directConnection();
145: } else {
146: return proxyConnection();
147: }
148: }
149:
150: private boolean directConnection() {
151: try {
152: Socket socket = new Socket(serviceHost, servicePort);
153: socket.close();
154: return true;
155: } catch (Exception soe) {
156: // soe.printStackTrace();
157: // logger.log(Level.SEVERE, "Unable to connect to " + serviceHost +
158: // ":" + servicePort, soe);
159: Object[] params2 = { serviceHost, ":", servicePort + "",
160: soe };
161: logger.log(Level.SEVERE, "PSSR_CSPU089", params2);
162: return false;
163: }
164: }
165:
166: private boolean proxyConnection() throws ProxyAuthNeededException,
167: ProxyAuthFailedException, ProxyUnreachableException {
168: StringBuffer header = new StringBuffer("CONNECT ").append(
169: serviceHost).append(":").append(servicePort).append(
170: " HTTP/1.0\r\n");
171:
172: if (proxyAuthRequired) {
173: if (proxyUser == null || proxyPassword == null) {
174: throw new ProxyAuthFailedException(
175: "Proxy Password or Proxy User is null");
176: }
177: BASE64Encoder encoder = new BASE64Encoder();
178: String encodeAuthInfo = encoder
179: .encode((proxyUser + ":" + proxyPassword)
180: .toString().getBytes());
181: header.append("Proxy-Authorization: Basic ").append(
182: encodeAuthInfo).append("\r\n\r\n");
183:
184: } else {
185: header.append("\r\n");
186: }
187:
188: try {
189: Socket toProxy = new Socket(proxyHost, proxyPort);
190: DataInputStream in_b = new DataInputStream(toProxy
191: .getInputStream());
192: DataOutputStream out_b = new DataOutputStream(toProxy
193: .getOutputStream());
194:
195: out_b.write(header.toString().getBytes(), 0, header
196: .length());
197:
198: BufferedReader br = new BufferedReader(
199: new InputStreamReader(in_b));
200: String line = "";
201: while (true) {
202: line = br.readLine();
203:
204: if (line == null || line.length() < 3)
205: break;
206: // looking for: HTTP/1.0 407 Proxy-Authentication Required
207: if (line.startsWith("HTTP/1")) {
208: int s = line.indexOf(" ");
209: String num = line.substring(s + 1);
210:
211: if (num.startsWith("407")) {
212: throw new ProxyAuthNeededException(
213: "Proxy Authentication Required");
214: } else if (num.startsWith("401")) {
215: throw new ProxyAuthFailedException(
216: "Proxy Authentication Failed : Digest Authentication not supported");
217: } else if (num.startsWith("500")) {
218: return false;
219: }
220: }
221: }
222:
223: } catch (StringIndexOutOfBoundsException e) {
224: return false;
225: } catch (IOException e) {
226: throw new ProxyUnreachableException(
227: "Proxy is not reachable " + proxyHost + ":"
228: + proxyPort);
229: }
230: return true;
231: }
232:
233: }
|