1 13 14 package org.ejbca.ui.web.pub.cluster; 15 16 import java.io.IOException ; 17 import java.io.Writer ; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.http.HttpServletResponse ; 21 22 import org.apache.log4j.Logger; 23 24 25 37 public class TextResponse implements IHealthResponse { 38 39 private static Logger log = Logger.getLogger(TextResponse.class); 40 41 private static final String OK_MESSAGE = "ALLOK"; 42 43 44 private String okMessage = null; 45 private boolean sendServerError = false; 46 private String customErrorMessage = null; 47 48 public void init(ServletConfig config) { 49 okMessage = config.getInitParameter("OKMessage"); 50 if(okMessage == null){ 51 okMessage = OK_MESSAGE; 52 } 53 54 if(config.getInitParameter("SendServerError") != null){ 55 sendServerError = config.getInitParameter("SendServerError").equalsIgnoreCase("TRUE"); 56 } 57 58 customErrorMessage = config.getInitParameter("CustomErrorMessage"); 59 60 61 } 62 63 public void respond(String status, HttpServletResponse resp) { 64 resp.setContentType("text/plain"); 65 try { 66 Writer out = resp.getWriter(); 67 if(status==null){ 68 out.write(okMessage); 70 }else{ 71 if(sendServerError){ 73 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,status); 74 }else{ 75 if(customErrorMessage != null){ 76 out.write(customErrorMessage); 77 }else{ 78 out.write(status); 79 } 80 } 81 82 } 83 out.flush(); 84 out.close(); 85 } catch (IOException e) { 86 log.error("Error writing to Servlet Response.",e); 87 } 88 89 90 } 91 92 } 93 | Popular Tags |