1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.ide;
  18. import com.ibm.ivj.toolserver.servletclasses.servlet.ServletException;
  19. import com.ibm.ivj.toolserver.servletclasses.servlet.http.HttpServlet;
  20. import com.ibm.ivj.toolserver.servletclasses.servlet.http.HttpServletRequest;
  21. import com.ibm.ivj.toolserver.servletclasses.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.util.StringUtils;
  25. /**
  26. * Abstract base class to provide common services for the
  27. * VAJ tool API servlets
  28. *
  29. */
  30. public abstract class VAJToolsServlet extends HttpServlet {
  31. /**
  32. * Adaptation of VAJUtil for servlet context.
  33. */
  34. class VAJLocalServletUtil extends VAJLocalUtil {
  35. public void log(String msg, int level) {
  36. try {
  37. if (msg != null) {
  38. msg = msg.replace('\r', ' ');
  39. int i = 0;
  40. while (i < msg.length()) {
  41. int nlPos = msg.indexOf('\n', i);
  42. if (nlPos == -1) {
  43. nlPos = msg.length();
  44. }
  45. response.getWriter().println(Integer.toString(level)
  46. + " " + msg.substring(i, nlPos));
  47. i = nlPos + 1;
  48. }
  49. }
  50. } catch (IOException e) {
  51. throw new BuildException("logging failed. msg was: "
  52. + e.getMessage());
  53. }
  54. }
  55. }
  56. // constants for servlet param names
  57. public static final String DIR_PARAM = "dir";
  58. public static final String INCLUDE_PARAM = "include";
  59. public static final String EXCLUDE_PARAM = "exclude";
  60. public static final String CLASSES_PARAM = "cls";
  61. public static final String SOURCES_PARAM = "src";
  62. public static final String RESOURCES_PARAM = "res";
  63. public static final String DEFAULT_EXCLUDES_PARAM = "dex";
  64. public static final String PROJECT_NAME_PARAM = "project";
  65. // current request
  66. HttpServletRequest request;
  67. // response to current request
  68. HttpServletResponse response;
  69. // implementation of VAJUtil used by the servlet
  70. VAJUtil util;
  71. /**
  72. * Execute the request by calling the appropriate
  73. * VAJ tool API methods. This method must be implemented
  74. * by the concrete servlets
  75. */
  76. protected abstract void executeRequest();
  77. /**
  78. * Respond to a HTTP request. This method initializes
  79. * the servlet and handles errors.
  80. * The real work is done in the abstract method executeRequest()
  81. */
  82. public void doGet(HttpServletRequest req, HttpServletResponse res)
  83. throws ServletException, IOException {
  84. try {
  85. response = res;
  86. request = req;
  87. initRequest();
  88. executeRequest();
  89. } catch (BuildException e) {
  90. util.log("Error occurred: " + e.getMessage(), VAJUtil.MSG_ERR);
  91. } catch (Exception e) {
  92. try {
  93. if (!(e instanceof BuildException)) {
  94. String trace = StringUtils.getStackTrace(e);
  95. util.log("Program error in " + this.getClass().getName()
  96. + ":\n" + trace, VAJUtil.MSG_ERR);
  97. }
  98. } catch (Throwable t) {
  99. t.printStackTrace();
  100. } finally {
  101. if (!(e instanceof BuildException)) {
  102. throw new ServletException(e.getMessage());
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * initialize the servlet.
  109. */
  110. protected void initRequest() throws IOException {
  111. response.setContentType("text/ascii");
  112. if (util == null) {
  113. util = new VAJLocalServletUtil();
  114. }
  115. }
  116. /**
  117. * Get the VAJUtil implementation
  118. */
  119. VAJUtil getUtil() {
  120. return util;
  121. }
  122. /**
  123. * Get the boolean value of a parameter.
  124. */
  125. protected boolean getBooleanParam(String param) {
  126. return getBooleanParam(param, false);
  127. }
  128. /**
  129. * Get the boolean value of a parameter, with a default value if
  130. * the parameter hasn't been passed to the servlet.
  131. */
  132. protected boolean getBooleanParam(String param, boolean defaultValue) {
  133. String value = getFirstParamValueString(param);
  134. if (value != null) {
  135. return toBoolean(value);
  136. } else {
  137. return defaultValue;
  138. }
  139. }
  140. /**
  141. * Returns the first encountered value for a parameter.
  142. */
  143. protected String getFirstParamValueString(String param) {
  144. String[] paramValuesArray = request.getParameterValues(param);
  145. if (paramValuesArray == null) {
  146. return null;
  147. }
  148. return paramValuesArray[0];
  149. }
  150. /**
  151. * Returns all values for a parameter.
  152. */
  153. protected String[] getParamValues(String param) {
  154. return request.getParameterValues(param);
  155. }
  156. /**
  157. * A utility method to translate the strings "yes", "true", and "ok"
  158. * to boolean true, and everything else to false.
  159. */
  160. protected boolean toBoolean(String string) {
  161. String lower = string.toLowerCase();
  162. return (lower.equals("yes") || lower.equals("true") || lower.equals("ok"));
  163. }
  164. }