001: /*
002: * $Id: StrutsUtil.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import java.io.StringWriter;
026: import java.io.UnsupportedEncodingException;
027: import java.net.URLEncoder;
028: import java.util.ArrayList;
029: import java.util.Arrays;
030: import java.util.Collection;
031: import java.util.Hashtable;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035:
036: import javax.servlet.RequestDispatcher;
037: import javax.servlet.ServletOutputStream;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import javax.servlet.http.HttpServletResponseWrapper;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044: import org.apache.struts2.views.jsp.ui.OgnlTool;
045: import org.apache.struts2.views.util.UrlHelper;
046:
047: import com.opensymphony.xwork2.util.TextUtils;
048: import com.opensymphony.xwork2.util.ValueStack;
049: import com.opensymphony.xwork2.ObjectFactory;
050:
051: /**
052: * Struts base utility class, for use in Velocity and Freemarker templates
053: *
054: */
055: public class StrutsUtil {
056:
057: protected static final Log log = LogFactory
058: .getLog(StrutsUtil.class);
059:
060: protected HttpServletRequest request;
061: protected HttpServletResponse response;
062: protected Map classes = new Hashtable();
063: protected OgnlTool ognl = OgnlTool.getInstance();
064: protected ValueStack stack;
065:
066: public StrutsUtil(ValueStack stack, HttpServletRequest request,
067: HttpServletResponse response) {
068: this .stack = stack;
069: this .request = request;
070: this .response = response;
071: }
072:
073: public Object bean(Object aName) throws Exception {
074: String name = aName.toString();
075: Class c = (Class) classes.get(name);
076:
077: if (c == null) {
078: c = ClassLoaderUtils.loadClass(name, StrutsUtil.class);
079: classes.put(name, c);
080: }
081:
082: return ObjectFactory.getObjectFactory().buildBean(c,
083: stack.getContext());
084: }
085:
086: public boolean isTrue(String expression) {
087: Boolean retVal = (Boolean) stack.findValue(expression,
088: Boolean.class);
089: if (retVal == null) {
090: return false;
091: }
092: return retVal.booleanValue();
093: }
094:
095: public Object findString(String name) {
096: return stack.findValue(name, String.class);
097: }
098:
099: public String include(Object aName) throws Exception {
100: return include(aName, request, response);
101: }
102:
103: /**
104: * @deprecated the request and response are stored in this util class, please use include(string)
105: */
106: public String include(Object aName, HttpServletRequest aRequest,
107: HttpServletResponse aResponse) throws Exception {
108: try {
109: RequestDispatcher dispatcher = aRequest
110: .getRequestDispatcher(aName.toString());
111:
112: if (dispatcher == null) {
113: throw new IllegalArgumentException(
114: "Cannot find included file " + aName);
115: }
116:
117: ResponseWrapper responseWrapper = new ResponseWrapper(
118: aResponse);
119:
120: dispatcher.include(aRequest, responseWrapper);
121:
122: return responseWrapper.getData();
123: } catch (Exception e) {
124: e.printStackTrace();
125: throw e;
126: }
127: }
128:
129: public String urlEncode(String s) {
130: try {
131: return URLEncoder.encode(s, "UTF-8");
132: } catch (UnsupportedEncodingException e) {
133: return s;
134: }
135: }
136:
137: public String buildUrl(String url) {
138: return UrlHelper.buildUrl(url, request, response, null);
139: }
140:
141: public Object findValue(String expression, String className)
142: throws ClassNotFoundException {
143: return stack.findValue(expression, Class.forName(className));
144: }
145:
146: public String getText(String text) {
147: return (String) stack.findValue("getText('" + text + "')");
148: }
149:
150: /*
151: * @return the url ContextPath. An empty string if one does not exist.
152: */
153: public String getContext() {
154: return (request == null) ? "" : request.getContextPath();
155: }
156:
157: /**
158: * the selectedList objects are matched to the list.listValue
159: * <p/>
160: * listKey and listValue are optional, and if not provided, the list item is used
161: *
162: * @param selectedList the name of the action property
163: * that contains the list of selected items
164: * or single item if its not an array or list
165: * @param list the name of the action property
166: * that contains the list of selectable items
167: * @param listKey an ognl expression that is exaluated relative to the list item
168: * to use as the key of the ListEntry
169: * @param listValue an ognl expression that is exaluated relative to the list item
170: * to use as the value of the ListEntry
171: * @return a List of ListEntry
172: */
173: public List makeSelectList(String selectedList, String list,
174: String listKey, String listValue) {
175: List selectList = new ArrayList();
176:
177: Collection selectedItems = null;
178:
179: Object i = stack.findValue(selectedList);
180:
181: if (i != null) {
182: if (i.getClass().isArray()) {
183: selectedItems = Arrays.asList((Object[]) i);
184: } else if (i instanceof Collection) {
185: selectedItems = (Collection) i;
186: } else {
187: // treat it is a single item
188: selectedItems = new ArrayList();
189: selectedItems.add(i);
190: }
191: }
192:
193: Collection items = (Collection) stack.findValue(list);
194:
195: if (items != null) {
196: for (Iterator iter = items.iterator(); iter.hasNext();) {
197: Object element = (Object) iter.next();
198: Object key = null;
199:
200: if ((listKey == null) || (listKey.length() == 0)) {
201: key = element;
202: } else {
203: key = ognl.findValue(listKey, element);
204: }
205:
206: Object value = null;
207:
208: if ((listValue == null) || (listValue.length() == 0)) {
209: value = element;
210: } else {
211: value = ognl.findValue(listValue, element);
212: }
213:
214: boolean isSelected = false;
215:
216: if ((value != null) && (selectedItems != null)
217: && selectedItems.contains(value)) {
218: isSelected = true;
219: }
220:
221: selectList.add(new ListEntry(key, value, isSelected));
222: }
223: }
224:
225: return selectList;
226: }
227:
228: public String htmlEncode(Object obj) {
229: if (obj == null) {
230: return null;
231: }
232:
233: return TextUtils.htmlEncode(obj.toString());
234: }
235:
236: public int toInt(long aLong) {
237: return (int) aLong;
238: }
239:
240: public long toLong(int anInt) {
241: return (long) anInt;
242: }
243:
244: public long toLong(String aLong) {
245: if (aLong == null) {
246: return 0;
247: }
248:
249: return Long.parseLong(aLong);
250: }
251:
252: public String toString(long aLong) {
253: return Long.toString(aLong);
254: }
255:
256: public String toString(int anInt) {
257: return Integer.toString(anInt);
258: }
259:
260: static class ResponseWrapper extends HttpServletResponseWrapper {
261: StringWriter strout;
262: PrintWriter writer;
263: ServletOutputStream sout;
264:
265: ResponseWrapper(HttpServletResponse aResponse) {
266: super (aResponse);
267: strout = new StringWriter();
268: sout = new ServletOutputStreamWrapper(strout);
269: writer = new PrintWriter(strout);
270: }
271:
272: public String getData() {
273: writer.flush();
274:
275: return strout.toString();
276: }
277:
278: public ServletOutputStream getOutputStream() {
279: return sout;
280: }
281:
282: public PrintWriter getWriter() throws IOException {
283: return writer;
284: }
285: }
286:
287: static class ServletOutputStreamWrapper extends ServletOutputStream {
288: StringWriter writer;
289:
290: ServletOutputStreamWrapper(StringWriter aWriter) {
291: writer = aWriter;
292: }
293:
294: public void write(int aByte) {
295: writer.write(aByte);
296: }
297: }
298: }
|