Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
152b0323
apollo
项目概览
superrain51
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
152b0323
编写于
12月 16, 2018
作者:
J
Jared Tan
提交者:
Jason Song
12月 16, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat:import and export configs. (#1767)
feat:import and export configs
上级
e4aa9f7f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
146 addition
and
0 deletion
+146
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java
...ork/apollo/portal/controller/ConfigsExportController.java
+116
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/ConfigToFileUtils.java
...ctrip/framework/apollo/portal/util/ConfigToFileUtils.java
+30
-0
未找到文件。
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java
0 → 100644
浏览文件 @
152b0323
package
com.ctrip.framework.apollo.portal.controller
;
import
com.ctrip.framework.apollo.common.dto.NamespaceDTO
;
import
com.ctrip.framework.apollo.common.exception.BadRequestException
;
import
com.ctrip.framework.apollo.common.exception.ServiceException
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO
;
import
com.ctrip.framework.apollo.portal.entity.model.NamespaceTextModel
;
import
com.ctrip.framework.apollo.portal.service.ItemService
;
import
com.ctrip.framework.apollo.portal.service.NamespaceService
;
import
com.ctrip.framework.apollo.portal.util.ConfigToFileUtils
;
import
com.google.common.base.Joiner
;
import
com.google.common.base.Splitter
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.stream.Collectors
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* jian.tan
*/
@RestController
public
class
ConfigsExportController
{
@Autowired
private
ItemService
configService
;
@Autowired
private
NamespaceService
namespaceService
;
@RequestMapping
(
value
=
"/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/import"
,
method
=
RequestMethod
.
POST
)
public
void
importConfigFile
(
@PathVariable
String
appId
,
@PathVariable
String
env
,
@PathVariable
String
clusterName
,
@PathVariable
String
namespaceName
,
@RequestParam
Integer
namespaceId
,
@RequestParam
(
"file"
)
MultipartFile
file
)
{
if
(
file
.
isEmpty
())
{
throw
new
BadRequestException
(
"The file is empty."
);
}
NamespaceDTO
namespaceDTO
=
namespaceService
.
loadNamespaceBaseInfo
(
appId
,
Env
.
fromString
(
env
),
clusterName
,
namespaceName
);
if
(
Objects
.
isNull
(
namespaceDTO
))
{
throw
new
BadRequestException
(
String
.
format
(
"Namespace: {} not exist."
,
namespaceName
));
}
NamespaceTextModel
model
=
new
NamespaceTextModel
();
List
<
String
>
fileNameSplit
=
Splitter
.
on
(
"."
).
splitToList
(
file
.
getOriginalFilename
());
if
(
fileNameSplit
.
size
()
<=
1
)
{
throw
new
BadRequestException
(
"The file format is invalid."
);
}
String
format
=
fileNameSplit
.
get
(
fileNameSplit
.
size
()
-
1
);
model
.
setFormat
(
format
);
model
.
setAppId
(
appId
);
model
.
setEnv
(
env
);
model
.
setClusterName
(
clusterName
);
model
.
setNamespaceName
(
namespaceName
);
model
.
setNamespaceId
(
namespaceId
);
String
configText
;
try
(
InputStream
in
=
file
.
getInputStream
()){
configText
=
ConfigToFileUtils
.
fileToString
(
in
);
}
catch
(
IOException
e
)
{
throw
new
ServiceException
(
"Read config file errors:{}"
,
e
);
}
model
.
setConfigText
(
configText
);
configService
.
updateConfigItemByText
(
model
);
}
@RequestMapping
(
value
=
"/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/export"
,
method
=
RequestMethod
.
GET
)
public
void
exportItems
(
@PathVariable
String
appId
,
@PathVariable
String
env
,
@PathVariable
String
clusterName
,
@PathVariable
String
namespaceName
,
HttpServletResponse
res
)
{
List
<
String
>
fileNameSplit
=
Splitter
.
on
(
"."
).
splitToList
(
namespaceName
);
String
fileName
=
fileNameSplit
.
size
()
<=
1
?
Joiner
.
on
(
"."
)
.
join
(
namespaceName
,
ConfigFileFormat
.
Properties
.
getValue
())
:
namespaceName
;
NamespaceBO
namespaceBO
=
namespaceService
.
loadNamespaceBO
(
appId
,
Env
.
fromString
(
env
),
clusterName
,
namespaceName
);
//generate a file.
res
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
fileName
);
List
<
String
>
fileItems
=
namespaceBO
.
getItems
().
stream
().
map
(
itemBO
->
{
String
key
=
itemBO
.
getItem
().
getKey
();
String
value
=
itemBO
.
getItem
().
getValue
();
if
(
ConfigConsts
.
CONFIG_FILE_CONTENT_KEY
.
equals
(
key
))
{
return
value
;
}
if
(
""
.
equals
(
key
))
{
return
Joiner
.
on
(
""
).
join
(
itemBO
.
getItem
().
getKey
(),
itemBO
.
getItem
().
getValue
());
}
return
Joiner
.
on
(
" = "
).
join
(
itemBO
.
getItem
().
getKey
(),
itemBO
.
getItem
().
getValue
());
}).
collect
(
Collectors
.
toList
());
try
{
ConfigToFileUtils
.
itemsToFile
(
res
.
getOutputStream
(),
fileItems
);
}
catch
(
Exception
e
)
{
throw
new
ServiceException
(
"export items failed:{}"
,
e
);
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/ConfigToFileUtils.java
0 → 100644
浏览文件 @
152b0323
package
com.ctrip.framework.apollo.portal.util
;
import
java.io.BufferedReader
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.PrintWriter
;
import
java.util.List
;
import
java.util.stream.Collectors
;
/**
* jian.tan
*/
public
class
ConfigToFileUtils
{
public
static
void
itemsToFile
(
OutputStream
os
,
List
<
String
>
items
)
{
try
{
PrintWriter
printWriter
=
new
PrintWriter
(
os
);
items
.
forEach
(
item
->
printWriter
.
println
(
item
));
printWriter
.
close
();
}
catch
(
Exception
e
)
{
throw
e
;
}
}
public
static
String
fileToString
(
InputStream
inputStream
)
{
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
return
bufferedReader
.
lines
().
collect
(
Collectors
.
joining
(
System
.
lineSeparator
()));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录