Skip to content

Commit 6a0bb20

Browse files
committed
version 0.0.0.9 some thing update bate 1
1. add deep folder support
1 parent 9124632 commit 6a0bb20

13 files changed

+87
-57
lines changed

src/top/gunplan/netty/handles/GunStdHttpHandle.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import top.gunplan.netty.protocol.GunHttp2RequestProtocl;
1010
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
11-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
11+
import top.gunplan.netty.protocol.AbstractGunHttp2Response;
1212
import top.gunplan.nio.utils.GunBaseLogUtil;
1313
import top.gunplan.nio.utils.GunDirectoryUtil;
1414
import top.gunplan.nio.utils.GunStringUtil;
@@ -24,32 +24,27 @@
2424
*/
2525

2626
public class GunStdHttpHandle implements GunBootServer.GunNetHandle {
27-
private final ThreadLocal<HashMap<String, Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>>>> localUrlMapping = new ThreadLocal<>();
28-
private HashMap<String, Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>>> urlMapping = new HashMap<>();
27+
private final ThreadLocal<HashMap<String, Class<? extends GunHttpMappingHandle<AbstractGunHttp2Response>>>> localUrlMapping = new ThreadLocal<>();
28+
private HashMap<String, Class<? extends GunHttpMappingHandle<AbstractGunHttp2Response>>> urlMapping = new HashMap<>();
2929

3030
public GunStdHttpHandle(final String handlePackName) {
3131
ClassLoader loader = this.getClass().getClassLoader();
32-
List<File> classfiles;
32+
List<GunDirectoryUtil.GunHttpMappingFileReference> classfiles;
3333
try {
3434
classfiles = GunDirectoryUtil.scanAllFilesFromDirectory(loader.getResource("").getPath().replace("%20", " ") + handlePackName.replace(".", "/"));
3535
} catch (IOException e) {
3636
throw new GunException(e);
3737
}
3838
assert classfiles != null;
3939
classfiles.forEach(classfilename -> {
40-
try {
41-
loader.loadClass("top.gunplan.netty.anno.GunHttpmapping");
42-
} catch (ClassNotFoundException e) {
43-
e.printStackTrace();
44-
}
45-
Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>> httpMapping;
40+
Class<? extends GunHttpMappingHandle<AbstractGunHttp2Response>> httpMapping;
4641
try {
4742
/**
4843
*
4944
* warning:It could be inside class in Mappingclass with out GunHttpmapping Annotation(
5045
*/
5146

52-
httpMapping = (Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>>) loader.loadClass(handlePackName + "." + classfilename.getName().replace(".class", ""));
47+
httpMapping = (Class<? extends GunHttpMappingHandle<AbstractGunHttp2Response>>) loader.loadClass(handlePackName + classfilename.getBase() + classfilename.getClcasfile().getName().replace(".class", ""));
5348
if (httpMapping.isAnnotationPresent(GunHttpmapping.class)) {
5449
urlMapping.put(httpMapping.getAnnotation(GunHttpmapping.class).mappingRule(), httpMapping);
5550
}
@@ -65,27 +60,28 @@ public GunNetResponseInterface dealDataEvent(GunNetRequestInterface requestInter
6560

6661
localUrlMapping.set(urlMapping);
6762
GunHttp2RequestProtocl request = ((GunHttp2RequestProtocl) requestInterface);
68-
GunHttpMappingHandle<GunHttp2ResponseInterface> runner = null;
63+
GunHttpMappingHandle<AbstractGunHttp2Response> runner = null;
6964
try {
7065
runner = findHandelandRun(request.getRequestUrl());
7166
} catch (Exception exp) {
7267
GunBaseLogUtil.error(exp.getMessage());
7368
}
69+
assert runner != null;
7470
return runner.doResponse(request);
7571
// localUrlMapping.get().
7672
}
7773

78-
private GunHttpMappingHandle<GunHttp2ResponseInterface> findHandelandRun(String requestUrl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
79-
HashMap<String, Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>>> dealmap = localUrlMapping.get();
80-
Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>> dealhandel = dealmap.get(requestUrl);
74+
private GunHttpMappingHandle<AbstractGunHttp2Response> findHandelandRun(String requestUrl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
75+
HashMap<String, Class<? extends GunHttpMappingHandle<AbstractGunHttp2Response>>> dealmap = localUrlMapping.get();
76+
Class<? extends GunHttpMappingHandle<AbstractGunHttp2Response>> dealhandel = dealmap.get(requestUrl);
8177
while (dealhandel == null) {
8278
requestUrl = GunStringUtil.removeLastUrl(requestUrl);
8379
dealhandel = dealmap.get(requestUrl + "*");
8480
if ("/".equals(requestUrl) && dealhandel == null) {
8581
throw new GunException("404 or 404 pages not found");
8682
}
8783
}
88-
GunHttpMappingHandle<GunHttp2ResponseInterface> instance = dealhandel.getConstructor().newInstance();
84+
GunHttpMappingHandle<AbstractGunHttp2Response> instance = dealhandel.getConstructor().newInstance();
8985
return instance;
9086
}
9187

src/top/gunplan/netty/handles/http/GunHttpMappingHandle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package top.gunplan.netty.handles.http;
22

3-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
3+
import top.gunplan.netty.protocol.AbstractGunHttp2Response;
44
import top.gunplan.netty.protocol.GunNetRequestInterface;
55

66
/**
77
* @param <T>
88
* @author dosdrtt
99
*/
10-
public interface GunHttpMappingHandle<T extends GunHttp2ResponseInterface> {
10+
public interface GunHttpMappingHandle<T extends AbstractGunHttp2Response> {
1111
/**
1212
* @return
1313
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package top.gunplan.netty.protocol;
2+
3+
/**
4+
* @author dosdrtt
5+
*/
6+
public abstract class AbstractGunHttp2Response implements GunNetResponseInterface {
7+
@Override
8+
public byte[] serialize() {
9+
return getResponseBody().getBytes();
10+
}
11+
12+
/**
13+
* this function is used to set http response result
14+
* @return response string
15+
*/
16+
public abstract String getResponseBody();
17+
}

src/top/gunplan/netty/protocol/BaseGunHttp2Response.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* @author dosdrtt
77
*/
8-
public abstract class BaseGunHttp2Response implements GunHttp2ResponseInterface {
8+
public abstract class BaseGunHttp2Response extends AbstractGunHttp2Response {
99
private GunHttpStdInfo.HttpProtoclType protoclType = GunHttpStdInfo.HttpProtoclType.HTTP1_1;
1010
private GunHttpStdInfo.statusCode code = GunHttpStdInfo.statusCode.OK;
1111
private GunHttpStdInfo.ContentType contentType = GunHttpStdInfo.ContentType.TEXT_JSON;
@@ -78,28 +78,26 @@ public void addCookie(GunHttpStdInfo.GunCookies cookies) {
7878
}
7979

8080
@Override
81-
public byte[] serialize() {
81+
public String getResponseBody() {
8282
Map<String, String> httpHead = this.mmap;
8383
StringBuilder http2resp = new StringBuilder();
84-
http2resp.append(protoclType.getVal());
85-
http2resp.append(" ");
86-
http2resp.append(code.getVal());
87-
http2resp.append(" ");
88-
http2resp.append(code).append("\r\n");
89-
http2resp.append("Content-Type:").append(contentType.getVal()).append("\r\n");
84+
http2resp.append(protoclType.getVal()).append(" ").append(code.getVal()).append(" ");
85+
http2resp.append(code).append("\n");
86+
http2resp.append("Content-Type:").append(contentType.getVal()).append("\n");
9087
for (String key : httpHead.keySet()) {
91-
http2resp.append(key).append(":").append(httpHead.get(key)).append("\r\n");
88+
http2resp.append(key).append(":").append(httpHead.get(key)).append("\n");
9289
}
9390
for (GunHttpStdInfo.GunCookies cookie : cookies) {
94-
http2resp.append("Set-Cookie").append(":").append(cookie.toString()).append("\r\n");
91+
http2resp.append("Set-Cookie").append(":").append(cookie.toString()).append("\n");
9592
}
9693

97-
http2resp.append("Content-Length:").append(this.toResponse().length()).append("\r\n\r\n");
94+
http2resp.append("Content-Length:").append(this.toResponse().length()).append("\n\n");
9895
http2resp.append(this.toResponse());
9996

100-
return http2resp.toString().getBytes();
101-
97+
return http2resp.toString();
10298
}
10399

100+
public abstract String toResponse();
101+
104102

105103
}

src/top/gunplan/netty/protocol/GunHttp2ResponseInterface.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/top/gunplan/netty/protocol/GunNetRequestInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
public interface GunNetRequestInterface {
77
/**
8+
* get the unSerialize result succeed or fail
89
* @param in byte[]
910
* @return serialize status
1011
*/

src/top/gunplan/netty/protocol/GunNetResponseInterface.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package top.gunplan.netty.protocol;
22

3-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
3+
44

55
/**
66
* @author dosdrtt
77
*/
88
public interface GunNetResponseInterface {
99
/**
10-
* @return bytes[]
10+
* @return bytes[] transfer to client
1111
*/
1212
byte[] serialize();
1313

1414
/**
15-
* @return
15+
* get is or not return
16+
* @return is or not return to client
1617
*/
1718
boolean isReturn();
1819
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package top.gunplan.netty.protocol.resputil;
2+
3+
public interface GunHttp2ResponseBody {
4+
public String toTransfer();
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package top.gunplan.netty.protocol.resputil;
2+
3+
import java.util.HashMap;
4+
5+
public class GunMappingJsonResp extends HashMap<String, String> implements GunHttp2ResponseBody {
6+
@Override
7+
public String toTransfer() {
8+
StringBuilder json = new StringBuilder("{\"");
9+
for (String key : super.keySet()) {
10+
json.append(key).append("\":\"").append(super.get(key)).append("\",");
11+
}
12+
json.append("}");
13+
return json.toString();
14+
}
15+
}

src/top/gunplan/netty/test/BaseMapping.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import top.gunplan.netty.anno.GunHttpmapping;
44
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
55
import top.gunplan.netty.protocol.BaseGunHttp2Response;
6-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
6+
import top.gunplan.netty.protocol.AbstractGunHttp2Response;
77
import top.gunplan.netty.protocol.GunHttpStdInfo;
88
import top.gunplan.netty.protocol.GunNetRequestInterface;
99

10-
@GunHttpmapping(mappingRule = "/index.jsp")
11-
public class BaseMapping implements GunHttpMappingHandle<GunHttp2ResponseInterface> {
10+
@GunHttpmapping(mappingRule = "/index.aspx")
11+
public class BaseMapping implements GunHttpMappingHandle<AbstractGunHttp2Response> {
1212
public BaseMapping() {
1313

1414
}
1515

1616
@Override
17-
public GunHttp2ResponseInterface doResponse(GunNetRequestInterface protocl) {
17+
public AbstractGunHttp2Response doResponse(GunNetRequestInterface protocl) {
1818
BaseGunHttp2Response response = new BaseGunHttp2Response() {
1919
@Override
2020
public String toResponse() {

src/top/gunplan/netty/test/ForbiddenMapping.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import top.gunplan.netty.anno.GunHttpmapping;
44
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
55
import top.gunplan.netty.protocol.BaseGunHttp2Response;
6-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
6+
import top.gunplan.netty.protocol.AbstractGunHttp2Response;
77
import top.gunplan.netty.protocol.GunHttpStdInfo;
88
import top.gunplan.netty.protocol.GunNetRequestInterface;
99
@GunHttpmapping(mappingRule = "/manage/*")
10-
public class ForbiddenMapping implements GunHttpMappingHandle<GunHttp2ResponseInterface> {
10+
public class ForbiddenMapping implements GunHttpMappingHandle<AbstractGunHttp2Response> {
1111

1212
@Override
13-
public GunHttp2ResponseInterface doResponse(GunNetRequestInterface protocl) {
13+
public AbstractGunHttp2Response doResponse(GunNetRequestInterface protocl) {
1414
BaseGunHttp2Response response = new BaseGunHttp2Response() {
1515

1616
@Override

src/top/gunplan/netty/test/TimeServer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package top.gunplan.netty.test;
22

3+
34
import top.gunplan.netty.anno.GunHttpmapping;
45
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
56
import top.gunplan.netty.protocol.BaseGunHttp2Response;
6-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
7+
import top.gunplan.netty.protocol.AbstractGunHttp2Response;
78
import top.gunplan.netty.protocol.GunNetRequestInterface;
9+
import top.gunplan.netty.protocol.resputil.GunMappingJsonResp;
810

911
@GunHttpmapping(mappingRule = "/time")
10-
public class TimeServer implements GunHttpMappingHandle<GunHttp2ResponseInterface> {
12+
public class TimeServer implements GunHttpMappingHandle<AbstractGunHttp2Response> {
1113
public TimeServer() {
1214
}
1315

1416

1517
@Override
16-
public GunHttp2ResponseInterface doResponse(GunNetRequestInterface protocl) {
18+
public AbstractGunHttp2Response doResponse(GunNetRequestInterface protocl) {
1719
BaseGunHttp2Response response = new BaseGunHttp2Response() {
1820
@Override
1921
public String toResponse() {
20-
return "{\"time\":\""+System.currentTimeMillis()+"\"}";
22+
GunMappingJsonResp resp = new GunMappingJsonResp();
23+
resp.put("time", String.valueOf(System.currentTimeMillis()));
24+
return resp.toTransfer();
2125
}
2226
};
2327
response.setIswrite(true);

src/top/gunplan/netty/test/_404_Not_Found.java renamed to src/top/gunplan/netty/test/error/_404_Not_Found.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package top.gunplan.netty.test;
1+
package top.gunplan.netty.test.error;
22

33

44
import top.gunplan.netty.anno.GunHttpmapping;
55
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
66
import top.gunplan.netty.protocol.BaseGunHttp2Response;
7-
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
7+
import top.gunplan.netty.protocol.AbstractGunHttp2Response;
88
import top.gunplan.netty.protocol.GunHttpStdInfo;
99
import top.gunplan.netty.protocol.GunNetRequestInterface;
1010

1111
@GunHttpmapping(mappingRule = "/*")
12-
public class _404_Not_Found implements GunHttpMappingHandle<GunHttp2ResponseInterface> {
12+
public class _404_Not_Found implements GunHttpMappingHandle<AbstractGunHttp2Response> {
1313

1414
@Override
15-
public GunHttp2ResponseInterface doResponse(GunNetRequestInterface protocl) {
15+
public AbstractGunHttp2Response doResponse(GunNetRequestInterface protocl) {
1616
BaseGunHttp2Response response = new BaseGunHttp2Response() {
1717
@Override
1818
public String toResponse() {
@@ -30,6 +30,7 @@ public String toResponse() {
3030
response.setIswrite(true);
3131
response.setProtoclType(GunHttpStdInfo.HttpProtoclType.HTTP2_0);
3232
response.setContentType(GunHttpStdInfo.ContentType.TEXT_HTML);
33+
response.setCode(GunHttpStdInfo.statusCode.NOTFOUND);
3334
return response;
3435
}
3536
}

0 commit comments

Comments
 (0)