bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / jkstatus / src / share / org / apache / jk / status / JkStatusAccessor.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 package org.apache.jk.status;
18
19 import java.io.IOException;
20 import java.net.HttpURLConnection;
21 import java.net.MalformedURLException;
22 import java.net.ProtocolException;
23 import java.net.URL;
24 import java.net.URLConnection;
25
26 import org.apache.catalina.util.Base64;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.tomcat.util.digester.Digester;
30
31 /**
32  * Create connection to mod_jk jkstatus page.
33  * Optional you can use Http basic auth user and password.
34  * @author Peter Rossbach
35  * @version $Revision: 485242 $ $Date: 2006-12-10 20:45:39 +0100 (Sun, 10 Dec 2006) $
36  * @see org.apache.jk.status.JkStatusParser
37  * @since 5.5.10
38  */
39 public class JkStatusAccessor {
40     
41     private static Log log = LogFactory.getLog(JkStatusAccessor.class);
42     /**
43      * The descriptive information about this implementation.
44      */
45     protected static final String info = "org.apache.jk.status.JkStatusAccessor/1.0";
46
47     /**
48      * Parse Apache mod_jk Status  from base url http://host:port/jkstatus)
49      * @param url
50      * @param username
51      * @param password
52      *  
53      */
54     public JkStatus status(String url, String username, String password)
55             throws Exception {
56
57         if(url == null || "".equals(url))
58             return null ;
59         HttpURLConnection hconn = null;
60         JkStatus status = null;
61
62         try {
63                 // FIXME: use cmd show for older mod_jk versions
64             hconn = openConnection(url + "?cmd=list&mime=xml", username, password);
65             Digester digester = JkStatusParser.getDigester();
66             synchronized (digester) {
67                 status = (JkStatus) digester.parse(hconn.getInputStream());
68             }
69         } catch (Throwable t) {
70             throw new Exception(t);
71         } finally {
72             if (hconn != null) {
73                 try {
74                     hconn.disconnect();
75                 } catch (Throwable u) {
76                     ;
77                 }
78                 hconn = null;
79             }
80         }
81         return status;
82     }
83
84     /**
85      * Create a auth http connection for this url
86      * 
87      * @param url
88      * @param username
89      * @param password
90      * @return HttpConnection
91      * @throws IOException
92      * @throws MalformedURLException
93      * @throws ProtocolException
94      */
95     protected HttpURLConnection openConnection(String url, String username,
96             String password) throws IOException, MalformedURLException,
97             ProtocolException {
98         URLConnection conn;
99         conn = (new URL(url)).openConnection();
100         HttpURLConnection hconn = (HttpURLConnection) conn;
101
102         // Set up standard connection characteristics
103         hconn.setAllowUserInteraction(false);
104         hconn.setDoInput(true);
105         hconn.setUseCaches(false);
106         hconn.setDoOutput(false);
107         hconn.setRequestMethod("GET");
108         hconn.setRequestProperty("User-Agent", "JkStatus-Client/1.0");
109
110         if(username != null && password != null ) {
111              setAuthHeader(hconn, username, password);
112         }
113         // Establish the connection with the server
114         hconn.connect();
115         return hconn;
116     }
117
118     /**
119      * Set Basic Auth Header
120      * 
121      * @param hconn
122      * @param username
123      * @param password
124      */
125     protected void setAuthHeader(HttpURLConnection hconn, String username,
126             String password) {
127         // Set up an authorization header with our credentials
128         String input = username + ":" + password;
129         String output = new String(Base64.encode(input.getBytes()));
130         hconn.setRequestProperty("Authorization", "Basic " + output);
131     }
132
133 }