Beispiel #1
0
  protected static void checkAndEncryptPasswordSyntax(
      Document doc, boolean encrypt, AMEncryption encryptObj) throws SMSException {
    // Get the node list of all AttributeSchema
    NodeList nl = doc.getElementsByTagName(SMSUtils.SCHEMA_ATTRIBUTE);
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      // Check if the "syntax" attribute is "password"
      String syntax = XMLUtils.getNodeAttributeValue(node, SMSUtils.ATTRIBUTE_SYNTAX);
      if (syntax.equals(AttributeSchema.Syntax.PASSWORD.toString())) {
        if (debug.messageEnabled()) {
          debug.message("ServiceManager: encrypting password syntax");
        }
        // Get the DefaultValues and encrypt then
        Node defaultNode;
        if ((defaultNode = XMLUtils.getChildNode(node, SMSUtils.ATTRIBUTE_DEFAULT_ELEMENT))
            != null) {
          // Get NodeList of "Value" nodes and encrypt them
          for (Iterator items =
                  XMLUtils.getChildNodes(defaultNode, SMSUtils.ATTRIBUTE_VALUE).iterator();
              items.hasNext(); ) {
            Node valueNode = (Node) items.next();
            String value = XMLUtils.getValueOfValueNode(valueNode);
            String encValue;

            if (encrypt) {
              if (encryptObj != null) {
                value = (String) AccessController.doPrivileged(new DecodeAction(value, encryptObj));
                if (value.equals("&amp;#160;")) {
                  try {
                    byte[] b = new byte[1];
                    b[0] = -96;
                    value = new String(b, "ISO-8859-1");
                  } catch (UnsupportedEncodingException e) {
                    // ignore
                  }
                }
              }
              encValue = (String) AccessController.doPrivileged(new EncodeAction(value));
            } else {
              encValue = (String) AccessController.doPrivileged(new DecodeAction(value));

              try {
                // this is catch the whitespace for password
                byte[] b = encValue.getBytes("ISO-8859-1");
                if ((b.length == 1) && (b[0] == -96)) {
                  encValue = "&amp;#160;";
                }
              } catch (UnsupportedEncodingException e) {
                // ignore
              }
              if (encryptObj != null) {
                encValue =
                    (String) AccessController.doPrivileged(new EncodeAction(encValue, encryptObj));
              }
            }

            // Construct the encrypted "Value" node
            StringBuffer sb = new StringBuffer(100);
            sb.append(AttributeSchema.VALUE_BEGIN)
                .append(encValue)
                .append(AttributeSchema.VALUE_END);
            Document newDoc = SMSSchema.getXMLDocument(sb.toString(), false);
            Node newValueNode = XMLUtils.getRootNode(newDoc, SMSUtils.ATTRIBUTE_VALUE);
            // Replace the node
            Node nValueNode = doc.importNode(newValueNode, true);
            defaultNode.replaceChild(nValueNode, valueNode);
          }
        }
      }
    }
  }