1 4 5 9 10 package org.openlaszlo.auth; 11 12 import org.openlaszlo.data.*; 13 import org.openlaszlo.server.*; 14 import org.openlaszlo.servlets.*; 15 import org.openlaszlo.utils.*; 16 import java.io.*; 17 import java.net.*; 18 import java.security.*; 19 import java.util.*; 20 import javax.servlet.http.*; 21 import org.apache.commons.httpclient.*; 22 import org.apache.commons.httpclient.methods.*; 23 import org.apache.log4j.*; 24 import org.jdom.*; 25 import org.jdom.input.*; 26 27 28 35 public class HTTPAuthentication implements Authentication 36 { 37 38 private String mDefaultURL = null; 39 40 41 private SAXBuilder mBuilder = new SAXBuilder(); 42 43 44 protected static Logger mLogger = Logger.getLogger(HTTPAuthentication.class); 45 46 public void init(Properties prop) 47 { 48 mDefaultURL = prop.getProperty("httpauthentication.url"); 49 mLogger.debug("default url: " + mDefaultURL); 50 } 51 52 53 71 public int login(HttpServletRequest req, HttpServletResponse res, 72 HashMap param, StringBuffer xmlResponse) 73 throws AuthenticationException { 74 75 mLogger.debug("login(req,res,param,xmlResponse)"); 76 int code=1; 77 String usr = req.getParameter("usr"); 78 String pwd = req.getParameter("pwd"); 79 String query = "rt=login&usr=" + usr + "&pwd=" + pwd; 80 callAuthenticationServer(req, res, param, query, xmlResponse); 81 if (xmlResponse.toString().indexOf("code=\"0\"") != -1) 82 code = 0; 83 return code; 84 } 85 86 87 104 public int logout(HttpServletRequest req, HttpServletResponse res, 105 HashMap param, StringBuffer xmlResponse) 106 throws AuthenticationException { 107 108 mLogger.debug("logout(req,res,param,xmlResponse)"); 109 int code = 1; 110 String query = "rt=logout"; 111 callAuthenticationServer(req, res, param, query, xmlResponse); 112 if (xmlResponse.toString().indexOf("code=\"0\"") != -1) 113 code = 0; 114 return code; 115 } 116 117 118 136 public String getUsername(HttpServletRequest req, HttpServletResponse res, 137 HashMap param) 138 throws AuthenticationException 139 { 140 mLogger.debug("getUsername(req,res,param)"); 141 try { 142 String query = "rt=getusername"; 143 StringBuffer buf = new StringBuffer (); 144 145 callAuthenticationServer(req, res, param, query, buf); 146 147 StringReader reader = new StringReader(buf.toString()); 148 Document document = mBuilder.build(reader); 149 Element root = document.getRootElement(); 150 Element eUsername = root.getChild("response"); 151 boolean isOk = (getStatusCode(eUsername)==0); 152 return isOk ? eUsername.getChildText("username") : null; 153 } catch (JDOMException e) { 154 throw new AuthenticationException(e.getMessage()); 155 } 156 } 157 158 159 162 private void callAuthenticationServer(HttpServletRequest req, 163 HttpServletResponse res, 164 HashMap param, String query, 165 StringBuffer xmlResponse) 166 throws AuthenticationException 167 { 168 if (mDefaultURL == null) { 169 String scheme = req.getScheme(); 170 String host = req.getServerName(); 171 int port = req.getServerPort(); 172 String path = req.getContextPath(); 173 mDefaultURL = scheme + "://" + host + ":" + port + path + "/AuthenticationServlet"; 174 } 175 176 Data data = null; 177 try { 178 179 String urlstr = (String ) param.get("url"); 180 if (urlstr == null) 181 urlstr = mDefaultURL; 182 urlstr += "?" + query; 183 data = HTTPDataSource.getHTTPData(req, res, urlstr, -1); 184 xmlResponse.append(data.getAsString()); 185 186 } catch (DataSourceException e) { 187 throw new AuthenticationException(e.getMessage()); 188 } catch (MalformedURLException e) { 189 throw new AuthenticationException(e.getMessage()); 190 } catch (IOException e) { 191 throw new AuthenticationException(e.getMessage()); 192 } finally { 193 if (data != null) 194 data.release(); 195 } 196 } 197 198 199 202 static private int getStatusCode(Element element) 203 { 204 mLogger.debug("getStatusCode(element)"); 205 206 int code = 1; 207 if (element != null) { 208 Element eStatus = element.getChild("status"); 209 String statCode = eStatus.getAttributeValue("code"); 210 try { 212 code = Integer.parseInt(statCode); 213 } catch (NumberFormatException e) { 214 mLogger.debug(e.getMessage()); 215 } 216 } 217 return code; 218 } 219 } 220 | Popular Tags |