Skip to content

Commit

Permalink
[alibaba#1839]add ContentType return while getConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweizZZ committed Jul 29, 2020
1 parent 5b76fd0 commit e4a478a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ private MediaType() {

public static final String TEXT_HTML = "text/html";

public static final String TEXT_PLAIN = "text/xml";
public static final String TEXT_PLAIN = "text/plain";

}
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.common.constant.HttpHeaderConsts;
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.ConfigInfoBase;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
Expand Down Expand Up @@ -136,8 +138,18 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
isBeta = true;
}
}
String configType = cacheItem.getType();
response.setHeader("Config-Type", (null != configType) ? configType : "text");

final String configType =
(null != cacheItem.getType()) ? cacheItem.getType() : FileTypeEnum.TEXT.getFileType();
response.setHeader("Config-Type", configType);

String contentTypeHeader;
try {
contentTypeHeader = FileTypeEnum.valueOf(configType.toUpperCase()).getContentType();
} catch (IllegalArgumentException ex) {
contentTypeHeader = FileTypeEnum.TEXT.getContentType();
}
response.setHeader(HttpHeaderConsts.CONTENT_TYPE, contentTypeHeader);
}
File file = null;
ConfigInfoBase configInfoBase = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.config.server.enums;

import com.alibaba.nacos.common.http.param.MediaType;

/**
* Config file type enum.
*
Expand All @@ -27,58 +29,73 @@ public enum FileTypeEnum {
/**
* Yaml file.
*/
YML("yaml"),
YML("yaml", MediaType.TEXT_PLAIN),

/**
* Yaml file.
*/
YAML("yaml"),
YAML("yaml", MediaType.TEXT_PLAIN),

/**
* Text file.
*/
TXT("text"),
TXT("text", MediaType.TEXT_PLAIN),

/**
* Text file.
*/
TEXT("text"),
TEXT("text", MediaType.TEXT_PLAIN),

/**
* Json file.
*/
JSON("json"),
JSON("json", MediaType.APPLICATION_JSON),

/**
* Xml file.
*/
XML("xml"),
XML("xml", MediaType.APPLICATION_XML),

/**
* Html file.
*/
HTM("html"),
HTM("html", MediaType.TEXT_HTML),

/**
* Html file.
*/
HTML("html"),
HTML("html", MediaType.TEXT_HTML),

/**
* Properties file.
*/
PROPERTIES("properties");
PROPERTIES("properties", MediaType.TEXT_PLAIN);

/**
* File type corresponding to file extension.
*/
private String fileType;

/**
* Http Content type corresponding to file extension.
*/
private String contentType;

FileTypeEnum(String fileType) {
this.fileType = fileType;
this.contentType = MediaType.TEXT_PLAIN;
}

FileTypeEnum(String fileType, String contentType) {
this.fileType = fileType;
this.contentType = contentType;
}

public String getFileType() {
return this.fileType;
}

public String getContentType() {
return contentType;
}
}

0 comments on commit e4a478a

Please sign in to comment.