1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.util;
17
18 import java.io.File;
19 import java.security.Principal;
20 import java.util.Arrays;
21 import java.util.Enumeration;
22 import java.util.List;
23 import java.util.Properties;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import javax.servlet.ServletContext;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31
32 import org.apache.commons.beanutils.DynaBean;
33 import org.apache.commons.beanutils.DynaProperty;
34 import org.seasar.framework.container.S2Container;
35 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
36 import org.seasar.framework.mock.servlet.MockServletContextImpl;
37 import org.seasar.tuigwaa.cms.core.CmsRequest;
38 import org.seasar.tuigwaa.cms.core.CmsResponse;
39 import org.seasar.tuigwaa.plugin.TgwPluginUtils;
40 import org.seasar.tuigwaa.security.PrincipalHttpServletRequestImpl;
41 import org.seasar.tuigwaa.security.auth.TgwUser;
42 import org.seasar.tuigwaa.system.Constants;
43 import org.seasar.tuigwaa.system.SiteConfig;
44
45 import com.isenshi.util.CharUtil;
46 import com.isenshi.util.ResourceUtils;
47
48 public class TgwContext {
49
50 private static final S2Container getContainer() {
51 return SingletonS2ContainerFactory.getContainer();
52 }
53
54 public static final HttpServletRequest getRequest() {
55 return getContainer().getRequest();
56 }
57
58 public static final HttpServletResponse getResponse() {
59 return getContainer().getResponse();
60 }
61
62 public static final ServletContext getServletContext() {
63 return getContainer().getServletContext();
64 }
65
66 public static final String getSessionId() {
67 HttpSession session = getContainer().getSession();
68 return session.getId();
69 }
70
71 public static final Principal getPrincipal() {
72 return getRequest().getUserPrincipal();
73 }
74
75 public static final String[] getRoles() {
76 return ((TgwUser) getPrincipal()).getRoles();
77 }
78
79 public static final void setRequestAttribute(String name, Object obj) {
80 getRequest().setAttribute(name, obj);
81 }
82
83 public static final Object getRequestAttribute(String name) {
84 return getRequest().getAttribute(name);
85 }
86
87 public static final void setAdmin() {
88 HttpServletRequest adminRequest = PrincipalHttpServletRequestImpl
89 .getAdminRequest();
90 getContainer().setRequest(adminRequest);
91 }
92
93 public static final SiteConfig getSiteConfig() {
94 return (SiteConfig) getRequest().getAttribute(
95 Constants.RATTR_SITECONFIG);
96 }
97
98 public static final String getSiteName() {
99 SiteConfig siteConfig = getSiteConfig();
100 if (siteConfig == null) {
101 return null;
102 }
103 return siteConfig.getName();
104 }
105
106 public static final String getPageName() {
107 return (String) getRequest().getAttribute(Constants.RATTR_PAGENAME);
108 }
109
110 public static final void bindPageName(String pageName) {
111 getRequest().setAttribute(Constants.RATTR_PAGENAME, pageName);
112 }
113
114 public static final String getEntityName() {
115 return (String) getRequest().getAttribute(Constants.RATTR_ENTITY_NAME);
116 }
117
118 public static final void bindEntityName(String entityName) {
119 getRequest().setAttribute(Constants.RATTR_ENTITY_NAME, entityName);
120 }
121
122 public static final void bindContextPath() {
123 HttpServletRequest request = getRequest();
124 request.setAttribute(Constants.RATTR_CONTEXTPATH, request
125 .getContextPath());
126 }
127
128 public static final String getContextPath() {
129 HttpServletRequest request = getRequest();
130 if(request.getAttribute(Constants.RATTR_CONTEXTPATH) == null){
131 bindContextPath();
132 }
133 return (String) request.getAttribute(Constants.RATTR_CONTEXTPATH);
134 }
135
136 public static final String getRealPath(String path) {
137 ServletContext context = getRequest().getSession().getServletContext();
138 String realPath = null;
139 if(context instanceof MockServletContextImpl){
140 File src = ResourceUtils.getFile("");
141 File root = src.getParentFile();
142 if (root.getName().equalsIgnoreCase("WEB-INF")) {
143 root = root.getParentFile();
144 }
145 File file = new File(root, CharUtil.charpop(path,'/'));
146 realPath = file.toString();
147 }else{
148 realPath =(path == null) ? context.getRealPath("") : context.getRealPath(path);
149 }
150 return realPath;
151 }
152
153 public static final boolean isFullMode() {
154 HttpServletRequest request = getRequest();
155 return "true".equals(request.getParameter(Constants.PARAM_FULLMODE));
156 }
157
158 public static final CmsRequest createNewCmsRequest() {
159 CmsRequest req = new CmsRequest(getRequest());
160 return req;
161 }
162
163 private static final CmsRequest createCmsRequest() {
164 CmsRequest req = createNewCmsRequest();
165 getRequest().setAttribute(Constants.RATTR_CMSREQUEST, req);
166 return req;
167 }
168
169 public static final void relayToCmsParameter(String includePrefix) {
170 relayToCmsParameter(new String[] { includePrefix });
171 }
172
173 public static final void relayToCmsParameter(String[] includePrefixes) {
174 relayToCmsParameter(Arrays.asList(includePrefixes));
175 }
176
177 public static final void relayEntity(DynaBean bean) {
178 DynaProperty[] props = bean.getDynaClass().getDynaProperties();
179 for (int i = 0; i < props.length; i++) {
180 String propName = props[i].getName();
181 if (propName.startsWith(Constants.PARAM_PREFIX_ENTITY_ESCAPE)) {
182 Object obj = bean.get(propName);
183 String entityName = propName
184 .substring(Constants.PARAM_PREFIX_ENTITY_ESCAPE
185 .length());
186 int lastIndex = entityName.lastIndexOf(Constants.PARAM_PREFIX_ATTR_ESCAPE);
187 entityName = entityName.substring(0, lastIndex);
188 String bindingName = TgwPluginUtils
189 .createEntityBindingName(entityName);
190
191 if (obj != null) {
192 setCmsParameter(bindingName, String.valueOf(obj));
193 }
194 }
195 }
196 }
197
198 public static final void relayEntityByRequestParameter(HttpServletRequest request) {
199 Enumeration enm = request.getParameterNames();
200 while(enm.hasMoreElements()){
201 String propName = (String)enm.nextElement();
202 if (propName.startsWith(Constants.PARAM_PREFIX_ENTITY_ESCAPE)) {
203 Object obj = request.getParameter(propName);
204 String entityName = propName
205 .substring(Constants.PARAM_PREFIX_ENTITY_ESCAPE
206 .length());
207 int lastIndex = entityName.lastIndexOf(Constants.PARAM_PREFIX_ATTR_ESCAPE);
208 entityName = entityName.substring(0, lastIndex);
209 String bindingName = TgwPluginUtils
210 .createEntityBindingName(entityName);
211 if (obj != null) {
212 setCmsParameter(bindingName, String.valueOf(obj));
213 }
214 }
215 }
216 }
217
218
219
220 public static final void relayToCmsParameter(List includePrefixList) {
221 HttpServletRequest req = getRequest();
222 Enumeration enm = req.getParameterNames();
223 while (enm.hasMoreElements()) {
224 String paramName = (String) enm.nextElement();
225 String prefix = null;
226 int index = -1;
227 if ((index = paramName.indexOf(".")) > 0) {
228 prefix = paramName.substring(0, index + 1);
229 }
230 if (prefix != null && includePrefixList.contains(prefix)) {
231
232
233
234
235
236 String id = getRequest().getParameter(paramName);
237 if (id != null) {
238 setCmsParameter(paramName, id);
239 }
240
241 }
242 }
243 }
244
245 public static final void setCmsParameter(String key, String value) {
246 getCmsRequest().setParameter(key, value);
247 }
248
249 public static final void setCmsAttribute(String key, Object value) {
250 getCmsRequest().setAttributes(key, value);
251 }
252
253 public static final CmsRequest getCmsRequest() {
254 CmsRequest req = (CmsRequest) getRequest().getAttribute(
255 Constants.RATTR_CMSREQUEST);
256 if (req == null) {
257 req = createCmsRequest();
258 }
259 req.setMainPagePath(getPageName());
260 return req;
261 }
262
263 public static final CmsResponse getCmsResponse() {
264 return new CmsResponse();
265 }
266
267 public static final void setSessionAttribute(String key, Object value) {
268 HttpSession session = getRequest().getSession();
269 session.setAttribute(key, value);
270 }
271
272 public static final boolean isUserInRole(String role) {
273 return getRequest().isUserInRole(role);
274 }
275
276 public static final void initPageName() {
277
278 String pagename = getPageName();
279
280 if (pagename == null || pagename.equals("")) {
281 bindPageName(getSiteConfig().getInitPagename());
282 }
283
284
285
286
287
288
289 }
290
291 public static final void bindPageSettings() {
292
293 Properties settings = (Properties) getRequest().getAttribute(
294 Constants.RATTR_PAGESETTINGS);
295
296 Enumeration penum = getRequest().getParameterNames();
297 Pattern p = Pattern.compile(Constants.PARAM_PREFIX_PAGESETTINGS
298 + "(.*)" + Constants.PARAM_SUFFIX_PAGESETTINGS);
299
300 while (penum.hasMoreElements()) {
301 String paramName = (String) penum.nextElement();
302 Matcher m = p.matcher(paramName);
303 if (m.find()) {
304 String key = m.group(1);
305 if (settings == null) {
306 settings = new Properties();
307 }
308 settings.setProperty(key, getRequest().getParameter(paramName));
309 }
310 }
311 if (settings != null)
312 getRequest().setAttribute(Constants.RATTR_PAGESETTINGS, settings);
313 }
314
315 public static final boolean isAdminUser() {
316 HttpServletRequest request = getRequest();
317 String adminRole = TgwResource.getProperty("ldap.admin.rolename");
318 return request.isUserInRole(adminRole);
319 }
320
321
322 public static final String getRemoteUser() {
323 HttpServletRequest request = getRequest();
324 return (request != null) ? request.getRemoteUser() : null;
325 }
326
327 public static final String getServerName(){
328 HttpServletRequest request = getRequest();
329 return (request !=null)? request.getServerName() : null;
330 }
331
332 public static final String getScheme(){
333 HttpServletRequest request = getRequest();
334 return (request !=null)? request.getScheme() : null;
335 }
336
337 public static final int getPort(){
338 HttpServletRequest request = getRequest();
339 return (request !=null)? request.getServerPort() : 80;
340 }
341
342 public static final String getDomainName(){
343 return getSiteName();
344 }
345
346
347
348
349
350 }