You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by co...@apache.org on 2016/10/10 12:09:22 UTC

svn commit: r1764070 - in /webservices/wss4j/branches/2_1_x-fixes: ws-security-common/src/main/java/org/apache/wss4j/common/util/ ws-security-dom/src/main/java/org/apache/wss4j/dom/message/token/ ws-security-dom/src/main/java/org/apache/wss4j/dom/util/

Author: coheigea
Date: Mon Oct 10 12:09:22 2016
New Revision: 1764070

URL: http://svn.apache.org/viewvc?rev=1764070&view=rev
Log:
Minor cleanup code

Modified:
    webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/AttachmentUtils.java
    webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/InetAddressUtils.java
    webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/XMLUtils.java
    webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/token/DerivedKeyToken.java
    webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/EncryptionUtils.java
    webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/XmlSchemaDateFormat.java

Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/AttachmentUtils.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/AttachmentUtils.java?rev=1764070&r1=1764069&r2=1764070&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/AttachmentUtils.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/AttachmentUtils.java Mon Oct 10 12:09:22 2016
@@ -185,8 +185,8 @@ public final class AttachmentUtils {
     public static String unfoldWhitespace(String text) {
         int count = 0;
         char[] chars = text.toCharArray();
-        for (int i = 0; i < chars.length; i++) {
-            if (SPACE != chars[i] && HTAB != chars[i]) {
+        for (char character : chars) {
+            if (SPACE != character && HTAB != character) {
                 break;
             }
             count++;
@@ -195,15 +195,16 @@ public final class AttachmentUtils {
     }
 
     //removes any CRLF followed by a whitespace
-    public static String unfold(String text) {
+    public static String unfold(final String text) {
 
-        if (text.length() < 3) {
+        int length = text.length();
+        if (length < 3) {
             return text;
         }
 
         StringBuilder stringBuilder = new StringBuilder();
 
-        for (int i = 0; i < text.length() - 2; i++) {
+        for (int i = 0; i < length - 2; i++) {
             char ch1 = text.charAt(i);
             final char ch2 = text.charAt(i + 1);
             final char ch3 = text.charAt(i + 2);
@@ -211,15 +212,15 @@ public final class AttachmentUtils {
             if (CARRIAGE_RETURN == ch1 && LINEFEED == ch2 && (SPACE == ch3 || HTAB == ch3)) {
 
                 i += 2;
-                if (i >= text.length() - 3) {
-                    for (i++; i < text.length(); i++) { //NOPMD
+                if (i >= length - 3) {
+                    for (i++; i < length; i++) { //NOPMD
                         stringBuilder.append(text.charAt(i));
                     }
                 }
                 continue;
             }
             stringBuilder.append(ch1);
-            if (i == text.length() - 3) {
+            if (i == length - 3) {
                 stringBuilder.append(ch2);
                 stringBuilder.append(ch3);
             }
@@ -352,9 +353,10 @@ public final class AttachmentUtils {
         }
     }
 
-    public static String unquoteInnerText(String text) {
+    public static String unquoteInnerText(final String text) {
         StringBuilder stringBuilder = new StringBuilder();
-        for (int i = 0; i < text.length() - 1; i++) {
+        int length = text.length();
+        for (int i = 0; i < length - 1; i++) {
             char c = text.charAt(i);
             char c1 = text.charAt(i + 1);
             if (i == 0 && DOUBLE_QUOTE == c) {
@@ -362,7 +364,7 @@ public final class AttachmentUtils {
                 continue;
             }
             if (BACKSLASH == c && (DOUBLE_QUOTE == c1 || BACKSLASH == c1)) {
-                if (i != 0 && i != text.length() - 2) {
+                if (i != 0 && i != length - 2) {
                     stringBuilder.append(c);
                 }
                 stringBuilder.append(c1);
@@ -375,7 +377,7 @@ public final class AttachmentUtils {
                 i++;
             } else {
                 stringBuilder.append(c);
-                if (i == text.length() - 2 && DOUBLE_QUOTE == c1) {
+                if (i == length - 2 && DOUBLE_QUOTE == c1) {
                     stringBuilder.append(c1);
                 }
             }
@@ -386,17 +388,18 @@ public final class AttachmentUtils {
     /*
      * Removes any comment outside quoted text. Comments are enclosed between ()
      */
-    public static String uncomment(String text) {
+    public static String uncomment(final String text) {
         StringBuilder stringBuilder = new StringBuilder();
 
         int inComment = 0;
+        int length = text.length();
         outer:
-        for (int i = 0; i < text.length(); i++) {
+        for (int i = 0; i < length; i++) {
             char ch = text.charAt(i);
 
             if (DOUBLE_QUOTE == ch) {
                 stringBuilder.append(ch);
-                for (i++; i < text.length(); i++) { //NOPMD
+                for (i++; i < length; i++) { //NOPMD
                     ch = text.charAt(i);
                     stringBuilder.append(ch);
                     if (DOUBLE_QUOTE == ch) {
@@ -406,7 +409,7 @@ public final class AttachmentUtils {
             }
             if (LEFT_PARENTHESIS == ch) {
                 inComment++;
-                for (i++; i < text.length(); i++) { //NOPMD
+                for (i++; i < length; i++) { //NOPMD
                     ch = text.charAt(i);
                     if (LEFT_PARENTHESIS == ch) {
                         inComment++;

Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/InetAddressUtils.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/InetAddressUtils.java?rev=1764070&r1=1764069&r2=1764070&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/InetAddressUtils.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/InetAddressUtils.java Mon Oct 10 12:09:22 2016
@@ -98,7 +98,8 @@ public final class InetAddressUtils {
      */
     public static boolean isIPv6HexCompressedAddress(final String input) {
         int colonCount = 0;
-        for (int i = 0; i < input.length(); i++) {
+        int length = input.length();
+        for (int i = 0; i < length; i++) {
             if (input.charAt(i) == COLON_CHAR) {
                 colonCount++;
             }

Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/XMLUtils.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/XMLUtils.java?rev=1764070&r1=1764069&r2=1764070&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/XMLUtils.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-common/src/main/java/org/apache/wss4j/common/util/XMLUtils.java Mon Oct 10 12:09:22 2016
@@ -207,7 +207,8 @@ public final class XMLUtils {
     public static String getPrefixNS(String uri, Node e) {
         while (e != null && e.getNodeType() == Element.ELEMENT_NODE) {
             NamedNodeMap attrs = e.getAttributes();
-            for (int n = 0; n < attrs.getLength(); n++) {
+            int length = attrs.getLength();
+            for (int n = 0; n < length; n++) {
                 Attr a = (Attr) attrs.item(n);
                 String name = a.getName();
                 if (name.startsWith("xmlns:") && a.getNodeValue().equals(uri)) {

Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/token/DerivedKeyToken.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/token/DerivedKeyToken.java?rev=1764070&r1=1764069&r2=1764070&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/token/DerivedKeyToken.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/message/token/DerivedKeyToken.java Mon Oct 10 12:09:22 2016
@@ -19,7 +19,6 @@
 
 package org.apache.wss4j.dom.message.token;
 
-import java.nio.charset.StandardCharsets;
 import java.security.Principal;
 import java.util.HashMap;
 import java.util.Map;
@@ -31,8 +30,7 @@ import org.apache.wss4j.dom.WSConstants;
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.common.bsp.BSPEnforcer;
 import org.apache.wss4j.common.derivedKey.ConversationConstants;
-import org.apache.wss4j.common.derivedKey.AlgoFactory;
-import org.apache.wss4j.common.derivedKey.DerivationAlgorithm;
+import org.apache.wss4j.common.derivedKey.DerivedKeyUtils;
 import org.apache.wss4j.common.principal.WSDerivedKeyTokenPrincipal;
 import org.apache.wss4j.common.token.SecurityTokenReference;
 import org.apache.wss4j.common.util.DOM2Writer;
@@ -543,28 +541,8 @@ public class DerivedKeyToken {
      */
     public byte[] deriveKey(int length, byte[] secret) throws WSSecurityException {
         try {
-            DerivationAlgorithm algo = AlgoFactory.getInstance(getAlgorithm());
-            byte[] labelBytes = null;
-            String label = getLabel();
-            if (label == null || label.length() == 0) {
-                String defaultLabel = ConversationConstants.DEFAULT_LABEL
-                    + ConversationConstants.DEFAULT_LABEL;
-                labelBytes = defaultLabel.getBytes(StandardCharsets.UTF_8);
-            } else {
-                labelBytes = label.getBytes(StandardCharsets.UTF_8);
-            }
-
             byte[] nonce = Base64.decode(getNonce());
-            byte[] seed = new byte[labelBytes.length + nonce.length];
-            System.arraycopy(labelBytes, 0, seed, 0, labelBytes.length);
-            System.arraycopy(nonce, 0, seed, labelBytes.length, nonce.length);
-
-            int keyLength = length;
-            if (keyLength <= 0) {
-                keyLength = getLength();
-            }
-            return algo.createKey(secret, seed, getOffset(), keyLength);
-
+            return DerivedKeyUtils.deriveKey(getAlgorithm(), getLabel(), length, secret, nonce, getOffset());
         } catch (Exception e) {
             throw new WSSecurityException(
                 WSSecurityException.ErrorCode.FAILURE, e

Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/EncryptionUtils.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/EncryptionUtils.java?rev=1764070&r1=1764069&r2=1764070&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/EncryptionUtils.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/EncryptionUtils.java Mon Oct 10 12:09:22 2016
@@ -435,7 +435,8 @@ public final class EncryptionUtils {
             && !(Node.DOCUMENT_NODE == parent.getParentNode().getNodeType())) {
             parent = parent.getParentNode();
             NamedNodeMap attributes = parent.getAttributes();
-            for (int i = 0; i < attributes.getLength(); i++) {
+            int length = attributes.getLength();
+            for (int i = 0; i < length; i++) {
                 Node attribute = attributes.item(i);
                 String attrDef = "xmlns:" + attribute.getLocalName();
                 if (WSConstants.XMLNS_NS.equals(attribute.getNamespaceURI()) && !prefix.toString().contains(attrDef)) {

Modified: webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/XmlSchemaDateFormat.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/XmlSchemaDateFormat.java?rev=1764070&r1=1764069&r2=1764070&view=diff
==============================================================================
--- webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/XmlSchemaDateFormat.java (original)
+++ webservices/wss4j/branches/2_1_x-fixes/ws-security-dom/src/main/java/org/apache/wss4j/dom/util/XmlSchemaDateFormat.java Mon Oct 10 12:09:22 2016
@@ -99,11 +99,12 @@ public class XmlSchemaDateFormat extends
 
             // parse optional milliseconds
             if (src != null) {
-                if (index < src.length() && src.charAt(index) == '.') {
+                int srcLength = src.length();
+                if (index < srcLength && src.charAt(index) == '.') {
                     int milliseconds = 0;
                     int start = ++index;
 
-                    while (index < src.length()
+                    while (index < srcLength
                             && Character.isDigit(src.charAt(index))) {
                         index++;
                     }
@@ -128,7 +129,7 @@ public class XmlSchemaDateFormat extends
                 }
 
                 // parse optional timezone
-                if (index + 5 < src.length()
+                if (index + 5 < srcLength
                         && (src.charAt(index) == '+' || src.charAt(index) == '-')) {
                     validateCharIsDigit(src, parsePos, index + 1, "EXPECTED_NUMERAL");
                     validateCharIsDigit(src, parsePos, index + 2, "EXPECTED_NUMERAL");
@@ -151,11 +152,11 @@ public class XmlSchemaDateFormat extends
                     index += 6;
                 }
 
-                if (index < src.length() && src.charAt(index) == 'Z') {
+                if (index < srcLength && src.charAt(index) == 'Z') {
                     index++;
                 }
 
-                if (index < src.length()) {
+                if (index < srcLength) {
                     handleParseError(parsePos, "TOO_MANY_CHARS");
                 }
             }