From 1cde9b7543547ecd2ae117285f7e366ed1a111a2 Mon Sep 17 00:00:00 2001 From: DoolinMa <564121522@qq.com> Date: Wed, 11 Dec 2019 18:57:48 +0800 Subject: [PATCH 1/3] issue #1839 content-type related to config format default content-type text/plain JSON->application/json XML->application/xml HTLM->text/html YAML,TEXT,Properties->text/plain --- .../server/controller/ConfigServletInner.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java index 8fa789a4d37..5dd99005061 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java @@ -16,7 +16,9 @@ package com.alibaba.nacos.config.server.controller; import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.config.server.enums.FileTypeEnum; import com.alibaba.nacos.config.server.model.CacheItem; +import com.alibaba.nacos.config.server.model.ConfigAllInfo; import com.alibaba.nacos.config.server.model.ConfigInfoBase; import com.alibaba.nacos.config.server.service.ConfigService; import com.alibaba.nacos.config.server.service.DiskUtil; @@ -40,6 +42,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; +import java.util.Optional; import static com.alibaba.nacos.config.server.utils.LogUtil.pullLog; import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE; @@ -219,7 +222,11 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon } } } - + /* + *根据选择的配置格式设置response的Content-Type + * 如果没有读取到对应的配置格式Content-Type默认为application/json + */ + setResponseContentType(response,dataId,group,tenant); response.setHeader(Constants.CONTENT_MD5, md5); /** * 禁用缓存 @@ -283,6 +290,23 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon return HttpServletResponse.SC_OK + ""; } + private void setResponseContentType(HttpServletResponse response,String dataId, String group, + String tenant) { + ConfigAllInfo configAllInfo = persistService.findConfigAllInfo(dataId, group, tenant); + Optional.ofNullable(configAllInfo).ifPresent(configAllInfo1 -> { + String type = configAllInfo.getType(); + if(FileTypeEnum.JSON.getFileType().equals(type)){ + response.setContentType("application/json;charset=" + Constants.ENCODE); + }else if(FileTypeEnum.XML.getFileType().equals(type)) { + response.setContentType("application/xml;charset=" + Constants.ENCODE); + }else if(FileTypeEnum.HTML.getFileType().equals(type) || FileTypeEnum.HTM.getFileType().equals(type)){ + response.setContentType("text/html;charset=" + Constants.ENCODE); + }else { + response.setContentType("text/plain;charset=" + Constants.ENCODE); + } + }); + } + private static void releaseConfigReadLock(String groupKey) { ConfigService.releaseReadLock(groupKey); } From c0497bcff85b4c40310553a0239ee808c45f9de4 Mon Sep 17 00:00:00 2001 From: DoolinMa <564121522@qq.com> Date: Fri, 13 Dec 2019 09:32:24 +0800 Subject: [PATCH 2/3] reformat code --- .../config/server/controller/ConfigServletInner.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java index 5dd99005061..06050d5cef4 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java @@ -226,7 +226,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon *根据选择的配置格式设置response的Content-Type * 如果没有读取到对应的配置格式Content-Type默认为application/json */ - setResponseContentType(response,dataId,group,tenant); + setResponseContentType(response, dataId, group, tenant); response.setHeader(Constants.CONTENT_MD5, md5); /** * 禁用缓存 @@ -290,18 +290,18 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon return HttpServletResponse.SC_OK + ""; } - private void setResponseContentType(HttpServletResponse response,String dataId, String group, + private void setResponseContentType(HttpServletResponse response, String dataId, String group, String tenant) { ConfigAllInfo configAllInfo = persistService.findConfigAllInfo(dataId, group, tenant); Optional.ofNullable(configAllInfo).ifPresent(configAllInfo1 -> { String type = configAllInfo.getType(); - if(FileTypeEnum.JSON.getFileType().equals(type)){ + if (FileTypeEnum.JSON.getFileType().equals(type)) { response.setContentType("application/json;charset=" + Constants.ENCODE); - }else if(FileTypeEnum.XML.getFileType().equals(type)) { + } else if (FileTypeEnum.XML.getFileType().equals(type)) { response.setContentType("application/xml;charset=" + Constants.ENCODE); - }else if(FileTypeEnum.HTML.getFileType().equals(type) || FileTypeEnum.HTM.getFileType().equals(type)){ + } else if (FileTypeEnum.HTML.getFileType().equals(type) || FileTypeEnum.HTM.getFileType().equals(type)) { response.setContentType("text/html;charset=" + Constants.ENCODE); - }else { + } else { response.setContentType("text/plain;charset=" + Constants.ENCODE); } }); From 76eb8b8e8d56c4b2e12e0f2218d4424e49d52503 Mon Sep 17 00:00:00 2001 From: DoolinMa <564121522@qq.com> Date: Mon, 30 Dec 2019 16:23:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9if-else=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E4=BD=BF=E9=80=BB=E8=BE=91=E5=88=A4=E6=96=AD=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=8D=E8=B6=85=E8=BF=873=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/ConfigServletInner.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java index 06050d5cef4..a098d5fcc1b 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java @@ -290,20 +290,23 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon return HttpServletResponse.SC_OK + ""; } - private void setResponseContentType(HttpServletResponse response, String dataId, String group, - String tenant) { + private void setResponseContentType(HttpServletResponse response, String dataId, String group, String tenant) { ConfigAllInfo configAllInfo = persistService.findConfigAllInfo(dataId, group, tenant); Optional.ofNullable(configAllInfo).ifPresent(configAllInfo1 -> { String type = configAllInfo.getType(); if (FileTypeEnum.JSON.getFileType().equals(type)) { response.setContentType("application/json;charset=" + Constants.ENCODE); - } else if (FileTypeEnum.XML.getFileType().equals(type)) { + return; + } + if (FileTypeEnum.XML.getFileType().equals(type)) { response.setContentType("application/xml;charset=" + Constants.ENCODE); - } else if (FileTypeEnum.HTML.getFileType().equals(type) || FileTypeEnum.HTM.getFileType().equals(type)) { + return; + } + if (FileTypeEnum.HTML.getFileType().equals(type) || FileTypeEnum.HTM.getFileType().equals(type)) { response.setContentType("text/html;charset=" + Constants.ENCODE); - } else { - response.setContentType("text/plain;charset=" + Constants.ENCODE); + return; } + response.setContentType("text/plain;charset=" + Constants.ENCODE); }); }