1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.manager;
import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.apache.catalina.Session;
import org.apache.catalina.manager.util.SessionUtils;
/**
* Helper JavaBean for JSPs, because JSTL 1.1/EL 2.0 is too dumb to
* to what I need (call methods with parameters), or I am too dumb to use it correctly. :)
* @author Cédrik LIME
*/
public class JspHelper {
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "HH:mm:ss";
/**
* Public constructor, so that this class can be considered a JavaBean
*/
private JspHelper() {
super();
}
/**
* Try to get user locale from the session, if possible.
* IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3 and Struts 1.x
* @param in_session
* @return String
*/
public static String guessDisplayLocaleFromSession(Session in_session) {
return localeToString(SessionUtils.guessLocaleFromSession(in_session));
}
private static String localeToString(Locale locale) {
if (locale != null) {
return escapeXml(locale.toString());//locale.getDisplayName();
} else {
return "";
}
}
/**
* Try to get user name from the session, if possible.
* @param in_session
* @return String
*/
public static String guessDisplayUserFromSession(Session in_session) {
Object user = SessionUtils.guessUserFromSession(in_session);
return escapeXml(user);
}
public static String getDisplayCreationTimeForSession(Session in_session) {
try {
DateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT);
return formatter.format(new Date(in_session.getCreationTime()));
} catch (IllegalStateException ise) {
//ignore: invalidated session
return "";
}
}
public static String getDisplayLastAccessedTimeForSession(Session in_session) {
try {
DateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT);
return formatter.format(new Date(in_session.getLastAccessedTime()));
} catch (IllegalStateException ise) {
//ignore: invalidated session
return "";
}
}
public static String getDisplayUsedTimeForSession(Session in_session) {
return secondsToTimeString(SessionUtils.getUsedTimeForSession(in_session)/1000);
}
public static String getDisplayTTLForSession(Session in_session) {
return secondsToTimeString(SessionUtils.getTTLForSession(in_session)/1000);
}
public static String getDisplayInactiveTimeForSession(Session in_session) {
return secondsToTimeString(SessionUtils.getInactiveTimeForSession(in_session)/1000);
}
public static String secondsToTimeString(long in_seconds) {
StringBuffer buff = new StringBuffer(9);
if (in_seconds < 0) {
buff.append('-');
in_seconds = -in_seconds;
}
long rest = in_seconds;
long hour = rest / 3600;
rest = rest % 3600;
long minute = rest / 60;
rest = rest % 60;
long second = rest;
if (hour < 10) {
buff.append('0');
}
buff.append(hour);
buff.append(':');
if (minute < 10) {
buff.append('0');
}
buff.append(minute);
buff.append(':');
if (second < 10) {
buff.append('0');
}
buff.append(second);
return buff.toString();
}
/**
* Following copied from org.apache.taglibs.standard.tag.common.core.OutSupport v1.1.2
*
* Optimized to create no extra objects and write directly
* to the JspWriter using blocks of escaped and unescaped characters
*
*/
private static void writeEscapedXml(char[] buffer, int length, Writer w) throws IOException {
int start = 0;
for (int i = 0; i < length; i++) {
char c = buffer[i];
if (c <= HIGHEST_SPECIAL) {
char[] escaped = specialCharactersRepresentation[c];
if (escaped != null) {
// add unescaped portion
if (start < i) {
w.write(buffer,start,i-start);
}
// add escaped xml
w.write(escaped);
start = i + 1;
}
}
}
// add rest of unescaped portion
if (start < length) {
w.write(buffer,start,length-start);
}
}
/*
* Following copied from org.apache.taglibs.standard.tag.common.core.Util v1.1.2
*/
private static final int HIGHEST_SPECIAL = '>';
private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
static {
specialCharactersRepresentation['&'] = "&".toCharArray();
specialCharactersRepresentation['<'] = "<".toCharArray();
specialCharactersRepresentation['>'] = ">".toCharArray();
specialCharactersRepresentation['"'] = """.toCharArray();
specialCharactersRepresentation['\''] = "'".toCharArray();
}
public static String escapeXml(Object obj) {
String value = null;
try {
value = (obj == null) ? null : String.valueOf(obj);
} catch (Exception e) {
// Ignore
}
return escapeXml(value);
}
/**
* Performs the following substring replacements
* (to facilitate output to XML/HTML pages):
*
* & -> &
* < -> <
* > -> >
* " -> "
* ' -> '
*
* See also OutSupport.writeEscapedXml().
*/
public static String escapeXml(String buffer) {
if (buffer == null) {
return "";
}
int start = 0;
int length = buffer.length();
char[] arrayBuffer = buffer.toCharArray();
StringBuffer escapedBuffer = null;
for (int i = 0; i < length; i++) {
char c = arrayBuffer[i];
if (c <= HIGHEST_SPECIAL) {
char[] escaped = specialCharactersRepresentation[c];
if (escaped != null) {
// create StringBuffer to hold escaped xml string
if (start == 0) {
escapedBuffer = new StringBuffer(length + 5);
}
// add unescaped portion
if (start < i) {
escapedBuffer.append(arrayBuffer,start,i-start);
}
start = i + 1;
// add escaped xml
escapedBuffer.append(escaped);
}
}
}
// no xml escaping was necessary
if (start == 0) {
return buffer;
}
// add rest of unescaped portion
if (start < length) {
escapedBuffer.append(arrayBuffer,start,length-start);
}
return escapedBuffer.toString();
}
public static String formatNumber(long number) {
return NumberFormat.getNumberInstance().format(number);
}
}
|