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.
17 package org.apache.jk.status;
19 import java.io.IOException;
20 import java.net.HttpURLConnection;
21 import java.net.MalformedURLException;
22 import java.net.ProtocolException;
24 import java.net.URLConnection;
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;
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
39 public class JkStatusAccessor {
41 private static Log log = LogFactory.getLog(JkStatusAccessor.class);
43 * The descriptive information about this implementation.
45 protected static final String info = "org.apache.jk.status.JkStatusAccessor/1.0";
48 * Parse Apache mod_jk Status from base url http://host:port/jkstatus)
54 public JkStatus status(String url, String username, String password)
57 if(url == null || "".equals(url))
59 HttpURLConnection hconn = null;
60 JkStatus status = null;
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());
69 } catch (Throwable t) {
70 throw new Exception(t);
75 } catch (Throwable u) {
85 * Create a auth http connection for this url
90 * @return HttpConnection
92 * @throws MalformedURLException
93 * @throws ProtocolException
95 protected HttpURLConnection openConnection(String url, String username,
96 String password) throws IOException, MalformedURLException,
99 conn = (new URL(url)).openConnection();
100 HttpURLConnection hconn = (HttpURLConnection) conn;
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");
110 if(username != null && password != null ) {
111 setAuthHeader(hconn, username, password);
113 // Establish the connection with the server
119 * Set Basic Auth Header
125 protected void setAuthHeader(HttpURLConnection hconn, String username,
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);