List of usage examples for org.apache.commons.lang BooleanUtils toBoolean
public static boolean toBoolean(String str)
Converts a String to a boolean (optimised for performance).
'true'
, 'on'
or 'yes'
(case insensitive) will return true
.
From source file:org.apache.cocoon.acting.DatabaseCookieAuthenticatorAction.java
/** * Main invocation routine.//www .j a va2s. c o m * * @param redirector Description of Parameter * @param resolver Description of Parameter * @param objectModel Description of Parameter * @param src Description of Parameter * @param parameters Description of Parameter * @return Description of the Returned Value * @exception Exception Description of Exception */ 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; }
From source file:org.apache.cocoon.acting.DatabaseCookieAuthenticatorAction.java
/** * Gets the authQuery attribute of the DatabaseCookieAuthenticatorAction * object/* w ww .j av a 2 s .c o m*/ * * @param objectModel Description of Parameter * @param conf Description of Parameter * @param req Description of Parameter * @return The authQuery value */ private String getAuthQuery(Map objectModel, Configuration conf, Request req) { boolean first_constraint = true; StringBuffer queryBuffer = new StringBuffer("SELECT "); StringBuffer queryBufferEnd = new StringBuffer(""); String dbcol; String cookie_name; String cookie_value; String nullstr; boolean nullable = false; Configuration table = conf.getChild("table"); Configuration[] select = table.getChildren("select"); try { for (int i = 0; i < select.length; i++) { if (i != 0) { queryBuffer.append(", "); } dbcol = select[i].getAttribute("dbcol"); queryBuffer.append(dbcol); cookie_name = select[i].getAttribute("cookie-name", ""); if (StringUtils.isEmpty(cookie_name.trim())) { continue; } nullstr = select[i].getAttribute("nullable", ""); if (BooleanUtils.toBoolean(nullstr.trim())) { nullable = true; } /* * if there is a cookie name, * but not the value, we exit immediately do * that authorization fails authomatically */ cookie_value = getCookie(objectModel, cookie_name).getValue(); if (cookie_value == null || cookie_value.trim().length() == 0) { // value is null if (!nullable) { if (getLogger().isDebugEnabled()) { getLogger().debug("DBCOOKIEAUTH: cookie-name " + cookie_name + " does not exist"); } return null; } } else { if (!first_constraint) { queryBufferEnd.append(" AND "); } queryBufferEnd.append(dbcol + "='" + cookie_value + "'"); first_constraint = false; } } queryBuffer.append(" FROM "); queryBuffer.append(table.getAttribute("name")); if (!queryBufferEnd.toString().trim().equals("")) { queryBuffer.append(" WHERE ").append(queryBufferEnd); } return queryBuffer.toString(); } catch (Exception e) { getLogger().error("Exception: ", e); return null; } }
From source file:org.apache.cocoon.acting.DbXMLAuthenticatorAction.java
private String getAuthQuery(Configuration conf, Request req) { StringBuffer queryBuffer = new StringBuffer("//"); StringBuffer queryBufferEnd = new StringBuffer(""); String dbcol, request_param, request_value, nullstr; boolean nullable = false; Configuration table = conf.getChild("root"); Configuration[] select = table.getChildren("select"); try {//from w ww. ja va2 s . c o m queryBuffer.append(table.getAttribute("name")); for (int i = 0; i < select.length; i++) { dbcol = "[" + select[i].getAttribute("element"); try { request_param = select[i].getAttribute("request-param"); if (request_param == null || request_param.trim().equals("")) { continue; } } catch (Exception e) { continue; } try { nullstr = select[i].getAttribute("nullable"); if (nullstr != null) nullstr = nullstr.trim(); if (BooleanUtils.toBoolean(nullstr)) { nullable = true; } } catch (Exception e1) { } /* if there is a request parameter name, * but not the value, we exit immediately do * that authorization fails authomatically */ request_value = req.getParameter(request_param); if (request_value == null || request_value.trim().equals("")) { // value is null if (!nullable) { getLogger().debug("DBXMLAUTH: request-param " + request_param + " does not exist"); return null; } } else { queryBufferEnd.append(dbcol).append("='").append(request_value).append("']"); } } if (!queryBufferEnd.toString().trim().equals("")) queryBuffer.append(queryBufferEnd); return queryBuffer.toString(); } catch (Exception e) { getLogger().debug("DBXMLAUTH: got exception: " + e); return null; } }
From source file:org.apache.cocoon.acting.modular.DatabaseAction.java
public void configure(Configuration conf) throws ConfigurationException { super.configure(conf); if (this.settings != null) { this.defaultModeNames.put(MODE_OTHERS, this.settings.get("input", inputHint)); this.defaultModeNames.put(MODE_OUTPUT, this.settings.get("output", outputHint)); this.defaultModeNames.put(MODE_AUTOINCR, this.settings.get("autoincrement", databaseHint)); this.pathSeparator = (String) this.settings.get("path-separator", this.pathSeparator); String tmp = (String) this.settings.get("first-row", null); if (tmp != null) { try { this.firstRow = Integer.parseInt(tmp); } catch (NumberFormatException nfe) { if (getLogger().isWarnEnabled()) getLogger().warn("problem parsing first row option " + tmp + " using default instead."); }//from w w w .j ava 2s. c o m } tmp = (String) this.settings.get("fail-on-empty", String.valueOf(this.failOnEmpty)); this.failOnEmpty = BooleanUtils.toBoolean(tmp); } }
From source file:org.apache.cocoon.acting.modular.DatabaseAction.java
/** * Add a record to the database. This action assumes that * the file referenced by the "descriptor" parameter conforms * to the AbstractDatabaseAction specifications. *//*from ww w . j a v a 2 s.c o m*/ public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception { DataSourceComponent datasource = null; Connection conn = null; Map results = new HashMap(); int rows = 0; boolean failed = false; // read global parameter settings boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT; // call specific default modes apart from output mode are not supported // set request attribute String outputMode = param.getParameter("output", (String) defaultModeNames.get(MODE_OUTPUT)); if (this.settings.containsKey("reloadable")) { reloadable = Boolean.valueOf((String) this.settings.get("reloadable")).booleanValue(); } // read local parameter settings try { Configuration conf = this.getConfiguration( param.getParameter("descriptor", (String) this.settings.get("descriptor")), resolver, param.getParameterAsBoolean("reloadable", reloadable)); // get database connection and try to turn off autocommit datasource = this.getDataSource(conf, param); conn = datasource.getConnection(); if (conn.getAutoCommit() == true) { try { conn.setAutoCommit(false); } catch (Exception ex) { String tmp = param.getParameter("use-transactions", (String) this.settings.get("use-transactions", null)); if (tmp != null && (tmp.equalsIgnoreCase("no") || tmp.equalsIgnoreCase("false") || tmp.equalsIgnoreCase("0"))) { if (getLogger().isErrorEnabled()) getLogger().error( "This DB connection does not support transactions. If you want to risk your data's integrity by continuing nonetheless set parameter \"use-transactions\" to \"no\"."); throw ex; } } } // find tables to work with Configuration[] tables = conf.getChildren("table"); String tablesetname = param.getParameter("table-set", (String) this.settings.get("table-set")); Map modeTypes = null; if (tablesetname == null) { modeTypes = new HashMap(6); modeTypes.put(MODE_AUTOINCR, "autoincr"); modeTypes.put(MODE_OTHERS, "others"); modeTypes.put(MODE_OUTPUT, outputMode); for (int i = 0; i < tables.length; i++) { rows += processTable(tables[i], conn, objectModel, results, modeTypes); } } else { // new set based behaviour // create index for table names / aliases Map tableIndex = new HashMap(2 * tables.length); String tableName = null; Object result = null; for (int i = 0; i < tables.length; i++) { tableName = tables[i].getAttribute("alias", tables[i].getAttribute("name", "")); result = tableIndex.put(tableName, new Integer(i)); if (result != null) { throw new IOException( "Duplicate table entry for " + tableName + " at positions " + result + " and " + i); } } Configuration[] tablesets = conf.getChildren("table-set"); String setname = null; boolean found = false; // find tables contained in tableset int j = 0; for (j = 0; j < tablesets.length; j++) { setname = tablesets[j].getAttribute("name", ""); if (tablesetname.trim().equals(setname.trim())) { found = true; break; } } if (!found) { throw new IOException(" given set " + tablesetname + " does not exists in a description file."); } Configuration[] set = tablesets[j].getChildren("table"); for (int i = 0; i < set.length; i++) { // look for alternative mode types modeTypes = new HashMap(6); modeTypes.put(MODE_AUTOINCR, set[i].getAttribute("autoincr-mode", "autoincr")); modeTypes.put(MODE_OTHERS, set[i].getAttribute("others-mode", "others")); modeTypes.put(MODE_OUTPUT, outputMode); tableName = set[i].getAttribute("name", ""); if (tableIndex.containsKey(tableName)) { j = ((Integer) tableIndex.get(tableName)).intValue(); rows += processTable(tables[j], conn, objectModel, results, modeTypes); } else { throw new IOException( " given table " + tableName + " does not exists in a description file."); } } } if (conn.getAutoCommit() == false) { conn.commit(); } // obtain output mode module and rollback output ServiceSelector outputSelector = null; OutputModule output = null; try { outputSelector = (ServiceSelector) this.manager.lookup(OUTPUT_MODULE_SELECTOR); if (outputMode != null && outputSelector != null && outputSelector.isSelectable(outputMode)) { output = (OutputModule) outputSelector.select(outputMode); } if (output != null) { output.commit(null, objectModel); } else if (getLogger().isWarnEnabled()) { getLogger().warn("Could not select output mode " + outputMode); } } catch (ServiceException e) { if (getLogger().isWarnEnabled()) { getLogger().warn("Could not select output mode " + outputMode + ":" + e.getMessage()); } } finally { if (outputSelector != null) { if (output != null) { outputSelector.release(output); } this.manager.release(outputSelector); } } } catch (Exception e) { failed = true; if (conn != null) { try { if (getLogger().isDebugEnabled()) { getLogger().debug("Rolling back transaction. Caused by " + e.getMessage()); e.printStackTrace(); } conn.rollback(); results = null; // obtain output mode module and commit output ServiceSelector outputSelector = null; OutputModule output = null; try { outputSelector = (ServiceSelector) this.manager.lookup(OUTPUT_MODULE_SELECTOR); if (outputMode != null && outputSelector != null && outputSelector.isSelectable(outputMode)) { output = (OutputModule) outputSelector.select(outputMode); } if (output != null) { output.rollback(null, objectModel, e); } else if (getLogger().isWarnEnabled()) { getLogger().warn("Could not select output mode " + outputMode); } } catch (ServiceException e2) { if (getLogger().isWarnEnabled()) { getLogger().warn("Could not select output mode " + outputMode + ":" + e2.getMessage()); } } finally { if (outputSelector != null) { if (output != null) { outputSelector.release(output); } this.manager.release(outputSelector); } } } catch (SQLException se) { if (getLogger().isDebugEnabled()) getLogger().debug("There was an error rolling back the transaction", se); } } //throw new ProcessingException("Could not add record :position = " + currentIndex, e); // don't throw an exception, an error has been signalled, that should suffice String throwException = (String) this.settings.get("throw-exception", param.getParameter("throw-exception", null)); if (throwException != null && BooleanUtils.toBoolean(throwException)) { throw new ProcessingException("Cannot process the requested SQL statement ", e); } } finally { if (conn != null) { try { conn.close(); } catch (SQLException sqe) { getLogger().warn("There was an error closing the datasource", sqe); } } if (datasource != null) this.dbselector.release(datasource); } if (results != null) { if (rows > 0 || (!failed && !this.failOnEmpty)) { results.put("row-count", new Integer(rows)); } else { results = null; } } else { if (rows > 0) { results = new HashMap(1); results.put("row-count", new Integer(rows)); } } return results; // (results == null? results : Collections.unmodifiableMap(results)); }
From source file:org.apache.cocoon.ajax.AjaxRequestSelector.java
public boolean select(String expression, Object selectorContext) { boolean test = BooleanUtils.toBoolean(expression); return test == ((Boolean) selectorContext).booleanValue(); }
From source file:org.apache.cocoon.bean.helpers.BeanConfigurator.java
private static boolean getBooleanAttributeValue(Node node, String attr) { NamedNodeMap nodes = node.getAttributes(); if (nodes != null) { Node attribute = nodes.getNamedItem(attr); if (attribute != null) { String value = attribute.getNodeValue(); return BooleanUtils.toBoolean(value); }/*w ww . jav a 2s. c o m*/ } return false; }
From source file:org.apache.cocoon.components.axis.SoapServerImpl.java
/** * Helper method to set the security provider. * * @param config a <code>Configuration</code> instance * @exception ConfigurationException if an error occurs *///from w w w . ja va 2s . co m private void setSecurityProvider(final Configuration config) throws ConfigurationException { final Configuration secProvider = config.getChild("security-provider", false); if (secProvider != null) { final String attr = secProvider.getAttribute("enabled"); final boolean providerIsEnabled = BooleanUtils.toBoolean(attr); if (providerIsEnabled) { m_securityProvider = new ServletSecurityProvider(); } } if (getLogger().isDebugEnabled()) { getLogger().debug("security provider = " + m_securityProvider); } }
From source file:org.apache.cocoon.components.language.markup.xsp.AbstractEsqlConnection.java
/** * It appears that some commercial DBMSs like Oracle and Informix * are broken in that they don't follow the JDBC standard and * calls to getUpdateCount after getMoreResults result either in * an exception (Informix) or return the same value (i.e. not -1) (Oracle). * In addition, this feature is only useful with stored procedures. * Hence we disable it per default.//from w ww. j a v a 2 s .c o m **/ public void setMultipleResults(String value) { this.multipleResults = BooleanUtils.toBoolean(value); }
From source file:org.apache.cocoon.components.language.markup.xsp.XSPCookieHelper.java
/** * This method will set a new cookie with values that are passed through parameters * * @param objectModel/* w ww .ja v a 2 s. c om*/ * @param name name to be set for the cookie * @param value value to be set for the cookie * @param comment comment to be set for the cookie * @param domain domain to be set for the cookie * @param maxage maxage to be set for the cookie * @param path path to be set for the cookie * @param secure secure property to be set for the cookie * @param version version to be set for the cookie */ public static void addCookie(Map objectModel, String name, String value, String comment, String domain, int maxage, String path, String secure, int version) { Response response = ObjectModelHelper.getResponse(objectModel); Cookie cookieToSet = response.createCookie(name, value); if ((comment.trim()).length() > 0) cookieToSet.setComment(comment); if ((domain.trim()).length() > 0) cookieToSet.setDomain(domain); if (maxage > 0) cookieToSet.setMaxAge(maxage); if ((path.trim()).length() > 0) cookieToSet.setPath("/"); cookieToSet.setSecure(BooleanUtils.toBoolean(secure)); cookieToSet.setVersion(version); response.addCookie(cookieToSet); }