public Map act(Redirector redirector,
SourceResolver resolver,
Map objectModel,
String src,
Parameters parameters) throws Exception {
DataSourceComponent datasource = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
// read global parameter settings
boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT;
if (this.settings.containsKey("reloadable")) {
reloadable = Boolean.valueOf((String) this.settings.get("reloadable")).booleanValue();
}
// read local settings
try {
Configuration conf = this.getConfiguration(
parameters.getParameter("descriptor", (String) this.settings.get("descriptor")),
resolver,
parameters.getParameterAsBoolean("reloadable", reloadable));
String create_session = parameters.getParameter("create-session",
(String)this.settings.get("create-session"));
String append_session = parameters.getParameter("append-session",
(String)this.settings.get("append-session"));
boolean cs = true;
if (create_session != null) {
cs = BooleanUtils.toBoolean(create_session.trim());
}
boolean as = BooleanUtils.toBoolean(append_session.trim());
datasource = this.getDataSource(conf);
conn = datasource.getConnection();
Request req = ObjectModelHelper.getRequest(objectModel);
/*
* check request validity
*/
if (req == null) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: no request object");
}
return null;
}
String query = this.getAuthQuery(objectModel, conf, req);
if (query == null) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: have not got query");
}
req.setAttribute("message", "The authenticator is misconfigured");
return null;
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: query is: " + query);
}
st = conn.createStatement();
rs = st.executeQuery(query);
if (rs.next()) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: authorized successfully");
}
Session session = null;
if (cs) {
session = req.getSession(false);
if (session != null) {
if (as == false) {
session.invalidate();
session = req.getSession(true);
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: session invalidated");
}
}
} else {
session = req.getSession(true);
}
if (session == null) {
return null;
}
if (getLogger().isDebugEnabled()) {
if (as) {
getLogger().debug("DBCOOKIEAUTH: appending to session");
} else {
getLogger().debug("DBCOOKIEAUTH: session created");
}
}
} else {
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: leaving session untouched");
}
}
HashMap actionMap = this.propagateParameters(conf, rs, session);
if (!conn.getAutoCommit()) {
conn.commit();
}
return Collections.unmodifiableMap(actionMap);
}
if (!conn.getAutoCommit()) {
conn.rollback();
}
req.setAttribute("message", "The username or password were incorrect, please check your CAPS LOCK key and try again.");
if (getLogger().isDebugEnabled()) {
getLogger().debug("DBCOOKIEAUTH: no results for query");
}
} catch (Exception e) {
if (conn != null) {
try {
if (!conn.getAutoCommit()) {
conn.rollback();
}
} catch (Exception se) {
// ignore
}
}
getLogger().error("Exception: ", e);
return null;
} finally {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
// ignore
}
}
}
return null;
}
|