Stub for WebAPI
修訂 | 95fbfba23c043545e35434429c7c9188a83422bd (tree) |
---|---|
時間 | 2021-10-03 12:59:24 |
作者 | NanaH |
Commiter | NanaH |
環境によってHTMLやJSが文字化けしてしまうのを修正
@@ -2,6 +2,7 @@ | ||
2 | 2 | |
3 | 3 | import java.io.File; |
4 | 4 | import java.io.IOException; |
5 | +import java.nio.charset.Charset; | |
5 | 6 | import java.util.Map; |
6 | 7 | import java.util.TreeMap; |
7 | 8 |
@@ -9,9 +10,11 @@ | ||
9 | 10 | import javax.servlet.http.HttpServletResponse; |
10 | 11 | |
11 | 12 | import org.apache.commons.io.FileUtils; |
13 | +import org.apache.commons.lang3.StringUtils; | |
12 | 14 | import org.slf4j.Logger; |
13 | 15 | import org.slf4j.LoggerFactory; |
14 | 16 | import org.springframework.beans.factory.annotation.Autowired; |
17 | +import org.springframework.beans.factory.annotation.Value; | |
15 | 18 | import org.springframework.http.HttpEntity; |
16 | 19 | import org.springframework.http.HttpHeaders; |
17 | 20 | import org.springframework.http.MediaType; |
@@ -52,7 +55,7 @@ | ||
52 | 55 | public HttpEntity<byte[]> getImageResource(HttpServletRequest req, HttpServletResponse res) throws IOException { |
53 | 56 | File rootDir = pgService.getPageDir(); |
54 | 57 | File file = pgService.getPageDataFile(rootDir, req); |
55 | - MediaType mt = getMediaType(req); | |
58 | + MediaType mt = getMediaType(req, false); | |
56 | 59 | |
57 | 60 | byte[] by = FileUtils.readFileToByteArray(file); |
58 | 61 | HttpHeaders headers = createHeaders(mt, by); |
@@ -64,34 +67,44 @@ | ||
64 | 67 | public HttpEntity<byte[]> getTextResource(HttpServletRequest req, HttpServletResponse res) throws IOException { |
65 | 68 | File rootDir = pgService.getPageDir(); |
66 | 69 | File file = pgService.getPageDataFile(rootDir, req); |
67 | - //logger.info("file={}", file.getAbsolutePath()); | |
68 | - MediaType mt = getMediaType(req); | |
70 | + MediaType mt = getMediaType(req, true); | |
69 | 71 | UsingInfo ui = UsingInfo.getInitInstance(req); |
70 | 72 | |
71 | 73 | 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(); | |
74 | + String org = new String(by, mt.getCharset()); | |
75 | + String dst = ui.replaceSring(org); | |
76 | + if (dst != org){ | |
77 | + by = dst.getBytes(mt.getCharset()); | |
76 | 78 | } |
77 | 79 | HttpHeaders headers = createHeaders(mt, by); |
78 | 80 | return new HttpEntity<byte[]>(by, headers); |
79 | 81 | |
80 | 82 | } |
81 | 83 | |
82 | - private MediaType getMediaType(HttpServletRequest req){ | |
84 | + @Value("${default.charset:UTF-8}") | |
85 | + private String defaultCharset; | |
86 | + | |
87 | + private MediaType getMediaType(HttpServletRequest req, boolean isText){ | |
83 | 88 | String id = req.getRequestURI(); |
84 | 89 | int n = id.lastIndexOf('.'); |
85 | 90 | String ext = (n >= 0) ? id.substring(n).toLowerCase() : ""; |
86 | 91 | MediaType mt = mediaTypeMap.get(ext); |
92 | + | |
93 | + if (isText){ | |
94 | + String ac = req.getHeader("Accept-Charset"); | |
95 | + ac = StringUtils.isEmpty(ac) ? defaultCharset : ac; | |
96 | + Charset charset = Charset.forName(ac); | |
97 | + mt = new MediaType(mt, charset); | |
98 | + } | |
99 | + | |
87 | 100 | return mt; |
88 | 101 | } |
89 | 102 | |
103 | + | |
104 | + | |
90 | 105 | private HttpHeaders createHeaders(MediaType mt, byte[] by){ |
91 | 106 | HttpHeaders headers = new HttpHeaders(); |
92 | - if (mt != null){ | |
93 | - headers.setContentType(mt); | |
94 | - } | |
107 | + headers.setContentType(mt); | |
95 | 108 | headers.setContentLength(by.length); |
96 | 109 | return headers; |
97 | 110 | } |
@@ -5,14 +5,19 @@ | ||
5 | 5 | |
6 | 6 | import javax.servlet.http.HttpServletRequest; |
7 | 7 | |
8 | +import org.slf4j.Logger; | |
9 | +import org.slf4j.LoggerFactory; | |
8 | 10 | import org.springframework.beans.factory.annotation.Value; |
9 | 11 | import org.springframework.stereotype.Service; |
10 | 12 | |
13 | +import jp.nanah.bastub.controller.AnyRestController; | |
11 | 14 | import nanah.oslib.ResUtils; |
12 | 15 | |
13 | 16 | @Service |
14 | 17 | public class PageDataService { |
15 | 18 | |
19 | + private static final Logger logger = LoggerFactory.getLogger(AnyRestController.class); | |
20 | + | |
16 | 21 | @Value("${pagedata.root:pagedata}") |
17 | 22 | private String pagedataPath; |
18 | 23 |
@@ -31,6 +36,8 @@ | ||
31 | 36 | } |
32 | 37 | |
33 | 38 | public File getPageDataFile(File dir, HttpServletRequest req){ |
34 | - return new File(dir, req.getRequestURI()); | |
39 | + File file = new File(dir, req.getRequestURI()); | |
40 | + logger.debug("Path=[{}]", file.getAbsolutePath()); | |
41 | + return file; | |
35 | 42 | } |
36 | 43 | } |
@@ -25,3 +25,7 @@ | ||
25 | 25 | # \u203bExcel\u30d5\u30a1\u30a4\u30eb\u3082\u540c\u69d8 |
26 | 26 | path_last_slash_valid=0 |
27 | 27 | |
28 | +#\u30c6\u30ad\u30b9\u30c8(.html,.css\u306a\u3069)\u3092\u8fd4\u3059\u6642\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u3002 | |
29 | +#HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u306eAccept-Charset\u3067\u6307\u5b9a\u3055\u308c\u306a\u3044\u5834\u5408\u306b\u3053\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3002 | |
30 | +#\u672a\u6307\u5b9a\u306e\u5834\u5408\u306fUTF-8\u304c\u4f7f\u308f\u308c\u308b\u3002 | |
31 | +#default.charset= | |
\ No newline at end of file |