bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / jkstatus / src / share / org / apache / jk / status / AbstractJkStatusTask.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 package org.apache.jk.status;
19
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;
27 import java.net.URL;
28 import java.net.URLConnection;
29
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;
34
35 /**
36  * Ant task that implements mod_jk 1.2.20 result message string
37  * 
38  * @author Peter Rossbach
39  * @version $Revision: 802231 $
40  * @since mod_jk 1.2.20
41  */
42 public abstract class AbstractJkStatusTask extends AbstractCatalinaTask {
43
44         /**
45      * Execute the requested operation.
46      * 
47      * @exception BuildException
48      *                if an error occurs
49      */
50     public void execute() throws BuildException {
51
52         super.execute();
53         checkParameter();
54         StringBuffer sb = createLink();
55         execute(sb.toString(), null, null, -1);
56
57     }
58     
59     protected abstract void checkParameter() ;
60     protected abstract StringBuffer createLink() ;
61          
62     /**
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.
66      *
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
71      *
72      * @exception BuildException if an error occurs
73      */
74     public void execute(String command, InputStream istream,
75                         String contentType, int contentLength)
76         throws BuildException {
77
78         InputStreamReader reader = null;
79         try {
80
81             HttpURLConnection hconn = send(command, istream, contentType, contentLength);
82
83             // Process the response message
84             reader = new InputStreamReader(hconn.getInputStream(), "UTF-8");
85             String error = null;
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);
91             }
92         } catch (Throwable t) {
93             if (isFailOnError()) {
94                 throw new BuildException(t);
95             } else {
96                 handleErrorOutput(t.getMessage());
97             }
98         } finally {
99             closeRedirector();
100             if (reader != null) {
101                 try {
102                     reader.close();
103                 } catch (Throwable u) {
104                     ;
105                 }
106                 reader = null;
107             }
108             if (istream != null) {
109                 try {
110                     istream.close();
111                 } catch (Throwable u) {
112                     ;
113                 }
114                 istream = null;
115             }
116         }
117
118     }
119
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;
124                 while (true) {
125                     int ch = reader.read();
126                     if (ch < 0) {
127                         break;
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();
134                             buff.setLength(0);
135                             if (first) {
136                                 if (!line.startsWith("Result: type=OK")) {
137                                     error = line;
138                                     msgPriority = Project.MSG_ERR;
139                                 }
140                                 first = false;
141                             }
142                             handleOutput(line, msgPriority);
143                         }
144                     } else {
145                         buff.append((char) ch);
146                     }
147                 }
148                 if (buff.length() > 0) {
149                     handleOutput(buff.toString(), msgPriority);
150                 }
151                 return error;
152         }
153     
154         protected HttpURLConnection send(String command, InputStream istream, String contentType, int contentLength) throws IOException, MalformedURLException, ProtocolException {
155                 URLConnection conn;
156                 // Create a connection for this command
157                 conn = (new URL(url + command)).openConnection();
158                 HttpURLConnection hconn = (HttpURLConnection) conn;
159
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);
169                     }
170                     if (contentLength >= 0) {
171                         hconn.setRequestProperty("Content-Length",
172                                                  "" + contentLength);
173                     }
174                 } else {
175                     hconn.setDoOutput(false);
176                     hconn.setRequestMethod("GET");
177                 }
178                 hconn.setRequestProperty("User-Agent",
179                                          "JkStatus-Ant-Task/1.1");
180
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",
185                                          "Basic " + output);
186
187                 // Establish the connection with the server
188                 hconn.connect();
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];
194             while (true) {
195                 int n = istream.read(buffer);
196                 if (n < 0) {
197                     break;
198                 }
199                 ostream.write(buffer, 0, n);
200             }
201             ostream.flush();
202             ostream.close();
203             istream.close();
204         }
205                 return hconn;
206         }
207
208
209 }