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 com.psibt.framework.net; 18 19 import java.io.*; 20 import java.net.*; 21 import java.util.*; 22 import org.apache.log4j.*; 23 24 /** 25 * This class implements a RequestHandler for log4j configuration. It serves the "/log4j/" path 26 * in the PluggableHTTPServer. If this path is requested a list of all current log4j categories 27 * with their current priorities is created. All priority settings can be changed by the user 28 * and can be submitted and taken over. 29 * 30 * @author <a HREF="mailto:V.Mentzner@psi-bt.de">Volker Mentzner</a> 31 */ 32 public class Log4jRequestHandler extends RootRequestHandler { 33 34 private Priority[] prios = Priority.getAllPossiblePriorities(); 35 36 /** 37 * Creates a new Log4jRequestHandler object 38 */ Log4jRequestHandler()39 public Log4jRequestHandler() { 40 this.setTitle("log4j"); 41 this.setDescription("log4j configuration"); 42 this.setHandledPath("/log4j/"); 43 } 44 45 /** 46 * Handles the given request and writes the reply to the given out-stream. 47 * 48 * @param request - client browser request 49 * @param out - Out stream for sending data to client browser 50 * @return if the request was handled by this handler : true, else : false 51 */ handleRequest(String request, Writer out)52 public boolean handleRequest(String request, Writer out) { 53 String path = ""; 54 String query = null; 55 String name; 56 try { 57 // check request url 58 URL url = new URL("http://localhost"+request); 59 path = url.getPath(); 60 query = url.getQuery(); 61 if (path.startsWith(this.getHandledPath()) == false) { 62 return false; 63 } 64 65 out.write("HTTP/1.0 200 OK\r\n"); 66 out.write("Content-type: text/html\r\n\r\n"); 67 out.write("<HTML><HEAD><TITLE>" + this.getTitle() + "</TITLE></HEAD>\r\n"); 68 out.write("<BODY><H1>log4j</H1>\r\n"); 69 out.write(this.getDescription() + "<br><br>\r\n"); 70 71 // handle a request with query 72 if ((query != null) && (query.length() >= 0)) { 73 StringTokenizer st = new StringTokenizer(query, "&"); 74 String cmd; 75 String catname; 76 String catval; 77 int idx; 78 while (st.hasMoreTokens()) { 79 cmd = st.nextToken(); 80 idx = cmd.indexOf("="); 81 catname = cmd.substring(0, idx); 82 catval = cmd.substring(idx+1, cmd.length()); 83 if (catname.equalsIgnoreCase("root")) 84 Category.getRoot().setPriority(Priority.toPriority(catval)); 85 else 86 Category.getInstance(catname).setPriority(Priority.toPriority(catval)); 87 } 88 } 89 90 // output category information in a form with a simple table 91 out.write("<form name=\"Formular\" ACTION=\""+this.getHandledPath()+"\" METHOD=\"PUT\">"); 92 out.write("<table cellpadding=4>\r\n"); 93 out.write(" <tr>\r\n"); 94 out.write(" <td><b>Category</b></td>\r\n"); 95 out.write(" <td><b>Priority</b></td>\r\n"); 96 out.write(" <td><b>Appender</b></td>\r\n"); 97 out.write(" </tr>\r\n"); 98 99 // output for root category 100 Category cat = Category.getRoot(); 101 out.write(" <tr><td>root</td>\r\n"); 102 out.write(" <td>\r\n"); 103 out.write(" <select size=1 name=\""+ cat.getName() +"\">"); 104 for (int i = 0; i < prios.length; i++) { 105 if (cat.getChainedPriority().toString().equals(prios[i].toString())) 106 out.write("<option selected>"+prios[i].toString()); 107 else 108 out.write("<option>"+prios[i].toString()); 109 } 110 out.write("</select>\r\n"); 111 out.write(" </td>\r\n"); 112 out.write(" <td>\r\n"); 113 for (Enumeration apds = cat.getAllAppenders(); apds.hasMoreElements();) { 114 Appender apd = (Appender)apds.nextElement(); 115 name = apd.getName(); 116 if (name == null) 117 name = "<i>(no name)</i>"; 118 out.write(name); 119 if (apd instanceof AppenderSkeleton) { 120 try { 121 AppenderSkeleton apskel = (AppenderSkeleton)apd; 122 out.write(" [" + apskel.getThreshold().toString() + "]"); 123 } catch (Exception ex) { 124 } 125 } 126 if (apds.hasMoreElements()) 127 out.write(", "); 128 } 129 out.write(" </td>\r\n"); 130 out.write(" </tr>\r\n"); 131 132 // output for all other categories 133 for (Enumeration en = Category.getCurrentCategories(); en.hasMoreElements();) { 134 cat = (Category)en.nextElement(); 135 out.write(" <tr>\r\n"); 136 out.write(" <td>" + cat.getName() + "</td>\r\n"); 137 out.write(" <td>\r\n"); 138 out.write(" <select size=1 name=\""+ cat.getName() +"\">"); 139 for (int i = 0; i < prios.length; i++) { 140 if (cat.getChainedPriority().toString().equals(prios[i].toString())) 141 out.write("<option selected>"+prios[i].toString()); 142 else 143 out.write("<option>"+prios[i].toString()); 144 } 145 out.write("</select>\r\n"); 146 out.write(" </td>\r\n"); 147 out.write(" <td>\r\n"); 148 for (Enumeration apds = cat.getAllAppenders(); apds.hasMoreElements();) { 149 Appender apd = (Appender)apds.nextElement(); 150 name = apd.getName(); 151 if (name == null) 152 name = "<i>(no name)</i>"; 153 out.write(name); 154 if (apd instanceof AppenderSkeleton) { 155 try { 156 AppenderSkeleton apskel = (AppenderSkeleton)apd; 157 out.write(" [" + apskel.getThreshold().toString() + "]"); 158 } catch (Exception ex) { 159 } 160 } 161 if (apds.hasMoreElements()) 162 out.write(", "); 163 } 164 out.write(" </td>\r\n"); 165 out.write(" </tr>\r\n"); 166 } 167 out.write("</table>\r\n"); 168 out.write("<input type=submit value=\"Submit\">"); 169 out.write("</form>"); 170 out.write("</BODY></HTML>\r\n"); 171 out.flush(); 172 return true; 173 } catch (Exception ex) { 174 return false; 175 } 176 } 177 }