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 7f4351a841f1d45a4d479340cf2160c3eed4933f
Author: yaohaishi <yaohai...@huawei.com>
AuthorDate: Mon Feb 12 06:04:52 2018 +0800

    SCB-338 refactor access log item parser
---
 .../transport/rest/vertx/accesslog/README.md       | 36 +++++------
 .../accesslog/parser/AccessLogItemLocation.java    | 69 ++++++++++++++++++++++
 .../parser/matcher/AccessLogItemMatcher.java       |  7 +++
 .../PercentagePrefixConfigurableMatcher.java       | 45 ++++++++++++++
 .../parser/matcher/SimpleItemMatcher.java          | 47 +++++++++++++++
 .../placeholder/AccessLogItemTypeEnum.java         | 47 +++++++++++++++
 .../PercentagePrefixConfigurableMatcherTest.java   | 53 +++++++++++++++++
 .../parser/matcher/SimpleItemMatcherTest.java      | 63 ++++++++++++++++++++
 8 files changed, 349 insertions(+), 18 deletions(-)

diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/README.md
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/README.md
index d62c61b..ed79f41 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/README.md
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/README.md
@@ -26,26 +26,26 @@ servicecomb:
 
 | Element | Apache | W3C | Comment |
 | ----|------|------------| --------|
-| Method | %m  | cs-method | |
-| Status | %s  | sc-status | |
-| Duration s | %T  | - |  |
-| Duration ms | %D  | - | |
-| Remote Host | %h  | - |  |
-| Local Host | %v  | - |  |
+| HTTP method | %m  | cs-method | |
+| HTTP status | %s  | sc-status | |
+| Duration in second | %T  | - | The time taken to serve the request, in 
seconds |
+| Duration in millisecond | %D  | - | The time taken to serve the request, in 
millisecond |
+| Remote hostname | %h  | - | |
+| Local hostname | %v  | - |  |
 | Local port | %p  | - |  |
-| Bytes Written v1 | %B | - | Zero Bytes written as 0 |
-| Bytes Written v2 | %b | - | Zero Bytes written as - |
+| Size of response | %B | - | |
+| Size of response | %b | - | In CLF format, i.e. "-" is written if response 
size is 0 |
 | First line of request | %r  | - | |
-| URI path only | %U | cs-uri-stem | |
-| Query only | %q | cs-uri-query | |
-| URI path incl query | - | cs-uri | |
-| Version / Protocol | %H | - | |
-| Datetime Apache | %t | - | Logs by default the request timestamp using 
format 'EEE, dd MMM yyyy HH:mm:ss zzz', Locale English and Timezone GMT  |
-| Datetime Apache Configurable v1 | %{PATTERN}t | - | Specify the format 
pattern, by default it is used Locale English and Timezone GMT |
-| Datetime Apache Configurable v2 | %{PATTERN\|TIMEZONE\|LOCALE}t | - | 
Specify format pattern, timezone and locale |
-| Incoming Headers | %{IDENTIFIER}i  | - | If not found - will be logged |
-| Outgoing Response Headers | %{IDENTIFIER}o  | - | If not found - will be 
logged |
-| Cookie | %{IDENTIFIER}c  | - | If not found - will be logged |
+| URI path | %U | cs-uri-stem | |
+| Query string | %q | cs-uri-query | |
+| URI path and query string | - | cs-uri | |
+| Request protocol | %H | - | |
+| Datetime the request was received | %t | - | Write in default format, i.e. 
pattern is "EEE, dd MMM yyyy HH:mm:ss zzz", Locale is US and Timezone is GMT |
+| Configurable datetime the request was received | 
%{PATTERN\|TIMEZONE\|LOCALE}t | - | Write datetime in specified format pattern, 
timezone and locale. TIMEZONE and LOCALE can be omitted |
+| Request Header | %{VARNAME}i  | - | '-' is written if not found |
+| Response header | %{VARNAME}o  | - | '-' is written if not found |
+| Cookie | %{VARNAME}C  | - | '-' is written if not found |
+| TraceId | - | - | TraceId provided by ServiceComb´╝îlog format placeholder is 
"%SCB-traceId" |
 
 ## Access log file settings
 
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogItemLocation.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogItemLocation.java
new file mode 100644
index 0000000..4d63ef4
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/AccessLogItemLocation.java
@@ -0,0 +1,69 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.parser;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
+
+import com.google.common.base.Objects;
+
+public class AccessLogItemLocation {
+  private int start;
+
+  private int end;
+
+  private AccessLogItemTypeEnum placeHolder;
+
+  public int getStart() {
+    return start;
+  }
+
+  public AccessLogItemLocation setStart(int start) {
+    this.start = start;
+    return this;
+  }
+
+  public int getEnd() {
+    return end;
+  }
+
+  public AccessLogItemLocation setEnd(int end) {
+    this.end = end;
+    return this;
+  }
+
+  public AccessLogItemTypeEnum getPlaceHolder() {
+    return placeHolder;
+  }
+
+  public AccessLogItemLocation setPlaceHolder(AccessLogItemTypeEnum 
placeHolder) {
+    this.placeHolder = placeHolder;
+    return this;
+  }
+
+  @Override
+  public String toString() {
+    final StringBuilder sb = new StringBuilder("AccessLogItemLocation{");
+    sb.append("start=").append(start);
+    sb.append(", end=").append(end);
+    sb.append(", placeHolder=").append(placeHolder);
+    sb.append('}');
+    return sb.toString();
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || !getClass().isAssignableFrom(o.getClass())) {
+      return false;
+    }
+    AccessLogItemLocation that = (AccessLogItemLocation) o;
+    return start == that.start
+        && end == that.end
+        && placeHolder == that.placeHolder;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hashCode(start, end, placeHolder);
+  }
+}
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
new file mode 100644
index 0000000..91bf0e4
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/AccessLogItemMatcher.java
@@ -0,0 +1,7 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+
+public interface AccessLogItemMatcher {
+  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
new file mode 100644
index 0000000..ac9520a
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcher.java
@@ -0,0 +1,45 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
+
+public class PercentagePrefixConfigurableMatcher implements 
AccessLogItemMatcher {
+
+  public static final String GENERAL_PREFIX = "%{";
+
+  private static final Map<String, AccessLogItemTypeEnum> 
suffixPlaceholderEnumMap = 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);
+  }
+
+  @Override
+  public AccessLogItemLocation match(String rawPattern, int offset) {
+    if (!rawPattern.startsWith(GENERAL_PREFIX, offset)) {
+      return null;
+    }
+
+    int index = rawPattern.indexOf(SUFFIX_HEAD, offset);
+    if (index < 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())
+            .setPlaceHolder(entry.getValue());
+      }
+    }
+
+    return null;
+  }
+}
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
new file mode 100644
index 0000000..a82606e
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcher.java
@@ -0,0 +1,47 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+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>();
+
+  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);
+  }
+
+  @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());
+      }
+    }
+
+    return null;
+  }
+}
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
new file mode 100644
index 0000000..0784edc
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/placeholder/AccessLogItemTypeEnum.java
@@ -0,0 +1,47 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder;
+
+/**
+ * record what kinds of access log item we support
+ */
+public enum AccessLogItemTypeEnum {
+  // %m, cs-method
+  HTTP_METHOD,
+  // %s, sc-status
+  HTTP_STATUS,
+  // %T
+  DURATION_IN_SECOND,
+  // %D
+  DURATION_IN_MILLISECOND,
+  // %h
+  REMOTE_HOSTNAME,
+  // %v
+  LOCAL_HOSTNAME,
+  // %p
+  LOCAL_PORT,
+  // %B
+  RESPONSE_SIZE,
+  // %b
+  RESPONSE_SIZE_CLF,
+  // %r
+  FIRST_LINE_OF_REQUEST,
+  // %U, cs-uri-stem
+  URL_PATH,
+  // %q, cs-uri-query
+  QUERY_STRING,
+  // cs-uri
+  URL_PATH_WITH_QUERY,
+  //%H
+  REQUEST_PROTOCOL,
+  // %t
+  DATETIME_DEFAULT,
+  // %{PATTERN}t, %{PATTERN|TIMEZONE|LOCALE}t
+  DATETIME_CONFIGURABLE,
+  // %{VARNAME}i
+  REQUEST_HEADER,
+  // %{VARNAME}o
+  RESPONSE_HEADER,
+  // %{VARNAME}C
+  COOKIE,
+  // %SCB-traceId
+  SCB_TRACE_ID;
+}
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
new file mode 100644
index 0000000..6f33797
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/PercentagePrefixConfigurableMatcherTest.java
@@ -0,0 +1,53 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PercentagePrefixConfigurableMatcherTest {
+
+  public static final PercentagePrefixConfigurableMatcher MATCHER = new 
PercentagePrefixConfigurableMatcher();
+
+  public static final String TEST_RAW_PATTERN = "%{pattern}t %{test pattern}C 
%{test pattern}t";
+
+  @Test
+  public void testMatch() {
+    AccessLogItemLocation location;
+    location = MATCHER.match(TEST_RAW_PATTERN, 0);
+    Assert.assertEquals(
+        location,
+        new AccessLogItemLocation()
+            .setStart(0)
+            .setEnd(11)
+            .setPlaceHolder(AccessLogItemTypeEnum.DATETIME_CONFIGURABLE));
+
+    location = MATCHER.match(TEST_RAW_PATTERN, 12);
+    Assert.assertEquals(
+        location,
+        new AccessLogItemLocation()
+            .setStart(12)
+            .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));
+  }
+
+  @Test
+  public void testNotMatch() {
+    AccessLogItemLocation location = MATCHER.match("notmatch", 0);
+    Assert.assertNull(location);
+  }
+
+  @Test
+  public void testNotMatchWithPrefix() {
+    AccessLogItemLocation location = MATCHER.match("%{notmatch}x", 0);
+    Assert.assertNull(location);
+  }
+}
\ No newline at end of file
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
new file mode 100644
index 0000000..4c3659e
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/parser/matcher/SimpleItemMatcherTest.java
@@ -0,0 +1,63 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog.parser.matcher;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogConfiguration;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogItemLocation;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.placeholder.AccessLogItemTypeEnum;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SimpleItemMatcherTest {
+
+  private static final SimpleItemMatcher MATCHER = new SimpleItemMatcher();
+
+  public static final String PATTERN = AccessLogConfiguration.DEFAULT_PATTERN;
+
+  @Test
+  public void testMatch() {
+    AccessLogItemLocation location = MATCHER.match(PATTERN, 0);
+    Assert.assertEquals(
+        location,
+        new AccessLogItemLocation()
+            .setStart(0)
+            .setEnd(2)
+            .setPlaceHolder(AccessLogItemTypeEnum.REMOTE_HOSTNAME));
+
+    location = MATCHER.match(PATTERN, 7);
+    Assert.assertEquals(
+        location,
+        new AccessLogItemLocation()
+            .setStart(7)
+            .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));
+  }
+
+  @Test
+  public void testNotMatch() {
+    AccessLogItemLocation location = MATCHER.match("notmatch", 0);
+    Assert.assertNull(location);
+  }
+}
\ No newline at end of file

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

Reply via email to