25
25
import com .ctrip .framework .apollo .configservice .util .WatchKeysUtil ;
26
26
import com .ctrip .framework .apollo .core .ConfigConsts ;
27
27
import com .ctrip .framework .apollo .core .dto .ApolloConfig ;
28
+ import com .ctrip .framework .apollo .core .enums .ConfigFileFormat ;
28
29
import com .ctrip .framework .apollo .core .utils .PropertiesUtil ;
29
30
import com .ctrip .framework .apollo .tracer .Tracer ;
30
31
import com .google .common .base .Joiner ;
@@ -67,8 +68,10 @@ public class ConfigFileController implements ReleaseMessageListener {
67
68
private static final Joiner STRING_JOINER = Joiner .on (ConfigConsts .CLUSTER_NAMESPACE_SEPARATOR );
68
69
private static final long MAX_CACHE_SIZE = 50 * 1024 * 1024 ; // 50MB
69
70
private static final long EXPIRE_AFTER_WRITE = 30 ;
70
- private final HttpHeaders propertiesResponseHeaders ;
71
+ private final HttpHeaders plainTextResponseHeaders ;
71
72
private final HttpHeaders jsonResponseHeaders ;
73
+ private final HttpHeaders yamlResponseHeaders ;
74
+ private final HttpHeaders xmlResponseHeaders ;
72
75
private final ResponseEntity <String > NOT_FOUND_RESPONSE ;
73
76
private Cache <String , String > localCache ;
74
77
private final Multimap <String , String >
@@ -106,10 +109,14 @@ public ConfigFileController(
106
109
logger .debug ("removed cache key: {}" , cacheKey );
107
110
})
108
111
.build ();
109
- propertiesResponseHeaders = new HttpHeaders ();
110
- propertiesResponseHeaders .add ("Content-Type" , "text/plain;charset=UTF-8" );
112
+ plainTextResponseHeaders = new HttpHeaders ();
113
+ plainTextResponseHeaders .add ("Content-Type" , "text/plain;charset=UTF-8" );
111
114
jsonResponseHeaders = new HttpHeaders ();
112
115
jsonResponseHeaders .add ("Content-Type" , "application/json;charset=UTF-8" );
116
+ yamlResponseHeaders = new HttpHeaders ();
117
+ yamlResponseHeaders .add ("Content-Type" , "application/yaml;charset=UTF-8" );
118
+ xmlResponseHeaders = new HttpHeaders ();
119
+ xmlResponseHeaders .add ("Content-Type" , "application/xml;charset=UTF-8" );
113
120
NOT_FOUND_RESPONSE = new ResponseEntity <>(HttpStatus .NOT_FOUND );
114
121
this .configController = configController ;
115
122
this .namespaceUtil = namespaceUtil ;
@@ -136,7 +143,7 @@ public ResponseEntity<String> queryConfigAsProperties(@PathVariable String appId
136
143
return NOT_FOUND_RESPONSE ;
137
144
}
138
145
139
- return new ResponseEntity <>(result , propertiesResponseHeaders , HttpStatus .OK );
146
+ return new ResponseEntity <>(result , plainTextResponseHeaders , HttpStatus .OK );
140
147
}
141
148
142
149
@ GetMapping (value = "/json/{appId}/{clusterName}/{namespace:.+}" )
@@ -160,6 +167,44 @@ public ResponseEntity<String> queryConfigAsJson(@PathVariable String appId,
160
167
return new ResponseEntity <>(result , jsonResponseHeaders , HttpStatus .OK );
161
168
}
162
169
170
+ @ GetMapping (value = "/raw/{appId}/{clusterName}/{namespace:.+}" )
171
+ public ResponseEntity <String > queryConfigAsRaw (@ PathVariable String appId ,
172
+ @ PathVariable String clusterName ,
173
+ @ PathVariable String namespace ,
174
+ @ RequestParam (value = "dataCenter" , required = false ) String dataCenter ,
175
+ @ RequestParam (value = "ip" , required = false ) String clientIp ,
176
+ @ RequestParam (value = "label" , required = false ) String clientLabel ,
177
+ HttpServletRequest request ,
178
+ HttpServletResponse response ) throws IOException {
179
+
180
+ String result =
181
+ queryConfig (ConfigFileOutputFormat .RAW , appId , clusterName , namespace , dataCenter ,
182
+ clientIp , clientLabel , request , response );
183
+
184
+ if (result == null ) {
185
+ return NOT_FOUND_RESPONSE ;
186
+ }
187
+
188
+ ConfigFileFormat format = determineNamespaceFormat (namespace );
189
+ HttpHeaders responseHeaders ;
190
+ switch (format ) {
191
+ case JSON :
192
+ responseHeaders = jsonResponseHeaders ;
193
+ break ;
194
+ case YML :
195
+ case YAML :
196
+ responseHeaders = yamlResponseHeaders ;
197
+ break ;
198
+ case XML :
199
+ responseHeaders = xmlResponseHeaders ;
200
+ break ;
201
+ default :
202
+ responseHeaders = plainTextResponseHeaders ;
203
+ break ;
204
+ }
205
+ return new ResponseEntity <>(result , responseHeaders , HttpStatus .OK );
206
+ }
207
+
163
208
String queryConfig (ConfigFileOutputFormat outputFormat , String appId , String clusterName ,
164
209
String namespace , String dataCenter , String clientIp , String clientLabel ,
165
210
HttpServletRequest request ,
@@ -247,11 +292,24 @@ private String loadConfig(ConfigFileOutputFormat outputFormat, String appId, Str
247
292
case JSON :
248
293
result = GSON .toJson (apolloConfig .getConfigurations ());
249
294
break ;
295
+ case RAW :
296
+ result = getRawConfigContent (apolloConfig );
297
+ break ;
250
298
}
251
299
252
300
return result ;
253
301
}
254
302
303
+ private String getRawConfigContent (ApolloConfig apolloConfig ) throws IOException {
304
+ ConfigFileFormat format = determineNamespaceFormat (apolloConfig .getNamespaceName ());
305
+ if (format == ConfigFileFormat .Properties ) {
306
+ Properties properties = new Properties ();
307
+ properties .putAll (apolloConfig .getConfigurations ());
308
+ return PropertiesUtil .toString (properties );
309
+ }
310
+ return apolloConfig .getConfigurations ().get ("content" );
311
+ }
312
+
255
313
String assembleCacheKey (ConfigFileOutputFormat outputFormat , String appId , String clusterName ,
256
314
String namespace ,
257
315
String dataCenter ) {
@@ -263,6 +321,17 @@ String assembleCacheKey(ConfigFileOutputFormat outputFormat, String appId, Strin
263
321
return STRING_JOINER .join (keyParts );
264
322
}
265
323
324
+ ConfigFileFormat determineNamespaceFormat (String namespaceName ) {
325
+ String lowerCase = namespaceName .toLowerCase ();
326
+ for (ConfigFileFormat format : ConfigFileFormat .values ()) {
327
+ if (lowerCase .endsWith ("." + format .getValue ())) {
328
+ return format ;
329
+ }
330
+ }
331
+
332
+ return ConfigFileFormat .Properties ;
333
+ }
334
+
266
335
@ Override
267
336
public void handleMessage (ReleaseMessage message , String channel ) {
268
337
logger .info ("message received - channel: {}, message: {}" , channel , message );
@@ -286,7 +355,7 @@ public void handleMessage(ReleaseMessage message, String channel) {
286
355
}
287
356
288
357
enum ConfigFileOutputFormat {
289
- PROPERTIES ("properties" ), JSON ("json" );
358
+ PROPERTIES ("properties" ), JSON ("json" ), RAW ( "raw" ) ;
290
359
291
360
private String value ;
292
361
0 commit comments