1 14 package org.compiere.wstore; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import javax.servlet.*; 20 import javax.servlet.http.*; 21 import org.apache.log4j.Logger; 22 23 import org.compiere.*; 24 import org.compiere.util.*; 25 import org.compiere.model.*; 26 27 33 public class JSPEnv 34 { 35 36 static private Logger s_log = Logger.getLogger (JSPEnv.class); 37 38 public final static String CONTEXT_NAME = "ctx"; 39 public final static String CTX_SERVER_CONTEXT = "context"; 40 public final static String CTX_DOCUMENT_DIR = "documentDir"; 41 42 public final static String HDR_MESSAGE = "hdrMessage"; 43 44 public final static String HDR_INFO = "hdrInfo"; 45 46 public static long s_refresh; 47 public static final int REFRESH_MS = 360000; 49 private static CCache s_cacheCtx = new CCache("JSPEnvCtx", 2); 50 51 52 57 public static Properties getCtx (HttpServletRequest request) 58 { 59 HttpSession session = request.getSession(true); 61 Properties ctx = (Properties)session.getAttribute(CONTEXT_NAME); 62 if (ctx != null && System.currentTimeMillis() < s_refresh) 63 { 64 session.setMaxInactiveInterval(1800); String info = (String )ctx.get(HDR_INFO); 66 if (info != null) 67 session.setAttribute(HDR_INFO, info); 68 return ctx; 69 } 70 71 s_log.info("getCtx - refresh"); 73 s_refresh = System.currentTimeMillis() + REFRESH_MS; 74 ctx = new Properties(); 75 ServletContext sc = session.getServletContext(); 77 Enumeration en = sc.getInitParameterNames(); 78 while (en.hasMoreElements()) 79 { 80 String key = (String )en.nextElement(); 81 String value = sc.getInitParameter(key); 82 ctx.setProperty(key, value); 83 } 84 int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID"); 86 if (AD_Client_ID == 0) 87 { 88 AD_Client_ID = DB.getSQLValue("SELECT AD_Client_ID FROM AD_Client WHERE AD_Client_ID > 11 AND IsActive='Y'"); 89 if (AD_Client_ID < 0) 90 AD_Client_ID = 11; Env.setContext (ctx, "#AD_Client_ID", AD_Client_ID); 92 } 93 94 ctx = getDefaults(ctx, AD_Client_ID); 95 ctx.put(CTX_SERVER_CONTEXT, request.getServerName() + request.getContextPath()); 97 98 s_log.debug("getCtx #" + ctx.size()); 100 session.setAttribute(CONTEXT_NAME, ctx); 101 return ctx; 102 } 104 110 private static Properties getDefaults (Properties ctx, int AD_Client_ID) 111 { 112 Integer key = new Integer (AD_Client_ID); 113 Properties pp = (Properties)s_cacheCtx.get(key); 114 if (pp != null) 115 { 116 Enumeration e = pp.keys(); 118 while (e.hasMoreElements()) 119 { 120 String pKey = (String )e.nextElement(); 121 ctx.setProperty(pKey, pp.getProperty(pKey)); 122 } 123 return ctx; 124 } 125 126 127 s_log.info("getDefaults - AD_Client_ID=" + AD_Client_ID); 128 129 if (Env.getContextAsInt(ctx, "#AD_Org_ID") == 0) 131 { 132 int AD_Org_ID = DB.getSQLValue("SELECT AD_Org_ID FROM AD_Org WHERE AD_Client_ID=? AND IsActive='Y' AND IsSummary='N' ORDER BY 1", AD_Client_ID); 133 Env.setContext(ctx, "#AD_Org_ID", AD_Org_ID); 134 } 135 if (Env.getContextAsInt(ctx, "#AD_User_ID") == 0) 137 ctx.setProperty("#AD_User_ID", "0"); 139 if (Env.getContextAsInt(ctx, "#M_Warehouse_ID") == 0) 141 { 142 int M_Warehouse_ID = DB.getSQLValue("SELECT M_Warehouse_ID FROM M_Warehouse WHERE AD_Client_ID=? AND IsActive='Y' ORDER BY 1", AD_Client_ID); 143 Env.setContext(ctx, "#M_Warehouse_ID", M_Warehouse_ID); 144 } 145 if (Env.getContextAsInt(ctx, "#SalesRep_ID") == 0) 147 { 148 int SalesRep_ID = 0; 149 Env.setContext(ctx, "#SalesRep_ID", SalesRep_ID); 150 } 151 if (Env.getContextAsInt(ctx, "#C_PaymentTerm_ID") == 0) 153 { 154 int C_PaymentTerm_ID = DB.getSQLValue("SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y' ORDER BY NetDays", AD_Client_ID); 155 Env.setContext(ctx, "#C_PaymentTerm_ID", C_PaymentTerm_ID); 156 } 157 158 159 160 MClient client = MClient.get (ctx, AD_Client_ID); 162 Env.setContext(ctx, "name", client.getName()); 164 Env.setContext(ctx, "description", client.getDescription()); 165 Env.setContext(ctx, "SMTPHost", client.getSMTPHost()); 166 Env.setContext(ctx, "RequestEMail", client.getRequestEMail()); 167 Env.setContext(ctx, "RequestUser", client.getRequestUser()); 168 Env.setContext(ctx, "RequestUserPw", client.getRequestUserPW()); 169 if (ctx.getProperty("#AD_Language") == null && client.getAD_Language() != null) 171 Env.setContext(ctx, "#AD_Language", client.getAD_Language()); 172 Env.setContext(ctx, "webDir", client.getWebDir()); 173 String s = client.getWebParam1(); 174 Env.setContext(ctx, "webParam1", s == null ? "" : s); 175 s = client.getWebParam2(); 176 Env.setContext(ctx, "webParam2", s == null ? "" : s); 177 s = client.getWebParam3(); 178 Env.setContext(ctx, "webParam3", s == null ? "" : s); 179 s = client.getWebParam4(); 180 Env.setContext(ctx, "webParam4", s == null ? "" : s); 181 s = client.getWebParam5(); 182 Env.setContext(ctx, "webParam5", s == null ? "" : s); 183 s = client.getWebParam6(); 184 Env.setContext(ctx, "webParam6", s == null ? "" : s); 185 s = client.getWebOrderEMail(); 186 Env.setContext(ctx, "webOrderEMail", s == null ? "" : s); 187 s = client.getWebInfo(); 188 if (s != null && s.length() > 0) 189 Env.setContext(ctx, HDR_INFO, s); 190 Env.setContext(ctx, "#M_PriceList_ID", client.getMClientInfo().getM_PriceList_ID()); 192 s = client.getDocumentDir(); 193 Env.setContext(ctx, CTX_DOCUMENT_DIR, s == null ? "" : s); 194 195 if (ctx.getProperty("#AD_Language") == null) 197 Env.setContext(ctx, "#AD_Language", "en_US"); 198 199 s_cacheCtx.put(key, ctx); return ctx; 201 } 203 204 205 private final static String COOKIE_NAME = "CompiereWebUser"; 206 207 212 public static String getCookieWebUser (HttpServletRequest request) 213 { 214 Cookie[] cookies = request.getCookies(); 215 if (cookies == null) 216 return null; 217 for (int i = 0; i < cookies.length; i++) 218 { 219 if (COOKIE_NAME.equals(cookies[i].getName())) 220 return cookies[i].getValue(); 221 } 222 return null; 223 } 224 225 231 public static void addCookieWebUser (HttpServletRequest request, HttpServletResponse response, String webUser) 232 { 233 Cookie cookie = new Cookie(COOKIE_NAME, webUser); 234 cookie.setComment("Compiere Web User"); 235 cookie.setPath(request.getContextPath()); 236 cookie.setMaxAge(2592000); response.addCookie(cookie); 238 } 240 245 public static void deleteCookieWebUser (HttpServletRequest request, HttpServletResponse response) 246 { 247 Cookie cookie = new Cookie(COOKIE_NAME, " "); 248 cookie.setComment("Compiere Web User"); 249 cookie.setPath(request.getContextPath()); 250 cookie.setMaxAge(1); response.addCookie(cookie); 252 } 254 } | Popular Tags |