forked from apache/hertzbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] make the controller response be easy (apache#2679)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com> Co-authored-by: shown <yuluo08290126@gmail.com> Co-authored-by: aias00 <rokkki@163.com>
- Loading branch information
1 parent
9080ccd
commit 98354f2
Showing
6 changed files
with
98 additions
and
51 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
common/src/main/java/org/apache/hertzbeat/common/util/ResponseUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.common.util; | ||
|
||
import javax.naming.AuthenticationException; | ||
|
||
import org.apache.hertzbeat.common.constants.CommonConstants; | ||
import org.apache.hertzbeat.common.entity.dto.Message; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
/** | ||
* A tool which make the restful response be easy to use | ||
*/ | ||
public class ResponseUtil { | ||
public static <T, E extends Exception> ResponseEntity<Message<T>> handle(Supplier<T, E> supplier) { | ||
try { | ||
T result = supplier.get(); | ||
return ResponseEntity.ok(Message.success(result)); | ||
} catch (Exception e) { | ||
byte err = CommonConstants.FAIL_CODE; | ||
if (e.getClass().equals(AuthenticationException.class)) { | ||
err = CommonConstants.LOGIN_FAILED_CODE; | ||
} | ||
return ResponseEntity.ok(Message.fail(err, e.getMessage())); | ||
} | ||
} | ||
|
||
public static <T, E extends Exception> ResponseEntity<Message<T>> handle(Runnable runner) { | ||
try { | ||
runner.run(); | ||
return ResponseEntity.ok(Message.success()); | ||
} catch (Exception e) { | ||
byte err = CommonConstants.FAIL_CODE; | ||
if (e.getClass().equals(AuthenticationException.class)) { | ||
err = CommonConstants.LOGIN_FAILED_CODE; | ||
} | ||
return ResponseEntity.ok(Message.fail(err, e.getMessage())); | ||
} | ||
} | ||
|
||
/** | ||
* Supplier interface for getting result | ||
*/ | ||
public interface Supplier<T, E extends Exception> { | ||
|
||
/** | ||
* Gets a result. | ||
* | ||
* @return a result | ||
*/ | ||
T get() throws E; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters