1717
1818package org .apache .hertzbeat .manager .controller ;
1919
20- import static org .apache .hertzbeat .common .constants .CommonConstants .FAIL_CODE ;
2120import static org .springframework .http .MediaType .APPLICATION_JSON_VALUE ;
2221import io .swagger .v3 .oas .annotations .Operation ;
2322import io .swagger .v3 .oas .annotations .Parameter ;
2928import org .apache .hertzbeat .common .entity .dto .Message ;
3029import org .apache .hertzbeat .common .entity .job .Job ;
3130import org .apache .hertzbeat .common .entity .manager .ParamDefine ;
31+ import org .apache .hertzbeat .common .util .ResponseUtil ;
3232import org .apache .hertzbeat .manager .pojo .dto .Hierarchy ;
3333import org .apache .hertzbeat .manager .pojo .dto .MonitorDefineDto ;
3434import org .apache .hertzbeat .manager .service .AppService ;
@@ -66,88 +66,72 @@ public class AppController {
6666 description = "The structure of the input parameters required to specify the monitoring type according to the app query" )
6767 public ResponseEntity <Message <List <ParamDefine >>> queryAppParamDefines (
6868 @ Parameter (description = "en: Monitoring type name" , example = "api" ) @ PathVariable ("app" ) final String app ) {
69- List <ParamDefine > paramDefines = appService .getAppParamDefines (app .toLowerCase ());
70- return ResponseEntity .ok (Message .success (paramDefines ));
69+ return ResponseUtil .handle (() -> appService .getAppParamDefines (app .toLowerCase ()));
7170 }
7271
7372 @ GetMapping (path = "/{monitorId}/pushdefine" )
7473 @ Operation (summary = "The definition structure of the specified monitoring type according to the push query" ,
7574 description = "The definition structure of the specified monitoring type according to the push query" )
7675 public ResponseEntity <Message <Job >> queryPushDefine (
7776 @ Parameter (description = "en: Monitoring type name" , example = "api" ) @ PathVariable ("monitorId" ) final Long monitorId ) {
78- Job define = appService .getPushDefine (monitorId );
79- return ResponseEntity .ok (Message .success (define ));
77+ return ResponseUtil .handle (() -> appService .getPushDefine (monitorId ));
8078 }
8179
8280 @ GetMapping (path = "/{monitorId}/define/dynamic" )
8381 @ Operation (summary = "The definition structure of the specified monitoring type according to the push query" ,
8482 description = "The definition structure of the specified monitoring type according to the push query" )
8583 public ResponseEntity <Message <Job >> queryAutoGenerateDynamicAppDefine (
8684 @ Parameter (description = "Monitoring id" , example = "5435345" ) @ PathVariable ("monitorId" ) final Long monitorId ) {
87- Job define = appService .getAutoGenerateDynamicDefine (monitorId );
88- return ResponseEntity .ok (Message .success (define ));
85+ return ResponseUtil .handle (() -> appService .getAutoGenerateDynamicDefine (monitorId ));
8986 }
9087
9188 @ GetMapping (path = "/{app}/define" )
9289 @ Operation (summary = "The definition structure of the specified monitoring type according to the app query" ,
9390 description = "The definition structure of the specified monitoring type according to the app query" )
9491 public ResponseEntity <Message <Job >> queryAppDefine (
9592 @ Parameter (description = "en: Monitoring type name" , example = "api" ) @ PathVariable ("app" ) final String app ) {
96- Job define = appService .getAppDefine (app .toLowerCase ());
97- return ResponseEntity .ok (Message .success (define ));
93+ return ResponseUtil .handle (() -> appService .getAppDefine (app .toLowerCase ()));
9894 }
9995
10096 @ GetMapping (path = "/{app}/define/yml" )
10197 @ Operation (summary = "The definition yml of the specified monitoring type according to the app query" ,
10298 description = "The definition yml of the specified monitoring type according to the app query" )
10399 public ResponseEntity <Message <String >> queryAppDefineYml (
104100 @ Parameter (description = "en: Monitoring type name" , example = "api" ) @ PathVariable ("app" ) final String app ) {
105- String defineContent = appService .getMonitorDefineFileContent (app );
106- return ResponseEntity .ok (Message .successWithData (defineContent ));
101+ return ResponseUtil .handle (() -> appService .getMonitorDefineFileContent (app ));
107102 }
108103
109104 @ DeleteMapping (path = "/{app}/define/yml" )
110105 @ Operation (summary = "Delete monitor define yml" , description = "Delete the definition YML for the specified monitoring type according to the app" )
111106 public ResponseEntity <Message <Void >> deleteAppDefineYml (
112107 @ Parameter (description = "en: Monitoring type name" , example = "api" ) @ PathVariable ("app" ) final String app ) {
113- try {
114- appService .deleteMonitorDefine (app );
115- } catch (Exception e ) {
116- return ResponseEntity .ok (Message .fail (FAIL_CODE , e .getMessage ()));
117- }
118- return ResponseEntity .ok (Message .success ());
108+ return ResponseUtil .handle (() -> appService .deleteMonitorDefine (app ));
119109 }
120110
121111 @ PostMapping (path = "/define/yml" )
122112 @ Operation (summary = "Add new monitoring type define yml" , description = "Add new monitoring type define yml" )
123113 public ResponseEntity <Message <Void >> newAppDefineYml (@ Valid @ RequestBody MonitorDefineDto defineDto ) {
124- try {
114+ return ResponseUtil . handle (() -> {
125115 for (String riskyToken : RISKY_STR_ARR ) {
126116 if (defineDto .getDefine ().contains (riskyToken )) {
127- return ResponseEntity . ok ( Message . fail ( FAIL_CODE , "can not has malicious remote script" ) );
117+ throw new RuntimeException ( "can not has malicious remote script" );
128118 }
129119 }
130120 appService .applyMonitorDefineYml (defineDto .getDefine (), false );
131- } catch (Exception e ) {
132- return ResponseEntity .ok (Message .fail (FAIL_CODE , e .getMessage ()));
133- }
134- return ResponseEntity .ok (Message .success ());
121+ });
135122 }
136123
137124 @ PutMapping (path = "/define/yml" )
138125 @ Operation (summary = "Update monitoring type define yml" , description = "Update monitoring type define yml" )
139126 public ResponseEntity <Message <Void >> updateAppDefineYml (@ Valid @ RequestBody MonitorDefineDto defineDto ) {
140- try {
127+ return ResponseUtil . handle (() -> {
141128 for (String riskyToken : RISKY_STR_ARR ) {
142129 if (defineDto .getDefine ().contains (riskyToken )) {
143- return ResponseEntity . ok ( Message . fail ( FAIL_CODE , "can not has malicious remote script" ) );
130+ throw new RuntimeException ( "can not has malicious remote script" );
144131 }
145132 }
146133 appService .applyMonitorDefineYml (defineDto .getDefine (), true );
147- } catch (Exception e ) {
148- return ResponseEntity .ok (Message .fail (FAIL_CODE , e .getMessage ()));
149- }
150- return ResponseEntity .ok (Message .success ());
134+ });
151135 }
152136
153137 @ GetMapping (path = "/hierarchy" )
@@ -156,9 +140,8 @@ public ResponseEntity<Message<List<Hierarchy>>> queryAppsHierarchy(
156140 @ Parameter (description = "en: language type" ,
157141 example = "zh-CN" )
158142 @ RequestParam (name = "lang" , required = false ) String lang ) {
159- lang = getLang (lang );
160- List <Hierarchy > appHierarchies = appService .getAllAppHierarchy (lang );
161- return ResponseEntity .ok (Message .success (appHierarchies ));
143+ String newLang = getLang (lang );
144+ return ResponseUtil .handle (() -> appService .getAllAppHierarchy (newLang ));
162145 }
163146
164147 @ GetMapping (path = "/hierarchy/{app}" )
@@ -168,9 +151,8 @@ public ResponseEntity<Message<List<Hierarchy>>> queryAppsHierarchyByApp(
168151 example = "zh-CN" )
169152 @ RequestParam (name = "lang" , required = false ) String lang ,
170153 @ Parameter (description = "en: Monitoring type name" , example = "api" ) @ PathVariable ("app" ) final String app ) {
171- lang = getLang (lang );
172- List <Hierarchy > appHierarchies = appService .getAppHierarchy (app , lang );
173- return ResponseEntity .ok (Message .success (appHierarchies ));
154+ String newLang = getLang (lang );
155+ return ResponseUtil .handle (() -> appService .getAppHierarchy (app , newLang ));
174156 }
175157
176158 @ GetMapping (path = "/defines" )
@@ -179,9 +161,8 @@ public ResponseEntity<Message<Map<String, String>>> getAllAppDefines(
179161 @ Parameter (description = "en: language type" ,
180162 example = "zh-CN" )
181163 @ RequestParam (name = "lang" , required = false ) String lang ) {
182- lang = getLang (lang );
183- Map <String , String > allAppDefines = appService .getI18nApps (lang );
184- return ResponseEntity .ok (Message .success (allAppDefines ));
164+ String newLang = getLang (lang );
165+ return ResponseUtil .handle (() -> appService .getI18nApps (newLang ));
185166 }
186167
187168 private String getLang (@ RequestParam (name = "lang" , required = false ) @ Parameter (description = "en: language type" , example = "zh-CN" ) String lang ) {
0 commit comments