Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a bunch of updates / fixs. #322

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

# Windows
*.txt
.idea/

**/.idea
**/*.iml
**/target

159 changes: 159 additions & 0 deletions sonic-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.tencent</groupId>
<artifactId>VasSonic</artifactId>
<version>1.1.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>com.github.tencent:VasSonic</name>
<description>VasSonic is a lightweight and high-performance Hybrid framework
developed by tencent VAS team.
This project is java backend part of VasSonic
</description>
<url>https://github.com/Tencent/VasSonic</url>

<licenses>
<license>
<name>The BSD 3-Clause License</name>
<url>https://github.com/Tencent/VasSonic/blob/master/LICENSE</url>
</license>
</licenses>

<developers>
<developer>
<name>sonic</name>
<email>[email protected]</email>
<organization>tencent</organization>
<organizationUrl>https://github.com/Tencent/VasSonic
</organizationUrl>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/Tencent/VasSonic.git</connection>
<developerConnection>scm:git:ssh://github.com:Tencent/VasSonic.git
</developerConnection>
<url>https://github.com/Tencent/VasSonic/tree/master</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
<java.version>8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<distributionManagement>
<snapshotRepository>
<!-- 这个id需要在setting.xml中设置 -->
<id>ossrh</id>
<name>vasSonic</name>
<!-- 这里的url就是Issue中回复的snapshots 的repo地址-->
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<name>OSS Staging Repository</name>
<!-- 这里的url就是Issue中回复的staging 的repo地址-->
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2
</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}
</source>
<target>${maven.compiler.target}
</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</reporting>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
}

}
32 changes: 16 additions & 16 deletions sonic-java/src/main/java/com/github/tencent/SonicFilter.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String, String> diffTagNames = new HashMap<String, String>();
public static HashMap<String, String> diffTagNames = new HashMap<String, String>();

@Override
public String doReplace(String text, int index, Matcher matcher) {
StringBuilder tagBuilder = new StringBuilder();
String tagName;
Expand All @@ -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();
Expand Down Expand Up @@ -65,7 +66,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
Map<String,String> 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");
Expand All @@ -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
Expand All @@ -106,7 +107,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
if(headerMap.containsKey("template-tag")) {
clientTemplateTag = headerMap.get("template-tag");
}

String stringTitlePattern = "<title(.*?)<\\/title>";
htmlTitle = SonicUtil.pregMatch(htmlContent, stringTitlePattern);
String htmlTemplate= htmlContent.replaceAll(stringTitlePattern,"{title}");
Expand All @@ -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<String, Object> result = new HashMap<String, Object>();
Map<String, String> dataMap = new HashMap<String, String>();
Map<String, Object> result = new HashMap<String, Object>(4);
Set<Map.Entry<String, String>> diffTagNamesEntrySet = TemplateReplace.diffTagNames.entrySet();
Map<String, String> dataMap = new HashMap<String, String>(diffTagNamesEntrySet.size()+1);
dataMap.put("{title}", htmlTitle);
for (Map.Entry<String, String> entry : TemplateReplace.diffTagNames.entrySet()) {
StringBuilder strKey = new StringBuilder();
strKey.append("{").append(entry.getKey()).append("}");
dataMap.put(strKey.toString(), entry.getValue());
for (Map.Entry<String, String> entry : diffTagNamesEntrySet) {
dataMap.put("{" + entry.getKey() + "}", entry.getValue());
}

TemplateReplace.reset();
Expand All @@ -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();
}

Expand Down
24 changes: 13 additions & 11 deletions sonic-java/src/main/java/com/github/tencent/SonicUtil.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

public class SonicUtil {

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -82,25 +82,27 @@ public static String replaceAllCallBack(String string, Pattern pattern, ReplaceC

/**
* get matched string
*
* @param strContent
* @param strPattern
* @return
*/
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 "";
}

/**
* get http headers
*
* @param httpRequest
* @return
*/
public static Map<String,String> getAllHttpHeaders(HttpServletRequest httpRequest) {
public static Map<String, String> getAllHttpHeaders(HttpServletRequest httpRequest) {
Map<String, String> headerMap = new HashMap<String, String>();
Enumeration<String> headerNames = httpRequest.getHeaderNames();
if (headerNames != null) {
Expand Down