View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.cms.core;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.Stack;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.seasar.tuigwaa.cms.core.wiki.engine.Node;
26  
27  import com.isenshi.util.CharUtil;
28  
29  /***
30   * @author someda
31   */
32  public class CmsRequest {
33  
34  	class CmsInfo {
35  
36  		public Page page_;
37  
38  		public Map attributes = new HashMap();
39  
40  		public Map parameters = new HashMap();
41  	}
42  
43  	private Stack scopeStack = new Stack();
44  
45  	private int type_;
46  
47  	private int mode = CmsConstants.MODE_NORMAL;
48  
49  	private String siteName;
50  
51  	private Node rootNode_;
52  
53  	private String contextPath;
54  
55  	private String scheme;
56  
57  	private String serverName;
58  
59  	private int serverPort;
60  
61  	private String mainPagePath;
62  
63  	public CmsRequest() {
64  		pushScope();
65  	}
66  
67  	public CmsRequest(Page page, int type) {
68  		pushScope();
69  		this.lastScope().page_ = page;
70  		this.type_ = type;
71  	}
72  
73  	public CmsRequest(HttpServletRequest httpRequest) {
74  		pushScope();
75  		this.serverName = httpRequest.getServerName();
76  		this.serverPort = httpRequest.getServerPort();
77  		this.contextPath = httpRequest.getContextPath();
78  		this.scheme = httpRequest.getScheme();
79  	}
80  
81  	public String getURLEncodedQuery() {
82  		return getURLEncodedQuery(null);
83  	}
84  
85  	public String getURLEncodedQuery(String exclusiveKey) {
86  		StringBuffer buf = new StringBuffer();
87  		for (Iterator i = lastScope().parameters.keySet().iterator(); i
88  				.hasNext();) {
89  			String key = (String) i.next();
90  			if (exclusiveKey != null && exclusiveKey.equals(key)) {
91  				continue;
92  			}
93  			String value = CharUtil.urlEncode(getParameter(key));
94  			buf.append("&");
95  			buf.append(key);
96  			buf.append("=");
97  			buf.append(value);
98  		}
99  		return buf.toString();
100 	}
101 
102 	public String getMainPagePath() {
103 		return mainPagePath;
104 	}
105 
106 	public void setMainPagePath(String mainPagePath) {
107 		this.mainPagePath = mainPagePath;
108 	}
109 
110 	public String getMainPageParentPath() {
111 		if (getMainPagePath() == null) {
112 			return null;
113 		}
114 		int idx = CharUtil.chartrim(getMainPagePath(), '/').lastIndexOf('/');
115 		if (idx > 0) {
116 			return getMainPagePath().substring(0, idx);
117 		} else {
118 			return "";
119 		}
120 	}
121 
122 	public boolean hasParentWikiRequest() {
123 		return scopeStack.size() > 1;
124 	}
125 
126 	public void pushScope() {
127 		scopeStack.push(new CmsInfo());
128 	}
129 
130 	public void popScope() {
131 		scopeStack.pop();
132 	}
133 
134 	public String getSiteName() {
135 		return siteName;
136 	}
137 
138 	public void setSiteName(String domain) {
139 		this.siteName = domain;
140 	}
141 
142 	public Page getPage() {
143 		return lastScope().page_;
144 	}
145 
146 	public void setPage(Page page) {
147 		lastScope().page_ = page;
148 	}
149 
150 	public void setContextPath(String contextPath) {
151 		this.contextPath = contextPath;
152 	}
153 
154 	public String getContextPath() {
155 		return contextPath;
156 	}
157 
158 	public String getScheme() {
159 		return scheme;
160 	}
161 
162 	public void setScheme(String scheme) {
163 		this.scheme = scheme;
164 	}
165 
166 	public String getServerName() {
167 		return serverName;
168 	}
169 
170 	public void setServerName(String serverName) {
171 		this.serverName = serverName;
172 	}
173 
174 	public int getServerPort() {
175 		return serverPort;
176 	}
177 
178 	public void setServerPort(int serverPort) {
179 		this.serverPort = serverPort;
180 	}
181 
182 	public int getType() {
183 		return type_;
184 	}
185 
186 	/***
187 	 * Set request type to the WikiEngine. It expects the param value is the one
188 	 * defined in WikiConstants. If unknown type would be specified, it throws
189 	 * IllegalArgumentException.
190 	 * 
191 	 * @throws IllegalArgumentException
192 	 */
193 	public void setType(int type) {
194 		if (type != CmsConstants.OUTPUTTYPE_HTML
195 				&& type != CmsConstants.OUTPUTTYPE_PDF
196 				&& type != CmsConstants.OUTPUTTYPE_RTF
197 				&& type != CmsConstants.OUTPUTTYPE_TEXT)
198 			throw new IllegalArgumentException("output type " + type
199 					+ " is not supported.");
200 		this.type_ = type;
201 	}
202 
203 	public Node getRootNode() {
204 		return rootNode_;
205 	}
206 
207 	public void setRootNode(Node rootNode) {
208 		this.rootNode_ = rootNode;
209 	}
210 
211 	public void setParameter(String key, String value) {
212 		lastScope().parameters.put(key, value);
213 	}
214 
215 	public void removeParameter(String key) {
216 		lastScope().parameters.remove(key);
217 	}
218 
219 	public void setAttributes(Object key, Object value) {
220 		lastScope().attributes.put(key, value);
221 	}
222 
223 	public void removeAttribute(Object key) {
224 		lastScope().attributes.remove(key);
225 	}
226 
227 	public String getParameter(String key) {
228 		return (String) lastScope().parameters.get(key);
229 	}
230 
231 	public Iterator getParamterNames() {
232 		return lastScope().parameters.keySet().iterator();
233 	}
234 
235 	public Object getAttribute(String key) {
236 		return lastScope().attributes.get(key);
237 	}
238 
239 	public boolean isHTMLRequest() {
240 		return (this.type_ == CmsConstants.OUTPUTTYPE_HTML);
241 	}
242 
243 	public boolean isPDFRequest() {
244 		return (this.type_ == CmsConstants.OUTPUTTYPE_PDF);
245 	}
246 
247 	public boolean isRTFRequest() {
248 		return (this.type_ == CmsConstants.OUTPUTTYPE_RTF);
249 	}
250 
251 	public boolean isTextRequest() {
252 		return (this.type_ == CmsConstants.OUTPUTTYPE_TEXT);
253 	}
254 
255 	private CmsInfo lastScope() {
256 		return (CmsInfo) scopeStack.lastElement();
257 	}
258 
259 	public int getMode() {
260 		return mode;
261 	}
262 
263 	public void setMode(int mode) {
264 		this.mode = mode;
265 	}
266 
267 }