Source Code Cross Referenced for WebUtils.java in  » ERP-CRM-Financial » Kuali-Financial-System » org » kuali » core » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » Kuali Financial System » org.kuali.core.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.