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 |
|
---|
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 | /**
|
---|
29 | * Abstract base class to provide common services for the
|
---|
30 | * VAJ tool API servlets
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public abstract class VAJToolsServlet extends HttpServlet {
|
---|
34 | /**
|
---|
35 | * Adaptation of VAJUtil for servlet context.
|
---|
36 | */
|
---|
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 | // constants for servlet param names
|
---|
61 | 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 | // current request
|
---|
72 | HttpServletRequest request;
|
---|
73 |
|
---|
74 | // response to current request
|
---|
75 | HttpServletResponse response;
|
---|
76 |
|
---|
77 | // implementation of VAJUtil used by the servlet
|
---|
78 | VAJUtil util;
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Execute the request by calling the appropriate
|
---|
83 | * VAJ tool API methods. This method must be implemented
|
---|
84 | * by the concrete servlets
|
---|
85 | */
|
---|
86 | protected abstract void executeRequest();
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Respond to a HTTP request. This method initializes
|
---|
90 | * the servlet and handles errors.
|
---|
91 | * The real work is done in the abstract method executeRequest()
|
---|
92 | */
|
---|
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 | /**
|
---|
120 | * initialize the servlet.
|
---|
121 | */
|
---|
122 | protected void initRequest() throws IOException {
|
---|
123 | response.setContentType("text/ascii");
|
---|
124 | if (util == null) {
|
---|
125 | util = new VAJLocalServletUtil();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Get the VAJUtil implementation
|
---|
131 | */
|
---|
132 | VAJUtil getUtil() {
|
---|
133 | return util;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Get the boolean value of a parameter.
|
---|
138 | */
|
---|
139 | protected boolean getBooleanParam(String param) {
|
---|
140 | return getBooleanParam(param, false);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Get the boolean value of a parameter, with a default value if
|
---|
145 | * the parameter hasn't been passed to the servlet.
|
---|
146 | */
|
---|
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 | /**
|
---|
157 | * Returns the first encountered value for a parameter.
|
---|
158 | */
|
---|
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 | /**
|
---|
168 | * Returns all values for a parameter.
|
---|
169 | */
|
---|
170 | protected String[] getParamValues(String param) {
|
---|
171 | return request.getParameterValues(param);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * A utility method to translate the strings "yes", "true", and "ok"
|
---|
176 | * to boolean true, and everything else to false.
|
---|
177 | */
|
---|
178 | protected boolean toBoolean(String string) {
|
---|
179 | String lower = string.toLowerCase();
|
---|
180 | return (lower.equals("yes") || lower.equals("true") || lower.equals("ok"));
|
---|
181 | }
|
---|
182 | }
|
---|