From 66f58163fc4828fd9c776e89fbb60ffb60be6633 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 22 Nov 2016 09:20:58 +0000
Subject: [PATCH] Code cleanups

---
 opendj-core/src/main/java/org/forgerock/opendj/io/LDAP.java                     |  262 +++++++++++++---------------------------------------
 opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LdapCodec.java        |    5 
 opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LDAPServerFilter.java |   15 --
 opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java       |    3 
 opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java     |   15 +-
 5 files changed, 80 insertions(+), 220 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/io/LDAP.java b/opendj-core/src/main/java/org/forgerock/opendj/io/LDAP.java
index 31c9248..d65d3a1 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/io/LDAP.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/io/LDAP.java
@@ -13,7 +13,6 @@
  *
  * Copyright 2013-2016 ForgeRock AS.
  */
-
 package org.forgerock.opendj.io;
 
 import java.io.IOException;
@@ -45,116 +44,70 @@
 public final class LDAP {
     // @Checkstyle:ignore AvoidNestedBlocks
 
-    /**
-     * The OID for the Kerberos V GSSAPI mechanism.
-     */
+    /** The OID for the Kerberos V GSSAPI mechanism. */
     public static final String OID_GSSAPI_KERBEROS_V = "1.2.840.113554.1.2.2";
 
-    /**
-     * The OID for the LDAP notice of disconnection extended operation.
-     */
+    /** The OID for the LDAP notice of disconnection extended operation. */
     public static final String OID_NOTICE_OF_DISCONNECTION = "1.3.6.1.4.1.1466.20036";
 
-    /**
-     * The protocol op type for abandon requests.
-     */
+    /** The protocol op type for abandon requests. */
     public static final byte OP_TYPE_ABANDON_REQUEST = 0x50;
 
-    /**
-     * The protocol op type for add requests.
-     */
+    /** The protocol op type for add requests. */
     public static final byte OP_TYPE_ADD_REQUEST = 0x68;
 
-    /**
-     * The protocol op type for add responses.
-     */
+    /** The protocol op type for add responses. */
     public static final byte OP_TYPE_ADD_RESPONSE = 0x69;
 
-    /**
-     * The protocol op type for bind requests.
-     */
+    /** The protocol op type for bind requests. */
     public static final byte OP_TYPE_BIND_REQUEST = 0x60;
 
-    /**
-     * The protocol op type for bind responses.
-     */
+    /** The protocol op type for bind responses. */
     public static final byte OP_TYPE_BIND_RESPONSE = 0x61;
 
-    /**
-     * The protocol op type for compare requests.
-     */
+    /** The protocol op type for compare requests. */
     public static final byte OP_TYPE_COMPARE_REQUEST = 0x6E;
 
-    /**
-     * The protocol op type for compare responses.
-     */
+    /** The protocol op type for compare responses. */
     public static final byte OP_TYPE_COMPARE_RESPONSE = 0x6F;
 
-    /**
-     * The protocol op type for delete requests.
-     */
+    /** The protocol op type for delete requests. */
     public static final byte OP_TYPE_DELETE_REQUEST = 0x4A;
 
-    /**
-     * The protocol op type for delete responses.
-     */
+    /** The protocol op type for delete responses. */
     public static final byte OP_TYPE_DELETE_RESPONSE = 0x6B;
 
-    /**
-     * The protocol op type for extended requests.
-     */
+    /** The protocol op type for extended requests. */
     public static final byte OP_TYPE_EXTENDED_REQUEST = 0x77;
 
-    /**
-     * The protocol op type for extended responses.
-     */
+    /** The protocol op type for extended responses. */
     public static final byte OP_TYPE_EXTENDED_RESPONSE = 0x78;
 
-    /**
-     * The protocol op type for intermediate responses.
-     */
+    /** The protocol op type for intermediate responses. */
     public static final byte OP_TYPE_INTERMEDIATE_RESPONSE = 0x79;
 
-    /**
-     * The protocol op type for modify DN requests.
-     */
+    /** The protocol op type for modify DN requests. */
     public static final byte OP_TYPE_MODIFY_DN_REQUEST = 0x6C;
 
-    /**
-     * The protocol op type for modify DN responses.
-     */
+    /** The protocol op type for modify DN responses. */
     public static final byte OP_TYPE_MODIFY_DN_RESPONSE = 0x6D;
 
-    /**
-     * The protocol op type for modify requests.
-     */
+    /** The protocol op type for modify requests. */
     public static final byte OP_TYPE_MODIFY_REQUEST = 0x66;
 
-    /**
-     * The protocol op type for modify responses.
-     */
+    /** The protocol op type for modify responses. */
     public static final byte OP_TYPE_MODIFY_RESPONSE = 0x67;
-    /**
-     * The protocol op type for search requests.
-     */
+    /** The protocol op type for search requests. */
     public static final byte OP_TYPE_SEARCH_REQUEST = 0x63;
-    /**
-     * The protocol op type for search result done elements.
-     */
+    /** The protocol op type for search result done elements. */
     public static final byte OP_TYPE_SEARCH_RESULT_DONE = 0x65;
-    /**
-     * The protocol op type for search result entries.
-     */
+    /** The protocol op type for search result entries. */
     public static final byte OP_TYPE_SEARCH_RESULT_ENTRY = 0x64;
-    /**
-     * The protocol op type for search result references.
-     */
+    /** The protocol op type for search result references. */
     public static final byte OP_TYPE_SEARCH_RESULT_REFERENCE = 0x73;
-    /**
-     * The protocol op type for unbind requests.
-     */
+    /** The protocol op type for unbind requests. */
     public static final byte OP_TYPE_UNBIND_REQUEST = 0x42;
-    /** Mapping between request protocol op and their respecetive response protocol op. */
+    /** Mapping between request protocol op and their respective response protocol op. */
     public static final byte[] OP_TO_RESULT_TYPE = new byte[0xFF];
     static {
         Arrays.fill(OP_TO_RESULT_TYPE, (byte) 0x00);
@@ -166,7 +119,7 @@
         OP_TO_RESULT_TYPE[OP_TYPE_MODIFY_DN_REQUEST] = OP_TYPE_MODIFY_DN_RESPONSE;
         OP_TO_RESULT_TYPE[OP_TYPE_MODIFY_REQUEST] = OP_TYPE_MODIFY_RESPONSE;
         OP_TO_RESULT_TYPE[OP_TYPE_SEARCH_REQUEST] = OP_TYPE_SEARCH_RESULT_DONE;
-    };
+    }
     /**
      * The BER type to use for the AuthenticationChoice element in a bind
      * request when SASL authentication is to be used.
@@ -177,117 +130,59 @@
      * request when simple authentication is to be used.
      */
     public static final byte TYPE_AUTHENTICATION_SIMPLE = (byte) 0x80;
-    /**
-     * The BER type to use for encoding the sequence of controls in an LDAP
-     * message.
-     */
+    /** The BER type to use for encoding the sequence of controls in an LDAP message. */
     public static final byte TYPE_CONTROL_SEQUENCE = (byte) 0xA0;
-    /**
-     * The BER type to use for the OID of an extended request.
-     */
+    /** The BER type to use for the OID of an extended request. */
     public static final byte TYPE_EXTENDED_REQUEST_OID = (byte) 0x80;
-    /**
-     * The BER type to use for the value of an extended request.
-     */
+    /** The BER type to use for the value of an extended request. */
     public static final byte TYPE_EXTENDED_REQUEST_VALUE = (byte) 0x81;
-    /**
-     * The BER type to use for the OID of an extended response.
-     */
+    /** The BER type to use for the OID of an extended response. */
     public static final byte TYPE_EXTENDED_RESPONSE_OID = (byte) 0x8A;
-    /**
-     * The BER type to use for the value of an extended response.
-     */
+    /** The BER type to use for the value of an extended response. */
     public static final byte TYPE_EXTENDED_RESPONSE_VALUE = (byte) 0x8B;
-    /**
-     * The BER type to use for AND filter components.
-     */
+    /** The BER type to use for AND filter components. */
     public static final byte TYPE_FILTER_AND = (byte) 0xA0;
-    /**
-     * The BER type to use for approximate filter components.
-     */
+    /** The BER type to use for approximate filter components. */
     public static final byte TYPE_FILTER_APPROXIMATE = (byte) 0xA8;
-    /**
-     * The BER type to use for equality filter components.
-     */
+    /** The BER type to use for equality filter components. */
     public static final byte TYPE_FILTER_EQUALITY = (byte) 0xA3;
-    /**
-     * The BER type to use for extensible matching filter components.
-     */
+    /** The BER type to use for extensible matching filter components. */
     public static final byte TYPE_FILTER_EXTENSIBLE_MATCH = (byte) 0xA9;
-    /**
-     * The BER type to use for greater than or equal to filter components.
-     */
+    /** The BER type to use for greater than or equal to filter components. */
     public static final byte TYPE_FILTER_GREATER_OR_EQUAL = (byte) 0xA5;
-    /**
-     * The BER type to use for less than or equal to filter components.
-     */
+    /** The BER type to use for less than or equal to filter components. */
     public static final byte TYPE_FILTER_LESS_OR_EQUAL = (byte) 0xA6;
-    /**
-     * The BER type to use for NOT filter components.
-     */
+    /** The BER type to use for NOT filter components. */
     public static final byte TYPE_FILTER_NOT = (byte) 0xA2;
-    /**
-     * The BER type to use for OR filter components.
-     */
+    /** The BER type to use for OR filter components. */
     public static final byte TYPE_FILTER_OR = (byte) 0xA1;
-    /**
-     * The BER type to use for presence filter components.
-     */
+    /** The BER type to use for presence filter components. */
     public static final byte TYPE_FILTER_PRESENCE = (byte) 0x87;
-    /**
-     * The BER type to use for substring filter components.
-     */
+    /** The BER type to use for substring filter components. */
     public static final byte TYPE_FILTER_SUBSTRING = (byte) 0xA4;
-    /**
-     * The BER type to use for the OID of an intermediate response message.
-     */
+    /** The BER type to use for the OID of an intermediate response message. */
     public static final byte TYPE_INTERMEDIATE_RESPONSE_OID = (byte) 0x80;
-    /**
-     * The BER type to use for the value of an intermediate response message.
-     */
+    /** The BER type to use for the value of an intermediate response message. */
     public static final byte TYPE_INTERMEDIATE_RESPONSE_VALUE = (byte) 0x81;
-    /**
-     * The BER type to use for the DN attributes flag in a matching rule
-     * assertion.
-     */
+    /** The BER type to use for the DN attributes flag in a matching rule assertion. */
     public static final byte TYPE_MATCHING_RULE_DN_ATTRIBUTES = (byte) 0x84;
-    /**
-     * The BER type to use for the matching rule OID in a matching rule
-     * assertion.
-     */
+    /** The BER type to use for the matching rule OID in a matching rule assertion. */
     public static final byte TYPE_MATCHING_RULE_ID = (byte) 0x81;
-    /**
-     * The BER type to use for the attribute type in a matching rule assertion.
-     */
+    /** The BER type to use for the attribute type in a matching rule assertion. */
     public static final byte TYPE_MATCHING_RULE_TYPE = (byte) 0x82;
-    /**
-     * The BER type to use for the assertion value in a matching rule assertion.
-     */
+    /** The BER type to use for the assertion value in a matching rule assertion. */
     public static final byte TYPE_MATCHING_RULE_VALUE = (byte) 0x83;
-    /**
-     * The BER type to use for the newSuperior component of a modify DN request.
-     */
+    /** The BER type to use for the newSuperior component of a modify DN request. */
     public static final byte TYPE_MODIFY_DN_NEW_SUPERIOR = (byte) 0x80;
-    /**
-     * The BER type to use for encoding the sequence of referral URLs in an
-     * LDAPResult element.
-     */
+    /** The BER type to use for encoding the sequence of referral URLs in an LDAPResult element. */
     public static final byte TYPE_REFERRAL_SEQUENCE = (byte) 0xA3;
-    /**
-     * The BER type to use for the server SASL credentials in a bind response.
-     */
+    /** The BER type to use for the server SASL credentials in a bind response. */
     public static final byte TYPE_SERVER_SASL_CREDENTIALS = (byte) 0x87;
-    /**
-     * The BER type to use for the subAny component(s) of a substring filter.
-     */
+    /** The BER type to use for the subAny component(s) of a substring filter. */
     public static final byte TYPE_SUBANY = (byte) 0x81;
-    /**
-     * The BER type to use for the subFinal components of a substring filter.
-     */
+    /** The BER type to use for the subFinal components of a substring filter. */
     public static final byte TYPE_SUBFINAL = (byte) 0x82;
-    /**
-     * The BER type to use for the subInitial component of a substring filter.
-     */
+    /** The BER type to use for the subInitial component of a substring filter. */
     public static final byte TYPE_SUBINITIAL = (byte) 0x80;
     private static final FilterVisitor<IOException, ASN1Writer> ASN1_ENCODER =
             new FilterVisitor<IOException, ASN1Writer>() {
@@ -313,29 +208,13 @@
                 @Override
                 public IOException visitApproxMatchFilter(final ASN1Writer writer,
                         final String attributeDescription, final ByteString assertionValue) {
-                    try {
-                        writer.writeStartSequence(LDAP.TYPE_FILTER_APPROXIMATE);
-                        writer.writeOctetString(attributeDescription);
-                        writer.writeOctetString(assertionValue);
-                        writer.writeEndSequence();
-                        return null;
-                    } catch (final IOException e) {
-                        return e;
-                    }
+                    return writeFilter(writer, LDAP.TYPE_FILTER_APPROXIMATE, attributeDescription, assertionValue);
                 }
 
                 @Override
                 public IOException visitEqualityMatchFilter(final ASN1Writer writer,
                         final String attributeDescription, final ByteString assertionValue) {
-                    try {
-                        writer.writeStartSequence(LDAP.TYPE_FILTER_EQUALITY);
-                        writer.writeOctetString(attributeDescription);
-                        writer.writeOctetString(assertionValue);
-                        writer.writeEndSequence();
-                        return null;
-                    } catch (final IOException e) {
-                        return e;
-                    }
+                    return writeFilter(writer, LDAP.TYPE_FILTER_EQUALITY, attributeDescription, assertionValue);
                 }
 
                 @Override
@@ -350,8 +229,7 @@
                         }
 
                         if (attributeDescription != null) {
-                            writer.writeOctetString(LDAP.TYPE_MATCHING_RULE_TYPE,
-                                    attributeDescription);
+                            writer.writeOctetString(LDAP.TYPE_MATCHING_RULE_TYPE, attributeDescription);
                         }
 
                         writer.writeOctetString(LDAP.TYPE_MATCHING_RULE_VALUE, assertionValue);
@@ -370,22 +248,19 @@
                 @Override
                 public IOException visitGreaterOrEqualFilter(final ASN1Writer writer,
                         final String attributeDescription, final ByteString assertionValue) {
-                    try {
-                        writer.writeStartSequence(LDAP.TYPE_FILTER_GREATER_OR_EQUAL);
-                        writer.writeOctetString(attributeDescription);
-                        writer.writeOctetString(assertionValue);
-                        writer.writeEndSequence();
-                        return null;
-                    } catch (final IOException e) {
-                        return e;
-                    }
+                    return writeFilter(writer, LDAP.TYPE_FILTER_GREATER_OR_EQUAL, attributeDescription, assertionValue);
                 }
 
                 @Override
                 public IOException visitLessOrEqualFilter(final ASN1Writer writer,
                         final String attributeDescription, final ByteString assertionValue) {
+                    return writeFilter(writer, LDAP.TYPE_FILTER_LESS_OR_EQUAL, attributeDescription, assertionValue);
+                }
+
+                public IOException writeFilter(final ASN1Writer writer,
+                        byte filterType, final String attributeDescription, final ByteString assertionValue) {
                     try {
-                        writer.writeStartSequence(LDAP.TYPE_FILTER_LESS_OR_EQUAL);
+                        writer.writeStartSequence(filterType);
                         writer.writeOctetString(attributeDescription);
                         writer.writeOctetString(assertionValue);
                         writer.writeEndSequence();
@@ -720,7 +595,6 @@
         } finally {
             reader.readEndSequence();
         }
-
     }
 
     private static Filter readEqualityMatchFilter(final ASN1Reader reader) throws IOException {
@@ -813,27 +687,25 @@
             final String attributeDescription = reader.readOctetStringAsString();
             reader.readStartSequence();
             try {
-                // FIXME: There should be at least one element in this substring
-                // filter sequence.
+                // FIXME: There should be at least one element in this substring filter sequence.
                 ByteString initialSubstring = null;
                 if (reader.peekType() == LDAP.TYPE_SUBINITIAL) {
                     initialSubstring = reader.readOctetString(LDAP.TYPE_SUBINITIAL);
                 }
                 final List<ByteString> anySubstrings;
-                if (reader.hasNextElement() && (reader.peekType() == LDAP.TYPE_SUBANY)) {
+                if (reader.hasNextElement() && reader.peekType() == LDAP.TYPE_SUBANY) {
                     anySubstrings = new LinkedList<>();
                     do {
                         anySubstrings.add(reader.readOctetString(LDAP.TYPE_SUBANY));
-                    } while (reader.hasNextElement() && (reader.peekType() == LDAP.TYPE_SUBANY));
+                    } while (reader.hasNextElement() && reader.peekType() == LDAP.TYPE_SUBANY);
                 } else {
                     anySubstrings = Collections.emptyList();
                 }
                 ByteString finalSubstring = null;
-                if (reader.hasNextElement() && (reader.peekType() == LDAP.TYPE_SUBFINAL)) {
+                if (reader.hasNextElement() && reader.peekType() == LDAP.TYPE_SUBFINAL) {
                     finalSubstring = reader.readOctetString(LDAP.TYPE_SUBFINAL);
                 }
-                return Filter.substrings(attributeDescription, initialSubstring, anySubstrings,
-                        finalSubstring);
+                return Filter.substrings(attributeDescription, initialSubstring, anySubstrings, finalSubstring);
             } finally {
                 reader.readEndSequence();
             }
diff --git a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java
index 8718e18..37ab80a0 100644
--- a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java
+++ b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java
@@ -40,9 +40,7 @@
 
 import static org.forgerock.opendj.ldap.LDAPConnectionFactory.*;
 
-/**
- * Common utility methods.
- */
+/** Common utility methods. */
 final class GrizzlyUtils {
     @SuppressWarnings("rawtypes")
     private static final ThreadCache.CachedTypeIndex<LDAPWriter> WRITER_INDEX = ThreadCache
@@ -53,7 +51,7 @@
 
     /**
      * Build a filter chain from the provided processor if possible and the
-     * provided filter.
+     * provided filters.
      * <p>
      * If the provided processor can't be used for building the new filter
      * chain, then a chain with only a {@code TransportFilter} is used as a base
@@ -64,11 +62,11 @@
      *            not a filter chain (for example, it can be a
      *            {@code StandaloneProcessor} then it is ignored to build the
      *            returned filter chain
-     * @param filter
-     *            filter to add at the end of the filter chain
+     * @param filters
+     *            filters to add at the end of the filter chain
      * @return a new filter chain, based on the provided processor if processor
-     *         is a {@code FilterChain}, and having the provided filter as the
-     *         last filter
+     *         is a {@code FilterChain}, and having the provided filters as the
+     *         last filters
      */
     static FilterChain buildFilterChain(Processor<?> processor, Filter... filters) {
         if (processor instanceof FilterChain) {
@@ -231,5 +229,4 @@
     private GrizzlyUtils() {
         // No implementation required.
     }
-
 }
diff --git a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LDAPServerFilter.java b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LDAPServerFilter.java
index 7849e9e..d749ddd 100644
--- a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LDAPServerFilter.java
+++ b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LDAPServerFilter.java
@@ -87,7 +87,6 @@
  * over LDAP.
  */
 final class LDAPServerFilter extends BaseFilter {
-
     private final Function<LDAPClientContext,
                            ReactiveHandler<LDAPClientContext, LdapRequestEnvelope, Stream<Response>>,
                            LdapException> connectionHandlerFactory;
@@ -127,12 +126,8 @@
     /**
      * Creates a server filter with provided listener, options and max size of ASN1 element.
      *
-     * @param listener
-     *            listen for incoming connections
      * @param options
      *            control how to decode requests and responses
-     * @param maxASN1ElementSize
-     *            The maximum BER element size, or <code>0</code> to indicate that there is no limit.
      */
     LDAPServerFilter(
             final Function<LDAPClientContext,
@@ -332,10 +327,10 @@
         @Override
         public void exceptionOccurred(final FilterChainContext ctx, final Throwable error) {
             final GrizzlyBackpressureSubscription immutableRef = downstream;
-            if (immutableRef == null) {
-                ctx.getConnection().closeSilently();
-            } else {
+            if (immutableRef != null) {
                 immutableRef.onError(error);
+            } else {
+                ctx.getConnection().closeSilently();
             }
         }
 
@@ -453,9 +448,7 @@
                 } else if ("high".equalsIgnoreCase(negStrength)) {
                     return 128;
                 }
-                /*
-                 * Treat anything else as if not security is provided and keep the server running
-                 */
+                // Treat anything else as if no security was provided and keep the server running
             }
             return ssf;
         }
diff --git a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LdapCodec.java b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LdapCodec.java
index c713819..cdc62e7 100644
--- a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LdapCodec.java
+++ b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LdapCodec.java
@@ -44,9 +44,8 @@
  * detection, a LdapCodec instance cannot be shared accross different connection
  */
 abstract class LdapCodec extends LDAPBaseFilter {
-
-    private boolean isLdapV2Pending = false;
-    private boolean isLdapV2 = false;
+    private boolean isLdapV2Pending;
+    private boolean isLdapV2;
 
     LdapCodec(final int maxElementSize, final DecodeOptions decodeOptions) {
         super(decodeOptions, maxElementSize);
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java b/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java
index ffba7fd..9f863d4 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java
@@ -387,7 +387,6 @@
         case "modify":
           return parseModifyChangeRecordEntry(entryDN, lines);
         case "modrdn":
-          return parseModifyDNChangeRecordEntry(entryDN, lines);
         case "moddn":
           return parseModifyDNChangeRecordEntry(entryDN, lines);
         default:
@@ -1508,7 +1507,7 @@
       try
       {
         builder.append("# ")
-                .append(String.valueOf(message))
+                .append(message)
                 .append(eol);
         for (final StringBuilder sb : lines)
         {

--
Gitblit v1.10.0