• R/O
  • SSH

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Stub for WebAPI


Commit MetaInfo

修訂95fbfba23c043545e35434429c7c9188a83422bd (tree)
時間2021-10-03 12:59:24
作者NanaH
CommiterNanaH

Log Message

環境によってHTMLやJSが文字化けしてしまうのを修正

Change Summary

差異

diff -r f473d240205c -r 95fbfba23c04 src/main/java/jp/nanah/bastub/controller/StaticController.java
--- a/src/main/java/jp/nanah/bastub/controller/StaticController.java Fri Oct 01 08:33:02 2021 +0900
+++ b/src/main/java/jp/nanah/bastub/controller/StaticController.java Sun Oct 03 12:59:24 2021 +0900
@@ -2,6 +2,7 @@
22
33 import java.io.File;
44 import java.io.IOException;
5+import java.nio.charset.Charset;
56 import java.util.Map;
67 import java.util.TreeMap;
78
@@ -9,9 +10,11 @@
910 import javax.servlet.http.HttpServletResponse;
1011
1112 import org.apache.commons.io.FileUtils;
13+import org.apache.commons.lang3.StringUtils;
1214 import org.slf4j.Logger;
1315 import org.slf4j.LoggerFactory;
1416 import org.springframework.beans.factory.annotation.Autowired;
17+import org.springframework.beans.factory.annotation.Value;
1518 import org.springframework.http.HttpEntity;
1619 import org.springframework.http.HttpHeaders;
1720 import org.springframework.http.MediaType;
@@ -52,7 +55,7 @@
5255 public HttpEntity<byte[]> getImageResource(HttpServletRequest req, HttpServletResponse res) throws IOException {
5356 File rootDir = pgService.getPageDir();
5457 File file = pgService.getPageDataFile(rootDir, req);
55- MediaType mt = getMediaType(req);
58+ MediaType mt = getMediaType(req, false);
5659
5760 byte[] by = FileUtils.readFileToByteArray(file);
5861 HttpHeaders headers = createHeaders(mt, by);
@@ -64,34 +67,44 @@
6467 public HttpEntity<byte[]> getTextResource(HttpServletRequest req, HttpServletResponse res) throws IOException {
6568 File rootDir = pgService.getPageDir();
6669 File file = pgService.getPageDataFile(rootDir, req);
67- //logger.info("file={}", file.getAbsolutePath());
68- MediaType mt = getMediaType(req);
70+ MediaType mt = getMediaType(req, true);
6971 UsingInfo ui = UsingInfo.getInitInstance(req);
7072
7173 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());
7678 }
7779 HttpHeaders headers = createHeaders(mt, by);
7880 return new HttpEntity<byte[]>(by, headers);
7981
8082 }
8183
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){
8388 String id = req.getRequestURI();
8489 int n = id.lastIndexOf('.');
8590 String ext = (n >= 0) ? id.substring(n).toLowerCase() : "";
8691 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+
87100 return mt;
88101 }
89102
103+
104+
90105 private HttpHeaders createHeaders(MediaType mt, byte[] by){
91106 HttpHeaders headers = new HttpHeaders();
92- if (mt != null){
93- headers.setContentType(mt);
94- }
107+ headers.setContentType(mt);
95108 headers.setContentLength(by.length);
96109 return headers;
97110 }
diff -r f473d240205c -r 95fbfba23c04 src/main/java/jp/nanah/bastub/service/PageDataService.java
--- a/src/main/java/jp/nanah/bastub/service/PageDataService.java Fri Oct 01 08:33:02 2021 +0900
+++ b/src/main/java/jp/nanah/bastub/service/PageDataService.java Sun Oct 03 12:59:24 2021 +0900
@@ -5,14 +5,19 @@
55
66 import javax.servlet.http.HttpServletRequest;
77
8+import org.slf4j.Logger;
9+import org.slf4j.LoggerFactory;
810 import org.springframework.beans.factory.annotation.Value;
911 import org.springframework.stereotype.Service;
1012
13+import jp.nanah.bastub.controller.AnyRestController;
1114 import nanah.oslib.ResUtils;
1215
1316 @Service
1417 public class PageDataService {
1518
19+ private static final Logger logger = LoggerFactory.getLogger(AnyRestController.class);
20+
1621 @Value("${pagedata.root:pagedata}")
1722 private String pagedataPath;
1823
@@ -31,6 +36,8 @@
3136 }
3237
3338 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;
3542 }
3643 }
diff -r f473d240205c -r 95fbfba23c04 src/main/resources/application.properties
--- a/src/main/resources/application.properties Fri Oct 01 08:33:02 2021 +0900
+++ b/src/main/resources/application.properties Sun Oct 03 12:59:24 2021 +0900
@@ -25,3 +25,7 @@
2525 # \u203bExcel\u30d5\u30a1\u30a4\u30eb\u3082\u540c\u69d8
2626 path_last_slash_valid=0
2727
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