Skip to content

Commit 2715f11

Browse files
committed
Fixed bug and add cross site header to customer configuration.
1 parent 22c8d62 commit 2715f11

File tree

10 files changed

+88
-14
lines changed

10 files changed

+88
-14
lines changed

EasyRest/src/main/java/tech/dbgsoftware/easyrest/model/Response.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import io.netty.handler.codec.http.HttpHeaderValues;
88
import io.netty.handler.codec.http.HttpResponseStatus;
99

10-
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
11-
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
12-
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import static io.netty.handler.codec.http.HttpHeaderNames.*;
1314
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
1415
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
1516

@@ -18,12 +19,17 @@ public class Response {
1819
private final static Gson GSON = new Gson();
1920
private FullHttpResponse response;
2021
private HttpResponseStatus status = HttpResponseStatus.OK;
22+
private Map<String, String> responseHeaders = new HashMap<>();
2123

2224
public Response(){
2325
this.response = newResponse(ResponseEntity.buildOkResponse());
2426
this.response.setStatus(status);
2527
}
2628

29+
public Map<String, String> getResponseHeaders() {
30+
return responseHeaders;
31+
}
32+
2733
public Response buildResponse(Object responseEntity){
2834
this.response = newResponse(responseEntity);
2935
this.response.setStatus(status);
@@ -43,6 +49,7 @@ private FullHttpResponse newResponse(Object responseEntity){
4349
response.headers().set(CONTENT_TYPE, "application/json");
4450
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
4551
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
52+
responseHeaders.forEach((k, v) -> response.headers().set(k, v));
4653
return response;
4754
}
4855

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/NettyInit.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package tech.dbgsoftware.easyrest.network;
22

3-
import tech.dbgsoftware.easyrest.network.conf.ChannelOptionBuilder;
4-
import tech.dbgsoftware.easyrest.network.core.api.BaseConfiguration;
5-
import tech.dbgsoftware.easyrest.network.core.pipeline.in.RequestProcessHandler;
63
import io.netty.bootstrap.ServerBootstrap;
74
import io.netty.channel.*;
85
import io.netty.channel.epoll.Epoll;
@@ -34,6 +31,7 @@
3431
public class NettyInit implements BaseConfiguration {
3532

3633
private int port = 8080;
34+
private String host = "*";
3735
private int ioExecutors = Runtime.getRuntime().availableProcessors() * 2;
3836
public static String SystemName = "EasyRest";
3937
private int maxContentLength = 20480;
@@ -49,7 +47,12 @@ public class NettyInit implements BaseConfiguration {
4947
public NettyInit(){}
5048

5149
public NettyInit(int port){
50+
this("*", port);
51+
}
52+
53+
public NettyInit(String host, int port) {
5254
setPort(port);
55+
setHost(host);
5356
}
5457

5558
public ServerBootstrap build(){
@@ -83,6 +86,15 @@ public NettyInit setPort(int port){
8386
return this;
8487
}
8588

89+
@Override
90+
public String getHost() {
91+
return host;
92+
}
93+
94+
public void setHost(String host) {
95+
this.host = host;
96+
}
97+
8698
public NettyInit setIoExecutors(int ioExecutors){
8799
if (ioExecutors < 1) {
88100
this.ioExecutors = Runtime.getRuntime().availableProcessors() * 2;

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/core/api/BaseConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public interface BaseConfiguration {
66

77
int getPort();
88

9+
String getHost();
10+
911
int getIoExecutors();
1012

1113
String getSystemName();

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/core/pipeline/in/RequestProcessHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHtt
3333

3434
@Override
3535
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
36+
if (ctx.channel().isActive()){
37+
ctx.close();
38+
}
3639
super.exceptionCaught(ctx, cause);
3740
}
3841

3942
private HttpEntity createNewRequestEntity(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest){
4043
Request request = new Request(fullHttpRequest);
4144
Response response = new Response();
42-
return new HttpEntity(request, response, channelHandlerContext);
45+
HttpEntity httpEntity = new HttpEntity(request, response, channelHandlerContext);
46+
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Origin", baseConfiguration.getHost().equalsIgnoreCase("*") ? "*" : (baseConfiguration.getHost() + ":" + baseConfiguration.getPort()));
47+
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
48+
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Headers", "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization");
49+
return httpEntity;
4350
}
4451

4552
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package tech.dbgsoftware.easyrest.network.core.pipeline.utils;
2+
23
import io.netty.buffer.ByteBuf;
4+
import tech.dbgsoftware.easyrest.utils.LogUtils;
35

46
public class ByteBufUtils {
57

68
public static byte[] readAll(ByteBuf byteBuf) {
7-
byte[] contentByte = new byte[byteBuf.readableBytes()];
8-
byteBuf.readBytes(contentByte);
9-
return contentByte;
9+
try {
10+
byte[] contentByte = new byte[byteBuf.readableBytes()];
11+
byteBuf.readBytes(contentByte);
12+
return contentByte;
13+
} catch (Exception e) {
14+
LogUtils.error(e.getMessage());
15+
}
16+
return new byte[0];
1017
}
1118
}

EasyRest/src/main/java/tech/dbgsoftware/easyrest/utils/LogUtils.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.GsonBuilder;
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
7+
import tech.dbgsoftware.easyrest.exception.PageNotFoundException;
78

89
import java.util.Date;
910

@@ -42,11 +43,23 @@ public static void info(Object obj, Class aClass){
4243
LOGGER.info(String.format("%s %s", String.valueOf(new Date()), "From " + aClass.getName() + ": " + GSON.toJson(obj)));
4344
}
4445

46+
public static void error(String message){
47+
LOGGER.error(String.format("%s %s", String.valueOf(new Date()), message));
48+
}
49+
4550
public static void error(String message, Object object){
46-
LOGGER.error(String.format("%s %s", String.valueOf(new Date()), message), object);
51+
if (object == null) {
52+
error(message);
53+
} else {
54+
LOGGER.error(String.format("%s %s", String.valueOf(new Date()), message), object);
55+
}
4756
}
4857

4958
public static void error(String message, Exception e){
50-
LOGGER.error(String.format("%s %s", String.valueOf(new Date()), message), e);
59+
if (e instanceof PageNotFoundException) {
60+
error(message);
61+
} else {
62+
LOGGER.error(String.format("%s %s", String.valueOf(new Date()), message), e);
63+
}
5164
}
5265
}

Example/Example-Quick-Start/src/main/java/com/example/Example.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Example {
77

88
public static void main(String[] args) {
99
EasyRest easyRest = new EasyRest("classpath:MyExampleApplicationContext.xml");
10-
easyRest.startup("EasyRestServer");
10+
easyRest.startup("EasyRestServer", 8080);
1111
}
1212

1313
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.rest;
2+
3+
import tech.dbgsoftware.easyrest.annotations.method.BindURL;
4+
import tech.dbgsoftware.easyrest.annotations.method.Get;
5+
6+
@BindURL("/test")
7+
public interface TestRestService {
8+
9+
@Get
10+
String ping();
11+
12+
}

Example/Example-Quick-Start/src/main/java/com/example/controller/StockInfoRestServiceImpl.java renamed to Example/Example-Quick-Start/src/main/java/com/example/services/StockInfoRestServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.controller;
1+
package com.example.services;
22

33
import tech.dbgsoftware.easyrest.model.ResponseEntity;
44
import com.example.model.Stock;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.services;
2+
3+
import com.example.rest.TestRestService;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class TestServiceImpl implements TestRestService {
8+
9+
@Override
10+
public String ping() {
11+
return ("Pong");
12+
}
13+
14+
}

0 commit comments

Comments
 (0)