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: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.interceptor.action.internal;
020:
021: import org.apache.beehive.netui.pageflow.interceptor.action.InterceptorForward;
022: import org.apache.beehive.netui.util.logging.Logger;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.ServletRequest;
026: import java.util.Map;
027: import java.util.HashMap;
028: import java.util.Enumeration;
029: import java.util.Iterator;
030: import java.io.Serializable;
031:
032: public class OriginalForward extends InterceptorForward {
033: private static final Logger _log = Logger
034: .getInstance(OriginalForward.class);
035:
036: private Map _savedAttrs;
037:
038: private static abstract class AttributeWrapper implements
039: Serializable {
040: public abstract Object get();
041: }
042:
043: private static final class TransientAttributeWrapper extends
044: AttributeWrapper {
045: private transient Object _object;
046:
047: public TransientAttributeWrapper(Object object) {
048: _object = object;
049: }
050:
051: public Object get() {
052: return _object;
053: }
054: }
055:
056: private static final class SerializableAttributeWrapper extends
057: AttributeWrapper {
058: private Object _object;
059:
060: public SerializableAttributeWrapper(Object object) {
061: _object = object;
062: }
063:
064: public Object get() {
065: return _object;
066: }
067: }
068:
069: public OriginalForward(HttpServletRequest request) {
070: super (request);
071: saveRequestAttrs(request);
072:
073: // By default, we do restore the original query string.
074: setRestoreQueryString(true);
075: }
076:
077: private void saveRequestAttrs(HttpServletRequest request) {
078: _savedAttrs = new HashMap();
079:
080: for (Enumeration e = request.getAttributeNames(); e
081: .hasMoreElements();) {
082: String attrName = (String) e.nextElement();
083: Object attrVal = request.getAttribute(attrName);
084:
085: if (attrVal instanceof Serializable) {
086: _savedAttrs.put(attrName,
087: new SerializableAttributeWrapper(attrVal));
088: } else {
089: _savedAttrs.put(attrName,
090: new TransientAttributeWrapper(attrVal));
091: }
092: }
093:
094: setQueryString(request.getQueryString());
095: }
096:
097: public void rehydrateRequest(HttpServletRequest request) {
098: //
099: // Restore the request attributes.
100: //
101: if (_savedAttrs != null) {
102: for (Iterator i = _savedAttrs.entrySet().iterator(); i
103: .hasNext();) {
104: Map.Entry entry = (Map.Entry) i.next();
105: String attrName = (String) entry.getKey();
106: if (request.getAttribute(attrName) == null) {
107: Object value = ((AttributeWrapper) entry.getValue())
108: .get();
109: if (value != null)
110: request.setAttribute(attrName, value);
111: }
112: }
113: }
114:
115: //
116: // Restore the query string.
117: //
118: if (doesRestoreQueryString()) {
119: String queryString = getQueryString();
120:
121: if (queryString != null && queryString.length() > 0) {
122: assert queryString.charAt(0) == '?';
123: String path = getPath();
124: if (path.indexOf('?') != -1) {
125: path += '&' + queryString.substring(1);
126: } else {
127: path += queryString;
128: }
129:
130: setPath(path);
131: }
132: }
133: }
134: }
|