Source Code Cross Referenced for PingServiceRequest.java in  » Portal » Open-Portal » com » sun » portal » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.