From 7453e603e02fb0454aa34e1ded51f6b072274a17 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Wed, 12 Jun 2019 20:51:48 +0800 Subject: [PATCH 1/4] add pom here and fix the pom. 1. Suggest using [semver](https://semver.org/) for versioning. 2. A version at github shall always be a SNAPSHOT version. 3. The license license.url in your pom does not point to a BSD license as you said in license.name Just point it to your LICENSE file in github repo is a better choice. 4. Specify java.version in properties. 5. Specify source/target version in maven compiler plugin. 6. Add maven-site-plugin and maven-project-info-reports-plugin to make sure when you run `mvn site`, a javadoc will generate and link to the generated site. 7. Update maven dependencies to latest version. 8. Update other dependencies to latest version of same major version. 9. refine .gitignore for excluding .iml s and target folders. --- .gitignore | 6 +- sonic-java/pom.xml | 159 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 sonic-java/pom.xml 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 + + + + From c9182c1473587c70d0d541102fb189b31fe47475 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Wed, 12 Jun 2019 21:24:06 +0800 Subject: [PATCH 2/4] fix bugs and malformed codes. 1. in class ServletOutputStreamCopier: 1.1 field ByteArrayOutputStream copy shall be final. 1.2 public boolean isReady()infinite loop itself. 1.3 public void setWriteListener(WriteListener writeListener) infinite loop itself. 2. in class TemplateReplace: 2.1 Don't create a StringBuilder for just 2 or 3 params. Just use + will be a faster / neater choice 3. in class SonicUtil: 3.1 Don't return inside a try block. Inside return it afterwards. And, you didn't use the local String encryptText. Even if you intend to return from the try block, you shall return null directly after the catch block. Return in try block will always cause problem for maintainers so really not recommended to do so. --- .../tencent/ServletOutputStreamCopier.java | 11 ++++----- .../java/com/github/tencent/SonicFilter.java | 15 +++++------- .../java/com/github/tencent/SonicUtil.java | 24 ++++++++++--------- 3 files changed, 24 insertions(+), 26 deletions(-) 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..8ebd93c 100644 --- a/sonic-java/src/main/java/com/github/tencent/SonicFilter.java +++ b/sonic-java/src/main/java/com/github/tencent/SonicFilter.java @@ -1,6 +1,7 @@ package com.github.tencent; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -31,9 +32,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(); @@ -89,7 +88,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 @@ -122,9 +121,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha Map dataMap = new HashMap(); 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()); + dataMap.put("{" + entry.getKey() + "}", entry.getValue()); } TemplateReplace.reset(); @@ -143,8 +140,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) { From b557d1fca4f0682576d85d1220b08249fac9b190 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Wed, 12 Jun 2019 21:33:17 +0800 Subject: [PATCH 3/4] add missing @Override. --- .../java/com/github/tencent/AbstractReplaceCallBack.java | 1 + .../src/main/java/com/github/tencent/SonicFilter.java | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) 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/SonicFilter.java b/sonic-java/src/main/java/com/github/tencent/SonicFilter.java index 8ebd93c..e92923c 100644 --- a/sonic-java/src/main/java/com/github/tencent/SonicFilter.java +++ b/sonic-java/src/main/java/com/github/tencent/SonicFilter.java @@ -19,11 +19,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; @@ -64,7 +65,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"); @@ -105,7 +106,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}"); From cb48c1db3217b939d2cd660ce4a988234a5f49b7 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Wed, 12 Jun 2019 21:40:01 +0800 Subject: [PATCH 4/4] specify init capacity for HashMap for better performance. --- .../src/main/java/com/github/tencent/SonicFilter.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 e92923c..ce1e315 100644 --- a/sonic-java/src/main/java/com/github/tencent/SonicFilter.java +++ b/sonic-java/src/main/java/com/github/tencent/SonicFilter.java @@ -4,6 +4,7 @@ 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; @@ -118,10 +119,11 @@ 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()) { + for (Map.Entry entry : diffTagNamesEntrySet) { dataMap.put("{" + entry.getKey() + "}", entry.getValue()); }