1 17 18 package org.apache.tools.ant.taskdefs.optional.ide; 19 20 import com.ibm.ivj.toolserver.servletclasses.servlet.ServletException; 21 import com.ibm.ivj.toolserver.servletclasses.servlet.http.HttpServlet; 22 import com.ibm.ivj.toolserver.servletclasses.servlet.http.HttpServletRequest; 23 import com.ibm.ivj.toolserver.servletclasses.servlet.http.HttpServletResponse; 24 import java.io.IOException ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.util.StringUtils; 27 28 33 public abstract class VAJToolsServlet extends HttpServlet { 34 37 class VAJLocalServletUtil extends VAJLocalUtil { 38 public void log(String msg, int level) { 39 try { 40 if (msg != null) { 41 msg = msg.replace('\r', ' '); 42 int i = 0; 43 while (i < msg.length()) { 44 int nlPos = msg.indexOf('\n', i); 45 if (nlPos == -1) { 46 nlPos = msg.length(); 47 } 48 response.getWriter().println(Integer.toString(level) 49 + " " + msg.substring(i, nlPos)); 50 i = nlPos + 1; 51 } 52 } 53 } catch (IOException e) { 54 throw new BuildException("logging failed. msg was: " 55 + e.getMessage()); 56 } 57 } 58 } 59 60 public static final String DIR_PARAM = "dir"; 62 public static final String INCLUDE_PARAM = "include"; 63 public static final String EXCLUDE_PARAM = "exclude"; 64 public static final String CLASSES_PARAM = "cls"; 65 public static final String SOURCES_PARAM = "src"; 66 public static final String RESOURCES_PARAM = "res"; 67 public static final String DEFAULT_EXCLUDES_PARAM = "dex"; 68 public static final String PROJECT_NAME_PARAM = "project"; 69 70 71 HttpServletRequest request; 73 74 HttpServletResponse response; 76 77 VAJUtil util; 79 80 81 86 protected abstract void executeRequest(); 87 88 93 public void doGet(HttpServletRequest req, HttpServletResponse res) 94 throws ServletException, IOException { 95 try { 96 response = res; 97 request = req; 98 initRequest(); 99 executeRequest(); 100 } catch (BuildException e) { 101 util.log("Error occurred: " + e.getMessage(), VAJUtil.MSG_ERR); 102 } catch (Exception e) { 103 try { 104 if (!(e instanceof BuildException)) { 105 String trace = StringUtils.getStackTrace(e); 106 util.log("Program error in " + this.getClass().getName() 107 + ":\n" + trace, VAJUtil.MSG_ERR); 108 } 109 } catch (Throwable t) { 110 t.printStackTrace(); 111 } finally { 112 if (!(e instanceof BuildException)) { 113 throw new ServletException(e.getMessage()); 114 } 115 } 116 } 117 } 118 119 122 protected void initRequest() throws IOException { 123 response.setContentType("text/ascii"); 124 if (util == null) { 125 util = new VAJLocalServletUtil(); 126 } 127 } 128 129 132 VAJUtil getUtil() { 133 return util; 134 } 135 136 139 protected boolean getBooleanParam(String param) { 140 return getBooleanParam(param, false); 141 } 142 143 147 protected boolean getBooleanParam(String param, boolean defaultValue) { 148 String value = getFirstParamValueString(param); 149 if (value != null) { 150 return toBoolean(value); 151 } else { 152 return defaultValue; 153 } 154 } 155 156 159 protected String getFirstParamValueString(String param) { 160 String [] paramValuesArray = request.getParameterValues(param); 161 if (paramValuesArray == null) { 162 return null; 163 } 164 return paramValuesArray[0]; 165 } 166 167 170 protected String [] getParamValues(String param) { 171 return request.getParameterValues(param); 172 } 173 174 178 protected boolean toBoolean(String string) { 179 String lower = string.toLowerCase(); 180 return (lower.equals("yes") || lower.equals("true") || lower.equals("ok")); 181 } 182 } 183 | Popular Tags |