Skip to content

Commit 425a6a1

Browse files
author
张立志
committed
修改时间误差函数
1 parent 2f885e6 commit 425a6a1

File tree

11 files changed

+123
-37
lines changed

11 files changed

+123
-37
lines changed

Diff for: sparrow-orm/src/main/java/com/sparrow/orm/query/BooleanCriteria.java

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.sparrow.orm.query;
1919

20-
import com.sparrow.protocol.SFunction;
21-
import com.sparrow.utility.ClassUtility;
22-
2320
import java.util.ArrayList;
2421
import java.util.List;
2522

Diff for: sparrow-orm/src/main/java/com/sparrow/orm/query/OrderCriteria.java

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.sparrow.enums.Order;
2121
import com.sparrow.orm.query.impl.SimpleCriteriaField;
22+
import com.sparrow.protocol.SFunction;
23+
import com.sparrow.utility.ClassUtility;
2224

2325
public class OrderCriteria {
2426
private CriteriaField field;
@@ -32,6 +34,17 @@ public static OrderCriteria desc(String field) {
3234
return new OrderCriteria(field, Order.DESC);
3335
}
3436

37+
public static <T> OrderCriteria asc(SFunction<T, ?> function) {
38+
String field = ClassUtility.getPropertyNameAndClassName(function).entityDotProperty();
39+
return new OrderCriteria(field, Order.ASC);
40+
}
41+
42+
public static <T> OrderCriteria desc(SFunction<T, ?> function) {
43+
String field = ClassUtility.getPropertyNameAndClassName(function).entityDotProperty();
44+
return new OrderCriteria(field, Order.DESC);
45+
}
46+
47+
3548
private OrderCriteria(String field, Order order) {
3649
this.field = new SimpleCriteriaField(field);
3750
this.order = order;

Diff for: sparrow-orm/src/main/java/com/sparrow/orm/query/UpdateSetClausePair.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package com.sparrow.orm.query;
1919

2020
import com.sparrow.orm.query.impl.SimpleCriteriaField;
21+
import com.sparrow.protocol.SFunction;
22+
import com.sparrow.utility.ClassUtility;
2123

2224
public class UpdateSetClausePair {
2325
private CriteriaField field;
@@ -28,6 +30,12 @@ public static UpdateSetClausePair field(String field) {
2830
return new UpdateSetClausePair(field);
2931
}
3032

33+
public static <T> UpdateSetClausePair field(SFunction<T, ?> function) {
34+
String field = ClassUtility.getPropertyNameAndClassName(function).entityDotProperty();
35+
return new UpdateSetClausePair(field);
36+
}
37+
38+
3139
private UpdateSetClausePair(String field) {
3240
this.field = new SimpleCriteriaField(field);
3341
}

Diff for: sparrow-orm/src/main/java/com/sparrow/orm/type/TypeHandlerRegistry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public TypeHandler<?> getTypeHandler(Class<?> javaType) {
121121
if (typeHandlerClazz == null) {
122122
return null;
123123
}
124-
return this.getTypeHandler(javaType);
124+
return this.allTypeHandlerMap.get(typeHandlerClazz);
125125
}
126126

127127

Diff for: sparrow-protocol/src/main/java/com/sparrow/protocol/LoginUser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void setExtensions(Map<String, Object> extensions) {
143143
}
144144

145145
public boolean isVisitor() {
146-
return VISITOR_ID.equals(this.getUserId());
146+
return VISITOR_ID.equals(this.getUserId()) || CATEGORY_VISITOR == this.getCategory();
147147
}
148148

149149
public Integer getCategory() {

Diff for: sparrow-protocol/src/main/java/com/sparrow/protocol/constant/Constant.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class Constant {
9898
/**
9999
* request header 不允许存在下划线
100100
*/
101-
public static final String REQUEST_HEADER_KEY_LOGIN_TOKEN = "sparrow-token";
101+
public static final String REQUEST_HEADER_KEY_LOGIN_TOKEN = "Authorization";
102102
public static final String REQUEST_INVOKABLE_HANDLER_METHOD = "request_invokable_handler_method";
103103
public static final String IS_AJAX = "ajax";
104104

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.sparrow.support.web;
19+
20+
import com.sparrow.utility.StringUtility;
21+
22+
import javax.servlet.Filter;
23+
import javax.servlet.FilterConfig;
24+
import javax.servlet.ServletException;
25+
import javax.servlet.http.HttpServletRequest;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
29+
public abstract class AbstractLoginFilter implements Filter {
30+
private List<String> excludePatternList = new ArrayList<String>();
31+
protected Boolean supportTemplate;
32+
protected String apiPrefix;
33+
34+
@Override
35+
public void init(FilterConfig config) throws ServletException {
36+
String excludePatterns = config.getInitParameter("excludePatterns");
37+
if (StringUtility.isNullOrEmpty(excludePatterns)) {
38+
return;
39+
}
40+
String[] patterns = excludePatterns.split(",");
41+
for (String pattern : patterns) {
42+
pattern = pattern.replace("*", ".*");
43+
this.excludePatternList.add(pattern);
44+
}
45+
}
46+
47+
public Boolean isAjax(HttpServletRequest request) {
48+
return ServletUtility.getInstance().isAjax(request, this.supportTemplate, this.apiPrefix);
49+
}
50+
51+
public boolean matchExcludePatterns(String path) {
52+
for (String pattern : this.excludePatternList) {
53+
if (path.matches(pattern)) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
}
59+
}

Diff for: sparrow/src/main/java/com/sparrow/support/web/LoginUserFilter.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,32 @@
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030

31-
import javax.servlet.*;
31+
import javax.servlet.FilterChain;
32+
import javax.servlet.ServletException;
33+
import javax.servlet.ServletRequest;
34+
import javax.servlet.ServletResponse;
3235
import javax.servlet.http.HttpServletRequest;
3336
import java.io.IOException;
34-
import java.util.List;
3537

3638
/**
3739
* 分布式场景下,从请求头中获取登录用户信息
3840
*/
39-
public class LoginUserFilter implements Filter {
40-
public LoginUserFilter(Boolean mockLoginUser, List<String> whiteList) {
41+
public class LoginUserFilter extends AbstractLoginFilter {
42+
public LoginUserFilter(Boolean mockLoginUser) {
4143
this.mockLoginUser = mockLoginUser;
42-
this.whiteList = whiteList;
4344
}
4445

4546
private static Logger logger = LoggerFactory.getLogger(LoginUserFilter.class);
4647
private Boolean mockLoginUser;
47-
private List<String> whiteList;
4848
private Json json = JsonFactory.getProvider();
4949

50-
@Override
51-
public void init(FilterConfig config) throws ServletException {
52-
53-
}
54-
5550
@Override
5651
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
5752
FilterChain filterChain) throws IOException, ServletException {
5853
if (servletRequest instanceof HttpServletRequest) {
5954
HttpServletRequest req = (HttpServletRequest) servletRequest;
6055
String currentUrl = req.getServletPath();
61-
if (this.whiteList.contains(currentUrl)) {
56+
if (this.matchExcludePatterns(currentUrl)) {
6257
filterChain.doFilter(servletRequest, servletResponse);
6358
return;
6459
}

Diff for: sparrow/src/main/java/com/sparrow/support/web/MonolithicLoginUserFilter.java

+15-14
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,29 @@
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333

34-
import javax.servlet.*;
34+
import javax.servlet.FilterChain;
35+
import javax.servlet.ServletException;
36+
import javax.servlet.ServletRequest;
37+
import javax.servlet.ServletResponse;
3538
import javax.servlet.http.HttpServletRequest;
3639
import javax.servlet.http.HttpServletResponse;
3740
import java.io.IOException;
38-
import java.util.List;
3941

4042
/**
4143
* 单体应用解析,需要签名认证
4244
*/
43-
public class MonolithicLoginUserFilter implements Filter {
45+
public class MonolithicLoginUserFilter extends AbstractLoginFilter {
4446
private static Logger logger = LoggerFactory.getLogger(MonolithicLoginUserFilter.class);
4547

46-
public MonolithicLoginUserFilter(Authenticator authenticator, Boolean mockLoginUser, List<String> whiteList, String tokenKey) {
48+
public MonolithicLoginUserFilter(Authenticator authenticator,
49+
Boolean mockLoginUser,
50+
String tokenKey,
51+
Boolean supportTemplate,
52+
String apiPrefix) {
4753
this.authenticator = authenticator;
4854
this.mockLoginUser = mockLoginUser;
49-
this.whiteList = whiteList;
55+
this.supportTemplate = supportTemplate;
56+
this.apiPrefix = apiPrefix;
5057
if (StringUtility.isNullOrEmpty(tokenKey)) {
5158
this.tokenKey = Constant.REQUEST_HEADER_KEY_LOGIN_TOKEN;
5259
} else {
@@ -56,15 +63,9 @@ public MonolithicLoginUserFilter(Authenticator authenticator, Boolean mockLoginU
5663

5764
private Boolean mockLoginUser;
5865
private Authenticator authenticator;
59-
private List<String> whiteList;
6066
private String tokenKey;
6167

6268

63-
@Override
64-
public void init(FilterConfig config) throws ServletException {
65-
66-
}
67-
6869
private void loginSuccess(LoginUser loginUser, FilterChain filterChain, HttpServletRequest request, HttpServletResponse response) {
6970
ThreadContext.bindLoginToken(loginUser);
7071
try {
@@ -77,7 +78,7 @@ private void loginSuccess(LoginUser loginUser, FilterChain filterChain, HttpServ
7778
}
7879

7980
private void loginFail(HttpServletRequest request, HttpServletResponse servletResponse, ErrorSupport e) throws IOException {
80-
boolean isAjax = ServletUtility.getInstance().isAjax(request);
81+
boolean isAjax = this.isAjax(request);
8182
if (isAjax) {
8283
servletResponse.setContentType(Constant.CONTENT_TYPE_JSON);
8384
Result result = Result.fail(e);
@@ -119,7 +120,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
119120
HttpServletResponse rep = (HttpServletResponse) servletResponse;
120121

121122
String currentUrl = req.getServletPath();
122-
if (this.whiteList.contains(currentUrl)) {
123+
if (this.matchExcludePatterns(currentUrl)) {
123124
filterChain.doFilter(servletRequest, servletResponse);
124125
return;
125126
}
@@ -141,7 +142,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
141142
return;
142143
}
143144
if (mockLoginUser) {
144-
loginUser = LoginUser.create(1L,1, "mock-user", "mock-nick-name", "header", "device id", 3);
145+
loginUser = LoginUser.create(1L, 1, "mock-user", "mock-nick-name", "header", "device id", 3);
145146
this.loginSuccess(loginUser, filterChain, req, rep);
146147
return;
147148
}

Diff for: sparrow/src/main/java/com/sparrow/support/web/ServletUtility.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,21 @@ public String referer(HttpServletRequest request) {
169169
return request.getHeader("Referer");
170170
}
171171

172-
public boolean isAjax(HttpServletRequest request) {
173-
String isAjax = request.getHeader(Constant.IS_AJAX);
174-
return "true".equalsIgnoreCase(isAjax);
172+
public boolean isAjax(HttpServletRequest request, boolean supportTemplate, String apiPrefix) {
173+
//非模板引擎,直接返回json
174+
if (!supportTemplate) {
175+
return true;
176+
}
177+
//支持模板引擎,判断URL前缀,如果是api前缀,则返回json
178+
if (!StringUtility.isNullOrEmpty(apiPrefix)) {
179+
String uri = request.getRequestURI();
180+
return uri.startsWith(apiPrefix);
181+
}
182+
//如果请求参数中明确指定了isAjax,则返回json
183+
String ajax = request.getHeader(Constant.IS_AJAX);
184+
if (!StringUtility.isNullOrEmpty(ajax)) {
185+
return "true".equalsIgnoreCase(ajax);
186+
}
187+
return false;
175188
}
176189
}

Diff for: sparrow/src/main/java/com/sparrow/utility/ClassUtility.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public static String getEntityNameByClass(Class entity) {
5858

5959
public static String getEntityNameByClass(String entityName) {
6060
if (entityName.contains(".")) {
61-
entityName = entityName.substring(entityName.lastIndexOf(".")+1);
61+
entityName = entityName.substring(entityName.lastIndexOf(".") + 1);
6262
}
6363
if (entityName.contains("/")) {
64-
entityName = entityName.substring(entityName.lastIndexOf("/")+1);
64+
entityName = entityName.substring(entityName.lastIndexOf("/") + 1);
6565
}
6666
entityName = StringUtility.setFirstByteLowerCase(entityName);
6767
if (entityName.endsWith("DTO")) {

0 commit comments

Comments
 (0)