1 10 package org.mmbase.servlet; 11 12 import java.util.*; 13 14 import java.net.InetAddress ; 15 import java.net.UnknownHostException ; 16 17 import javax.servlet.ServletException ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 import javax.servlet.http.Cookie ; 21 22 import org.mmbase.module.core.*; 23 import org.mmbase.util.AuthorizationException; 24 import org.mmbase.util.HttpAuth; 25 import org.mmbase.util.NotLoggedInException; 26 27 import org.mmbase.util.logging.Logger; 28 import org.mmbase.util.logging.Logging; 29 30 31 45 46 public class JamesServlet extends MMBaseServlet { 47 private static final Logger log = Logging.getLoggerInstance(JamesServlet.class); 48 protected static Logger pageLog; 49 50 53 public void init() throws ServletException { 54 super.init(); 55 pageLog = Logging.getLoggerInstance(Logging.PAGE_CATEGORY); 57 } 58 59 66 protected final Object getModule(String name) { 67 return org.mmbase.module.Module.getModule(name); 68 } 69 70 74 protected final Hashtable getInitParameters() { 75 return null; 76 } 77 78 81 protected final Hashtable getProperties(String name) { 82 return null; 83 } 84 85 88 protected final String getProperty(String name, String var) { 89 return null; 90 } 91 92 100 public String getAuthorization(HttpServletRequest req,HttpServletResponse res) throws AuthorizationException, NotLoggedInException { 101 return getAuthorization(req, res, "www", "Basic"); 102 } 103 104 114 public String getAuthorization(HttpServletRequest req,HttpServletResponse res,String server, String level) throws AuthorizationException, NotLoggedInException { 115 return HttpAuth.getAuthorization(req, res, server, level); 116 } 117 118 128 public String getCookie(HttpServletRequest req, HttpServletResponse res) { 129 130 final String MMBASE_COOKIENAME = "MMBase_Ident"; 131 final String PATH = "/"; 133 String domain = null; 134 Cookie [] cookies = req.getCookies(); 136 if (cookies!= null) { 137 for (int i = 0; i < cookies.length; i++) { 138 Cookie cookie = cookies[i]; 139 if (cookie.getName().equals(MMBASE_COOKIENAME)) { 140 return MMBASE_COOKIENAME + '/' + cookie.getValue(); 141 } 142 } 143 } 144 145 log.debug("No mmbase cookie found"); 146 MMBase mmbase = MMBase.getMMBase(); 153 if (mmbase == null) { 154 log.warn("No mmbase found"); 155 return null; 156 } 157 String cookieValue = "" + System.currentTimeMillis(); 158 domain = mmbase.getInitParameter("COOKIEDOMAIN"); 159 log.debug("Setting MMBase cookie on domain " + domain); 160 Cookie cookie = new Cookie (MMBASE_COOKIENAME, cookieValue); 161 cookie.setPath(PATH); 162 if (domain != null) { 163 cookie.setDomain(domain); 164 } 165 cookie.setMaxAge((int) (20 * 365.25 * 24 * 60 * 60)); 166 res.addCookie(cookie); 167 if (res.isCommitted()) { 168 log.error("Could not add cookie " + cookie + " because response is already committed"); 169 } 170 return MMBASE_COOKIENAME + '/' + cookieValue; 171 172 } 173 174 178 public String getParam(HttpServletRequest req,int num) { 179 String str; 180 181 Vector params=buildparams(req); 183 try { 184 str=(String )params.elementAt(num); 185 } catch(IndexOutOfBoundsException e) { 186 str=null; 187 } 188 return str; 189 } 190 191 197 private Vector buildparams(HttpServletRequest req) { 198 Vector params=new Vector(); 199 if (req.getQueryString()!=null) { 200 StringTokenizer tok=new StringTokenizer(req.getQueryString(),"+\n\r"); 201 while(tok.hasMoreTokens()) { 203 params.addElement(tok.nextToken()); 204 } 205 } 206 return params; 207 } 208 209 213 public Vector getParamVector(HttpServletRequest req) { 214 Vector params=buildparams(req); 215 return params; 216 } 217 218 224 private static String VPROProxyName = "vpro6d.vpro.nl"; 229 private static String VPROProxyAddress = "145.58.172.6"; 234 private static String proxyName = "zen.vpro.nl"; 236 247 public String getAddress(HttpServletRequest req) { 248 String result = null; 249 boolean fromProxy = false; 250 String addr = req.getRemoteHost(); 251 252 if( addr != null && !addr.equals("") ) { 253 if( addr.indexOf( VPROProxyName ) != -1 || addr.indexOf( VPROProxyAddress ) != -1 ) { 256 fromProxy = true; 259 addr = req.getHeader("X-Forwarded-For"); 260 if(addr != null && !addr.equals("")) { 261 result = addr; 262 } 263 } else { 264 result = addr; 265 } 266 } 267 result = getHostNames( addr ); 268 if( fromProxy ) { 269 result = proxyName+"->" + result; 270 } 271 return result; 272 } 273 274 282 private String getHostNames( String host ) { 283 String result = null; 284 String hn = null; 285 286 if( host.indexOf(",") != -1 ) { 289 int pos; 290 while( (pos = host.indexOf(",")) != -1) { 293 hn = host.substring( 0, pos ); 294 host = host.substring( pos + 2 ); 295 if( result == null ) { 296 result = getHostName( hn ); 297 } else { 298 result += "->" + getHostName( hn ); 299 } 300 } 301 } else { 304 result = getHostName( host ); 305 } 306 return result; 307 } 308 309 316 private String getHostName( String hostname ) { 317 320 String hn = null; 321 if( hostname != null && !hostname.equals("")) { 322 try { 323 hn = InetAddress.getByName( hostname ).getHostName(); 324 } catch( UnknownHostException e ) { 325 hn = hostname; 326 } 327 } else { 328 hn = hostname; 329 } 330 return hn; 331 } 332 333 } 334 | Popular Tags |