001: package org.claros.intouch.common.services;
002:
003: import java.io.File;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.Locale;
007:
008: import javax.servlet.http.Cookie;
009: import javax.servlet.http.HttpServlet;
010: import javax.servlet.http.HttpServletRequest;
011:
012: import org.apache.commons.configuration.Configuration;
013: import org.apache.commons.configuration.ConfigurationException;
014: import org.apache.commons.configuration.PropertiesConfiguration;
015: import org.claros.commons.auth.models.AuthProfile;
016: import org.claros.commons.configuration.Paths;
017: import org.claros.commons.mail.models.ConnectionMetaHandler;
018: import org.claros.commons.mail.models.ConnectionProfile;
019: import org.claros.intouch.common.utility.Utility;
020:
021: import com.jenkov.mrpersister.itf.IGenericDao;
022:
023: public class BaseService extends HttpServlet {
024: private static final long serialVersionUID = -2549828936277983597L;
025: private static HashMap configs = new HashMap();
026:
027: /**
028: * Searches for the variable in various places and returns it.
029: * @param request
030: * @param name
031: * @return Object
032: */
033: public Object getVariable(HttpServletRequest request, String name) {
034: Object obj = request.getParameter(name);
035: if (obj == null) {
036: obj = request.getAttribute(name);
037: if (obj == null) {
038: obj = request.getSession().getAttribute(name);
039: if (obj == null) {
040: obj = getCookie(request, name);
041: if (obj == null) {
042: obj = getServletContext().getAttribute(name);
043: }
044: }
045: }
046: }
047: return obj;
048: }
049:
050: /**
051: *
052: * @param request
053: * @param name
054: * @return
055: */
056: public String getCookie(HttpServletRequest request, String name) {
057: Cookie cookies[] = request.getCookies();
058: Cookie cookie = null;
059: if (cookies != null) {
060: for (int i = 0; i < cookies.length; i++) {
061: cookie = cookies[i];
062: if (cookie.getName().equals(name)) {
063: return cookie.getValue();
064: }
065: }
066: }
067: return null;
068: }
069:
070: /**
071: *
072: * @param request
073: * @return
074: */
075: public ConnectionProfile getConnectionProfile(
076: HttpServletRequest request) {
077: return (ConnectionProfile) request.getSession().getAttribute(
078: "profile");
079: }
080:
081: /**
082: *
083: * @param request
084: * @return
085: */
086: public ConnectionMetaHandler getConnectionHandler(
087: HttpServletRequest request) {
088: return (ConnectionMetaHandler) request.getSession()
089: .getAttribute("handler");
090: }
091:
092: /**
093: *
094: * @return
095: * @throws Exception
096: */
097: public IGenericDao getDbConnection() throws Exception {
098: return Utility.getDbConnection("file");
099: }
100:
101: /**
102: *
103: * @param name
104: * @return
105: * @throws Exception
106: */
107: public IGenericDao getDbConnection(String name) throws Exception {
108: return Utility.getDbConnection(name);
109: }
110:
111: /**
112: *
113: * @param request
114: * @return
115: */
116: public AuthProfile getAuthProfile(HttpServletRequest request) {
117: return (AuthProfile) request.getSession().getAttribute("auth");
118: }
119:
120: /**
121: *
122: * @param request
123: * @return
124: */
125: public ArrayList getUserSettings(HttpServletRequest request) {
126: return (ArrayList) request.getSession().getAttribute("prefs");
127: }
128:
129: /**
130: *
131: * @param req
132: * @param key
133: * @return
134: */
135: public String getText(HttpServletRequest req, String key) {
136: try {
137: String lang = (String) getVariable(req, "lang");
138: if (lang == null)
139: lang = "en";
140: Locale loc = new Locale("en");
141: try {
142: loc = new Locale(lang);
143: } catch (Exception e) {
144: }
145:
146: String clsPath = Paths.getClsFolder();
147:
148: Configuration config = (Configuration) configs.get(lang);
149: if (config == null) {
150: config = new PropertiesConfiguration(new File(clsPath
151: + "/org/claros/intouch/i18n/lang_" + loc
152: + ".properties"));
153: configs.put(lang, config);
154: }
155: return config.getString(key);
156: } catch (ConfigurationException e) {
157: return null;
158: }
159: }
160: }
|