001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.manager;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.text.DateFormat;
023: import java.text.NumberFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026: import java.util.Locale;
027:
028: import org.apache.catalina.Session;
029: import org.apache.catalina.manager.util.SessionUtils;
030:
031: /**
032: * Helper JavaBean for JSPs, because JSTL 1.1/EL 2.0 is too dumb to
033: * to what I need (call methods with parameters), or I am too dumb to use it correctly. :)
034: * @author Cédrik LIME
035: */
036: public class JspHelper {
037:
038: private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
039: private static final String DATE_FORMAT = "yyyy-MM-dd";
040: private static final String TIME_FORMAT = "HH:mm:ss";
041:
042: /**
043: * Public constructor, so that this class can be considered a JavaBean
044: */
045: private JspHelper() {
046: super ();
047: }
048:
049: /**
050: * Try to get user locale from the session, if possible.
051: * IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3 and Struts 1.x
052: * @param in_session
053: * @return String
054: */
055: public static String guessDisplayLocaleFromSession(
056: Session in_session) {
057: return localeToString(SessionUtils
058: .guessLocaleFromSession(in_session));
059: }
060:
061: private static String localeToString(Locale locale) {
062: if (locale != null) {
063: return locale.toString();//locale.getDisplayName();
064: } else {
065: return "";
066: }
067: }
068:
069: /**
070: * Try to get user name from the session, if possible.
071: * @param in_session
072: * @return String
073: */
074: public static String guessDisplayUserFromSession(Session in_session) {
075: Object user = SessionUtils.guessUserFromSession(in_session);
076: return escapeXml(user);
077: }
078:
079: public static String getDisplayCreationTimeForSession(
080: Session in_session) {
081: try {
082: DateFormat formatter = new SimpleDateFormat(
083: DATE_TIME_FORMAT);
084: return formatter.format(new Date(in_session
085: .getCreationTime()));
086: } catch (IllegalStateException ise) {
087: //ignore: invalidated session
088: return "";
089: }
090: }
091:
092: public static String getDisplayLastAccessedTimeForSession(
093: Session in_session) {
094: try {
095: DateFormat formatter = new SimpleDateFormat(
096: DATE_TIME_FORMAT);
097: return formatter.format(new Date(in_session
098: .getLastAccessedTime()));
099: } catch (IllegalStateException ise) {
100: //ignore: invalidated session
101: return "";
102: }
103: }
104:
105: public static String getDisplayUsedTimeForSession(Session in_session) {
106: return secondsToTimeString(SessionUtils
107: .getUsedTimeForSession(in_session) / 1000);
108: }
109:
110: public static String getDisplayTTLForSession(Session in_session) {
111: return secondsToTimeString(SessionUtils
112: .getTTLForSession(in_session) / 1000);
113: }
114:
115: public static String getDisplayInactiveTimeForSession(
116: Session in_session) {
117: return secondsToTimeString(SessionUtils
118: .getInactiveTimeForSession(in_session) / 1000);
119: }
120:
121: public static String secondsToTimeString(long in_seconds) {
122: StringBuffer buff = new StringBuffer(9);
123: long rest = in_seconds;
124: long hour = rest / 3600;
125: rest = rest % 3600;
126: long minute = rest / 60;
127: rest = rest % 60;
128: long second = rest;
129: if (hour < 10) {
130: buff.append('0');
131: }
132: buff.append(hour);
133: buff.append(':');
134: if (minute < 10) {
135: buff.append('0');
136: }
137: buff.append(minute);
138: buff.append(':');
139: if (second < 10) {
140: buff.append('0');
141: }
142: buff.append(second);
143: return buff.toString();
144: }
145:
146: /**
147: * Following copied from org.apache.taglibs.standard.tag.common.core.OutSupport v1.1.2
148: *
149: * Optimized to create no extra objects and write directly
150: * to the JspWriter using blocks of escaped and unescaped characters
151: *
152: */
153: private static void writeEscapedXml(char[] buffer, int length,
154: Writer w) throws IOException {
155: int start = 0;
156:
157: for (int i = 0; i < length; i++) {
158: char c = buffer[i];
159: if (c <= HIGHEST_SPECIAL) {
160: char[] escaped = specialCharactersRepresentation[c];
161: if (escaped != null) {
162: // add unescaped portion
163: if (start < i) {
164: w.write(buffer, start, i - start);
165: }
166: // add escaped xml
167: w.write(escaped);
168: start = i + 1;
169: }
170: }
171: }
172: // add rest of unescaped portion
173: if (start < length) {
174: w.write(buffer, start, length - start);
175: }
176: }
177:
178: /*
179: * Following copied from org.apache.taglibs.standard.tag.common.core.Util v1.1.2
180: */
181:
182: private static final int HIGHEST_SPECIAL = '>';
183: private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
184: static {
185: specialCharactersRepresentation['&'] = "&".toCharArray();
186: specialCharactersRepresentation['<'] = "<".toCharArray();
187: specialCharactersRepresentation['>'] = ">".toCharArray();
188: specialCharactersRepresentation['"'] = """.toCharArray();
189: specialCharactersRepresentation['\''] = "'".toCharArray();
190: }
191:
192: public static String escapeXml(Object obj) {
193: String value = null;
194: try {
195: value = (obj == null) ? null : String.valueOf(obj);
196: } catch (Exception e) {
197: // Ignore
198: }
199: return escapeXml(value);
200: }
201:
202: /**
203: * Performs the following substring replacements
204: * (to facilitate output to XML/HTML pages):
205: *
206: * & -> &
207: * < -> <
208: * > -> >
209: * " -> "
210: * ' -> '
211: *
212: * See also OutSupport.writeEscapedXml().
213: */
214: public static String escapeXml(String buffer) {
215: if (buffer == null) {
216: return "";
217: }
218: int start = 0;
219: int length = buffer.length();
220: char[] arrayBuffer = buffer.toCharArray();
221: StringBuffer escapedBuffer = null;
222:
223: for (int i = 0; i < length; i++) {
224: char c = arrayBuffer[i];
225: if (c <= HIGHEST_SPECIAL) {
226: char[] escaped = specialCharactersRepresentation[c];
227: if (escaped != null) {
228: // create StringBuffer to hold escaped xml string
229: if (start == 0) {
230: escapedBuffer = new StringBuffer(length + 5);
231: }
232: // add unescaped portion
233: if (start < i) {
234: escapedBuffer.append(arrayBuffer, start, i
235: - start);
236: }
237: start = i + 1;
238: // add escaped xml
239: escapedBuffer.append(escaped);
240: }
241: }
242: }
243: // no xml escaping was necessary
244: if (start == 0) {
245: return buffer;
246: }
247: // add rest of unescaped portion
248: if (start < length) {
249: escapedBuffer.append(arrayBuffer, start, length - start);
250: }
251: return escapedBuffer.toString();
252: }
253:
254: public static String formatNumber(long number) {
255: return NumberFormat.getNumberInstance().format(number);
256: }
257: }
|