Skip to content

Commit 213bb10

Browse files
author
Philip Zhang
committed
update caf framework
1 parent b88b115 commit 213bb10

File tree

7 files changed

+115
-58
lines changed

7 files changed

+115
-58
lines changed

bizcore/WEB-INF/caf_core_src/com/skynet/bootstrap/MultiReadHttpServletRequest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ public String[] getParameterValues(String key) {
8787
@Override
8888
public Map<String, String[]> getParameterMap() {
8989
if (parameterMap == null) {
90-
Map<String, String[]> result = new LinkedHashMap<String, String[]>();
91-
decode(getQueryString(), result);
92-
decode(getPostBodyAsString(), result);
93-
parameterMap = Collections.unmodifiableMap(result);
90+
// Map<String, String[]> result = new LinkedHashMap<String, String[]>();
91+
// decode(getQueryString(), result);
92+
// decode(getPostBodyAsString(), result);
93+
// parameterMap = Collections.unmodifiableMap(result);
94+
parameterMap = getRequest().getParameterMap();
9495
}
9596
return parameterMap;
9697
}
@@ -108,6 +109,8 @@ private Iterable<NameValuePair> decodeParams(String body) {
108109
if (ct.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
109110
List<NameValuePair> postParams = URLEncodedUtils.parse(IOUtils.toString(getReader()), UTF8_CHARSET);
110111
params = Iterables.concat(params, postParams);
112+
}else if (ct.getMimeType().equals(ContentType.MULTIPART_FORM_DATA.getMimeType())) {
113+
111114
}
112115
}
113116
} catch (IOException e) {
@@ -163,4 +166,4 @@ public int available() throws IOException {
163166
}
164167

165168

166-
}
169+
}

bizcore/WEB-INF/caf_core_src/com/terapico/caf/ServletResultRenderer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ private void renderAsString(InvocationResult result, HttpServletRequest request,
9999
}
100100
byte [] value = result.getActualResult().toString().getBytes();
101101
int length = value.length;
102+
fillOrigin(result, request, response);
102103
response.addHeader("X-Class", renderClass);
103104
response.addHeader("X-Env-Type", result.getEnvType());
104105
response.addHeader("X-Env-Name", result.getEnvName());
105106
response.addHeader("Content-Length", Long.valueOf(length).toString());
106-
response.addHeader("Access-Control-Expose-Headers", "X-Class, X-Redirect, X-Env-Type, X-Env-Name");
107+
response.addHeader("Access-Control-Expose-Headers", "X-Class, X-Redirect, X-Env-Type, X-Env-Name, X-Action");
107108

108109
response.getOutputStream().write(value);;
109110

@@ -228,7 +229,7 @@ protected void fillOrigin(InvocationResult result, HttpServletRequest request, H
228229
response.addHeader("Access-Control-Allow-Origin", origin);
229230
response.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
230231
// Access-Control-Expose-Headers
231-
response.addHeader("Access-Control-Expose-Headers", "Set-Cookie, X-Redirect, X-Env-Type, X-Env-Name");
232+
response.addHeader("Access-Control-Expose-Headers", "Set-Cookie, X-Redirect, X-Env-Type, X-Env-Name, X-Action");
232233
response.addHeader("Access-Control-Allow-Credentials", "true");
233234

234235
}
@@ -255,7 +256,7 @@ protected void renderJson(InvocationResult result, HttpServletRequest request, H
255256
response.addHeader("X-Class", renderClass);
256257
response.addHeader("X-Env-Type", result.getEnvType());
257258
response.addHeader("X-Env-Name", result.getEnvName());
258-
response.addHeader("Access-Control-Expose-Headers", "X-Class, X-Redirect, X-Env-Type, X-Env-Name");
259+
response.addHeader("Access-Control-Expose-Headers", "X-Class, X-Redirect, X-Env-Type, X-Env-Name, X-Action");
259260
// Access-Control-Expose-Headers
260261

261262
log("Render JSON result with class: " + renderClass + "(" + renderLogResult(result.getActualResult()) + ")");

bizcore/WEB-INF/caf_core_src/com/terapico/uccaf/AccessControledService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ public interface AccessControledService {
44
public Object checkAccess(BaseUserContext userContext,String methodName, Object[] parameters) throws Exception;
55
public void enforceAccess(BaseUserContext userContext, Object targetObject) throws Exception;
66

7-
public void onAccess(BaseUserContext baseUserContext, String methodName, Object[] parameters);
7+
default public void onAccess(BaseUserContext baseUserContext, String methodName, Object[] parameters) {}
8+
default public void afterInvoke(BaseUserContext baseUserContext, String methodName, Object[] parameters, boolean success, Object result, Throwable throwable) {}
9+
810
}
911

1012

bizcore/WEB-INF/caf_core_src/com/terapico/uccaf/UCInvocationServlet.java

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class UCInvocationServlet extends SimpleInvocationServlet{
2424

2525
/**
26-
*
26+
*
2727
*/
2828
private static final long serialVersionUID = 1L;
2929

@@ -40,20 +40,20 @@ public void setApplicationContext(ApplicationContext pApplicationContext) {
4040

4141
@Override
4242
public void init() throws ServletException {
43-
43+
4444
super.init();
4545
replaceBeans();
4646
}
4747

4848
@Override
4949
public void init(ServletConfig config) throws ServletException {
50-
50+
5151
super.init(config);
5252
replaceBeans();
5353
}
54-
54+
5555
protected boolean isAccessControledService(InvocationContext context){
56-
56+
5757
int parameterLength = context.getParameters().length;
5858
if(parameterLength == 0){
5959
//no context and no parameter
@@ -62,7 +62,7 @@ protected boolean isAccessControledService(InvocationContext context){
6262
Object firstParamter = context.getParameters()[0];
6363
if(!(firstParamter instanceof BaseUserContext)){
6464
//first parameter is not based on base user context, will not regard as access controlled service.
65-
65+
6666
return false;
6767
}
6868
if(!(context instanceof UCInvocationContext)){
@@ -72,38 +72,38 @@ protected boolean isAccessControledService(InvocationContext context){
7272
if(!(targetObject instanceof AccessControledService)){
7373
return false;
7474
}
75-
76-
75+
76+
7777
//UCInvocationContext uInvocationContext = (UCInvocationContext)context;
78-
79-
78+
79+
8080
return true;
81-
81+
8282
}
83-
83+
8484
protected InvocationResult invoke(InvocationContext context) throws ServletException
8585
{
86-
86+
8787
//check the user context before invoke
8888
//before any call, check the URL
89-
90-
89+
90+
9191
if(!isAccessControledService(context)){
92-
92+
9393
return super.invoke(context);
9494
}
95-
95+
9696
Object targetObject = context.getTargetObject();
9797
Object []parameters = context.getParameters();
98-
98+
9999
AccessControledService targetService = (AccessControledService)targetObject;
100100
UCInvocationContext ucInvocationContext = (UCInvocationContext)context;
101101

102102
try {
103103
System.out.println("InvocationResult result = super.invoke(context); called");
104104
String methodName = ucInvocationContext.getMethodToCall().getName();
105105
BaseUserContext baseUserContext = ucInvocationContext.getUserContext();
106-
106+
107107
targetService.onAccess(baseUserContext, methodName ,parameters);
108108

109109
Object checkResult = targetService.checkAccess(baseUserContext, methodName ,parameters);
@@ -115,36 +115,42 @@ protected InvocationResult invoke(InvocationContext context) throws ServletExcep
115115
result.setInvocationContext(context);
116116
return result;
117117
}
118-
118+
119119
//null means the request passed the access check
120120
InvocationResult result = super.invoke(context);
121+
if (result.getActualResult() instanceof Throwable){
122+
targetService.afterInvoke(baseUserContext, methodName, parameters, false, null, (Throwable) result.getActualResult());
123+
}else{
124+
targetService.afterInvoke(baseUserContext, methodName, parameters, true, result.getActualResult(), null);
125+
}
121126
logExceptionResult(baseUserContext,result);
122-
127+
123128
targetService.enforceAccess(baseUserContext, result.getActualResult());
124-
125-
126-
129+
130+
131+
127132
return result;
128-
133+
129134
} catch (Exception e) {
130-
135+
131136
InvocationResult result=new SimpleInvocationResult();
132-
result.setActualResult(e);
137+
result.setActualResult(e);
138+
result.setInvocationContext(context);
133139
//InvocationResult result = super.invoke(context);
134140
System.out.println("the call throws the exception not handled by the app layer, framework catches");
135141
e.printStackTrace();
136-
142+
137143
return result;
138144
}
139145

140146
}
141-
142-
147+
148+
143149
protected Method searchMethod(Class <?> clazz,String name){
144150
for(Method m:clazz.getMethods()){
145151
System.out.println("mmm: "+m.getName());
146152
if(name.equals(m.getName())){
147-
153+
148154
return m;
149155
}
150156
}
@@ -160,12 +166,12 @@ protected InvocationResult logExceptionResult(BaseUserContext baseUserContext, I
160166
Class<?> clzz []=new Class[]{String.class, String.class, String.class};
161167
//baseUserContext.getClass().getDeclaredMethod(name, parameterTypes)
162168
//public void sendEmail(String to, String subject, String content)
163-
169+
164170
//Method sendMailMethod = searchMethod(baseUserContext.getClass(),"sendMail");//.getMethod("sendMail", String.class, String.class, String.class);
165-
171+
166172
Method sendMailMethod = baseUserContext.getClass().getMethod("sendEmail", String.class, String.class, String.class);
167-
168-
173+
174+
169175
sendMailMethod.invoke(baseUserContext, getExceptionMonitorEmail(),
170176
result.getActualResult().getClass().getSimpleName()+" from " + baseUserContext.getEnvironmentName(),
171177
wrapExceptionToString((Throwable)result.getActualResult()));
@@ -174,15 +180,15 @@ protected InvocationResult logExceptionResult(BaseUserContext baseUserContext, I
174180
e.printStackTrace();
175181
//System.out.println("Method not found"+e);
176182
}
177-
183+
178184
return result;
179-
185+
180186
//Class<?> parameterTypes;
181187
//Method sendMailMethod = baseUserContext.getClass().getMethod("sendMail", parameterTypes);
182-
183-
188+
189+
184190
}
185-
191+
186192
protected Object getExceptionMonitorEmail() {
187193
String envValue = System.getenv("EXCEPTION_MONITOR");
188194
if (TextUtil.isBlank(envValue)) {
@@ -200,22 +206,22 @@ protected String wrapExceptionToString(Throwable result) {
200206
}
201207

202208
/*
203-
*
209+
*
204210
* public void sendEmail(String to, String subject, String content){
205211
this.ensureSMTPService();
206212
smtpService.send(to, subject, content);
207-
213+
208214
}
209-
*
215+
*
210216
* */
211217
public void replaceBeans()
212218
{
213219
InternalBeanFactory.replaceFormBuilder(new UCFormBuilder());
214220
InternalBeanFactory.replaceServletInvocationContextFactory(new UCInvocationContextFactory(mApplicationContext));
215-
221+
216222
}
217-
218-
219-
223+
224+
225+
220226

221227
}

bizcore/WEB-INF/caf_core_src/com/terapico/utils/JWTUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public static DecodedJWT decodeToken(String token) {
2828
.build(); //Reusable verifier instance
2929
DecodedJWT jwt = verifier.verify(token);
3030
return jwt;
31-
} catch (JWTVerificationException exception){
32-
exception.printStackTrace();
31+
} catch (JWTVerificationException e){
32+
// exception.printStackTrace();
33+
System.out.println("[ Exception]: " + e.getMessage());
3334
return null;
3435
}
3536
}

bizcore/WEB-INF/caf_core_src/com/terapico/utils/TaskUtil.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import java.util.Iterator;
55
import java.util.Map;
66
import java.util.Map.Entry;
7+
import java.util.concurrent.Callable;
78
import java.util.concurrent.ExecutorService;
89
import java.util.concurrent.Executors;
10+
import java.util.concurrent.Future;
911
import java.util.concurrent.atomic.AtomicLong;
1012
import java.util.function.Supplier;
1113

@@ -157,6 +159,9 @@ public synchronized static boolean checkTaskRunning(String taskName, boolean set
157159
public static void runAsync(Runnable runnable) {
158160
executor.execute(runnable);
159161
}
162+
public static <V> Future<V> runAsync(Callable<V> callable) {
163+
return executor.submit(callable);
164+
}
160165

161166
public synchronized static String addScheduleTask(String taskName, long intervalInMs, Runnable runnable) {
162167
if (runningScheduleTask.containsKey(taskName)){

bizcore/WEB-INF/caf_core_src/com/terapico/utils/TextUtil.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,43 @@ private static char convert(byte[] bytes) {
514514
return result;
515515
}
516516

517+
public static double calcMatchRate(String str, String target) {
518+
519+
int[][] d; // 矩阵
520+
int n = str.length();
521+
int m = target.length();
522+
int i; // 遍历str的
523+
int j; // 遍历target的
524+
char ch1; // str的
525+
char ch2; // target的
526+
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
527+
if (n == 0 || m == 0) {
528+
return 0;
529+
}
530+
d = new int[n + 1][m + 1];
531+
for (i = 0; i <= n; i++) { // 初始化第一列
532+
d[i][0] = i;
533+
}
534+
535+
for (j = 0; j <= m; j++) { // 初始化第一行
536+
d[0][j] = j;
537+
}
538+
539+
for (i = 1; i <= n; i++) { // 遍历str
540+
ch1 = str.charAt(i - 1);
541+
// 去匹配target
542+
for (j = 1; j <= m; j++) {
543+
ch2 = target.charAt(j - 1);
544+
if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
545+
temp = 0;
546+
} else {
547+
temp = 1;
548+
}
549+
// 左边+1,上边+1, 左上角+temp取最小
550+
d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
551+
}
552+
}
553+
554+
return (1 - (double) d[n][m] / Math.max(str.length(), target.length())) * 100.0;
555+
}
517556
}

0 commit comments

Comments
 (0)