001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.util;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.util.Arrays;
023: import java.util.Collection;
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.apache.commons.lang.StringUtils;
032: import org.apache.log4j.Level;
033: import org.apache.log4j.Logger;
034: import org.kuali.RiceConstants;
035: import org.kuali.core.web.struts.form.KualiForm;
036:
037: /**
038: * General helper methods for handling requests.
039: *
040: *
041: */
042: public class WebUtils {
043: private static final Logger LOG = Logger.getLogger(WebUtils.class);
044:
045: /**
046: * Checks for methodToCall parameter, and picks off the value using set dot notation. Handles the problem of image submits.
047: *
048: * @param request
049: * @return methodToCall String
050: */
051: public static String parseMethodToCall(HttpServletRequest request) {
052: String methodToCall = null;
053:
054: // check if is specified cleanly
055: if (StringUtils
056: .isNotBlank(request
057: .getParameter(RiceConstants.DISPATCH_REQUEST_PARAMETER))) {
058: methodToCall = request
059: .getParameter(RiceConstants.DISPATCH_REQUEST_PARAMETER);
060: }
061:
062: if (methodToCall == null) {
063: // iterate through parameters looking for methodToCall
064: for (Enumeration i = request.getParameterNames(); i
065: .hasMoreElements();) {
066: String parameterName = (String) i.nextElement();
067:
068: // check if the parameter name is a specifying the methodToCall
069: if (parameterName
070: .startsWith(RiceConstants.DISPATCH_REQUEST_PARAMETER)
071: && parameterName.endsWith(".x")) {
072: methodToCall = StringUtils.substringBetween(
073: parameterName,
074: RiceConstants.DISPATCH_REQUEST_PARAMETER
075: + ".", ".");
076: request.setAttribute(
077: RiceConstants.METHOD_TO_CALL_ATTRIBUTE,
078: parameterName);
079: }
080: }
081: }
082:
083: return methodToCall;
084: }
085:
086: /**
087: * Iterates through and logs (at the given level) all attributes and parameters of the given request onto the given Logger
088: *
089: * @param request
090: * @param logger
091: */
092: public static void logRequestContents(Logger logger, Level level,
093: HttpServletRequest request) {
094: if (logger.isEnabledFor(level)) {
095: logger.log(level, "--------------------");
096: logger.log(level, "HttpRequest attributes:");
097: for (Enumeration e = request.getAttributeNames(); e
098: .hasMoreElements();) {
099: String attrName = (String) e.nextElement();
100: Object attrValue = request.getAttribute(attrName);
101:
102: if (attrValue.getClass().isArray()) {
103: logCollection(logger, level, attrName, Arrays
104: .asList((Object[]) attrValue));
105: } else if (attrValue instanceof Collection) {
106: logCollection(logger, level, attrName,
107: (Collection) attrValue);
108: } else if (attrValue instanceof Map) {
109: logMap(logger, level, attrName, (Map) attrValue);
110: } else {
111: logObject(logger, level, attrName, attrValue);
112: }
113: }
114:
115: logger.log(level, "--------------------");
116: logger.log(level, "HttpRequest parameters:");
117: for (Enumeration i = request.getParameterNames(); i
118: .hasMoreElements();) {
119: String paramName = (String) i.nextElement();
120: String[] paramValues = (String[]) request
121: .getParameterValues(paramName);
122:
123: logArray(logger, level, paramName, paramValues);
124: }
125:
126: logger.log(level, "--------------------");
127: }
128: }
129:
130: private static void logArray(Logger logger, Level level,
131: String arrayName, Object[] array) {
132: StringBuffer value = new StringBuffer("[");
133: for (int i = 0; i < array.length; ++i) {
134: if (i > 0) {
135: value.append(",");
136: }
137: value.append(array[i]);
138: }
139: value.append("]");
140:
141: logThing(logger, level, arrayName, value);
142: }
143:
144: private static void logCollection(Logger logger, Level level,
145: String collectionName, Collection c) {
146: StringBuffer value = new StringBuffer("{");
147: for (Iterator i = c.iterator(); i.hasNext();) {
148: value.append(i.next());
149: if (i.hasNext()) {
150: value.append(",");
151: }
152: }
153: value.append("}");
154:
155: logThing(logger, level, collectionName, value);
156: }
157:
158: private static void logMap(Logger logger, Level level,
159: String mapName, Map m) {
160: StringBuffer value = new StringBuffer("{");
161: for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
162: Map.Entry e = (Map.Entry) i.next();
163: value.append("('" + e.getKey() + "','" + e.getValue()
164: + "')");
165: }
166: value.append("}");
167:
168: logThing(logger, level, mapName, value);
169: }
170:
171: private static void logObject(Logger logger, Level level,
172: String objectName, Object o) {
173: logThing(logger, level, objectName, "'" + o + "'");
174: }
175:
176: private static void logThing(Logger logger, Level level,
177: String thingName, Object thing) {
178: logger.log(level, " '" + thingName + "' => " + thing);
179: }
180:
181: /**
182: * A file that is not of type text/plain or text/html can be output through the response using this method.
183: *
184: * @param response
185: * @param contentType
186: * @param outStream
187: * @param fileName
188: */
189: public static void saveMimeOutputStreamAsFile(
190: HttpServletResponse response, String contentType,
191: ByteArrayOutputStream byteArrayOutputStream, String fileName)
192: throws IOException {
193:
194: // set response
195: response.setContentType(contentType);
196: response.setHeader("Content-disposition",
197: "attachment; filename=" + fileName);
198: response.setHeader("Expires", "0");
199: response.setHeader("Cache-Control",
200: "must-revalidate, post-check=0, pre-check=0");
201: response.setHeader("Pragma", "public");
202: response.setContentLength(byteArrayOutputStream.size());
203:
204: // write to output
205: OutputStream outputStream = response.getOutputStream();
206: byteArrayOutputStream.writeTo(response.getOutputStream());
207: outputStream.flush();
208: outputStream.close();
209: }
210:
211: /**
212: * A file that is not of type text/plain or text/html can be output through the response using this method.
213: *
214: * @param response
215: * @param contentType
216: * @param outStream
217: * @param fileName
218: */
219: public static void saveMimeInputStreamAsFile(
220: HttpServletResponse response, String contentType,
221: InputStream inStream, String fileName, int fileSize)
222: throws IOException {
223:
224: // set response
225: response.setContentType(contentType);
226: response.setHeader("Content-disposition",
227: "attachment; filename=" + fileName);
228: response.setHeader("Expires", "0");
229: response.setHeader("Cache-Control",
230: "must-revalidate, post-check=0, pre-check=0");
231: response.setHeader("Pragma", "public");
232: response.setContentLength(fileSize);
233:
234: // write to output
235: OutputStream out = response.getOutputStream();
236: while (inStream.available() > 0) {
237: out.write(inStream.read());
238: }
239: out.flush();
240: }
241:
242: /**
243: * JSTL function to return the tab state of the tab from the form.
244: *
245: * @param form
246: * @param tabKey
247: * @return
248: */
249: public static String getTabState(KualiForm form, String tabKey) {
250: return form.getTabState(tabKey);
251: }
252:
253: public static void incrementTabIndex(KualiForm form, String tabKey) {
254: form.incrementTabIndex();
255: }
256:
257: /**
258: * Generates a String from the title that can be used as a Map key.
259: *
260: * @param tabTitle
261: * @return
262: */
263: public static String generateTabKey(String tabTitle) {
264: String key = "";
265: if (!StringUtils.isBlank(tabTitle)) {
266: key = tabTitle.replaceAll("\\W", "");
267: // if (key.length() > 25) {
268: // key = key.substring(0, 24);
269: // }
270: }
271:
272: return key;
273: }
274: }
|