ParseControl.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. package com.netscape.dsml.gateway;
  38. import org.w3c.dom.*;
  39. import java.util.logging.*;
  40. public class ParseControl {
  41. public static netscape.ldap.LDAPControl parseControlFromNode(org.w3c.dom.Node n) {
  42. Logger logger = Logger.getLogger("com.netscape.dsml.gateway.ParseControl");
  43. String type = null;
  44. byte[] value = null;
  45. boolean criticality = false;
  46. try {
  47. type = n.getAttributes().getNamedItem("type").getNodeValue();
  48. } catch (Exception e) {
  49. // throw new gatewayException("control type can not be omitted");
  50. }
  51. try {
  52. criticality = Boolean.valueOf( n.getAttributes().getNamedItem("criticality").getNodeValue() ).booleanValue();
  53. } catch (Exception e) { /* ignore */ }
  54. netscape.ldap.LDAPControl lc = null;
  55. if (type.equals( netscape.ldap.controls.LDAPSortControl.SORTREQUEST ) ) {
  56. try {
  57. java.util.Vector ldskv = new java.util.Vector();
  58. org.w3c.dom.NodeList nl = n.getFirstChild().getNextSibling().getChildNodes();
  59. for (int i=0; i< nl.getLength(); i++) {
  60. if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
  61. if (nl.item(i).getLocalName().equals("attr") ) {
  62. // should check to make sure there's only one child to this filter
  63. Node mod = nl.item(i);
  64. String attribute = mod.getAttributes().getNamedItem("name").getNodeValue().trim();
  65. boolean reverse = false;
  66. try {
  67. reverse = new Boolean( mod.getAttributes().getNamedItem("reverse").getNodeValue().trim()).booleanValue();
  68. } catch (Exception e) {}
  69. netscape.ldap.LDAPSortKey ldsk = new netscape.ldap.LDAPSortKey(attribute, reverse);
  70. logger.log(Level.FINE, "SSS: reverse: {0}", String.valueOf(reverse));
  71. logger.log(Level.FINE, "SSS: attribute: {0}", attribute );
  72. ldskv.add(ldsk);
  73. }
  74. }
  75. }
  76. netscape.ldap.LDAPSortKey sortkeys[] = new netscape.ldap.LDAPSortKey[ ldskv.size() ];
  77. for (int j=0; j< ldskv.size(); j++)
  78. sortkeys[j] = (netscape.ldap.LDAPSortKey) ldskv.get(j);
  79. lc = new netscape.ldap.controls.LDAPSortControl(sortkeys, criticality);
  80. } catch (Exception e)
  81. { e.printStackTrace(); }
  82. } else if (type.equals( netscape.ldap.controls.LDAPVirtualListControl.VIRTUALLIST ) ) {
  83. //lc = new netscape.ldap.controls.LDAPVirtualListControl(,);
  84. int index = 0;
  85. int before = 0;
  86. int after = 0;
  87. int content = 0;
  88. java.util.Vector ldskv = new java.util.Vector();
  89. org.w3c.dom.NodeList nl = n.getFirstChild().getNextSibling().getChildNodes();
  90. for (int i=0; i< nl.getLength(); i++) {
  91. if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
  92. if (nl.item(i).getLocalName().equals("index") ) {
  93. try {
  94. index = Integer.parseInt( nl.item(i).getFirstChild().getNodeValue() );
  95. } catch (Exception e) {}
  96. } else if (nl.item(i).getLocalName().equals("before") ) {
  97. try {
  98. before = Integer.parseInt( nl.item(i).getFirstChild().getNodeValue() );
  99. } catch (Exception e) {}
  100. } else if (nl.item(i).getLocalName().equals("after") ) {
  101. try {
  102. after = Integer.parseInt( nl.item(i).getFirstChild().getNodeValue() );
  103. } catch (Exception e) {}
  104. } else if (nl.item(i).getLocalName().equals("count") ) {
  105. try {
  106. content = Integer.parseInt( nl.item(i).getFirstChild().getNodeValue() );
  107. } catch (Exception e) {}
  108. }
  109. }
  110. }
  111. logger.log(Level.FINE, "VLV: index: {0}", String.valueOf(index));
  112. logger.log(Level.FINE, "VLV: before: {0}", String.valueOf(before));
  113. logger.log(Level.FINE, "VLV: after: {0}", String.valueOf(after));
  114. logger.log(Level.FINE, "VLV: content: {0}", String.valueOf(content));
  115. lc = new netscape.ldap.controls.LDAPVirtualListControl( index, before, after, content );
  116. } else {
  117. if (criticality) {
  118. // throw new gatewayException("unrecognized control oid");
  119. } else {
  120. // do nothing
  121. }
  122. }
  123. return lc;
  124. }
  125. }