1 package com.ca.commons.naming; 2 3 import javax.naming.*; 4 import javax.naming.directory.*; 5 6 import java.util.*; 7 import java.util.logging.Logger ; 8 import java.util.logging.Level ; 9 import java.io.*; 10 11 import com.ca.commons.cbutil.*; 12 13 14 15 21 22 24 public class LdifUtility 25 { 26 private static boolean debug = false; 27 28 private static boolean handleXML = false; 29 30 private Hashtable params = null; private String filedir = null; 33 34 private String cr = System.getProperty("line.separator", "\n"); 35 36 private final static Logger log = Logger.getLogger(LdifUtility.class.getName()); 37 38 public LdifUtility() 39 { 40 } 41 42 46 public static void setSupportXML_LDIF_RFC(boolean state) 47 { 48 handleXML = state; 49 } 50 51 57 public LdifUtility(Hashtable params, String filedir) 58 { 59 this.params = params; 60 this.filedir = filedir; 61 } 62 63 68 public void setFileDir(String filedir) 69 { 70 this.filedir = filedir + "\\"; 71 } 72 73 78 public void setParams(Hashtable params) 79 { 80 this.params = params; 81 } 82 83 96 public String ldifEncode(Object o, int offset, boolean forceBase64Encoding) 97 { 98 if (forceBase64Encoding == false) 99 return ldifEncode(o, offset); 100 101 String ret = ":: "; 102 if (o.getClass().isArray()) 103 { 104 try 105 { 106 byte b[] = (byte[]) o; 107 ret += CBBase64.binaryToString(b, offset + 3); 108 return ret; 109 } 110 catch (ClassCastException e) 111 { 112 System.out.println("unable to cast array to byte array."); 113 } 114 } 115 116 122 ret += CBBase64.binaryToString(o.toString().getBytes(), offset + 3); 123 return ret; 124 } 125 126 142 143 public String ldifEncode(Object o, int offset) 144 { 145 boolean base64Encode = false; 146 147 boolean xmlEncode = false; 148 149 if ((o instanceof String ) == false) 150 { 151 if (debug == true) System.out.println("found a " + o.getClass().toString()); 152 if (o.getClass().isArray()) 153 { 154 try 155 { 156 byte b[] = (byte[]) o; 157 String ret = ":: " + CBBase64.binaryToString(b, offset + 3); 158 if (debug == true) System.out.println("phenomenal - identified and wrote '" + ret + "'"); 159 return ret; 160 } 161 catch (ClassCastException e) 162 { 163 if (debug == true) System.out.println("unable to cast array to byte array."); 164 } 165 } 166 return o.toString(); 167 } 168 else { 170 String s = o.toString(); 171 int len = s.length(); 172 173 if (len == 0) return ": "; 175 176 char startChar = s.charAt(0); 178 if ("\n\r :".indexOf(startChar) != -1) base64Encode = true; 180 else if (startChar == '<') 181 { 182 if (handleXML == true && s.startsWith("<?xml ")) xmlEncode = true; 184 else 185 base64Encode = true; 186 } 187 else 188 { 189 char test[] = new char[len]; 190 s.getChars(0, len, test, 0); 191 for (int i = 0; i < len; i++) 192 { 193 if (test[i] > 126 || test[i] < 32) { 196 base64Encode = true; break; 198 } 199 } 200 } 201 202 if (s.charAt(s.length() - 1) == ' ') base64Encode = true; 204 205 if (base64Encode) 206 { 207 return translateToLdifBase64(s, offset); 208 } 209 else if (xmlEncode) 210 return translateToLdifXML(s); 211 else 212 return ": " + s; } 214 } 215 216 private String translateToLdifBase64(String s, int offset) 217 { 218 try 219 { 220 s = CBBase64.binaryToString(s.getBytes("UTF8"), offset + 3); 221 } 222 catch (UnsupportedEncodingException e) { 224 log.log(Level.WARNING, "error utf8 encoding strings...", e); 225 s = CBBase64.binaryToString(s.getBytes(), offset + 3); 226 } 227 return ":: " + s; 228 } 229 230 235 private String translateToLdifXML(String s) 236 { 237 StringBuffer xml = new StringBuffer (";transfer-rxer>:").append(cr).append(s); 238 239 241 if (s.indexOf("\r") != -1) { 243 CBParse.replaceAllBufferString(xml, "\r", "\r>"); 244 } 245 if (s.indexOf("\n") != -1) { 247 CBParse.replaceAllBufferString(xml, "\n", "\n>"); 248 } 249 250 return xml.toString(); 251 } 252 253 257 268 269 public void writeLdifEntry(String dn, FileWriter saveFile, String originalPrefix, String replacementPrefix, Attributes atts) 270 throws NamingException, IOException 271 { 272 if (atts == null) 273 { 274 log.info("no attributes available for " + dn); 275 return; 276 } 277 287 if ((originalPrefix != null) && (dn.endsWith(originalPrefix))) { 289 if (debug == true) System.out.println("original DN = '" + dn + "'"); 290 dn = dn.substring(0, dn.length() - originalPrefix.length()) + replacementPrefix; 291 if (debug == true) System.out.println("after replacement DN = '" + dn + "'"); 292 } 293 294 Attribute oc; oc = atts.get("oc"); if (oc != null) { 298 if (oc instanceof DXAttribute) 299 ((DXAttribute) oc).setName("objectClass"); 300 } 301 else oc = atts.get("objectclass"); if (oc == null) 304 oc = atts.get("objectClass"); if (oc == null) 306 { 307 if (dn.endsWith("cn=schema")) oc = new BasicAttribute("oc", "schema"); 309 } 310 311 if (oc == null) 312 { 313 log.info("unable to identify object class for " + dn + " - skipping entry"); 314 return; 315 } 316 317 if (debug) 318 System.out.println("dn" + ldifEncode(dn, 2)); 319 else 320 saveFile.write("dn" + ldifEncode(dn, 2) + "\n"); 321 322 323 NamingEnumeration ocs = oc.getAll(); 324 while (ocs.hasMore()) 325 { 326 if (debug) 327 System.out.println(oc.getID() + ": " + ocs.next()); 328 else 329 saveFile.write(oc.getID() + ldifEncode(ocs.next(), oc.getID().length()) + "\n"); 330 } 331 332 NamingEnumeration allAtts = atts.getAll(); 333 String attName; 334 Attribute currentAtt; 335 while (allAtts.hasMore()) 336 { 337 currentAtt = (Attribute) allAtts.next(); 338 boolean binary = false; 339 if (currentAtt instanceof DXAttribute) 340 binary = !((DXAttribute) currentAtt).isString(); 341 342 attName = currentAtt.getID(); 343 344 347 348 349 if ((attName.equals("dn") == false) && (attName.equals(oc.getID()) == false)) 350 { 351 NamingEnumeration values = currentAtt.getAll(); 352 353 while (values.hasMore()) 354 { 355 356 Object value = values.next(); 357 358 if (value != null) 359 { 360 if (debug) 362 { 363 System.out.println("value class = " + value.getClass().toString() + " : " + value); 364 System.out.println(attName + ": " + value.toString()); 365 } 366 else 367 { 368 if (binary) 369 saveFile.write(attName + ldifEncode(value, attName.length(), true) + "\n"); 370 else 371 saveFile.write(attName + ldifEncode(value, attName.length()) + "\n"); 372 } 373 } 374 } 375 } 376 } 377 if (!debug) 378 { 379 saveFile.write("\n"); 380 saveFile.flush(); 381 } 382 } 383 384 392 393 public void ldifDecode(String parseableLine, DXEntry newEntry) 394 { 395 boolean isBinary = false; 396 int breakpos = parseableLine.indexOf(':'); 397 if (breakpos < 0) 398 { 399 log.warning("Error - illegal line in ldif file\n" + parseableLine); 400 return; 401 } 402 403 String attribute = parseableLine.substring(0, breakpos); 404 Object value = null; 405 406 int attLen = attribute.length(); 407 408 if (attribute.equals("oc")) attribute = "objectClass"; 410 411 412 int startpos = 2; 413 414 if (parseableLine.length() <= breakpos + 1) { 416 value = ""; 417 } 418 else if (parseableLine.charAt(breakpos + 1) == ':') { 420 value = getBase64Value(parseableLine, attLen, startpos, attribute); if (value instanceof String == false) 422 isBinary = true; 423 } 424 else 425 { 426 if (parseableLine.charAt(attLen + 1) != ' ') startpos = 1; 428 value = parseableLine.substring(attLen + startpos); 429 430 value = expandValueParams(value); 432 433 } 434 435 if ("dn".equalsIgnoreCase(attribute)) 436 { 437 if (value instanceof String ) 438 { 439 DN dn = new DN((String ) value); 440 if (dn.error()) 441 log.warning("Error trying to initialise ldif DN: \n" + dn.getError()); 442 else 443 newEntry.putDN(dn); 444 } 445 else { 447 try 448 { 449 DN dn = new DN(new String ((byte[]) value, "UTF8")); 450 if (dn.error()) 451 log.log(Level.WARNING, "Error trying to initialise ldif DN: \n", dn.getError()); 452 else 453 newEntry.putDN(dn); 454 } 455 catch (UnsupportedEncodingException e) 456 { 457 } } 459 } 460 else if (attribute != null) 461 { 462 Attribute existing = newEntry.get(attribute); 463 464 if (existing == null) 465 { 466 DXAttribute att = new DXAttribute(attribute, value); 467 att.setString(!isBinary); 468 newEntry.put(att); 469 } 470 else 471 { 472 existing.add(value); 473 newEntry.put(existing); 474 } 475 } 476 } 477 478 481 private Object getBase64Value(String parseableLine, int attLen, int startpos, String attribute) 482 { 483 byte[] rawBinaryData; 484 485 if (parseableLine.charAt(attLen + 2) == ' ') startpos = 3; 487 488 rawBinaryData = CBBase64.stringToBinary(parseableLine.substring(attribute.length() + startpos)); 489 490 494 byte[] testBytes; 496 if (rawBinaryData.length > 256) 497 { 498 testBytes = new byte[256]; 499 System.arraycopy(rawBinaryData, 0, testBytes, 0, 256); 500 } 501 else 502 testBytes = rawBinaryData; 503 504 507 508 if (CBParse.isUTF8(testBytes)) 509 { 510 try 511 { 512 return new String (rawBinaryData, "UTF-8"); 513 } 514 catch (Exception e) { 516 } 518 } 519 return rawBinaryData; 520 } 521 522 523 531 532 public DXEntry readLdifEntry(BufferedReader textReader) 533 throws IOException 534 { 535 DXEntry entry = new DXEntry(); 536 537 String line = ""; 539 540 String firstLine = ""; 542 543 548 549 551 StringBuffer multiLineText = null; 553 while ((line = textReader.readLine()) != null) 554 { 555 if (line.length() > 0 && line.charAt(0) == ' ') { 557 if (multiLineText == null) 558 multiLineText = new StringBuffer (firstLine); 559 560 multiLineText.append(line.substring(1)); 561 } 563 else if (line.length() > 0 && line.charAt(0) == '>') { 565 if (multiLineText == null) 566 multiLineText = new StringBuffer (firstLine); 567 568 multiLineText.append(line.substring(1)).append(cr); 569 } 571 else if (firstLine.length() > 1 && firstLine.charAt(0) == '#') 572 { 573 } 575 else if (firstLine.length() > 2 || multiLineText != null) 576 { 577 if (multiLineText != null) 578 ldifDecode(multiLineText.toString(), entry); 579 else 580 ldifDecode(firstLine, entry); 581 582 multiLineText = null; 583 } 584 585 if (line == null || line.equals("")) { 587 return entry; 588 } 589 590 firstLine = line; 591 } 592 593 if (entry.getDN().size() > 0) { 595 if (firstLine != null && firstLine.trim().length() > 0) 599 ldifDecode(firstLine, entry); 600 601 return entry; } 603 604 return null; } 606 607 614 public Object expandValueParams(Object value) 615 { 616 if (params != null) 617 { 618 Enumeration keys = params.keys(); 619 while (keys.hasMoreElements()) 620 { 621 String key = (String ) keys.nextElement(); 622 String keyvalue = (String ) params.get(key); 623 624 String oldValue = (String ) value; 626 int index = oldValue.indexOf(key); 627 if (index > -1) 628 { 629 String newValue = oldValue.substring(0, index) + keyvalue + 630 oldValue.substring(index + key.length(), oldValue.length()); 631 System.out.println(newValue); 632 value = newValue; 633 } 634 } 635 } 636 637 if (filedir != null) 639 { 640 String oldValue = (String ) value; 642 String match = "< file://"; 643 644 int index = (oldValue.toLowerCase()).indexOf(match); 645 646 if (index > -1) 647 { 648 String filename = filedir + oldValue.substring(index + 9, oldValue.length()); 649 File file = new File(filename); 650 try 651 { 652 FileInputStream input = new FileInputStream(file); 653 654 int length = (int) file.length(); 655 if (length > 0) 656 { 657 byte[] bytes = new byte[length]; 658 int read = input.read(bytes); 659 if (read > 0) value = bytes; 660 } 661 input.close(); 662 } 663 catch (IOException e) 664 { 665 System.out.println("Error opening the file!" + e); 666 } 667 } 668 } 669 return value; 670 } 671 } | Popular Tags |