Skip to content

Commit 41a03ce

Browse files
authored
Merge pull request #302 from lets-blade/dev
Dev
2 parents 6757d03 + 2bf3505 commit 41a03ce

19 files changed

+130
-130
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ Run with `Maven`:
6464
<dependency>
6565
<groupId>com.bladejava</groupId>
6666
<artifactId>blade-mvc</artifactId>
67-
<version>2.0.12.BETA</version>
67+
<version>2.0.12.RELEASE</version>
6868
</dependency>
6969
```
7070

7171
or `Gradle`:
7272

7373
```sh
74-
compile 'com.bladejava:blade-mvc:2.0.12.BETA'
74+
compile 'com.bladejava:blade-mvc:2.0.12.RELEASE'
7575
```
7676

7777
Write the `main` method and the `Hello World`:

README_CN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>com.bladejava</groupId>
6363
<artifactId>blade-mvc</artifactId>
64-
<version>2.0.12.BETA</version>
64+
<version>2.0.12.RELEASE</version>
6565
</dependency>
6666
```
6767

@@ -70,7 +70,7 @@
7070
或者 `Gradle`:
7171

7272
```sh
73-
compile 'com.bladejava:blade-mvc:2.0.12.BETA'
73+
compile 'com.bladejava:blade-mvc:2.0.12.RELEASE'
7474
```
7575

7676
编写 `main` 函数写一个 `Hello World`

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.bladejava</groupId>
77
<artifactId>blade-mvc</artifactId>
8-
<version>2.0.12.BETA</version>
8+
<version>2.0.12.RELEASE</version>
99
<packaging>jar</packaging>
1010

1111
<name>blade</name>

src/main/java/com/blade/Blade.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public class Blade {
153153
*/
154154
private ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
155155

156+
private CorsMiddleware corsMiddleware;
157+
156158
/**
157159
* Used to identify whether the web server has started
158160
*/
@@ -548,11 +550,15 @@ public Blade enableCors(boolean enableCors) {
548550
public Blade enableCors(boolean enableCors, CorsConfiger corsConfig) {
549551
this.environment.set(ENV_KEY_CORS_ENABLE, enableCors);
550552
if (enableCors) {
551-
this.use(new CorsMiddleware(corsConfig));
553+
this.corsMiddleware = new CorsMiddleware(corsConfig);
552554
}
553555
return this;
554556
}
555557

558+
public CorsMiddleware corsMiddleware() {
559+
return corsMiddleware;
560+
}
561+
556562
/**
557563
* Get blade statics list.
558564
* e.g: "/favicon.ico", "/robots.txt", "/static/", "/upload/", "/webjars/"
@@ -1103,4 +1109,5 @@ private void loadConfig(String[] args) {
11031109
}
11041110

11051111
}
1112+
11061113
}

src/main/java/com/blade/ioc/annotation/Bean.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
String value() default "";
1717

18+
@Deprecated
1819
boolean singleton() default true;
1920

2021
}

src/main/java/com/blade/ioc/annotation/Inject.java

-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717

1818
String value() default "";
1919

20-
boolean singleton() default true;
21-
2220
}

src/main/java/com/blade/kit/IocKit.java

-15
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717

1818
import com.blade.Environment;
1919
import com.blade.ioc.Ioc;
20-
import com.blade.ioc.annotation.Bean;
2120
import com.blade.ioc.annotation.Inject;
2221
import com.blade.ioc.annotation.InjectWith;
2322
import com.blade.ioc.annotation.Value;
2423
import com.blade.ioc.bean.BeanDefine;
2524
import com.blade.ioc.bean.ClassDefine;
2625
import com.blade.ioc.bean.FieldInjector;
2726
import com.blade.ioc.bean.ValueInjector;
28-
import com.blade.mvc.WebContext;
29-
import com.blade.mvc.annotation.Path;
3027
import lombok.experimental.UtilityClass;
3128

3229
import java.lang.reflect.Field;
@@ -132,18 +129,6 @@ public static void injectionValue(Environment environment, BeanDefine beanDefine
132129
}
133130

134131
public static boolean isSingleton(Class<?> type) {
135-
Bean bean = type.getAnnotation(Bean.class);
136-
if (null != bean) {
137-
return bean.singleton();
138-
}
139-
Path path = type.getAnnotation(Path.class);
140-
if (null != path) {
141-
return path.singleton();
142-
}
143-
Inject inject = type.getAnnotation(Inject.class);
144-
if (null != inject) {
145-
return inject.singleton();
146-
}
147132
return true;
148133
}
149134

src/main/java/com/blade/mvc/Const.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface Const {
3131
int DEFAULT_SERVER_PORT = 9000;
3232
String DEFAULT_SERVER_ADDRESS = "0.0.0.0";
3333
String LOCAL_IP_ADDRESS = "127.0.0.1";
34-
String VERSION = "2.0.12.BETA";
34+
String VERSION = "2.0.12.RELEASE";
3535
String WEB_JARS = "/webjars/";
3636
String CLASSPATH = BladeKit.getCurrentClassPath();
3737
String CONTENT_TYPE_HTML = "text/html; charset=UTF-8";

src/main/java/com/blade/mvc/RouteContext.java

+28-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.blade.ioc.bean.BeanDefine;
1919
import com.blade.kit.IocKit;
20+
import com.blade.mvc.handler.RouteHandler;
2021
import com.blade.mvc.http.Body;
2122
import com.blade.mvc.http.Request;
2223
import com.blade.mvc.http.Response;
@@ -46,6 +47,7 @@ public class RouteContext {
4647
private Request request;
4748
private Response response;
4849
private Object[] routeActionParameters;
50+
private boolean abort;
4951

5052
private static final String LAMBDA_IDENTIFY = "$$Lambda$";
5153

@@ -521,25 +523,35 @@ public Object[] routeParameters() {
521523
return this.routeActionParameters;
522524
}
523525

526+
public void abort() {
527+
this.abort = true;
528+
}
529+
530+
public boolean isAbort() {
531+
return this.abort;
532+
}
533+
524534
public void initRoute(Route route) {
525535
this.request.initPathParams(route);
526536
this.route = route;
527-
528-
boolean singleton = IocKit.isSingleton(route.getTargetType());
529-
530-
if (singleton) {
531-
BeanDefine beanDefine = WebContext.blade().ioc().getBeanDefine(route.getTargetType());
532-
if(beanDefine.isFieldHasPrototype()){
533-
// reset initialize
534-
IocKit.injection(WebContext.blade().ioc(), beanDefine);
535-
} else {
536-
Object target = WebContext.blade().ioc().getBean(route.getTargetType());
537-
this.route.setTarget(target);
538-
}
539-
} else {
540-
Object target = WebContext.blade().ioc().createBean(route.getTargetType());
541-
this.route.setTarget(target);
542-
}
537+
// if (null != route.getTarget() && route.getTargetType().equals(RouteHandler.class)) {
538+
// return;
539+
// }
540+
// boolean singleton = IocKit.isSingleton(route.getTargetType());
541+
//
542+
// if (singleton) {
543+
// BeanDefine beanDefine = WebContext.blade().ioc().getBeanDefine(route.getTargetType());
544+
// if (beanDefine.isFieldHasPrototype()) {
545+
// // reset initialize
546+
// IocKit.injection(WebContext.blade().ioc(), beanDefine);
547+
// } else {
548+
// Object target = WebContext.blade().ioc().getBean(route.getTargetType());
549+
// this.route.setTarget(target);
550+
// }
551+
// } else {
552+
// Object target = WebContext.blade().ioc().createBean(route.getTargetType());
553+
// this.route.setTarget(target);
554+
// }
543555
}
544556

545557
public void injectParameters() {

src/main/java/com/blade/mvc/annotation/Path.java

-6
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
*/
3232
boolean restful() default false;
3333

34-
/**
35-
* @return Whether to create a controller as a singleton, the default is.
36-
* When false, a new controller instance is created for each request.
37-
*/
38-
boolean singleton() default true;
39-
4034
/**
4135
* @return path description
4236
*/

src/main/java/com/blade/security/web/cors/CorsMiddleware.java

+43-41
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package com.blade.security.web.cors;
22

33
import com.blade.mvc.RouteContext;
4-
import com.blade.mvc.hook.WebHook;
4+
import com.blade.mvc.handler.RouteHandler;
5+
import com.blade.mvc.http.Response;
6+
import lombok.extern.slf4j.Slf4j;
7+
58
import java.util.StringJoiner;
69
import java.util.stream.Collector;
7-
import lombok.extern.slf4j.Slf4j;
810

911
/**
1012
* CorsMiddleware
13+
* <p>
14+
* This is a simple CORS policy,
15+
* you can also implement the {@link CorsMiddleware#handle} method of the class to perform custom filtering.
1116
*
1217
* @author biezhi
1318
* @date 2018/7/11
1419
*/
1520
@Slf4j
16-
public class CorsMiddleware implements WebHook {
21+
public class CorsMiddleware implements RouteHandler {
1722

1823
private CorsConfiger corsConfig;
1924

@@ -25,79 +30,76 @@ public CorsMiddleware(CorsConfiger corsConfiger) {
2530
}
2631

2732
@Override
28-
public boolean before(RouteContext context) {
29-
this.allowCredentials(context)
30-
.allowMethods(context)
31-
.allowHeads(context)
32-
.setMaxAge(context)
33-
.allowCredentials(context);
34-
if ("OPTIONS".equals(context.method())) {
35-
context.status(202);
36-
}
37-
return true;
33+
public void handle(RouteContext context) {
34+
context.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
35+
context.header("Access-Control-Allow-Origin", "*");
36+
context.header("Access-Control-Allow-Headers", CorsConfiger.ALL);
37+
context.status(204);
3838
}
3939

40-
private CorsMiddleware allowHeads(RouteContext context) {
40+
private CorsMiddleware allowHeads(Response response) {
4141
boolean isDefaultAllowHeads = corsConfig == null || corsConfig.getAllowedHeaders() == null
42-
|| corsConfig.getAllowedHeaders().size() == 0;
42+
|| corsConfig.getAllowedHeaders().size() == 0;
4343

4444
if (isDefaultAllowHeads) {
45-
context.response().header("Access-Control-Allow-Headers", CorsConfiger.ALL);
45+
response.header("Access-Control-Allow-Headers", CorsConfiger.ALL);
4646
return this;
4747
}
4848

49-
String heads = corsConfig.getAllowedHeaders().stream().collect(Collector.of(
50-
() -> new StringJoiner(","),
51-
(j, head) -> j.add(head),
52-
StringJoiner::merge,
53-
StringJoiner::toString
54-
));
55-
context.response().header("Access-Control-Allow-Headers", heads);
49+
String heads = corsConfig.getAllowedHeaders().stream()
50+
.collect(Collector.of(
51+
() -> new StringJoiner(","),
52+
StringJoiner::add,
53+
StringJoiner::merge,
54+
StringJoiner::toString
55+
));
56+
57+
response.header("Access-Control-Allow-Headers", heads);
5658
return this;
5759
}
5860

59-
private CorsMiddleware allowMethods(RouteContext context) {
61+
private CorsMiddleware allowMethods(Response response) {
6062
boolean isDefaultAllowMethods = corsConfig == null || corsConfig.getAllowedMethods() == null
61-
|| corsConfig.getAllowedMethods().size() == 0;
63+
|| corsConfig.getAllowedMethods().size() == 0;
6264

6365
if (isDefaultAllowMethods) {
64-
context.header("Access-Control-Allow-Methods",
65-
CorsConfiger.DEFAULT_ALLOWED_METHODS);
66+
response.header("Access-Control-Allow-Methods",
67+
CorsConfiger.DEFAULT_ALLOWED_METHODS);
6668
return this;
6769
}
6870

6971
String methods = corsConfig.getAllowedMethods().stream().collect(Collector.of(
70-
() -> new StringJoiner(", "),
71-
(j, method) -> j.add(method.toUpperCase()),
72-
StringJoiner::merge,
73-
StringJoiner::toString
72+
() -> new StringJoiner(", "),
73+
(j, method) -> j.add(method.toUpperCase()),
74+
StringJoiner::merge,
75+
StringJoiner::toString
7476
));
7577

76-
context.response().header("Access-Control-Allow-Methods", methods);
78+
response.header("Access-Control-Allow-Methods", methods);
7779
return this;
7880
}
7981

80-
private CorsMiddleware allowCredentials(RouteContext context) {
82+
private CorsMiddleware allowCredentials(Response response) {
8183
boolean isDefaultAllowCredentials = corsConfig == null || corsConfig.getAllowCredentials() == null;
8284

8385
if (isDefaultAllowCredentials) {
84-
context.header("Access-Control-Allow-Credentials",
85-
CorsConfiger.DEFAULT_ALLOW_CREDENTIALS);
86+
response.header("Access-Control-Allow-Credentials",
87+
CorsConfiger.DEFAULT_ALLOW_CREDENTIALS);
8688
return this;
8789
}
88-
context.response().header("Access-Control-Allow-Credentials",
89-
corsConfig.getAllowCredentials().toString());
90+
response.header("Access-Control-Allow-Credentials",
91+
corsConfig.getAllowCredentials().toString());
9092
return this;
9193
}
9294

93-
private CorsMiddleware setMaxAge(RouteContext context) {
95+
private CorsMiddleware setMaxAge(Response response) {
9496
boolean isDefaultMaxAge = corsConfig == null || corsConfig.getMaxAge() == null;
9597
if (isDefaultMaxAge) {
96-
context.response().header("Access-Control-Max-Age",
97-
CorsConfiger.DEFAULT_MAX_AGE.toString());
98+
response.header("Access-Control-Max-Age",
99+
CorsConfiger.DEFAULT_MAX_AGE.toString());
98100
return this;
99101
}
100-
context.header("Access-Control-Max-Age", corsConfig.getMaxAge().toString());
102+
response.header("Access-Control-Max-Age", corsConfig.getMaxAge().toString());
101103
return this;
102104
}
103105

0 commit comments

Comments
 (0)