001: package ru.emdev.EmForge.util;
002:
003: import java.io.UnsupportedEncodingException;
004:
005: import javax.faces.context.ExternalContext;
006: import javax.faces.context.FacesContext;
007: import javax.servlet.ServletContext;
008: import javax.servlet.http.HttpServletRequest;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.emforge.BpmService;
012: import org.emforge.report.web.bean.ReportViewBean;
013: import org.hibernate.Session;
014: import org.hibernate.SessionFactory;
015: import org.springframework.orm.hibernate3.SessionFactoryUtils;
016: import org.springframework.orm.hibernate3.SessionHolder;
017: import org.springframework.transaction.support.TransactionSynchronizationManager;
018: import org.springframework.web.context.WebApplicationContext;
019: import org.springframework.web.context.support.WebApplicationContextUtils;
020:
021: import ru.emdev.EmForge.EmForgeContext;
022: import ru.emdev.EmForge.security.UserFactory;
023:
024: import com.ecyrd.jspwiki.WikiEngine;
025:
026: /**
027: * Helper class with some utility functions used in EmForge
028: */
029: public class Helper {
030:
031: /**
032: * Gets id from http request from specified attribute
033: *
034: * @param i_request
035: * @param i_attrName
036: * @return
037: */
038: public static long getID(HttpServletRequest i_request,
039: String i_attrName) {
040:
041: assert i_request != null;
042: assert i_attrName != null;
043:
044: String idStr = i_request.getParameter(i_attrName);
045: Long id = new Long(idStr);
046:
047: return id.longValue();
048: }
049:
050: /**
051: * Gets ID object from FacesContext
052: *
053: * @return
054: */
055: public static long getIDFromFacesContext(String i_attrName) {
056:
057: assert i_attrName != null;
058:
059: ExternalContext context = FacesContext.getCurrentInstance()
060: .getExternalContext();
061: HttpServletRequest request = (HttpServletRequest) context
062: .getRequest();
063:
064: String idStr = request.getParameter(i_attrName);
065: Long id = new Long(idStr);
066:
067: return id.longValue();
068: }
069:
070: /**
071: * Gets user factory from current application context
072: *
073: * @param i_request
074: * @return
075: */
076: public static UserFactory getUserFactory(
077: HttpServletRequest i_request) {
078:
079: WebApplicationContext appContext = WebApplicationContextUtils
080: .getWebApplicationContext(i_request.getSession()
081: .getServletContext());
082: assert appContext != null;
083:
084: return (UserFactory) appContext.getBean("userFactory");
085: }
086:
087: /**
088: * Gets bpm context from current application context
089: */
090: public static BpmService getBpmService(HttpServletRequest i_request) {
091:
092: WebApplicationContext appContext = WebApplicationContextUtils
093: .getWebApplicationContext(i_request.getSession()
094: .getServletContext());
095: assert appContext != null;
096:
097: return (BpmService) appContext.getBean("bpmService");
098: }
099:
100: /**
101: * Gets application context
102: */
103: public static EmForgeContext getAppContext(
104: HttpServletRequest i_request) {
105:
106: WebApplicationContext appContext = WebApplicationContextUtils
107: .getWebApplicationContext(i_request.getSession()
108: .getServletContext());
109: assert appContext != null;
110:
111: return (EmForgeContext) appContext.getBean("emForgeContext");
112: }
113:
114: /**
115: * Get parameter from the request. After some time of working I found that my application doesn't work correctly
116: * with UTF-8 No, it displays it quite well, it stored it in database as well also, but - problem was in sending
117: * utf-8 data from html form to the servlet. In servlet i_request.getParameter always returns ISO-8859-1 I read this
118: * article from Sun: http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/ It describes this question
119: * quite well, but, solution proposed by this article:
120: *
121: * @code i_request.setCharacterEncoding("UTF-8");
122: * @endcode doesn't work under JBoss 4.0.3SP1 :( After reading a lot of threads in internet there people asked about
123: * this problem, and after consulting with exprienced guru I found only this solution, implemented in this
124: * function:
125: * @code value = new String(i_request.getParameter(i_paramName).getBytes("ISO-8859-1"), "UTF-8");
126: * @endcode
127: * @return
128: */
129: static public String get_parameter(HttpServletRequest i_request,
130: String i_paramName) {
131:
132: assert i_request != null;
133: assert i_paramName != null;
134:
135: String value = null;
136:
137: try {
138: if (i_request.getParameter(i_paramName) != null) {
139: value = new String(i_request.getParameter(i_paramName)
140: .getBytes("ISO-8859-1"), "UTF-8");
141: }
142: } catch (UnsupportedEncodingException ex) {
143: // nothing to do, just skip this error end return null
144: }
145:
146: return value;
147: }
148:
149: /**
150: * Gets WikiEngine via FacesContext
151: *
152: * @param i_facesContex
153: * @return
154: */
155: static public WikiEngine getWikiEngine(FacesContext i_facesContex)
156: throws Exception {
157:
158: ServletContext servletContext = (ServletContext) i_facesContex
159: .getExternalContext().getContext();
160:
161: WebApplicationContext appContext = WebApplicationContextUtils
162: .getWebApplicationContext(servletContext);
163: assert appContext != null;
164:
165: return (WikiEngine) appContext.getBean("wikiEngine");
166: }
167:
168: /**
169: * Gets ReportViewBean via FacesContext
170: *
171: * @param i_facesContex
172: * @return
173: */
174: static public ReportViewBean getReportEngine(
175: FacesContext i_facesContex) throws Exception {
176:
177: ServletContext servletContext = (ServletContext) i_facesContex
178: .getExternalContext().getContext();
179:
180: WebApplicationContext appContext = WebApplicationContextUtils
181: .getWebApplicationContext(servletContext);
182: assert appContext != null;
183:
184: return (ReportViewBean) appContext.getBean("reportViewBean");
185: }
186:
187: /**
188: * Returns true if string is number
189: *
190: * @param n
191: * @return
192: */
193: public static boolean isNumber(String n) {
194:
195: try {
196: Double.parseDouble(n);
197: } catch (Exception e) {
198: return false;
199: }
200: return true;
201: }
202:
203: /**
204: * @return
205: */
206: public static String getPath() {
207:
208: if (FacesContext.getCurrentInstance() != null
209: && FacesContext.getCurrentInstance()
210: .getExternalContext() != null) {
211: return FacesContext.getCurrentInstance()
212: .getExternalContext().getRequestContextPath();
213: } else {
214: return null;
215: }
216: }
217:
218: /**
219: * Determines - is specified path - relative path or not?
220: *
221: * @param i_href
222: * @return
223: */
224: public static boolean isRelativePath(String i_href) {
225:
226: if (StringUtils.isEmpty(i_href)) {
227: return false;
228: }
229:
230: if (i_href.startsWith("mailto:")) {
231: return false;
232: }
233:
234: if (i_href.startsWith("javascript:")) {
235: return false;
236: }
237:
238: if (i_href.contains("://")) {
239: // it is absolute path with protocol specified
240: return false;
241: }
242:
243: if (i_href.charAt(0) == '/') {
244: // absolute path inside the site
245: return false;
246: }
247: if (i_href.charAt(0) == '#') {
248: // link inside page or dummy link
249: return false;
250: }
251:
252: return true;
253: }
254:
255: /**
256: * @param sessionFactory
257: * @return
258: */
259: public static Session bindSession(SessionFactory sessionFactory) {
260:
261: Session session = SessionFactoryUtils.getSession(
262: sessionFactory, true);
263: TransactionSynchronizationManager.bindResource(sessionFactory,
264: new SessionHolder(session));
265: return session;
266: }
267:
268: /**
269: * @param sessionFactory
270: */
271: public static void unbindSession(SessionFactory sessionFactory) {
272:
273: SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
274: .unbindResource(sessionFactory);
275: SessionFactoryUtils.closeSession(sessionHolder.getSession());
276: }
277:
278: /**
279: * Concatenates the specified part of URL
280: *
281: * @param i_baseUrl is a first part of URL
282: * @param i_path is a second part of URL
283: * @return the concatenated URL, or empty string if parameters are empty or both null.
284: */
285: public static String concatUrl(String i_baseUrl, String i_path) {
286: // checks for nulls
287: if (i_baseUrl == null) {
288: return i_path;
289: }
290: if (i_path == null) {
291: return i_baseUrl;
292: }
293: if (i_baseUrl == null && i_path == null) {
294: return null;
295: }
296:
297: String result = i_baseUrl;
298: String toAdd = i_path;
299: Boolean addSlash = true;
300:
301: // check - is latest symbol in part1 is "/"
302: if (result.endsWith("/")) {
303: addSlash = false;
304:
305: } else if (toAdd.startsWith("/")) {
306: // check first symbol in part2
307: addSlash = false;
308: }
309:
310: if (addSlash) {
311: result += '/';
312: }
313: result += toAdd;
314:
315: return result;
316: }
317: }
|