Skip to content

Commit

Permalink
🔖 release 2.0.8.RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jun 16, 2018
1 parent 1cce172 commit 781e30e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 69 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Create a basic `Maven` project
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.8-RELEASE</version>
<version>2.0.8.RELEASE</version>
</dependency>
```

Expand All @@ -73,7 +73,7 @@ Create a basic `Maven` project
or `Gradle`:

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

Write `main` method, try `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 @@ -63,7 +63,7 @@
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.8-RELEASE</version>
<version>2.0.8.RELEASE</version>
</dependency>
```

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

```sh
compile 'com.bladejava:blade-mvc:2.0.8-RELEASE'
compile 'com.bladejava:blade-mvc:2.0.8.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.8-SNAPSHOT</version>
<version>2.0.8.RELEASE</version>
<packaging>jar</packaging>

<name>blade</name>
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/com/blade/mvc/http/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
@Slf4j
public class HttpResponse implements Response {

private HttpHeaders headers = new DefaultHttpHeaders(false);
private Set<Cookie> cookies = new HashSet<>(4);
private int statusCode = 200;
private String contentType = null;
private Body body;
private Map<String, String> headers = new HashMap<>(8);
private Set<Cookie> cookies = new HashSet<>(4);

private int statusCode = 200;
private String contentType = HttpConst.CONTENT_TYPE_HTML;
private Body body;

@Override
public int statusCode() {
Expand All @@ -56,17 +57,13 @@ public String contentType() {

@Override
public Map<String, String> headers() {
Map<String, String> map = new HashMap<>(this.headers.size());
this.headers.forEach(header -> map.put(header.getKey(), header.getValue()));
if (StringKit.isNotEmpty(this.contentType)) {
map.put(HttpConst.CONTENT_TYPE_STRING, this.contentType);
}
return map;
this.headers.put(HttpConst.CONTENT_TYPE_STRING, this.contentType);
return this.headers;
}

@Override
public Response header(CharSequence name, CharSequence value) {
this.headers.set(name, value);
public Response header(String name, String value) {
this.headers.put(name, value);
return this;
}

Expand Down Expand Up @@ -152,10 +149,9 @@ public void download(@NonNull String fileName, @NonNull File file) throws Except
throw new NotFoundException("Not found file: " + file.getPath());
}
String contentType = StringKit.mimeType(file.getName());
headers.set("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1"));
headers.set(HttpConst.CONTENT_LENGTH, file.length());
headers.set(HttpConst.CONTENT_TYPE_STRING, contentType);

headers.put("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1"));
headers.put(HttpConst.CONTENT_LENGTH.toString(), String.valueOf(file.length()));
headers.put(HttpConst.CONTENT_TYPE_STRING, contentType);
this.body = new StreamBody(new FileInputStream(file));
}

Expand All @@ -173,7 +169,7 @@ public void render(@NonNull ModelAndView modelAndView) {

@Override
public void redirect(@NonNull String newUri) {
headers.set(HttpConst.LOCATION, newUri);
headers.put(HttpConst.LOCATION.toString(), newUri);
this.status(302);
this.body = EmptyBody.empty();
}
Expand All @@ -190,7 +186,7 @@ public HttpResponse(Response response) {
this.contentType = response.contentType();
this.statusCode = response.statusCode();
if (null != response.headers()) {
response.headers().forEach(this.headers::add);
response.headers().forEach(this.headers::put);
}
if (null != response.cookies()) {
response.cookies().forEach((k, v) -> this.cookies.add(new DefaultCookie(k, v)));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/blade/mvc/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ default Response notFound() {
* @param value Header Value
* @return Return Response
*/
Response header(CharSequence name, CharSequence value);
Response header(String name, String value);

/**
* Get current response cookies
Expand Down
76 changes: 32 additions & 44 deletions src/main/java/com/blade/server/netty/HttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) {
}
}

private void exceptionCaught(String uri, String method, Exception e) {
if (e instanceof BladeException) {
} else {
log500(log, method, uri);
}
if (null != exceptionHandler) {
exceptionHandler.handle(e);
} else {
log.error("Request execution error", e);
}
}

private boolean execution(ChannelHandlerContext ctx, boolean keepAlive, Signature signature, String cleanUri) throws Exception {
Request request = signature.request();

Expand Down Expand Up @@ -191,7 +179,7 @@ private CompletableFuture<Response> timeoutResponseFuture() {

private static final Timer timer = new HashedWheelTimer();

public static <T> CompletableFuture<T> afterTimeout(T value, long millis) {
private static <T> CompletableFuture<T> afterTimeout(T value, long millis) {
CompletableFuture<T> future = new CompletableFuture<>();
timer.newTimeout(t -> future.complete(value), millis, TimeUnit.MILLISECONDS);
return future;
Expand Down Expand Up @@ -265,9 +253,8 @@ private Map<String, String> getDefaultHeader() {
private void handleStreamResponse(int status, Map<String, String> headers, InputStream body,
ChannelHandlerContext context, boolean keepAlive) {
DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(status));
response.headers().set(TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
headers.entrySet().stream().forEach(header ->
response.headers().set(header.getKey(), header.getValue()));
response.headers().set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
headers.forEach((key, value) -> response.headers().set(key, value));
context.write(response);

context.write(new ChunkedStream(body));
Expand Down Expand Up @@ -300,7 +287,7 @@ private FullHttpResponse createFullResponse(int status, Map<String, String> head
*
* @param signature signature
*/
public void routeHandle(Signature signature) throws Exception {
private void routeHandle(Signature signature) {
Object target = signature.getRoute().getTarget();
if (null == target) {
Class<?> clazz = signature.getAction().getDeclaringClass();
Expand All @@ -311,20 +298,7 @@ public void routeHandle(Signature signature) throws Exception {
RouteHandler routeHandler = (RouteHandler) target;
routeHandler.handle(signature.request(), signature.response());
} else {
this.handle(signature);
}
}

/**
* handle route signature
*
* @param signature route request signature
* @throws Exception throw like parse param exception
*/
public void handle(Signature signature) throws Exception {
try {
Method actionMethod = signature.getAction();
Object target = signature.getRoute().getTarget();
Class<?> returnType = actionMethod.getReturnType();

Response response = signature.response();
Expand All @@ -335,31 +309,33 @@ public void handle(Signature signature) throws Exception {
boolean isRestful = (null != JSON) || (null != path && path.restful());

// if request is restful and not InternetExplorer userAgent
if (isRestful && !signature.request().userAgent().contains(HttpConst.IE_UA)) {
signature.response().contentType(Const.CONTENT_TYPE_JSON);
if (isRestful) {
if (!signature.request().isIE()) {
signature.response().contentType(Const.CONTENT_TYPE_JSON);
} else {
signature.response().contentType(Const.CONTENT_TYPE_HTML);
}
}

int len = actionMethod.getParameterTypes().length;

MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
Object returnParam = methodAccess.invoke(target, actionMethod.getName(), len > 0 ? signature.getParameters() : null);

if (null == returnParam) return;
if (null == returnParam) {
return;
}

if (isRestful) {
response.json(returnParam);
return;
}
if (returnType == String.class) {
response.render(returnParam.toString());
response.body(new ViewBody(new ModelAndView(returnParam.toString())));
return;
}
if (returnType == ModelAndView.class) {
ModelAndView modelAndView = (ModelAndView) returnParam;
response.render(modelAndView);
response.body(new ViewBody((ModelAndView) returnParam));
}
} catch (Exception e) {
throw e;
}
}

Expand All @@ -371,7 +347,7 @@ public void handle(Signature signature) throws Exception {
* @return Return true then next handler, and else interrupt request
* @throws Exception throw like parse param exception
*/
public boolean invokeHook(Signature routeSignature, Route hookRoute) throws Exception {
private boolean invokeHook(Signature routeSignature, Route hookRoute) throws Exception {
Method hookMethod = hookRoute.getAction();
Object target = hookRoute.getTarget();
if (null == target) {
Expand All @@ -388,10 +364,10 @@ public boolean invokeHook(Signature routeSignature, Route hookRoute) throws Exce
if (len > 0) {
if (len == 1) {
MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
returnParam = methodAccess.invoke(target, hookMethod.getName(), new Object[]{routeSignature});
returnParam = methodAccess.invoke(target, hookMethod.getName(), routeSignature);
} else if (len == 2) {
MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
returnParam = methodAccess.invoke(target, hookMethod.getName(), new Object[]{routeSignature.request(), routeSignature.response()});
returnParam = methodAccess.invoke(target, hookMethod.getName(), routeSignature.request(), routeSignature.response());
} else {
throw new InternalErrorException("Bad web hook structure");
}
Expand All @@ -408,7 +384,7 @@ public boolean invokeHook(Signature routeSignature, Route hookRoute) throws Exce
return true;
}

public boolean invokeMiddleware(List<Route> middleware, Signature signature) throws BladeException {
private boolean invokeMiddleware(List<Route> middleware, Signature signature) throws BladeException {
if (BladeKit.isEmpty(middleware)) {
return true;
}
Expand All @@ -427,7 +403,7 @@ public boolean invokeMiddleware(List<Route> middleware, Signature signature) thr
* @param signature http request
* @return return invoke hook is abort
*/
public boolean invokeHook(List<Route> hooks, Signature signature) throws Exception {
private boolean invokeHook(List<Route> hooks, Signature signature) throws Exception {
for (Route hook : hooks) {
if (hook.getTargetType() == RouteHandler.class) {
RouteHandler routeHandler = (RouteHandler) hook.getTarget();
Expand All @@ -445,4 +421,16 @@ private boolean isStaticFile(String uri) {
return result.isPresent();
}

private void exceptionCaught(String uri, String method, Exception e) {
if (e instanceof BladeException) {
} else {
log500(log, method, uri);
}
if (null != exceptionHandler) {
exceptionHandler.handle(e);
} else {
log.error("Request Exception", e);
}
}

}

0 comments on commit 781e30e

Please sign in to comment.