This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 752c221337860873f92187b70614f3f8e30cb0f9
Author: yaohaishi <yaohai...@huawei.com>
AuthorDate: Mon Feb 12 11:28:11 2018 +0800

    SCB-338 add AccessLogItemMatcher
---
 .../accesslog/parser/AccessLogPatternParser.java   |  1 +
 .../parser/impl/DefaultAccessLogPatternParser.java | 90 ++++++++++++++++++++++
 .../parser/matcher/AccessLogItemMatcher.java       |  7 ++
 .../PercentagePrefixConfigurableMatcher.java       | 23 +++---
 .../parser/matcher/SimpleItemMatcher.java          | 63 ++++++++-------
 .../placeholder/AccessLogItemTypeEnum.java         |  1 +
 .../vertx/accesslog/AccessLogGeneratorTest.java    | 24 ++++--
 .../vertx/accesslog/impl/AccessLogHandlerTest.java | 24 ++++--
 .../impl/DefaultAccessLogPatternParserTest.java    | 78 ++++++++++++++++++-
 .../PercentagePrefixConfigurableMatcherTest.java   | 11 +--
 .../parser/matcher/SimpleItemMatcherTest.java      | 29 +------
 11 files changed, 267 insertions(+), 84 deletions(-)

diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogPatternParser.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogPatternParser.java
index 6fd99a9..d29b1d0 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogPatternParser.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogPatternParser.java
@@ -21,4 +21,5 @@ import java.util.List;
 
 public interface AccessLogPatternParser {
   List<AccessLogElementExtraction> parsePattern(String rawPattern);
+  List<AccessLogItemLocation> parsePattern2(String rawPattern);
 }
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParser.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParser.java
index 5bf5b65..b2d8f08 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParser.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParser.java
@@ -24,8 +24,12 @@ import java.util.List;
 
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.PlainTextElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogPatternParser;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.AccessLogElementMatcher;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.AccessLogItemMatcher;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.PercentagePrefixConfigurableMatcher;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.SimpleItemMatcher;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl.BytesWrittenV1Matcher;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl.BytesWrittenV2Matcher;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl.CookieElementMatcher;
@@ -46,12 +50,98 @@ import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl.UriPathIncludeQueryMatcher;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl.UriPathOnlyMatcher;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher.impl.VersionOrProtocolMatcher;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * contains all kinds of {@link AccessLogElementMatcher},
  * generate a chain of {@link AccessLogElementExtraction} according to the raw 
access log pattern.
  */
 public class DefaultAccessLogPatternParser implements AccessLogPatternParser {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultAccessLogPatternParser.class);
+
+  private static final List<AccessLogItemMatcher> matcherList = Arrays.asList(
+      new SimpleItemMatcher(), new PercentagePrefixConfigurableMatcher()
+  );
+
+  @Override
+  public List<AccessLogItemLocation> parsePattern2(String rawPattern) {
+    LOGGER.info("parse access log pattern: [{}]", rawPattern);
+    List<AccessLogItemLocation> locationList = new ArrayList<>();
+    for (int i = 0; i < rawPattern.length(); ) {
+      AccessLogItemLocation location = match(rawPattern, i);
+      if (null == location) {
+        break;
+      }
+
+      locationList.add(location);
+      i = location.getEnd();
+    }
+
+    checkLocationList(rawPattern, locationList);
+
+    locationList = fillInTextPlain(rawPattern, locationList);
+
+    return locationList;
+  }
+
+  private AccessLogItemLocation match(String rawPattern, int offset) {
+    AccessLogItemLocation result = null;
+    for (AccessLogItemMatcher matcher : matcherList) {
+      AccessLogItemLocation location = matcher.match(rawPattern, offset);
+      if ((null == result) || (null != location && location.getStart() < 
result.getStart())) {
+        // if result is null or location is nearer to offset, use location as 
result
+        result = location;
+      }
+    }
+    return result;
+  }
+
+  /**
+   * The content not matched in rawPattern will be printed as it is, so should 
be converted to {@link AccessLogItemTypeEnum#TEXT_PLAIN}
+   * @param rawPattern access log string pattern
+   * @param locationList {@link AccessLogItemLocation} list indicating the 
position of each access log item
+   */
+  private List<AccessLogItemLocation> fillInTextPlain(String rawPattern, 
List<AccessLogItemLocation> locationList) {
+    int cursor = 0;
+    List<AccessLogItemLocation> result = new ArrayList<>();
+
+    for (AccessLogItemLocation location : locationList) {
+      if (cursor == location.getStart()) {
+        result.add(location);
+      } else if (cursor < location.getStart()) {
+        result.add(new 
AccessLogItemLocation().setStart(cursor).setEnd(location.getStart()).setPlaceHolder(
+            AccessLogItemTypeEnum.TEXT_PLAIN));
+        result.add(location);
+      }
+      cursor = location.getEnd();
+    }
+
+    if (cursor < rawPattern.length()) {
+      result.add(new 
AccessLogItemLocation().setStart(cursor).setEnd(rawPattern.length())
+          .setPlaceHolder(AccessLogItemTypeEnum.TEXT_PLAIN));
+    }
+
+    return result;
+  }
+
+  private void checkLocationList(String rawPattern, 
List<AccessLogItemLocation> locationList) {
+    int preEnd = -1;
+    for (AccessLogItemLocation location : locationList) {
+      if (preEnd > location.getStart()) {
+        throw new IllegalArgumentException("access log pattern contains 
illegal placeholder, please check it.");
+      }
+
+      preEnd = location.getEnd();
+    }
+
+    if (preEnd > rawPattern.length()) {
+      throw new IllegalArgumentException("access log pattern contains illegal 
placeholder, please check it.");
+    }
+  }
+
+
   private static final List<AccessLogElementMatcher> MATCHER_LIST = 
Arrays.asList(
       new RequestHeaderElementMatcher(),
       new DatetimeConfigurableMatcher(),
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/AccessLogItemMatcher.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/AccessLogItemMatcher.java
index 91bf0e4..711aa7c 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/AccessLogItemMatcher.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/AccessLogItemMatcher.java
@@ -3,5 +3,12 @@ package 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
 
 public interface AccessLogItemMatcher {
+  /**
+   * Return an {@link AccessLogItemLocation} which matches part of rawPattern 
and is nearest to the offset(That means
+   * the {@link AccessLogItemLocation#start} is no less than offset and is 
smallest among the potential matched Item).
+   * @param rawPattern
+   * @param offset
+   * @return
+   */
   AccessLogItemLocation match(String rawPattern, int offset);
 }
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcher.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcher.java
index ac9520a..95c7acf 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcher.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcher.java
@@ -11,31 +11,32 @@ public class PercentagePrefixConfigurableMatcher implements 
AccessLogItemMatcher
 
   public static final String GENERAL_PREFIX = "%{";
 
-  private static final Map<String, AccessLogItemTypeEnum> 
suffixPlaceholderEnumMap = new LinkedHashMap<>();
+  private static final Map<String, AccessLogItemTypeEnum> 
SUFFIX_PLACEHOLDER_ENUM_MAP = new LinkedHashMap<>();
 
   public static final String SUFFIX_HEAD = "}";
 
   static {
-    suffixPlaceholderEnumMap.put("}t", 
AccessLogItemTypeEnum.DATETIME_CONFIGURABLE);
-    suffixPlaceholderEnumMap.put("}i", AccessLogItemTypeEnum.REQUEST_HEADER);
-    suffixPlaceholderEnumMap.put("}o", AccessLogItemTypeEnum.RESPONSE_HEADER);
-    suffixPlaceholderEnumMap.put("}C", AccessLogItemTypeEnum.COOKIE);
+    SUFFIX_PLACEHOLDER_ENUM_MAP.put("}t", 
AccessLogItemTypeEnum.DATETIME_CONFIGURABLE);
+    SUFFIX_PLACEHOLDER_ENUM_MAP.put("}i", 
AccessLogItemTypeEnum.REQUEST_HEADER);
+    SUFFIX_PLACEHOLDER_ENUM_MAP.put("}o", 
AccessLogItemTypeEnum.RESPONSE_HEADER);
+    SUFFIX_PLACEHOLDER_ENUM_MAP.put("}C", AccessLogItemTypeEnum.COOKIE);
   }
 
   @Override
   public AccessLogItemLocation match(String rawPattern, int offset) {
-    if (!rawPattern.startsWith(GENERAL_PREFIX, offset)) {
+    int begin = rawPattern.indexOf(GENERAL_PREFIX, offset);
+    if (begin < 0) {
       return null;
     }
 
-    int index = rawPattern.indexOf(SUFFIX_HEAD, offset);
-    if (index < 0) {
+    int end = rawPattern.indexOf(SUFFIX_HEAD, begin);
+    if (end < 0) {
       return null;
     }
 
-    for (Entry<String, AccessLogItemTypeEnum> entry : 
suffixPlaceholderEnumMap.entrySet()) {
-      if (rawPattern.startsWith(entry.getKey(), index)) {
-        return new AccessLogItemLocation().setStart(offset).setEnd(index + 
entry.getKey().length())
+    for (Entry<String, AccessLogItemTypeEnum> entry : 
SUFFIX_PLACEHOLDER_ENUM_MAP.entrySet()) {
+      if (rawPattern.startsWith(entry.getKey(), end)) {
+        return new AccessLogItemLocation().setStart(begin).setEnd(end + 
entry.getKey().length())
             .setPlaceHolder(entry.getValue());
       }
     }
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcher.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcher.java
index a82606e..57dfca3 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcher.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcher.java
@@ -8,40 +8,51 @@ import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogIte
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
 
 public class SimpleItemMatcher implements AccessLogItemMatcher {
-  private static final Map<String, AccessLogItemTypeEnum> placeholderEnumMap = 
new LinkedHashMap<String, AccessLogItemTypeEnum>();
+  private static final Map<String, AccessLogItemTypeEnum> PLACEHOLDER_ENUM_MAP 
= new LinkedHashMap<String, AccessLogItemTypeEnum>();
 
   static {
-    placeholderEnumMap.put("%m", AccessLogItemTypeEnum.HTTP_METHOD);
-    placeholderEnumMap.put("cs-method", AccessLogItemTypeEnum.HTTP_METHOD);
-    placeholderEnumMap.put("%s", AccessLogItemTypeEnum.HTTP_STATUS);
-    placeholderEnumMap.put("sc-status", AccessLogItemTypeEnum.HTTP_STATUS);
-    placeholderEnumMap.put("%T", AccessLogItemTypeEnum.DURATION_IN_SECOND);
-    placeholderEnumMap.put("%D", 
AccessLogItemTypeEnum.DURATION_IN_MILLISECOND);
-    placeholderEnumMap.put("%h", AccessLogItemTypeEnum.REMOTE_HOSTNAME);
-    placeholderEnumMap.put("%v", AccessLogItemTypeEnum.LOCAL_HOSTNAME);
-    placeholderEnumMap.put("%p", AccessLogItemTypeEnum.LOCAL_PORT);
-    placeholderEnumMap.put("%B", AccessLogItemTypeEnum.RESPONSE_SIZE);
-    placeholderEnumMap.put("%b", AccessLogItemTypeEnum.RESPONSE_SIZE_CLF);
-    placeholderEnumMap.put("%r", AccessLogItemTypeEnum.FIRST_LINE_OF_REQUEST);
-    placeholderEnumMap.put("%U", AccessLogItemTypeEnum.URL_PATH);
-    placeholderEnumMap.put("cs-uri-stem", AccessLogItemTypeEnum.URL_PATH);
-    placeholderEnumMap.put("%q", AccessLogItemTypeEnum.QUERY_STRING);
-    placeholderEnumMap.put("cs-uri-query", AccessLogItemTypeEnum.QUERY_STRING);
-    placeholderEnumMap.put("cs-uri", 
AccessLogItemTypeEnum.URL_PATH_WITH_QUERY);
-    placeholderEnumMap.put("%H", AccessLogItemTypeEnum.REQUEST_PROTOCOL);
-    placeholderEnumMap.put("%t", AccessLogItemTypeEnum.DATETIME_DEFAULT);
-    placeholderEnumMap.put("%SCB-traceId", AccessLogItemTypeEnum.SCB_TRACE_ID);
+    PLACEHOLDER_ENUM_MAP.put("%m", AccessLogItemTypeEnum.HTTP_METHOD);
+    PLACEHOLDER_ENUM_MAP.put("cs-method", AccessLogItemTypeEnum.HTTP_METHOD);
+    PLACEHOLDER_ENUM_MAP.put("%s", AccessLogItemTypeEnum.HTTP_STATUS);
+    PLACEHOLDER_ENUM_MAP.put("sc-status", AccessLogItemTypeEnum.HTTP_STATUS);
+    PLACEHOLDER_ENUM_MAP.put("%T", AccessLogItemTypeEnum.DURATION_IN_SECOND);
+    PLACEHOLDER_ENUM_MAP.put("%D", 
AccessLogItemTypeEnum.DURATION_IN_MILLISECOND);
+    PLACEHOLDER_ENUM_MAP.put("%h", AccessLogItemTypeEnum.REMOTE_HOSTNAME);
+    PLACEHOLDER_ENUM_MAP.put("%v", AccessLogItemTypeEnum.LOCAL_HOSTNAME);
+    PLACEHOLDER_ENUM_MAP.put("%p", AccessLogItemTypeEnum.LOCAL_PORT);
+    PLACEHOLDER_ENUM_MAP.put("%B", AccessLogItemTypeEnum.RESPONSE_SIZE);
+    PLACEHOLDER_ENUM_MAP.put("%b", AccessLogItemTypeEnum.RESPONSE_SIZE_CLF);
+    PLACEHOLDER_ENUM_MAP.put("%r", 
AccessLogItemTypeEnum.FIRST_LINE_OF_REQUEST);
+    PLACEHOLDER_ENUM_MAP.put("%U", AccessLogItemTypeEnum.URL_PATH);
+    PLACEHOLDER_ENUM_MAP.put("cs-uri-stem", AccessLogItemTypeEnum.URL_PATH);
+    PLACEHOLDER_ENUM_MAP.put("%q", AccessLogItemTypeEnum.QUERY_STRING);
+    PLACEHOLDER_ENUM_MAP.put("cs-uri-query", 
AccessLogItemTypeEnum.QUERY_STRING);
+    PLACEHOLDER_ENUM_MAP.put("cs-uri", 
AccessLogItemTypeEnum.URL_PATH_WITH_QUERY);
+    PLACEHOLDER_ENUM_MAP.put("%H", AccessLogItemTypeEnum.REQUEST_PROTOCOL);
+    PLACEHOLDER_ENUM_MAP.put("%t", AccessLogItemTypeEnum.DATETIME_DEFAULT);
+    PLACEHOLDER_ENUM_MAP.put("%SCB-traceId", 
AccessLogItemTypeEnum.SCB_TRACE_ID);
   }
 
   @Override
   public AccessLogItemLocation match(String rawPattern, int offset) {
-    for (Entry<String, AccessLogItemTypeEnum> entry : 
placeholderEnumMap.entrySet()) {
-      if (rawPattern.startsWith(entry.getKey(), offset)) {
-        return new AccessLogItemLocation().setStart(offset).setEnd(offset + 
entry.getKey().length())
-            .setPlaceHolder(entry.getValue());
+    int start = -1;
+    Entry<String, AccessLogItemTypeEnum> nearestEntry = null;
+    for (Entry<String, AccessLogItemTypeEnum> entry : 
PLACEHOLDER_ENUM_MAP.entrySet()) {
+      int cursor = rawPattern.indexOf(entry.getKey(), offset);
+      if (cursor < 0) {
+        continue;
       }
+      if (start < 0 || cursor < start) {
+        start = cursor;
+        nearestEntry = entry;
+      }
+    }
+
+    if (null == nearestEntry) {
+      return null;
     }
 
-    return null;
+    return new AccessLogItemLocation().setStart(start).setEnd(start + 
nearestEntry.getKey().length())
+        .setPlaceHolder(nearestEntry.getValue());
   }
 }
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/placeholder/AccessLogItemTypeEnum.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/placeholder/AccessLogItemTypeEnum.java
index 0784edc..e0189f8 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/placeholder/AccessLogItemTypeEnum.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/placeholder/AccessLogItemTypeEnum.java
@@ -4,6 +4,7 @@ package 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder;
  * record what kinds of access log item we support
  */
 public enum AccessLogItemTypeEnum {
+  TEXT_PLAIN,
   // %m, cs-method
   HTTP_METHOD,
   // %s, sc-status
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
index f7747fe..b05983c 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
@@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals;
 
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.List;
 import java.util.TimeZone;
 
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.AccessLogElement;
@@ -11,6 +12,8 @@ import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.Dateti
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.MethodElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.PlainTextElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogPatternParser;
 import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -28,12 +31,21 @@ public class AccessLogGeneratorTest {
 
   private static final AccessLogElement plainTextElement = new 
PlainTextElement(" - ");
 
-  private static final AccessLogGenerator ACCESS_LOG_GENERATOR = new 
AccessLogGenerator("rawPattern", s -> {
-    assertEquals("rawPattern", s);
-    return Arrays.asList(new 
AccessLogElementExtraction().setAccessLogElement(methodElement),
-        new AccessLogElementExtraction().setAccessLogElement(plainTextElement),
-        new AccessLogElementExtraction().setAccessLogElement(datetimeElement));
-  });
+  private static final AccessLogGenerator ACCESS_LOG_GENERATOR = new 
AccessLogGenerator("rawPattern",
+      new AccessLogPatternParser() {
+        @Override
+        public List<AccessLogElementExtraction> parsePattern(String 
rawPattern) {
+          assertEquals("rawPattern", rawPattern);
+          return Arrays.asList(new 
AccessLogElementExtraction().setAccessLogElement(methodElement),
+              new 
AccessLogElementExtraction().setAccessLogElement(plainTextElement),
+              new 
AccessLogElementExtraction().setAccessLogElement(datetimeElement));
+        }
+
+        @Override
+        public List<AccessLogItemLocation> parsePattern2(String rawPattern) {
+          return null;
+        }
+      });
 
   @Test
   public void testConstructor() {
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
index d907a15..7c41adf 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
@@ -20,12 +20,15 @@ package 
org.apache.servicecomb.transport.rest.vertx.accesslog.impl;
 import static org.junit.Assert.assertEquals;
 
 import java.util.Arrays;
+import java.util.List;
 
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.AccessLogElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.DatetimeConfigurableElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.MethodElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.PlainTextElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogPatternParser;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.slf4j.Logger;
@@ -42,12 +45,21 @@ public class AccessLogHandlerTest {
 
   private static final Logger logger = Mockito.mock(Logger.class);
 
-  private static final AccessLogHandler ACCESS_LOG_HANDLER = new 
AccessLogHandler("rawPattern", s -> {
-    assertEquals("rawPattern", s);
-    return Arrays.asList(new 
AccessLogElementExtraction().setAccessLogElement(methodElement),
-        new AccessLogElementExtraction().setAccessLogElement(plainTextElement),
-        new AccessLogElementExtraction().setAccessLogElement(datetimeElement));
-  });
+  private static final AccessLogHandler ACCESS_LOG_HANDLER = new 
AccessLogHandler("rawPattern",
+      new AccessLogPatternParser() {
+        @Override
+        public List<AccessLogElementExtraction> parsePattern(String 
rawPattern) {
+          assertEquals("rawPattern", rawPattern);
+          return Arrays.asList(new 
AccessLogElementExtraction().setAccessLogElement(methodElement),
+              new 
AccessLogElementExtraction().setAccessLogElement(plainTextElement),
+              new 
AccessLogElementExtraction().setAccessLogElement(datetimeElement));
+        }
+
+        @Override
+        public List<AccessLogItemLocation> parsePattern2(String rawPattern) {
+          return null;
+        }
+      });
 
   @Test
   public void handle() {
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParserTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParserTest.java
index 86f9942..1d12880 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParserTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/impl/DefaultAccessLogPatternParserTest.java
@@ -47,6 +47,8 @@ import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.UriPat
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.UriPathOnlyElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.VersionOrProtocolElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
 import org.hamcrest.Matchers;
 import org.junit.Test;
 
@@ -58,7 +60,7 @@ public class DefaultAccessLogPatternParserTest {
       + "%{yyyy MM dd HH:mm:ss|GMT+0|en-US}t"
       + "%{incoming-header}i"
       + "%{outgoing-header}o"
-      + "%{cookie}c"
+      + "%{cookie}C"
       + "%SCB-traceId";
 
   private static DefaultAccessLogPatternParser accessLogPatternParser = new 
DefaultAccessLogPatternParser();
@@ -66,6 +68,44 @@ public class DefaultAccessLogPatternParserTest {
   @Test
   @SuppressWarnings(value = "unchecked")
   public void testParsePattern() {
+    List<AccessLogItemLocation> result = 
accessLogPatternParser.parsePattern2(ROW_PATTERN);
+    assertEquals(27, result.size());
+
+    assertThat(result.stream().map(AccessLogItemLocation::getPlaceHolder)
+            .filter(Objects::nonNull).collect(Collectors.toList()),
+        Matchers.contains(
+            AccessLogItemTypeEnum.TEXT_PLAIN,
+            AccessLogItemTypeEnum.HTTP_METHOD,
+            AccessLogItemTypeEnum.TEXT_PLAIN,
+            AccessLogItemTypeEnum.HTTP_METHOD,
+            AccessLogItemTypeEnum.TEXT_PLAIN,
+            AccessLogItemTypeEnum.HTTP_STATUS,
+            AccessLogItemTypeEnum.DURATION_IN_SECOND,
+            AccessLogItemTypeEnum.DURATION_IN_MILLISECOND,
+            AccessLogItemTypeEnum.REMOTE_HOSTNAME,
+            AccessLogItemTypeEnum.LOCAL_HOSTNAME,
+            AccessLogItemTypeEnum.LOCAL_PORT,
+            AccessLogItemTypeEnum.RESPONSE_SIZE,
+            AccessLogItemTypeEnum.RESPONSE_SIZE_CLF,
+            AccessLogItemTypeEnum.FIRST_LINE_OF_REQUEST,
+            AccessLogItemTypeEnum.URL_PATH,
+            AccessLogItemTypeEnum.QUERY_STRING,
+            AccessLogItemTypeEnum.URL_PATH,
+            AccessLogItemTypeEnum.QUERY_STRING,
+            AccessLogItemTypeEnum.URL_PATH_WITH_QUERY,
+            AccessLogItemTypeEnum.REQUEST_PROTOCOL,
+            AccessLogItemTypeEnum.DATETIME_DEFAULT,
+            AccessLogItemTypeEnum.DATETIME_CONFIGURABLE,
+            AccessLogItemTypeEnum.DATETIME_CONFIGURABLE,
+            AccessLogItemTypeEnum.REQUEST_HEADER,
+            AccessLogItemTypeEnum.RESPONSE_HEADER,
+            AccessLogItemTypeEnum.COOKIE,
+            AccessLogItemTypeEnum.SCB_TRACE_ID));
+  }
+
+  @Test
+  @SuppressWarnings(value = "unchecked")
+  public void testParsePattern2() {
     List<AccessLogElementExtraction> result = 
accessLogPatternParser.parsePattern(ROW_PATTERN);
     assertEquals(27, result.size());
 
@@ -98,11 +138,45 @@ public class DefaultAccessLogPatternParserTest {
             DatetimeConfigurableElement.class,
             RequestHeaderElement.class,
             ResponseHeaderElement.class,
-            CookieElement.class,
+            PlainTextElement.class,
             TraceIdElement.class));
   }
 
   @Test
+  public void testCheckLocationList() {
+    List<AccessLogItemLocation> locationList = new ArrayList<>(3);
+    locationList.add(new AccessLogItemLocation().setStart(0).setEnd(3));
+    locationList.add(new AccessLogItemLocation().setStart(3).setEnd(6));
+    locationList.add(new AccessLogItemLocation().setStart(5).setEnd(9));
+
+    try {
+      Deencapsulation.invoke(new DefaultAccessLogPatternParser(), 
"checkLocationList",
+          "0123456789", locationList);
+      fail("expect an exception");
+    } catch (Exception e) {
+      assertEquals(IllegalArgumentException.class, e.getClass());
+      assertEquals("access log pattern contains illegal placeholder, please 
check it.", e.getMessage());
+    }
+  }
+
+  @Test
+  public void testCheckLocationListOnLocationEndGreaterThanPatternLength() {
+    List<AccessLogItemLocation> locationList = new ArrayList<>(3);
+    locationList.add(new AccessLogItemLocation().setStart(0).setEnd(3));
+    locationList.add(new AccessLogItemLocation().setStart(3).setEnd(6));
+    locationList.add(new AccessLogItemLocation().setStart(7).setEnd(9));
+
+    try {
+      Deencapsulation.invoke(new DefaultAccessLogPatternParser(), 
"checkLocationList",
+          "0123456", locationList);
+      fail("expect an exception");
+    } catch (Exception e) {
+      assertEquals(IllegalArgumentException.class, e.getClass());
+      assertEquals("access log pattern contains illegal placeholder, please 
check it.", e.getMessage());
+    }
+  }
+
+  @Test
   public void testCheckExtractionList() {
     List<AccessLogElementExtraction> extractionList = new ArrayList<>(3);
     extractionList.add(new AccessLogElementExtraction().setStart(0).setEnd(3));
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcherTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcherTest.java
index 6f33797..1efc88f 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcherTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcherTest.java
@@ -22,7 +22,7 @@ public class PercentagePrefixConfigurableMatcherTest {
             .setEnd(11)
             .setPlaceHolder(AccessLogItemTypeEnum.DATETIME_CONFIGURABLE));
 
-    location = MATCHER.match(TEST_RAW_PATTERN, 12);
+    location = MATCHER.match(TEST_RAW_PATTERN, 10);
     Assert.assertEquals(
         location,
         new AccessLogItemLocation()
@@ -30,13 +30,8 @@ public class PercentagePrefixConfigurableMatcherTest {
             .setEnd(28)
             .setPlaceHolder(AccessLogItemTypeEnum.COOKIE));
 
-    location = MATCHER.match(TEST_RAW_PATTERN, 29);
-    Assert.assertEquals(
-        location,
-        new AccessLogItemLocation()
-            .setStart(29)
-            .setEnd(45)
-            .setPlaceHolder(AccessLogItemTypeEnum.DATETIME_CONFIGURABLE));
+    location = MATCHER.match(TEST_RAW_PATTERN, 30);
+    Assert.assertNull(location);
   }
 
   @Test
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcherTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcherTest.java
index 4c3659e..8497eca 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcherTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcherTest.java
@@ -10,7 +10,7 @@ public class SimpleItemMatcherTest {
 
   private static final SimpleItemMatcher MATCHER = new SimpleItemMatcher();
 
-  public static final String PATTERN = AccessLogConfiguration.DEFAULT_PATTERN;
+  public static final String PATTERN = "%h - - %t %r %s %B";
 
   @Test
   public void testMatch() {
@@ -22,7 +22,7 @@ public class SimpleItemMatcherTest {
             .setEnd(2)
             .setPlaceHolder(AccessLogItemTypeEnum.REMOTE_HOSTNAME));
 
-    location = MATCHER.match(PATTERN, 7);
+    location = MATCHER.match(PATTERN, 3);
     Assert.assertEquals(
         location,
         new AccessLogItemLocation()
@@ -30,29 +30,8 @@ public class SimpleItemMatcherTest {
             .setEnd(9)
             .setPlaceHolder(AccessLogItemTypeEnum.DATETIME_DEFAULT));
 
-    location = MATCHER.match(PATTERN, 10);
-    Assert.assertEquals(
-        location,
-        new AccessLogItemLocation()
-            .setStart(10)
-            .setEnd(12)
-            .setPlaceHolder(AccessLogItemTypeEnum.FIRST_LINE_OF_REQUEST));
-
-    location = MATCHER.match(PATTERN, 13);
-    Assert.assertEquals(
-        location,
-        new AccessLogItemLocation()
-            .setStart(13)
-            .setEnd(15)
-            .setPlaceHolder(AccessLogItemTypeEnum.HTTP_STATUS));
-
-    location = MATCHER.match(PATTERN, 16);
-    Assert.assertEquals(
-        location,
-        new AccessLogItemLocation()
-            .setStart(16)
-            .setEnd(18)
-            .setPlaceHolder(AccessLogItemTypeEnum.RESPONSE_SIZE));
+    location = MATCHER.match(PATTERN, 17);
+    Assert.assertNull(location);
   }
 
   @Test

-- 
To stop receiving notification emails like this one, please contact
ningji...@apache.org.

Reply via email to