KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > pub > HealthCheckServlet


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.web.pub;
15
16 import java.io.IOException JavaDoc;
17
18 import javax.servlet.ServletConfig JavaDoc;
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServlet JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.ejbca.core.model.InternalResources;
26 import org.ejbca.ui.web.pub.cluster.IHealthCheck;
27 import org.ejbca.ui.web.pub.cluster.IHealthResponse;
28 import org.ejbca.util.CertTools;
29
30
31
32
33 /**
34  * Servlet used to check the health of an EJBCA instance and can be used
35  * to build a cluster using a loadbalancer.
36  *
37  * This servlet should be configured with two init params:
38  * HealthCheckClassPath : containing the classpath to the IHealthCheck class to be used to check.
39  * HealthResponseClassPath : containing the classpath to the IHealthResponse class to be used
40  * for the HTTPResponse
41  *
42  * The loadbalancer or monitoring application should perform a GET request
43  * to the url defined in web.xml.
44  *
45  * @author Philip Vendil
46  * @version $Id: HealthCheckServlet.java,v 1.7 2006/12/13 10:36:04 anatom Exp $
47  */

48 public class HealthCheckServlet extends HttpServlet JavaDoc {
49     private static final Logger log = Logger.getLogger(HealthCheckServlet.class);
50     /** Internal localization of logs and errors */
51     private static final InternalResources intres = InternalResources.getInstance();
52     
53     private IHealthCheck healthcheck = null;
54     private IHealthResponse healthresponse = null;
55
56     private String JavaDoc[] authIPs = null;
57     
58     /**
59      * Servlet init
60      *
61      * @param config servlet configuration
62      *
63      * @throws ServletException on error
64      */

65     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
66         super.init(config);
67
68         try {
69             // Install BouncyCastle provider
70
CertTools.installBCProvider();
71
72             String JavaDoc authIPString = config.getInitParameter("AuthorizedIPs");
73             if(authIPString != null){
74                 authIPs = authIPString.split(";");
75             }
76             
77             
78             healthcheck = (IHealthCheck) HealthCheckServlet.class.getClassLoader().loadClass(config.getInitParameter("HealthCheckClassPath")).newInstance();
79             healthcheck.init(config);
80             
81             healthresponse = (IHealthResponse) HealthCheckServlet.class.getClassLoader().loadClass(config.getInitParameter("HealthResponseClassPath")).newInstance();
82             healthresponse.init(config);
83             
84         } catch( Exception JavaDoc e ) {
85             throw new ServletException JavaDoc(e);
86         }
87     }
88
89     /**
90      * Handles HTTP POST
91      *
92      * @param request servlet request
93      * @param response servlet response
94      *
95      * @throws IOException input/output error
96      * @throws ServletException on error
97      */

98     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
99         throws IOException JavaDoc, ServletException JavaDoc {
100         log.debug(">doPost()");
101         check(request, response);
102         log.debug("<doPost()");
103     }
104
105     //doPost
106

107     /**
108      * Handles HTTP GET
109      *
110      * @param request servlet request
111      * @param response servlet response
112      *
113      * @throws IOException input/output error
114      * @throws ServletException on error
115      */

116     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
117         throws IOException JavaDoc, ServletException JavaDoc {
118         log.debug(">doGet()");
119         check(request, response);
120         log.debug("<doGet()");
121     }
122     
123     private void check(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response){
124         boolean authorizedIP = false;
125         String JavaDoc remoteIP = request.getRemoteAddr();
126         if ( (authIPs != null) && (authIPs.length > 0) ) {
127             for(int i=0; i < authIPs.length ; i++) {
128                 if(remoteIP.equals(authIPs[i])) {
129                     authorizedIP = true;
130                 }
131             }
132         } else {
133             String JavaDoc iMsg = intres.getLocalizedMessage("healthcheck.allipsauthorized");
134             log.info(iMsg);
135             authorizedIP = true;
136         }
137
138         if (authorizedIP) {
139             healthresponse.respond(healthcheck.checkHealth(request),response);
140         } else {
141             if ((remoteIP == null) || (remoteIP.length() > 100) ) {
142                 remoteIP="unknown";
143             }
144             try {
145                 response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"ERROR : Healthcheck request recieved from an non authorized IP: "+remoteIP);
146             } catch (IOException JavaDoc e) {
147                 log.error("ERROR : Problems generating unauthorized http response.");
148             }
149             String JavaDoc iMsg = intres.getLocalizedMessage("healthcheck.errorauth", remoteIP);
150             log.error(iMsg);
151         }
152     }
153
154 }
155
156
157 // HealthCheckServlet
158
Popular Tags