KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > pub > cluster > TextResponse


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.cluster;
15
16 import java.io.IOException JavaDoc;
17 import java.io.Writer JavaDoc;
18
19 import javax.servlet.ServletConfig JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24
25 /**
26  * Class that responds with a text string of status is OK else it responds the error message (optional).
27  *
28  * The following servlet init parameters might be used:
29  * OKMessage : the string to return when everything is ok.
30  * SendServerError : (boolean) Send A 500 Server error is returned instead of errormessage
31  * CustomErrorMsg : Send a static predefined errormessage instead of the on created by the healthchecker.
32  *
33  * @author Philip Vendil
34  * @version $Id: TextResponse.java,v 1.2 2006/02/08 07:31:48 anatom Exp $
35  *
36  */

37 public class TextResponse implements IHealthResponse {
38
39     private static Logger log = Logger.getLogger(TextResponse.class);
40     
41     private static final String JavaDoc OK_MESSAGE = "ALLOK";
42     
43     
44     private String JavaDoc okMessage = null;
45     private boolean sendServerError = false;
46     private String JavaDoc customErrorMessage = null;
47     
48     public void init(ServletConfig JavaDoc 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 JavaDoc status, HttpServletResponse JavaDoc resp) {
64         resp.setContentType("text/plain");
65         try {
66             Writer JavaDoc out = resp.getWriter();
67             if(status==null){
68                 // Return "EJBCAOK" Message
69
out.write(okMessage);
70             }else{
71                 // Return failinfo
72
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 JavaDoc e) {
86             log.error("Error writing to Servlet Response.",e);
87         }
88         
89         
90     }
91
92 }
93
Popular Tags