2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.jk.status;
20 import java.io.BufferedOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.net.HttpURLConnection;
25 import java.net.MalformedURLException;
26 import java.net.ProtocolException;
28 import java.net.URLConnection;
30 import org.apache.catalina.ant.AbstractCatalinaTask;
31 import org.apache.catalina.util.Base64;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.Project;
36 * Ant task that implements mod_jk 1.2.20 result message string
38 * @author Peter Rossbach
39 * @version $Revision: 802231 $
40 * @since mod_jk 1.2.20
42 public abstract class AbstractJkStatusTask extends AbstractCatalinaTask {
45 * Execute the requested operation.
47 * @exception BuildException
50 public void execute() throws BuildException {
54 StringBuffer sb = createLink();
55 execute(sb.toString(), null, null, -1);
59 protected abstract void checkParameter() ;
60 protected abstract StringBuffer createLink() ;
63 * Execute the specified command, based on the configured properties.
64 * The input stream will be closed upon completion of this task, whether
65 * it was executed successfully or not.
67 * @param command Command to be executed
68 * @param istream InputStream to include in an HTTP PUT, if any
69 * @param contentType Content type to specify for the input, if any
70 * @param contentLength Content length to specify for the input, if any
72 * @exception BuildException if an error occurs
74 public void execute(String command, InputStream istream,
75 String contentType, int contentLength)
76 throws BuildException {
78 InputStreamReader reader = null;
81 HttpURLConnection hconn = send(command, istream, contentType, contentLength);
83 // Process the response message
84 reader = new InputStreamReader(hconn.getInputStream(), "UTF-8");
86 error = handleResult(reader, error);
87 if (error != null && isFailOnError()) {
88 // exception should be thrown only if failOnError == true
89 // or error line will be logged twice
90 throw new BuildException(error);
92 } catch (Throwable t) {
93 if (isFailOnError()) {
94 throw new BuildException(t);
96 handleErrorOutput(t.getMessage());
100 if (reader != null) {
103 } catch (Throwable u) {
108 if (istream != null) {
111 } catch (Throwable u) {
120 private String handleResult(InputStreamReader reader, String error) throws IOException {
121 StringBuffer buff = new StringBuffer();
122 int msgPriority = Project.MSG_INFO;
123 boolean first = true;
125 int ch = reader.read();
128 } else if ((ch == '\r') || (ch == '\n')) {
129 // in Win \r\n would cause handleOutput() to be called
130 // twice, the second time with an empty string,
131 // producing blank lines
132 if (buff.length() > 0) {
133 String line = buff.toString();
136 if (!line.startsWith("Result: type=OK")) {
138 msgPriority = Project.MSG_ERR;
142 handleOutput(line, msgPriority);
145 buff.append((char) ch);
148 if (buff.length() > 0) {
149 handleOutput(buff.toString(), msgPriority);
154 protected HttpURLConnection send(String command, InputStream istream, String contentType, int contentLength) throws IOException, MalformedURLException, ProtocolException {
156 // Create a connection for this command
157 conn = (new URL(url + command)).openConnection();
158 HttpURLConnection hconn = (HttpURLConnection) conn;
160 // Set up standard connection characteristics
161 hconn.setAllowUserInteraction(false);
162 hconn.setDoInput(true);
163 hconn.setUseCaches(false);
164 if (istream != null) {
165 hconn.setDoOutput(true);
166 hconn.setRequestMethod("PUT");
167 if (contentType != null) {
168 hconn.setRequestProperty("Content-Type", contentType);
170 if (contentLength >= 0) {
171 hconn.setRequestProperty("Content-Length",
175 hconn.setDoOutput(false);
176 hconn.setRequestMethod("GET");
178 hconn.setRequestProperty("User-Agent",
179 "JkStatus-Ant-Task/1.1");
181 // Set up an authorization header with our credentials
182 String input = username + ":" + password;
183 String output = new String(Base64.encode(input.getBytes()));
184 hconn.setRequestProperty("Authorization",
187 // Establish the connection with the server
189 // Send the request data (if any)
190 if (istream != null) {
191 BufferedOutputStream ostream =
192 new BufferedOutputStream(hconn.getOutputStream(), 1024);
193 byte buffer[] = new byte[1024];
195 int n = istream.read(buffer);
199 ostream.write(buffer, 0, n);