diff --git a/.gitignore b/.gitignore
index ff0bbd7..ab62652 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,8 @@
# Windows
*.txt
-.idea/
+
+**/.idea
+**/*.iml
+**/target
+
diff --git a/sonic-java/pom.xml b/sonic-java/pom.xml
new file mode 100644
index 0000000..b7ef75c
--- /dev/null
+++ b/sonic-java/pom.xml
@@ -0,0 +1,159 @@
+
+ 4.0.0
+
+ com.github.tencent
+ VasSonic
+ 1.1.1-SNAPSHOT
+ jar
+
+ com.github.tencent:VasSonic
+ VasSonic is a lightweight and high-performance Hybrid framework
+ developed by tencent VAS team.
+ This project is java backend part of VasSonic
+
+ https://github.com/Tencent/VasSonic
+
+
+
+ The BSD 3-Clause License
+ https://github.com/Tencent/VasSonic/blob/master/LICENSE
+
+
+
+
+
+ sonic
+ janestar92@gmail.com
+ tencent
+ https://github.com/Tencent/VasSonic
+
+
+
+
+
+ scm:git:git://github.com/Tencent/VasSonic.git
+ scm:git:ssh://github.com:Tencent/VasSonic.git
+
+ https://github.com/Tencent/VasSonic/tree/master
+
+
+
+ UTF-8
+ UTF-8
+
+ 8
+ ${java.version}
+ ${java.version}
+
+
+
+
+
+ ossrh
+ vasSonic
+
+ https://oss.sonatype.org/content/repositories/snapshots
+
+
+ ossrh
+ OSS Staging Repository
+
+ https://oss.sonatype.org/service/local/staging/deploy/maven2
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+ ${maven.compiler.source}
+
+ ${maven.compiler.target}
+
+
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+ 3.1.0
+
+
+ attach-sources
+
+ jar-no-fork
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.1.0
+
+
+ attach-javadocs
+
+ jar
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-site-plugin
+ 3.7.1
+
+
+ org.apache.maven.plugins
+ maven-project-info-reports-plugin
+ 3.0.0
+
+
+ org.apache.maven.plugins
+ maven-gpg-plugin
+ 1.6
+
+
+ verify
+
+ sign
+
+
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.1.0
+
+
+
+
+
+
+
+ com.google.code.gson
+ gson
+ 2.8.5
+
+
+
+ javax.servlet
+ javax.servlet-api
+ 4.0.0
+ provided
+
+
+
+
diff --git a/sonic-java/src/main/java/com/github/tencent/AbstractReplaceCallBack.java b/sonic-java/src/main/java/com/github/tencent/AbstractReplaceCallBack.java
index 6c0a871..30038b9 100644
--- a/sonic-java/src/main/java/com/github/tencent/AbstractReplaceCallBack.java
+++ b/sonic-java/src/main/java/com/github/tencent/AbstractReplaceCallBack.java
@@ -6,6 +6,7 @@ public abstract class AbstractReplaceCallBack implements ReplaceCallBack {
protected Matcher matcher;
+ @Override
final public String replace(String text, int index, Matcher matcher) {
this.matcher = matcher;
try {
diff --git a/sonic-java/src/main/java/com/github/tencent/ServletOutputStreamCopier.java b/sonic-java/src/main/java/com/github/tencent/ServletOutputStreamCopier.java
index 8200157..dd7db57 100644
--- a/sonic-java/src/main/java/com/github/tencent/ServletOutputStreamCopier.java
+++ b/sonic-java/src/main/java/com/github/tencent/ServletOutputStreamCopier.java
@@ -1,14 +1,13 @@
package com.github.tencent;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
public class ServletOutputStreamCopier extends ServletOutputStream {
- private ByteArrayOutputStream copy;
+ private final ByteArrayOutputStream copy;
public ServletOutputStreamCopier() {
this.copy = new ByteArrayOutputStream();
@@ -25,12 +24,12 @@ public byte[] getCopy() {
@Override
public boolean isReady() {
- return this.isReady();
+ return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
- this.setWriteListener(writeListener);
+ throw new UnsupportedOperationException();
}
}
diff --git a/sonic-java/src/main/java/com/github/tencent/SonicFilter.java b/sonic-java/src/main/java/com/github/tencent/SonicFilter.java
index 6b28c7b..ce1e315 100644
--- a/sonic-java/src/main/java/com/github/tencent/SonicFilter.java
+++ b/sonic-java/src/main/java/com/github/tencent/SonicFilter.java
@@ -1,8 +1,10 @@
package com.github.tencent;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -18,11 +20,12 @@
import com.google.gson.*;
class TemplateReplace extends AbstractReplaceCallBack {
- public static boolean shoudSonicDiffBodyReplace = false;
+ public static boolean shoudSonicDiffBodyReplace = false;
public static int diffIndex = 0;
public static String tagPrefix = "auto";
- public static HashMap diffTagNames = new HashMap();
+ public static HashMap diffTagNames = new HashMap();
+ @Override
public String doReplace(String text, int index, Matcher matcher) {
StringBuilder tagBuilder = new StringBuilder();
String tagName;
@@ -31,9 +34,7 @@ public String doReplace(String text, int index, Matcher matcher) {
tagName = matcher.group(1);
}
else {
- StringBuilder sb = new StringBuilder();
- sb.append(tagPrefix).append(diffIndex++);
- tagName = sb.toString();
+ tagName = tagPrefix + diffIndex++;
}
diffTagNames.put(tagName, matcher.group(0));
return tagBuilder.append("{").append(tagName).append("}").toString();
@@ -65,7 +66,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
Map headerMap = SonicUtil.getAllHttpHeaders(httpRequest);
String etag = "";
String htmlContent;
- String htmlContentSha1 ="";
+ String htmlContentSha1 ="";
String value = headerMap.get("accept-diff");
if (headerMap.containsKey("accept-diff") && value.equals("true")) {
httpResponse.addHeader("Cache-Control", "no-cache");
@@ -89,7 +90,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
out.close();
return;
}
- htmlContent = new String(copy, "UTF-8");
+ htmlContent = new String(copy, StandardCharsets.UTF_8);
htmlContentSha1 = SonicUtil.encrypt(htmlContent, "sha-1");
}
// if not modified, return 304
@@ -106,7 +107,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
if(headerMap.containsKey("template-tag")) {
clientTemplateTag = headerMap.get("template-tag");
}
-
+
String stringTitlePattern = "";
htmlTitle = SonicUtil.pregMatch(htmlContent, stringTitlePattern);
String htmlTemplate= htmlContent.replaceAll(stringTitlePattern,"{title}");
@@ -118,13 +119,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
String templateMd5 = SonicUtil.encrypt(htmlTemplate, "sha-1");
httpResponse.addHeader("template-tag", templateMd5);
- Map result = new HashMap();
- Map dataMap = new HashMap();
+ Map result = new HashMap(4);
+ Set> diffTagNamesEntrySet = TemplateReplace.diffTagNames.entrySet();
+ Map dataMap = new HashMap(diffTagNamesEntrySet.size()+1);
dataMap.put("{title}", htmlTitle);
- for (Map.Entry entry : TemplateReplace.diffTagNames.entrySet()) {
- StringBuilder strKey = new StringBuilder();
- strKey.append("{").append(entry.getKey()).append("}");
- dataMap.put(strKey.toString(), entry.getValue());
+ for (Map.Entry entry : diffTagNamesEntrySet) {
+ dataMap.put("{" + entry.getKey() + "}", entry.getValue());
}
TemplateReplace.reset();
@@ -143,8 +143,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
resultStr = htmlContent;
}
ServletOutputStream out = httpResponse.getOutputStream();
- out.write(resultStr.getBytes("UTF-8"));
- httpResponse.addHeader("Content-Length", String.valueOf(resultStr.getBytes("UTF-8").length));
+ out.write(resultStr.getBytes(StandardCharsets.UTF_8));
+ httpResponse.addHeader("Content-Length", String.valueOf(resultStr.getBytes(StandardCharsets.UTF_8).length));
out.close();
}
diff --git a/sonic-java/src/main/java/com/github/tencent/SonicUtil.java b/sonic-java/src/main/java/com/github/tencent/SonicUtil.java
index 845d748..d8c46c1 100644
--- a/sonic-java/src/main/java/com/github/tencent/SonicUtil.java
+++ b/sonic-java/src/main/java/com/github/tencent/SonicUtil.java
@@ -1,6 +1,7 @@
package com.github.tencent;
-import java.io.UnsupportedEncodingException;
+import javax.servlet.http.HttpServletRequest;
+import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
@@ -8,7 +9,6 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import javax.servlet.http.HttpServletRequest;
public class SonicUtil {
@@ -19,15 +19,16 @@ public class SonicUtil {
* @return
*/
public static String hex(byte[] arr) {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < arr.length; ++i) {
- sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
+ StringBuilder sb = new StringBuilder();
+ for (byte b : arr) {
+ sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
/**
* encrypt string
+ *
* @param inputText
* @param algorithmName
* @return
@@ -42,19 +43,18 @@ public static String encrypt(String inputText, String algorithmName) {
String encryptText = null;
try {
MessageDigest m = MessageDigest.getInstance(algorithmName);
- m.update(inputText.getBytes("UTF8"));
+ m.update(inputText.getBytes(StandardCharsets.UTF_8));
byte s[] = m.digest();
- return hex(s);
+ encryptText = hex(s);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
}
return encryptText;
}
/**
* replace string which match the pattern with callback
+ *
* @param string
* @param pattern
* @param replacement
@@ -82,6 +82,7 @@ public static String replaceAllCallBack(String string, Pattern pattern, ReplaceC
/**
* get matched string
+ *
* @param strContent
* @param strPattern
* @return
@@ -89,7 +90,7 @@ public static String replaceAllCallBack(String string, Pattern pattern, ReplaceC
public static String pregMatch(String strContent, String strPattern) {
Pattern titlePattern = Pattern.compile(strPattern, Pattern.CASE_INSENSITIVE);
Matcher titleMatcher = titlePattern.matcher(strContent);
- if(titleMatcher.find()) {
+ if (titleMatcher.find()) {
return titleMatcher.group(0);
}
return "";
@@ -97,10 +98,11 @@ public static String pregMatch(String strContent, String strPattern) {
/**
* get http headers
+ *
* @param httpRequest
* @return
*/
- public static Map getAllHttpHeaders(HttpServletRequest httpRequest) {
+ public static Map getAllHttpHeaders(HttpServletRequest httpRequest) {
Map headerMap = new HashMap();
Enumeration headerNames = httpRequest.getHeaderNames();
if (headerNames != null) {