Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #1839 content-type related to config format #2167

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
/**
* 禁用缓存
Expand Down Expand Up @@ -283,6 +290,26 @@ 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);
return;
}
if (FileTypeEnum.XML.getFileType().equals(type)) {
response.setContentType("application/xml;charset=" + Constants.ENCODE);
return;
}
if (FileTypeEnum.HTML.getFileType().equals(type) || FileTypeEnum.HTM.getFileType().equals(type)) {
response.setContentType("text/html;charset=" + Constants.ENCODE);
return;
}
response.setContentType("text/plain;charset=" + Constants.ENCODE);
});
}

private static void releaseConfigReadLock(String groupKey) {
ConfigService.releaseReadLock(groupKey);
}
Expand Down