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 com.isenshi.util.extlib;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.servlet.ServletContext;
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.commons.collections.CollectionUtils;
25  import org.apache.commons.validator.ValidatorResources;
26  import org.apache.struts.Globals;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionMapping;
29  import org.apache.struts.config.FormBeanConfig;
30  import org.apache.struts.config.ModuleConfig;
31  import org.apache.struts.util.ModuleUtils;
32  import org.apache.struts.validator.ValidatorPlugIn;
33  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
34  import org.seasar.tuigwaa.util.TgwUtils;
35  
36  import com.isenshi.util.CharUtil;
37  
38  /***
39   * @author nishioka
40   */
41  public class StrutsUtil {
42  
43  	private static final String QUERY_DELIMITER = "&";
44  
45  	private static final String QUERY_ASIGN = "=";
46  
47  	public static String getFormKey(String siteName, String formName) {
48  		return "/" + siteName + "/" + formName;
49  	}
50  
51  	public static String getFormKey(ActionMapping mapping) {
52  		FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(
53  				mapping.getName());
54  		return mapping.getModuleConfig().getPrefix() + "/" + config.getName();
55  	}
56  
57  	public static void clearSessionForm(HttpServletRequest request,
58  			ActionMapping mapping) {
59  		request.getSession().removeAttribute(mapping.getName());
60  	}
61  
62  	public static void bindSessionForm(HttpServletRequest request, String name,
63  			ActionForm form) {
64  		request.getSession().setAttribute(name, form);
65  	}
66  
67  	public static String getURLDecodedParameter(HttpServletRequest request,
68  			String name) {
69  		return getURLDecodedParameter(request.getQueryString(), name);
70  	}
71  
72  	public static String getURLDecodedParameter(String query, String name) {
73  		String value = getParameter(query, name);
74  		if (value == null) {
75  			return null;
76  		}
77  		return CharUtil.urlDecode(value);
78  	}
79  
80  	public static String getParameter(HttpServletRequest req, String name) {
81  		return getParameter(req.getQueryString(), name);
82  	}
83  
84  	public static String getParameter(String query, String name) {
85  		if (query == null)
86  			return null;
87  
88  		String matcher = name + QUERY_ASIGN;
89  		int midx;
90  		int mlength = matcher.length();
91  		if ((midx = query.indexOf(matcher)) != -1) {
92  			int eidx = query.indexOf(QUERY_DELIMITER, midx);
93  			return (eidx == -1) ? query.substring(midx + mlength) : query
94  					.substring(midx + mlength, eidx);
95  		}
96  		return null;
97  	}
98  
99  	public static String getMaxFileSize(ActionMapping mapping) {
100 		ModuleConfig ac = mapping.getModuleConfig();
101 		return ac.getControllerConfig().getMaxFileSize();
102 	}
103 
104 	public static void setContextAttribute(String key, Object obj) {
105 		ServletContext context = SingletonS2ContainerFactory.getContainer()
106 				.getServletContext();
107 		if (context != null) {
108 			context.setAttribute(key, obj);
109 		}
110 	}
111 
112 	public static Object getContextAttribute(String key) {
113 		ServletContext context = SingletonS2ContainerFactory.getContainer()
114 				.getServletContext();
115 		if (context != null) {
116 			return context.getAttribute(key);
117 		} else {
118 			return null;
119 		}
120 	}
121 
122 	public static void putModuleConfig(ModuleConfig moduleConfig) {
123 		setContextAttribute(Globals.MODULE_KEY + moduleConfig.getPrefix(),
124 				moduleConfig);
125 	}
126 
127 	public static ValidatorResources getValidatorResources(
128 			ModuleConfig moduleConfig) {
129 		return (ValidatorResources) getContextAttribute(ValidatorPlugIn.VALIDATOR_KEY
130 				+ moduleConfig.getPrefix());
131 	}
132 
133 	public static void addPrefix(String prefix) {
134 		String[] prefixes = (String[]) getContextAttribute(Globals.MODULE_PREFIXES_KEY);
135 		List prefixList = new ArrayList();
136 		if (prefixes != null) {
137 			CollectionUtils.addAll(prefixList, prefixes);
138 		}
139 		if (!prefixList.contains(prefix)) {
140 			prefixList.add(prefix);
141 			prefixes = (String[]) prefixList.toArray(new String[prefixList
142 					.size()]);
143 			setContextAttribute(Globals.MODULE_PREFIXES_KEY, prefixes);
144 		}
145 	}
146 
147 	public static void removePrefix(String prefix) {
148 		String[] prefixes = (String[]) getContextAttribute(Globals.MODULE_PREFIXES_KEY);
149 		List prefixList = new ArrayList();
150 		if (prefixes != null) {
151 			CollectionUtils.addAll(prefixList, prefixes);
152 		}
153 		if (!prefixList.contains(prefix)) {
154 			prefixList.remove(prefix);
155 			prefixes = (String[]) prefixList.toArray(new String[prefixList
156 					.size()]);
157 			setContextAttribute(Globals.MODULE_PREFIXES_KEY, prefixes);
158 		}
159 	}
160 
161 	public static ModuleConfig getModuleConfig(String moduleName) {
162 		String prefix = "/" + moduleName;
163 		HttpServletRequest request = SingletonS2ContainerFactory.getContainer()
164 				.getRequest();
165 		ServletContext context = TgwUtils.getTuigwaaActionServlet()
166 				.getServletContext();
167 		return ModuleUtils.getInstance().getModuleConfig(prefix, request,
168 				context);
169 	}
170 }