private String createBlanks(int count) { StringBuffer strBuf = new StringBuffer(); for (int i = 0; i < count; i++) { strBuf.append(" "); } return strBuf.toString(); }
/** * Validate User * * @param ldapURL provider url - e.g. ldap://dc.compiere.org * @param domain domain name = e.g. compiere.org * @param userName user name - e.g. jjanke * @param password password * @return true if validated with ldap */ public static boolean validate(String ldapURL, String domain, String userName, String password) { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // ldap://dc.compiere.org env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); // [email protected] StringBuffer principal = new StringBuffer(userName).append("@").append(domain); env.put(Context.SECURITY_PRINCIPAL, principal.toString()); env.put(Context.SECURITY_CREDENTIALS, password); // try { // Create the initial context InitialLdapContext ctx = new InitialLdapContext(env, null); // DirContext ctx = new InitialDirContext(env); // Test - Get the attributes Attributes answer = ctx.getAttributes(""); // Print the answer if (false) dump(answer); } catch (AuthenticationException e) { log.info("Error: " + principal + " - " + e.getLocalizedMessage()); return false; } catch (Exception e) { log.log(Level.SEVERE, ldapURL + " - " + principal, e); return false; } log.info("OK: " + principal); return true; } // validate
// createPageString - // Create list of pages for search results private String createPageString( int numberOfItems, int itemsPerPage, int currentPage, String baseUrl) { StringBuffer pageString = new StringBuffer(); // Calculate the total number of pages int totalPages = 1; if (numberOfItems > itemsPerPage) { double pages = Math.ceil(numberOfItems / (double) itemsPerPage); totalPages = (int) Math.ceil(pages); } // if (totalPages > 1) { for (int i = 1; i <= totalPages; i++) { if (i == currentPage) { pageString.append(i); } else { pageString.append("<a href=\"" + baseUrl + i + "\" title=\"" + i + "\">" + i + "</a>"); } if (i != totalPages) pageString.append(" "); } } else { pageString.append("1"); } return pageString.toString(); }
/** Creates the Base64 value. */ private String base64(String value) { StringBuffer cb = new StringBuffer(); int i = 0; for (i = 0; i + 2 < value.length(); i += 3) { long chunk = (int) value.charAt(i); chunk = (chunk << 8) + (int) value.charAt(i + 1); chunk = (chunk << 8) + (int) value.charAt(i + 2); cb.append(encode(chunk >> 18)); cb.append(encode(chunk >> 12)); cb.append(encode(chunk >> 6)); cb.append(encode(chunk)); } if (i + 1 < value.length()) { long chunk = (int) value.charAt(i); chunk = (chunk << 8) + (int) value.charAt(i + 1); chunk <<= 8; cb.append(encode(chunk >> 18)); cb.append(encode(chunk >> 12)); cb.append(encode(chunk >> 6)); cb.append('='); } else if (i < value.length()) { long chunk = (int) value.charAt(i); chunk <<= 16; cb.append(encode(chunk >> 18)); cb.append(encode(chunk >> 12)); cb.append('='); cb.append('='); } return cb.toString(); }
/** EJECUTA UNA CONSULTA Y GENERA XML */ protected String getXML() throws Exception { StringBuffer xml = new StringBuffer(); xml.append("<registros>"); if (conectar()) { ResultSet rs = ejecutarSQL(this.SQL); while (rs.next()) { ResultSetMetaData rsm = rs.getMetaData(); xml.append("<registro>"); for (int i = 1; i <= rsm.getColumnCount(); i++) { xml.append("<" + rsm.getColumnName(i) + ">"); xml.append(rs.getString(i)); xml.append("</" + rsm.getColumnName(i) + ">"); } xml.append("</registro>"); } desconectar(); } xml.append("</registros>"); return xml.toString(); } // Fin getXML
/** EJECUTA UNA CONSULTA Y GENERA LA TABLA HTML */ protected String getHTML(String SQL) throws Exception { StringBuffer html = new StringBuffer(); html.append("<TABLE border='1'>"); html.append("<TR>"); if (conectar()) { ResultSet rs = ejecutarSQL(SQL); ResultSetMetaData rsm = rs.getMetaData(); html.append("<TR>"); for (int i = 1; i <= rsm.getColumnCount(); i++) { html.append("<TH>" + rsm.getColumnName(i) + "</TH>"); } html.append("</TR>"); while (rs.next()) { html.append("<TR>"); for (int i = 1; i <= rsm.getColumnCount(); i++) { html.append("<TD>" + rs.getString(i) + ".</TD>"); } html.append("</TR>"); } desconectar(); } html.append("</TR>"); html.append("</TABLE>"); return html.toString(); } // Fin getHTML