|
3 | 3 | import java.lang.reflect.Field; |
4 | 4 | import java.lang.reflect.InvocationTargetException; |
5 | 5 | import java.lang.reflect.Method; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
6 | 8 | import java.util.Arrays; |
| 9 | +import java.util.Date; |
7 | 10 | import java.util.HashMap; |
8 | 11 | import java.util.HashSet; |
9 | 12 | import java.util.Locale; |
|
15 | 18 | import util.Config; |
16 | 19 | import util.HttpMessage; |
17 | 20 | import util.Log; |
| 21 | +import util.MessageHelper; |
| 22 | +import util.consts.Headers; |
| 23 | +import util.consts.WebMethods; |
18 | 24 |
|
19 | 25 | /** |
20 | 26 | * A singleton handler |
@@ -105,26 +111,57 @@ public HttpResponseMessage handle(HttpRequestMessage msg) { |
105 | 111 | target = target.toLowerCase(Locale.ROOT); |
106 | 112 |
|
107 | 113 | if (!targetToMethod.containsKey(target)) { |
108 | | - String path = Config.STATIC_DIR + target; |
109 | | - if (ClassLoader.getSystemClassLoader().getResource(path) == null) |
| 114 | + // -------------------- 1. Static Resource -------------------- // |
| 115 | + Path path = getResourcePath(target); |
| 116 | + |
| 117 | + Log.debug("Search resource in path: ", path); |
| 118 | + |
| 119 | + if (WebMethods.GET.equals(msg.getMethod()) |
| 120 | + && Files.exists(path) |
| 121 | + ) { |
| 122 | + return loadStaticResource(path, msg); |
| 123 | + } else { |
110 | 124 | target = "Missing"; |
111 | | - else { |
112 | | - HttpResponseMessage hrm = factory.produce(200); |
113 | | - hrm.setBodyAsFile(path); |
114 | | - return hrm; |
115 | 125 | } |
116 | 126 | } |
117 | 127 |
|
118 | | - Method method = targetToMethod.get(target); |
| 128 | + // -------------------- 2. Matched Target -------------------- // |
119 | 129 |
|
| 130 | + /* Check method validity */ |
| 131 | + Method method = targetToMethod.get(target); |
120 | 132 | if (Arrays.binarySearch(method.getDeclaredAnnotation(Mapping.class).method(), msg.getMethod()) < 0) |
121 | 133 | return factory.produce(405); |
122 | 134 |
|
| 135 | + |
123 | 136 | return (HttpResponseMessage) targetToMethod.get(target).invoke(null, msg); |
124 | 137 |
|
125 | 138 | } catch (IllegalAccessException | InvocationTargetException e) { |
126 | 139 | e.printStackTrace(); |
127 | 140 | return factory.produce(500); |
128 | 141 | } |
129 | 142 | } |
| 143 | + |
| 144 | + private Path getResourcePath(String target) { |
| 145 | + if (target.endsWith("/")) |
| 146 | + target += "index.html"; |
| 147 | + return Path.of(Config.STATIC_DIR, target); |
| 148 | + } |
| 149 | + |
| 150 | + private HttpResponseMessage loadStaticResource(Path path, HttpRequestMessage request) { |
| 151 | + Log.debug("Resource found"); |
| 152 | + |
| 153 | + if (request.containsHeader(Headers.IF_MODIFIED_SINCE)) { |
| 154 | + String time = request.getHeaderVal(Headers.IF_MODIFIED_SINCE); |
| 155 | + Date date = MessageHelper.parseTime(time); |
| 156 | + assert date != null; |
| 157 | + Date myDate = Config.getResourceLastModifiedTimeAsDate(path); |
| 158 | + if (myDate.compareTo(date) < 0) { |
| 159 | + return factory.produce(304); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + HttpResponseMessage hrm = factory.produce(200); |
| 164 | + hrm.setBodyAsFileWithAbsPath(path); |
| 165 | + return hrm; |
| 166 | + } |
130 | 167 | } |
0 commit comments