Skip to content

Commit

Permalink
Merge pull request #302 from lets-blade/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hellokaton authored Dec 2, 2018
2 parents 6757d03 + 2bf3505 commit 41a03ce
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 130 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ Run with `Maven`:
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.12.BETA</version>
<version>2.0.12.RELEASE</version>
</dependency>
```

or `Gradle`:

```sh
compile 'com.bladejava:blade-mvc:2.0.12.BETA'
compile 'com.bladejava:blade-mvc:2.0.12.RELEASE'
```

Write the `main` method and the `Hello World`:
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.12.BETA</version>
<version>2.0.12.RELEASE</version>
</dependency>
```

Expand All @@ -70,7 +70,7 @@
或者 `Gradle`:

```sh
compile 'com.bladejava:blade-mvc:2.0.12.BETA'
compile 'com.bladejava:blade-mvc:2.0.12.RELEASE'
```

编写 `main` 函数写一个 `Hello World`
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.12.BETA</version>
<version>2.0.12.RELEASE</version>
<packaging>jar</packaging>

<name>blade</name>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/blade/Blade.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public class Blade {
*/
private ExceptionHandler exceptionHandler = new DefaultExceptionHandler();

private CorsMiddleware corsMiddleware;

/**
* Used to identify whether the web server has started
*/
Expand Down Expand Up @@ -548,11 +550,15 @@ public Blade enableCors(boolean enableCors) {
public Blade enableCors(boolean enableCors, CorsConfiger corsConfig) {
this.environment.set(ENV_KEY_CORS_ENABLE, enableCors);
if (enableCors) {
this.use(new CorsMiddleware(corsConfig));
this.corsMiddleware = new CorsMiddleware(corsConfig);
}
return this;
}

public CorsMiddleware corsMiddleware() {
return corsMiddleware;
}

/**
* Get blade statics list.
* e.g: "/favicon.ico", "/robots.txt", "/static/", "/upload/", "/webjars/"
Expand Down Expand Up @@ -1103,4 +1109,5 @@ private void loadConfig(String[] args) {
}

}

}
1 change: 1 addition & 0 deletions src/main/java/com/blade/ioc/annotation/Bean.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

String value() default "";

@Deprecated
boolean singleton() default true;

}
2 changes: 0 additions & 2 deletions src/main/java/com/blade/ioc/annotation/Inject.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@

String value() default "";

boolean singleton() default true;

}
15 changes: 0 additions & 15 deletions src/main/java/com/blade/kit/IocKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

import com.blade.Environment;
import com.blade.ioc.Ioc;
import com.blade.ioc.annotation.Bean;
import com.blade.ioc.annotation.Inject;
import com.blade.ioc.annotation.InjectWith;
import com.blade.ioc.annotation.Value;
import com.blade.ioc.bean.BeanDefine;
import com.blade.ioc.bean.ClassDefine;
import com.blade.ioc.bean.FieldInjector;
import com.blade.ioc.bean.ValueInjector;
import com.blade.mvc.WebContext;
import com.blade.mvc.annotation.Path;
import lombok.experimental.UtilityClass;

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

public static boolean isSingleton(Class<?> type) {
Bean bean = type.getAnnotation(Bean.class);
if (null != bean) {
return bean.singleton();
}
Path path = type.getAnnotation(Path.class);
if (null != path) {
return path.singleton();
}
Inject inject = type.getAnnotation(Inject.class);
if (null != inject) {
return inject.singleton();
}
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/blade/mvc/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface Const {
int DEFAULT_SERVER_PORT = 9000;
String DEFAULT_SERVER_ADDRESS = "0.0.0.0";
String LOCAL_IP_ADDRESS = "127.0.0.1";
String VERSION = "2.0.12.BETA";
String VERSION = "2.0.12.RELEASE";
String WEB_JARS = "/webjars/";
String CLASSPATH = BladeKit.getCurrentClassPath();
String CONTENT_TYPE_HTML = "text/html; charset=UTF-8";
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/com/blade/mvc/RouteContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.blade.ioc.bean.BeanDefine;
import com.blade.kit.IocKit;
import com.blade.mvc.handler.RouteHandler;
import com.blade.mvc.http.Body;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class RouteContext {
private Request request;
private Response response;
private Object[] routeActionParameters;
private boolean abort;

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

Expand Down Expand Up @@ -521,25 +523,35 @@ public Object[] routeParameters() {
return this.routeActionParameters;
}

public void abort() {
this.abort = true;
}

public boolean isAbort() {
return this.abort;
}

public void initRoute(Route route) {
this.request.initPathParams(route);
this.route = route;

boolean singleton = IocKit.isSingleton(route.getTargetType());

if (singleton) {
BeanDefine beanDefine = WebContext.blade().ioc().getBeanDefine(route.getTargetType());
if(beanDefine.isFieldHasPrototype()){
// reset initialize
IocKit.injection(WebContext.blade().ioc(), beanDefine);
} else {
Object target = WebContext.blade().ioc().getBean(route.getTargetType());
this.route.setTarget(target);
}
} else {
Object target = WebContext.blade().ioc().createBean(route.getTargetType());
this.route.setTarget(target);
}
// if (null != route.getTarget() && route.getTargetType().equals(RouteHandler.class)) {
// return;
// }
// boolean singleton = IocKit.isSingleton(route.getTargetType());
//
// if (singleton) {
// BeanDefine beanDefine = WebContext.blade().ioc().getBeanDefine(route.getTargetType());
// if (beanDefine.isFieldHasPrototype()) {
// // reset initialize
// IocKit.injection(WebContext.blade().ioc(), beanDefine);
// } else {
// Object target = WebContext.blade().ioc().getBean(route.getTargetType());
// this.route.setTarget(target);
// }
// } else {
// Object target = WebContext.blade().ioc().createBean(route.getTargetType());
// this.route.setTarget(target);
// }
}

public void injectParameters() {
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/blade/mvc/annotation/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
*/
boolean restful() default false;

/**
* @return Whether to create a controller as a singleton, the default is.
* When false, a new controller instance is created for each request.
*/
boolean singleton() default true;

/**
* @return path description
*/
Expand Down
84 changes: 43 additions & 41 deletions src/main/java/com/blade/security/web/cors/CorsMiddleware.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.blade.security.web.cors;

import com.blade.mvc.RouteContext;
import com.blade.mvc.hook.WebHook;
import com.blade.mvc.handler.RouteHandler;
import com.blade.mvc.http.Response;
import lombok.extern.slf4j.Slf4j;

import java.util.StringJoiner;
import java.util.stream.Collector;
import lombok.extern.slf4j.Slf4j;

/**
* CorsMiddleware
* <p>
* This is a simple CORS policy,
* you can also implement the {@link CorsMiddleware#handle} method of the class to perform custom filtering.
*
* @author biezhi
* @date 2018/7/11
*/
@Slf4j
public class CorsMiddleware implements WebHook {
public class CorsMiddleware implements RouteHandler {

private CorsConfiger corsConfig;

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

@Override
public boolean before(RouteContext context) {
this.allowCredentials(context)
.allowMethods(context)
.allowHeads(context)
.setMaxAge(context)
.allowCredentials(context);
if ("OPTIONS".equals(context.method())) {
context.status(202);
}
return true;
public void handle(RouteContext context) {
context.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
context.header("Access-Control-Allow-Origin", "*");
context.header("Access-Control-Allow-Headers", CorsConfiger.ALL);
context.status(204);
}

private CorsMiddleware allowHeads(RouteContext context) {
private CorsMiddleware allowHeads(Response response) {
boolean isDefaultAllowHeads = corsConfig == null || corsConfig.getAllowedHeaders() == null
|| corsConfig.getAllowedHeaders().size() == 0;
|| corsConfig.getAllowedHeaders().size() == 0;

if (isDefaultAllowHeads) {
context.response().header("Access-Control-Allow-Headers", CorsConfiger.ALL);
response.header("Access-Control-Allow-Headers", CorsConfiger.ALL);
return this;
}

String heads = corsConfig.getAllowedHeaders().stream().collect(Collector.of(
() -> new StringJoiner(","),
(j, head) -> j.add(head),
StringJoiner::merge,
StringJoiner::toString
));
context.response().header("Access-Control-Allow-Headers", heads);
String heads = corsConfig.getAllowedHeaders().stream()
.collect(Collector.of(
() -> new StringJoiner(","),
StringJoiner::add,
StringJoiner::merge,
StringJoiner::toString
));

response.header("Access-Control-Allow-Headers", heads);
return this;
}

private CorsMiddleware allowMethods(RouteContext context) {
private CorsMiddleware allowMethods(Response response) {
boolean isDefaultAllowMethods = corsConfig == null || corsConfig.getAllowedMethods() == null
|| corsConfig.getAllowedMethods().size() == 0;
|| corsConfig.getAllowedMethods().size() == 0;

if (isDefaultAllowMethods) {
context.header("Access-Control-Allow-Methods",
CorsConfiger.DEFAULT_ALLOWED_METHODS);
response.header("Access-Control-Allow-Methods",
CorsConfiger.DEFAULT_ALLOWED_METHODS);
return this;
}

String methods = corsConfig.getAllowedMethods().stream().collect(Collector.of(
() -> new StringJoiner(", "),
(j, method) -> j.add(method.toUpperCase()),
StringJoiner::merge,
StringJoiner::toString
() -> new StringJoiner(", "),
(j, method) -> j.add(method.toUpperCase()),
StringJoiner::merge,
StringJoiner::toString
));

context.response().header("Access-Control-Allow-Methods", methods);
response.header("Access-Control-Allow-Methods", methods);
return this;
}

private CorsMiddleware allowCredentials(RouteContext context) {
private CorsMiddleware allowCredentials(Response response) {
boolean isDefaultAllowCredentials = corsConfig == null || corsConfig.getAllowCredentials() == null;

if (isDefaultAllowCredentials) {
context.header("Access-Control-Allow-Credentials",
CorsConfiger.DEFAULT_ALLOW_CREDENTIALS);
response.header("Access-Control-Allow-Credentials",
CorsConfiger.DEFAULT_ALLOW_CREDENTIALS);
return this;
}
context.response().header("Access-Control-Allow-Credentials",
corsConfig.getAllowCredentials().toString());
response.header("Access-Control-Allow-Credentials",
corsConfig.getAllowCredentials().toString());
return this;
}

private CorsMiddleware setMaxAge(RouteContext context) {
private CorsMiddleware setMaxAge(Response response) {
boolean isDefaultMaxAge = corsConfig == null || corsConfig.getMaxAge() == null;
if (isDefaultMaxAge) {
context.response().header("Access-Control-Max-Age",
CorsConfiger.DEFAULT_MAX_AGE.toString());
response.header("Access-Control-Max-Age",
CorsConfiger.DEFAULT_MAX_AGE.toString());
return this;
}
context.header("Access-Control-Max-Age", corsConfig.getMaxAge().toString());
response.header("Access-Control-Max-Age", corsConfig.getMaxAge().toString());
return this;
}

Expand Down
Loading

0 comments on commit 41a03ce

Please sign in to comment.