1 13 14 package org.ejbca.ui.web.pub; 15 16 import java.io.IOException ; 17 18 import javax.servlet.ServletConfig ; 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.HttpServlet ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 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 48 public class HealthCheckServlet extends HttpServlet { 49 private static final Logger log = Logger.getLogger(HealthCheckServlet.class); 50 51 private static final InternalResources intres = InternalResources.getInstance(); 52 53 private IHealthCheck healthcheck = null; 54 private IHealthResponse healthresponse = null; 55 56 private String [] authIPs = null; 57 58 65 public void init(ServletConfig config) throws ServletException { 66 super.init(config); 67 68 try { 69 CertTools.installBCProvider(); 71 72 String 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 e ) { 85 throw new ServletException (e); 86 } 87 } 88 89 98 public void doPost(HttpServletRequest request, HttpServletResponse response) 99 throws IOException , ServletException { 100 log.debug(">doPost()"); 101 check(request, response); 102 log.debug("<doPost()"); 103 } 104 105 107 116 public void doGet(HttpServletRequest request, HttpServletResponse response) 117 throws IOException , ServletException { 118 log.debug(">doGet()"); 119 check(request, response); 120 log.debug("<doGet()"); 121 } 122 123 private void check(HttpServletRequest request, HttpServletResponse response){ 124 boolean authorizedIP = false; 125 String 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 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 e) { 147 log.error("ERROR : Problems generating unauthorized http response."); 148 } 149 String iMsg = intres.getLocalizedMessage("healthcheck.errorauth", remoteIP); 150 log.error(iMsg); 151 } 152 } 153 154 } 155 156 157 | Popular Tags |