Source Code Cross Referenced for RequestUtil.java in  » Blogger-System » apache-roller-3.1 » org » apache » roller » ui » 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 » Blogger System » apache roller 3.1 » org.apache.roller.ui.core.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  The ASF licenses this file to You
004:         * under the Apache License, Version 2.0 (the "License"); you may not
005:         * use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
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.  For additional information regarding
015:         * copyright in this work, please see the NOTICE file in the top level
016:         * directory of this distribution.
017:         */
018:        package org.apache.roller.ui.core.util;
019:
020:        import java.util.Enumeration;
021:        import java.util.HashMap;
022:        import java.util.Iterator;
023:        import java.util.Map;
024:        import java.util.Set;
025:        import javax.servlet.http.Cookie;
026:        import javax.servlet.http.HttpServletRequest;
027:        import javax.servlet.http.HttpServletResponse;
028:        import org.apache.commons.logging.Log;
029:        import org.apache.commons.logging.LogFactory;
030:        import org.apache.struts.taglib.TagUtils;
031:
032:        /**
033:         * RequestUtil utility class Good ol' copy-n-paste from <a
034:         * href="http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt">
035:         * http://www.javaworld.com/javaworld/jw-02-2002/ssl/utilityclass.txt </a> which
036:         * is referenced in the following article: <a
037:         * href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html">
038:         * http://www.javaworld.com/javaworld/jw-02-2002/jw-0215-ssl.html </a>
039:         */
040:        public class RequestUtil {
041:            private static final String STOWED_REQUEST_ATTRIBS = "ssl.redirect.attrib.stowed";
042:            private transient static Log log = LogFactory
043:                    .getLog(RequestUtil.class);
044:
045:            /**
046:             * Creates query String from request body parameters
047:             */
048:            public static String getRequestParameters(
049:                    HttpServletRequest aRequest) {
050:                // set the ALGORIGTHM as defined for the application
051:                //ALGORITHM = (String) aRequest.getAttribute(Constants.ENC_ALGORITHM);
052:                Map m = aRequest.getParameterMap();
053:                return createQueryStringFromMap(m, "&").toString();
054:            }
055:
056:            /**
057:             * Builds a query string from a given map of parameters
058:             * 
059:             * @param m
060:             *            A map of parameters
061:             * @param ampersand
062:             *            String to use for ampersands (e.g. "&" or "&amp;" )
063:             * 
064:             * @return query string (with no leading "?")
065:             */
066:            public static StringBuffer createQueryStringFromMap(Map m,
067:                    String ampersand) {
068:                StringBuffer aReturn = new StringBuffer("");
069:                Set aEntryS = m.entrySet();
070:                Iterator aEntryI = aEntryS.iterator();
071:                while (aEntryI.hasNext()) {
072:                    Map.Entry aEntry = (Map.Entry) aEntryI.next();
073:                    Object o = aEntry.getValue();
074:                    if (o == null) {
075:                        append(aEntry.getKey(), "", aReturn, ampersand);
076:                    } else if (o instanceof  String) {
077:                        append(aEntry.getKey(), o, aReturn, ampersand);
078:                    } else if (o instanceof  String[]) {
079:                        String[] aValues = (String[]) o;
080:                        for (int i = 0; i < aValues.length; i++) {
081:                            append(aEntry.getKey(), aValues[i], aReturn,
082:                                    ampersand);
083:                        }
084:                    } else {
085:                        append(aEntry.getKey(), o, aReturn, ampersand);
086:                    }
087:                }
088:                return aReturn;
089:            }
090:
091:            /**
092:             * Appends new key and value pair to query string
093:             * 
094:             * @param key
095:             *            parameter name
096:             * @param value
097:             *            value of parameter
098:             * @param queryString
099:             *            existing query string
100:             * @param ampersand
101:             *            string to use for ampersand (e.g. "&" or "&amp;")
102:             * 
103:             * @return query string (with no leading "?")
104:             */
105:            private static StringBuffer append(Object key, Object value,
106:                    StringBuffer queryString, String ampersand) {
107:                if (queryString.length() > 0) {
108:                    queryString.append(ampersand);
109:                }
110:                TagUtils tagUtils = TagUtils.getInstance();
111:                // Use encodeURL from Struts' RequestUtils class - it's JDK 1.3 and 1.4
112:                // compliant
113:                queryString.append(tagUtils.encodeURL(key.toString()));
114:                queryString.append("=");
115:                queryString.append(tagUtils.encodeURL(value.toString()));
116:                return queryString;
117:            }
118:
119:            /**
120:             * Stores request attributes in session
121:             * 
122:             * @param aRequest
123:             *            the current request
124:             */
125:            public static void stowRequestAttributes(HttpServletRequest aRequest) {
126:                if (aRequest.getSession().getAttribute(STOWED_REQUEST_ATTRIBS) != null) {
127:                    return;
128:                }
129:                Enumeration e = aRequest.getAttributeNames();
130:                Map map = new HashMap();
131:                while (e.hasMoreElements()) {
132:                    String name = (String) e.nextElement();
133:                    map.put(name, aRequest.getAttribute(name));
134:                }
135:                aRequest.getSession().setAttribute(STOWED_REQUEST_ATTRIBS, map);
136:            }
137:
138:            /**
139:             * Returns request attributes from session to request
140:             * 
141:             * @param aRequest
142:             *            DOCUMENT ME!
143:             */
144:            public static void reclaimRequestAttributes(
145:                    HttpServletRequest aRequest) {
146:                Map map = (Map) aRequest.getSession().getAttribute(
147:                        STOWED_REQUEST_ATTRIBS);
148:                if (map == null) {
149:                    return;
150:                }
151:                Iterator itr = map.keySet().iterator();
152:                while (itr.hasNext()) {
153:                    String name = (String) itr.next();
154:                    aRequest.setAttribute(name, map.get(name));
155:                }
156:                aRequest.getSession().removeAttribute(STOWED_REQUEST_ATTRIBS);
157:            }
158:
159:            /**
160:             * Convenience method to set a cookie
161:             * 
162:             * @param response
163:             * @param name
164:             * @param value
165:             * @param path
166:             */
167:            public static void setCookie(HttpServletResponse response,
168:                    String name, String value, String path) {
169:                if (log.isDebugEnabled()) {
170:                    log.debug("Setting cookie '" + name + "' on path '" + path
171:                            + "'");
172:                }
173:                Cookie cookie = new Cookie(name, value);
174:                cookie.setSecure(false);
175:                // if path is nothing, use "/" so remember me will work 
176:                // when installed as root app
177:                cookie.setPath((path.length() == 0) ? "/" : path);
178:                cookie.setMaxAge(3600 * 24 * 30); // 30 days
179:                response.addCookie(cookie);
180:            }
181:
182:            /**
183:             * Convenience method to get a cookie by name
184:             * 
185:             * @param request
186:             *            the current request
187:             * @param name
188:             *            the name of the cookie to find
189:             * 
190:             * @return the cookie (if found), null if not found
191:             */
192:            public static Cookie getCookie(HttpServletRequest request,
193:                    String name) {
194:                Cookie[] cookies = request.getCookies();
195:                Cookie returnCookie = null;
196:                if (cookies == null) {
197:                    return returnCookie;
198:                }
199:                for (int i = 0; i < cookies.length; i++) {
200:                    Cookie this Cookie = cookies[i];
201:                    if (this Cookie.getName().equals(name)) {
202:                        // cookies with no value do me no good!
203:                        if (!this Cookie.getValue().equals("")) {
204:                            returnCookie = this Cookie;
205:                            break;
206:                        }
207:                    }
208:                }
209:                return returnCookie;
210:            }
211:
212:            /**
213:             * Convenience method for deleting a cookie by name
214:             * 
215:             * @param response
216:             *            the current web response
217:             * @param cookie
218:             *            the cookie to delete
219:             * @param path
220:             *            the path on which the cookie was set (i.e. /appfuse)
221:             */
222:            public static void deleteCookie(HttpServletResponse response,
223:                    Cookie cookie, String path) {
224:                if (cookie != null) {
225:                    // Delete the cookie by setting its maximum age to zero
226:                    cookie.setMaxAge(0);
227:                    cookie.setPath(path);
228:                    response.addCookie(cookie);
229:                }
230:            }
231:
232:            /**
233:             * Convenience method to get the application's URL based on request
234:             * variables.
235:             */
236:            public static String getAppURL(HttpServletRequest request) {
237:                StringBuffer url = new StringBuffer();
238:                int port = request.getServerPort();
239:                if (port < 0) {
240:                    port = 80; // Work around java.net.URL bug
241:                }
242:                String scheme = request.getScheme();
243:                url.append(scheme);
244:                url.append("://");
245:                url.append(request.getServerName());
246:                if ((scheme.equals("http") && (port != 80))
247:                        || (scheme.equals("https") && (port != 443))) {
248:                    url.append(':');
249:                    url.append(port);
250:                }
251:                return url.toString();
252:            }
253:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.