, RouteAdviceHandler> exceptionHandlerMap = new HashMap<>();
40 |
41 | public void registerAdviceHandler(Class extends Throwable> throwableCls, RouteAdviceHandler routeAdviceHandler) {
42 | Validate.notNull(throwableCls, "throwableCls can' be null");
43 | Validate.notNull(routeAdviceHandler, "errorHandler can' be null");
44 | this.exceptionHandlerMap.put(throwableCls, routeAdviceHandler);
45 | }
46 |
47 | public void handleException(Throwable throwable, RequestContext requestContext) {
48 | if (exceptionHandlerMap.isEmpty() || !exceptionHandlerMap.containsKey(throwable.getClass())) {
49 | requestContext.error(HttpStatus.INTERNAL_SERVER_ERROR, throwable.getLocalizedMessage());
50 | }
51 | try {
52 | final RouteAdviceHandler routeAdviceHandler = exceptionHandlerMap.get(throwable.getClass());
53 | if (null != routeAdviceHandler) {
54 | routeAdviceHandler.handle(throwable);
55 | }
56 | } catch (Exception e) {
57 | handleException(e, requestContext);
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/org/aquiver/mvc/router/PathVarMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019 1619kHz
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package org.aquiver.mvc.router;
25 |
26 | /**
27 | * @author WangYi
28 | * @since 2020/5/29
29 | */
30 | public class PathVarMatcher {
31 | private PathVarMatcher() {
32 | }
33 |
34 | public static boolean checkMatch(String[] lookupPathSplit, String... mappingUrlSplit) {
35 | for (int i = 0; i < lookupPathSplit.length; i++) {
36 | if (lookupPathSplit[i].equals(mappingUrlSplit[i])) {
37 | continue;
38 | }
39 | if (!mappingUrlSplit[i].startsWith("{")) {
40 | return false;
41 | }
42 | }
43 | return true;
44 | }
45 |
46 | public static String getMatch(String url) {
47 | StringBuilder matcher = new StringBuilder(128);
48 | for (char c : url.toCharArray()) {
49 | if (c == '{') {
50 | break;
51 | }
52 | matcher.append(c);
53 | }
54 | return matcher.toString();
55 | }
56 |
57 | public static String getPathVariable(String url, String mappingUrl, String name) {
58 | String[] urlSplit = url.split("/");
59 | String[] mappingUrlSplit = mappingUrl.split("/");
60 | for (int i = 0; i < mappingUrlSplit.length; i++) {
61 | if (mappingUrlSplit[i].equals("{" + name + "}")) {
62 | if (urlSplit[i].contains("?")) {
63 | return urlSplit[i].split("[?]")[0];
64 | }
65 | if (urlSplit[i].contains("&")) {
66 | return urlSplit[i].split("&")[0];
67 | }
68 | return urlSplit[i];
69 | }
70 | }
71 | return null;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/org/aquiver/RouteRepeatException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019 1619kHz
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package org.aquiver;
25 |
26 | /**
27 | * This exception is thrown when the route is added repeatedly
28 | *
29 | * @author WangYi
30 | * @since 2020/5/26
31 | */
32 | public class RouteRepeatException extends RuntimeException {
33 |
34 | /**
35 | * Constructs a new runtime exception with the specified detail message.
36 | * The cause is not initialized, and may subsequently be initialized by a
37 | * call to {@link #initCause}.
38 | *
39 | * @param message the detail message. The detail message is saved for
40 | * later retrieval by the {@link #getMessage()} method.
41 | */
42 | public RouteRepeatException(String message) {
43 | super(message);
44 | }
45 |
46 | /**
47 | * Constructs a new runtime exception with the specified detail message and
48 | * cause. Note that the detail message associated with
49 | * {@code cause} is not automatically incorporated in
50 | * this runtime exception's detail message.
51 | *
52 | * @param message the detail message (which is saved for later retrieval
53 | * by the {@link #getMessage()} method).
54 | * @param cause the cause (which is saved for later retrieval by the
55 | * {@link #getCause()} method). (A {@code null} value is
56 | * permitted, and indicates that the cause is nonexistent or
57 | * unknown.)
58 | * @since 1.4
59 | */
60 | public RouteRepeatException(String message, Throwable cause) {
61 | super(message, cause);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/org/aquiver/mvc/result/view/HtmlTemplateViewHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019 1619kHz
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package org.aquiver.mvc.result.view;
25 |
26 | import io.netty.handler.codec.http.FullHttpResponse;
27 | import org.apex.io.FileBaseResource;
28 | import org.apex.io.Resource;
29 | import org.aquiver.RequestContext;
30 | import org.aquiver.ResponseBuilder;
31 | import org.aquiver.ServerSpec;
32 | import org.aquiver.ViewHandlerType;
33 |
34 | import java.net.URL;
35 | import java.nio.file.Files;
36 | import java.nio.file.Paths;
37 |
38 | /**
39 | * @author WangYi
40 | * @since 2020/8/27
41 | */
42 | public final class HtmlTemplateViewHandler extends AbstractTemplateViewHandler {
43 |
44 | @Override
45 | public String getPrefix() {
46 | return environment.get(ServerSpec.PATH_SERVER_VIEW_PREFIX, "");
47 | }
48 |
49 | @Override
50 | public String getSuffix() {
51 | return ServerSpec.SERVER_VIEW_SUFFIX;
52 | }
53 |
54 | @Override
55 | protected void doRender(RequestContext ctx, String viewPathName) throws Exception {
56 | URL viewUrl = this.getClass().getClassLoader().getResource(viewPathName);
57 | Resource resource = new FileBaseResource(Paths.get(viewUrl.toURI()));
58 | String htmlContent = new String(Files.readAllBytes(resource.getPath()));
59 | FullHttpResponse response = ResponseBuilder.builder().body(htmlContent).build();
60 | ctx.tryPush(response);
61 | }
62 |
63 | @Override
64 | public String getType() {
65 | return "html";
66 | }
67 |
68 | @Override
69 | public ViewHandlerType getHandlerType() {
70 | return ViewHandlerType.TEMPLATE_VIEW;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/org/aquiver/Request.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019 1619kHz
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package org.aquiver;
25 |
26 | import io.netty.buffer.ByteBuf;
27 | import io.netty.handler.codec.http.multipart.FileUpload;
28 | import org.aquiver.mvc.http.Cookie;
29 | import org.aquiver.mvc.http.Header;
30 | import org.aquiver.mvc.router.session.Session;
31 |
32 | import java.util.Map;
33 | import java.util.Set;
34 |
35 | /**
36 | * @author WangYi
37 | * @since 2020/10/17
38 | */
39 | public interface Request {
40 | Map fileUploads();
41 |
42 | FileUpload fileUploads(String fileKey);
43 |
44 | Boolean isMultipart();
45 |
46 | ByteBuf body();
47 |
48 | T body(Class type);
49 |
50 | Session session();
51 |
52 | String method();
53 |
54 | String protocol();
55 |
56 | Integer minorVersion();
57 |
58 | Integer majorVersion();
59 |
60 | Header header(String key);
61 |
62 | String param(String key);
63 |
64 | Set paramNames();
65 |
66 | Set headerNames();
67 |
68 | String uri();
69 |
70 | String path();
71 |
72 | String rawPath();
73 |
74 | String rawQuery();
75 |
76 | Integer port();
77 |
78 | String remoteAddress();
79 |
80 | String host();
81 |
82 | String accept();
83 |
84 | String connection();
85 |
86 | Map cookies();
87 |
88 | Cookie cookie(String key);
89 |
90 | String sessionKey();
91 |
92 | String referer();
93 |
94 | String userAgent();
95 |
96 | Map header();
97 |
98 | Boolean isKeepAlive();
99 |
100 | Boolean is100ContinueExpected();
101 | }
102 |
--------------------------------------------------------------------------------