Stub for WebAPI
リビジョン | f473d240205cae7c9bb475180a41edc973c317b7 (tree) |
---|---|
日時 | 2021-10-01 08:33:02 |
作者 | NanaH |
コミッター | NanaH |
JSONで返さないもの(画像ファイルや.css、.htmlなど)は処理を別にした
@@ -4,7 +4,7 @@ | ||
4 | 4 | <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script> |
5 | 5 | <script type="text/javascript" src="./js/example.js"></script> |
6 | 6 | <html> |
7 | -<h1>BASTUB 使用例1</h1> | |
7 | +<h1><img src="/bastub.png"/> BASTUB 使用例1</h1> | |
8 | 8 | <hr/> |
9 | 9 | |
10 | 10 | ■例1<br/> |
@@ -4,7 +4,7 @@ | ||
4 | 4 | <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script> |
5 | 5 | <script type="text/javascript" src="./js/example.js"></script> |
6 | 6 | <html> |
7 | -<h1>BASTUB 使用例2</h1> | |
7 | +<h1><img src="/bastub.png"/> BASTUB 使用例2</h1> | |
8 | 8 | <hr/> |
9 | 9 | |
10 | 10 | ■例2<br/> |
@@ -4,7 +4,7 @@ | ||
4 | 4 | <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script> |
5 | 5 | <script type="text/javascript" src="./js/example.js"></script> |
6 | 6 | <html> |
7 | -<h1>BASTUB 使用例3</h1> | |
7 | +<h1><img src="/bastub.png"/> BASTUB 使用例3</h1> | |
8 | 8 | <hr/> |
9 | 9 | |
10 | 10 | ■例3<br/> |
@@ -7,7 +7,7 @@ | ||
7 | 7 | tableType=2; |
8 | 8 | </script> |
9 | 9 | <html> |
10 | -<h1>BASTUB 使用例4</h1> | |
10 | +<h1><img src="/bastub.png"/> BASTUB 使用例4</h1> | |
11 | 11 | <hr/> |
12 | 12 | |
13 | 13 | ■例4<br/> |
@@ -46,7 +46,7 @@ | ||
46 | 46 | type : "POST", |
47 | 47 | data : jsontext, |
48 | 48 | headers : { |
49 | - 'Content-Type': 'application/json' | |
49 | + 'Content-Type': 'application/json;charset=utf-8' | |
50 | 50 | }, |
51 | 51 | dataType : "json", |
52 | 52 | timeout : 10000, |
@@ -9,14 +9,12 @@ | ||
9 | 9 | import java.util.Collections; |
10 | 10 | import java.util.List; |
11 | 11 | import java.util.Map; |
12 | -import java.util.Optional; | |
13 | 12 | import java.util.Set; |
14 | 13 | import java.util.TreeMap; |
15 | 14 | |
16 | 15 | import javax.servlet.http.HttpServletRequest; |
17 | 16 | import javax.servlet.http.HttpServletResponse; |
18 | 17 | |
19 | -import org.apache.commons.io.FileUtils; | |
20 | 18 | import org.apache.commons.lang3.ArrayUtils; |
21 | 19 | import org.apache.commons.lang3.StringUtils; |
22 | 20 | import org.apache.commons.lang3.math.NumberUtils; |
@@ -31,7 +29,6 @@ | ||
31 | 29 | import org.springframework.beans.factory.annotation.Value; |
32 | 30 | import org.springframework.http.MediaType; |
33 | 31 | import org.springframework.ui.ModelMap; |
34 | -import org.springframework.web.bind.annotation.PathVariable; | |
35 | 32 | import org.springframework.web.bind.annotation.RequestBody; |
36 | 33 | import org.springframework.web.bind.annotation.RequestMapping; |
37 | 34 | import org.springframework.web.bind.annotation.RequestParam; |
@@ -42,9 +39,10 @@ | ||
42 | 39 | import jp.nanah.bastub.data.JsonInfo; |
43 | 40 | import jp.nanah.bastub.data.KvData; |
44 | 41 | import jp.nanah.bastub.service.JsonService; |
42 | +import jp.nanah.bastub.service.PageDataService; | |
45 | 43 | import jp.nanah.bastub.service.UsingInfo; |
46 | 44 | import jp.nanah.bastub.util.BastubUtils; |
47 | -import nanah.oslib.ResUtils; | |
45 | + | |
48 | 46 | @RestController |
49 | 47 | public class AnyRestController { |
50 | 48 | /** 動作ログ */ |
@@ -77,38 +75,17 @@ | ||
77 | 75 | * パスの深さは最大10まで(可変にできる書き方を知らないので)。 |
78 | 76 | * HTTPのメソッドは、GET/POST/DELETE/PUTのみ対応。(これ以外は使わないと思うので) |
79 | 77 | * |
80 | - * @param p1~p10 | |
81 | 78 | * @param body |
82 | 79 | * @param model |
83 | 80 | * @param req |
84 | 81 | * @param res |
85 | 82 | * @return |
86 | 83 | */ |
87 | - @RequestMapping(path={ "/{p1}" | |
88 | - ,"{p1}/{p2}" | |
89 | - ,"{p1}/{p2}/{p3}" | |
90 | - ,"{p1}/{p2}/{p3}/{p4}" | |
91 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}" | |
92 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}" | |
93 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}" | |
94 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}" | |
95 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}/{p9}" | |
96 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}/{p9}/{p10}" | |
97 | - } | |
98 | - , method={GET, POST, PUT, DELETE}) | |
99 | - public String path1( @PathVariable("p1") Optional<String> p1 | |
100 | - , @PathVariable("p2") Optional<String> p2 | |
101 | - , @PathVariable("p3") Optional<String> p3 | |
102 | - , @PathVariable("p4") Optional<String> p4 | |
103 | - , @PathVariable("p5") Optional<String> p5 | |
104 | - , @PathVariable("p6") Optional<String> p6 | |
105 | - , @PathVariable("p7") Optional<String> p7 | |
106 | - , @PathVariable("p8") Optional<String> p8 | |
107 | - , @PathVariable("p9") Optional<String> p9 | |
108 | - , @PathVariable("p10") Optional<String> p10 | |
109 | - , @RequestBody(required=false) ModelMap body, ModelMap model, HttpServletRequest req, HttpServletResponse res) { | |
110 | - | |
111 | - return path_common(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, body, model, req, res); | |
84 | + @RequestMapping(path="/**" | |
85 | + , method={GET, POST, PUT, DELETE}) | |
86 | + public String path1(@RequestBody(required=false) ModelMap body, ModelMap model, HttpServletRequest req, HttpServletResponse res) { | |
87 | + logger.debug("★1;" + req.getRequestURI()); | |
88 | + return path_common(body, model, req, res); | |
112 | 89 | } |
113 | 90 | |
114 | 91 | /** |
@@ -121,41 +98,18 @@ | ||
121 | 98 | * @param res |
122 | 99 | * @return |
123 | 100 | */ |
124 | - @RequestMapping(path={ "/{p1}" | |
125 | - ,"{p1}/{p2}" | |
126 | - ,"{p1}/{p2}/{p3}" | |
127 | - ,"{p1}/{p2}/{p3}/{p4}" | |
128 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}" | |
129 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}" | |
130 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}" | |
131 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}" | |
132 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}/{p9}" | |
133 | - ,"{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}/{p9}/{p10}" | |
134 | - } | |
135 | - , method={GET, POST, PUT, DELETE} | |
136 | - , consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE} | |
101 | + @RequestMapping(path= "/**" | |
102 | + , method={GET, POST, PUT, DELETE} | |
103 | + , consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE} | |
137 | 104 | ) |
138 | - public String path1_multipart( @PathVariable("p1") Optional<String> p1 | |
139 | - , @PathVariable("p2") Optional<String> p2 | |
140 | - , @PathVariable("p3") Optional<String> p3 | |
141 | - , @PathVariable("p4") Optional<String> p4 | |
142 | - , @PathVariable("p5") Optional<String> p5 | |
143 | - , @PathVariable("p6") Optional<String> p6 | |
144 | - , @PathVariable("p7") Optional<String> p7 | |
145 | - , @PathVariable("p8") Optional<String> p8 | |
146 | - , @PathVariable("p9") Optional<String> p9 | |
147 | - , @PathVariable("p10") Optional<String> p10 | |
148 | - , @RequestParam(required=false) Map<String, String> paramMap, ModelMap model, HttpServletRequest req, HttpServletResponse res) { | |
149 | - | |
150 | - return path_common(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, paramMap, model, req, res); | |
105 | + public String path1_multipart( @RequestParam(required=false) Map<String, String> paramMap, ModelMap model, HttpServletRequest req, HttpServletResponse res) { | |
106 | + return path_common(paramMap, model, req, res); | |
151 | 107 | } |
152 | 108 | |
153 | 109 | /** |
154 | 110 | * リクエストの共通処理。 |
155 | 111 | */ |
156 | - private String path_common(Optional<String> p1, Optional<String> p2, Optional<String> p3, Optional<String> p4, Optional<String> p5 | |
157 | - , Optional<String> p6, Optional<String> p7, Optional<String> p8, Optional<String> p9, Optional<String> p10 | |
158 | - , Map paramMap, ModelMap model, HttpServletRequest req, HttpServletResponse res){ | |
112 | + private String path_common( Map paramMap, ModelMap model, HttpServletRequest req, HttpServletResponse res){ | |
159 | 113 | |
160 | 114 | synchronized (APP_SNO){ |
161 | 115 | Thread.currentThread().setName(APP_SNO + "_" + reqNo); |
@@ -171,7 +125,9 @@ | ||
171 | 125 | |
172 | 126 | logger.info("== ▼ == [{}] {}", method, reqPath); |
173 | 127 | res.setContentType("application/json;charset=UTF-8"); |
174 | - List<String> pathList = BastubUtils.toValidList(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); | |
128 | + String[] pathArray = req.getRequestURI().split("/"); | |
129 | + List<String> pathList = Arrays.asList(pathArray); //BastubUtils.toValidList(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); | |
130 | + | |
175 | 131 | if (pathLastSlashValid == 1){ |
176 | 132 | //URLがスラッシュ終わりなら、ファイル名が""の要求が来たものとする |
177 | 133 | if (reqPath.endsWith("/")){ |
@@ -189,13 +145,8 @@ | ||
189 | 145 | // 共通処理 |
190 | 146 | //================================= |
191 | 147 | |
192 | - @Value("${pagedata.root:pagedata}") | |
193 | - private String pagedataPath; | |
194 | - | |
195 | - /** | |
196 | - * 静的リソースファイルの拡張子。 | |
197 | - */ | |
198 | - private static String STATIC_RESOURCE_EXT = ".html.htm.css.js.png.jpg.jpeg"; | |
148 | + @Autowired | |
149 | + private PageDataService pgService; | |
199 | 150 | |
200 | 151 | /** |
201 | 152 | * 【共通処理】ここが主処理。 |
@@ -209,12 +160,7 @@ | ||
209 | 160 | */ |
210 | 161 | public String getAny(List<String> pathList, Map body, ModelMap model, HttpServletRequest req, HttpServletResponse res){ |
211 | 162 | try { |
212 | - File pageDir = new File(pagedataPath); | |
213 | - if (pageDir.isAbsolute() == false){ | |
214 | - File appRoot = ResUtils.getRootDirAs(this.getClass()); | |
215 | - pageDir = new File(appRoot, pagedataPath); | |
216 | - } | |
217 | - pageDir = pageDir.getCanonicalFile(); | |
163 | + File pageDir = pgService.getPageDir(); | |
218 | 164 | |
219 | 165 | if (pageDir.exists() == false){ |
220 | 166 | String errormsg = "Bastubの設定フォルダ[" + pageDir.getAbsolutePath() + "]が見つかりません。application.propertiesのpagedata.rootを見直してください。"; |
@@ -224,15 +170,6 @@ | ||
224 | 170 | |
225 | 171 | UsingInfo ui = UsingInfo.getInitInstance(pathList, req); |
226 | 172 | |
227 | - //静的リソースはここで読み込んで返す | |
228 | - String ext = BastubUtils.getExtText(req.getRequestURI()); | |
229 | - if (ext.length() > 0 && STATIC_RESOURCE_EXT.indexOf(ext) >= 0){ | |
230 | - File htmlFile = BastubUtils.getPagedataPath(pageDir, req.getRequestURI(), req.getMethod(), ""); | |
231 | - res.setContentType("text/html; charset=" +res.getCharacterEncoding()); | |
232 | - String text = FileUtils.readFileToString(htmlFile, res.getCharacterEncoding()); | |
233 | - return ui.replaceSring(text); | |
234 | - } | |
235 | - | |
236 | 173 | //パスを生成 |
237 | 174 | String path = StringUtils.join(pathList, "/"); |
238 | 175 | File dataFile = BastubUtils.getPagedataPath(pageDir, path, req.getMethod(), ".xlsx,.xls"); |
@@ -0,0 +1,99 @@ | ||
1 | +package jp.nanah.bastub.controller; | |
2 | + | |
3 | +import java.io.File; | |
4 | +import java.io.IOException; | |
5 | +import java.util.Map; | |
6 | +import java.util.TreeMap; | |
7 | + | |
8 | +import javax.servlet.http.HttpServletRequest; | |
9 | +import javax.servlet.http.HttpServletResponse; | |
10 | + | |
11 | +import org.apache.commons.io.FileUtils; | |
12 | +import org.slf4j.Logger; | |
13 | +import org.slf4j.LoggerFactory; | |
14 | +import org.springframework.beans.factory.annotation.Autowired; | |
15 | +import org.springframework.http.HttpEntity; | |
16 | +import org.springframework.http.HttpHeaders; | |
17 | +import org.springframework.http.MediaType; | |
18 | +import org.springframework.stereotype.Controller; | |
19 | +import org.springframework.web.bind.annotation.RequestMapping; | |
20 | +import org.springframework.web.bind.annotation.ResponseBody; | |
21 | + | |
22 | +import jp.nanah.bastub.service.PageDataService; | |
23 | +import jp.nanah.bastub.service.UsingInfo; | |
24 | + | |
25 | +@Controller | |
26 | +public class StaticController { | |
27 | + | |
28 | + private static final Logger logger = LoggerFactory.getLogger(AnyRestController.class); | |
29 | + | |
30 | + static Map<String, MediaType> mediaTypeMap = new TreeMap<String, MediaType>(); | |
31 | + | |
32 | + static { | |
33 | + mediaTypeMap.put(".html", MediaType.TEXT_HTML); | |
34 | + mediaTypeMap.put(".htm", MediaType.TEXT_HTML); | |
35 | + mediaTypeMap.put(".txt", MediaType.TEXT_PLAIN); | |
36 | + mediaTypeMap.put(".js", new MediaType("application","javascript")); | |
37 | + mediaTypeMap.put(".css", new MediaType("text","css")); | |
38 | + mediaTypeMap.put(".jpg", MediaType.IMAGE_JPEG); | |
39 | + mediaTypeMap.put(".jpeg", MediaType.IMAGE_JPEG); | |
40 | + mediaTypeMap.put(".png", MediaType.IMAGE_PNG); | |
41 | + mediaTypeMap.put(".gif", MediaType.IMAGE_GIF); | |
42 | + mediaTypeMap.put(".pdf", MediaType.APPLICATION_PDF); | |
43 | + mediaTypeMap.put(".ico", new MediaType("image","vnd.microsoft.icon")); | |
44 | + mediaTypeMap.put("", MediaType.TEXT_HTML); | |
45 | + } | |
46 | + | |
47 | + @Autowired | |
48 | + private PageDataService pgService; | |
49 | + | |
50 | + @ResponseBody | |
51 | + @RequestMapping(value = { "/**/*.jpg","/**/*.jpeg","/**/*.png","/**/*.gif","/**/*.pdf","/**/*.ico"}) | |
52 | + public HttpEntity<byte[]> getImageResource(HttpServletRequest req, HttpServletResponse res) throws IOException { | |
53 | + File rootDir = pgService.getPageDir(); | |
54 | + File file = pgService.getPageDataFile(rootDir, req); | |
55 | + MediaType mt = getMediaType(req); | |
56 | + | |
57 | + byte[] by = FileUtils.readFileToByteArray(file); | |
58 | + HttpHeaders headers = createHeaders(mt, by); | |
59 | + return new HttpEntity<byte[]>(by, headers); | |
60 | + } | |
61 | + | |
62 | + @ResponseBody | |
63 | + @RequestMapping(value = { "/**/*.js","/**/*.css","/**/*.html","/**/*.htm","/**/*.txt"}) | |
64 | + public HttpEntity<byte[]> getTextResource(HttpServletRequest req, HttpServletResponse res) throws IOException { | |
65 | + File rootDir = pgService.getPageDir(); | |
66 | + File file = pgService.getPageDataFile(rootDir, req); | |
67 | + //logger.info("file={}", file.getAbsolutePath()); | |
68 | + MediaType mt = getMediaType(req); | |
69 | + UsingInfo ui = UsingInfo.getInitInstance(req); | |
70 | + | |
71 | + byte[] by = FileUtils.readFileToByteArray(file); | |
72 | + if (mt == MediaType.TEXT_HTML){ | |
73 | + String org = new String(by, "UTF-8"); | |
74 | + String dst = ui.replaceSring(org); | |
75 | + by = dst.getBytes(); | |
76 | + } | |
77 | + HttpHeaders headers = createHeaders(mt, by); | |
78 | + return new HttpEntity<byte[]>(by, headers); | |
79 | + | |
80 | + } | |
81 | + | |
82 | + private MediaType getMediaType(HttpServletRequest req){ | |
83 | + String id = req.getRequestURI(); | |
84 | + int n = id.lastIndexOf('.'); | |
85 | + String ext = (n >= 0) ? id.substring(n).toLowerCase() : ""; | |
86 | + MediaType mt = mediaTypeMap.get(ext); | |
87 | + return mt; | |
88 | + } | |
89 | + | |
90 | + private HttpHeaders createHeaders(MediaType mt, byte[] by){ | |
91 | + HttpHeaders headers = new HttpHeaders(); | |
92 | + if (mt != null){ | |
93 | + headers.setContentType(mt); | |
94 | + } | |
95 | + headers.setContentLength(by.length); | |
96 | + return headers; | |
97 | + } | |
98 | + | |
99 | +} |
@@ -0,0 +1,36 @@ | ||
1 | +package jp.nanah.bastub.service; | |
2 | + | |
3 | +import java.io.File; | |
4 | +import java.io.IOException; | |
5 | + | |
6 | +import javax.servlet.http.HttpServletRequest; | |
7 | + | |
8 | +import org.springframework.beans.factory.annotation.Value; | |
9 | +import org.springframework.stereotype.Service; | |
10 | + | |
11 | +import nanah.oslib.ResUtils; | |
12 | + | |
13 | +@Service | |
14 | +public class PageDataService { | |
15 | + | |
16 | + @Value("${pagedata.root:pagedata}") | |
17 | + private String pagedataPath; | |
18 | + | |
19 | + public File getPageDir(){ | |
20 | + File pageDir = new File(pagedataPath); | |
21 | + if (pageDir.isAbsolute() == false){ | |
22 | + File appRoot = ResUtils.getRootDirAs(this.getClass()); | |
23 | + pageDir = new File(appRoot, pagedataPath); | |
24 | + } | |
25 | + try { | |
26 | + pageDir = pageDir.getCanonicalFile(); | |
27 | + } catch (IOException e){ | |
28 | + pageDir = pageDir.getAbsoluteFile(); | |
29 | + } | |
30 | + return pageDir; | |
31 | + } | |
32 | + | |
33 | + public File getPageDataFile(File dir, HttpServletRequest req){ | |
34 | + return new File(dir, req.getRequestURI()); | |
35 | + } | |
36 | +} |
@@ -2,6 +2,7 @@ | ||
2 | 2 | |
3 | 3 | import java.text.SimpleDateFormat; |
4 | 4 | import java.util.ArrayList; |
5 | +import java.util.Arrays; | |
5 | 6 | import java.util.Date; |
6 | 7 | import java.util.HashMap; |
7 | 8 | import java.util.Iterator; |
@@ -46,6 +47,19 @@ | ||
46 | 47 | * @param req |
47 | 48 | * @return |
48 | 49 | */ |
50 | + public static UsingInfo getInitInstance(HttpServletRequest req){ | |
51 | + String[] pathArray = req.getRequestURI().split("/"); | |
52 | + List<String> pathList = Arrays.asList(pathArray); | |
53 | + return getInitInstance(pathList, req); | |
54 | + } | |
55 | + | |
56 | + | |
57 | + /** | |
58 | + * JSONの値を固定値に変換するためのMap。 | |
59 | + * @param pathList | |
60 | + * @param req | |
61 | + * @return | |
62 | + */ | |
49 | 63 | public static UsingInfo getInitInstance(List<String> pathList, HttpServletRequest req){ |
50 | 64 | UsingInfo ui = new UsingInfo(); |
51 | 65 |
@@ -6,7 +6,6 @@ | ||
6 | 6 | import java.util.Iterator; |
7 | 7 | import java.util.List; |
8 | 8 | import java.util.Map; |
9 | -import java.util.Optional; | |
10 | 9 | |
11 | 10 | import org.apache.commons.lang3.StringUtils; |
12 | 11 | import org.apache.poi.ss.usermodel.Cell; |
@@ -29,26 +28,6 @@ | ||
29 | 28 | //=============================== |
30 | 29 | |
31 | 30 | /** |
32 | - * URLで受け取ったパスのうち、有効なものだけ(パスとして受け取ったものだけ)を持つListにして返す。 | |
33 | - * 実際は前詰めだから途中で無効な値が出てきたら以降は全部無効値だが、 | |
34 | - * 汎用性を考えて全引数を判定することにする。 | |
35 | - * | |
36 | - * @param <T> | |
37 | - * @param vals | |
38 | - * @return | |
39 | - */ | |
40 | - @SafeVarargs | |
41 | - public static <T> List<T> toValidList(Optional<T> ... vals) { | |
42 | - List<T> list = new ArrayList<T>(); | |
43 | - for (Optional<T> v : vals) { | |
44 | - if (v.isPresent()) { | |
45 | - list.add(v.get()); | |
46 | - } | |
47 | - } | |
48 | - return list; | |
49 | - } | |
50 | - | |
51 | - /** | |
52 | 31 | * データファイル(エクセル及びJSON)のファイルパスを取得する。 |
53 | 32 | * |
54 | 33 | * @param dir |