You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by bi...@apache.org on 2017/02/10 09:03:48 UTC
[14/17] kylin git commit: KYLIN-2441 protocol for REST API result
format
KYLIN-2441 protocol for REST API result format
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/41332f3e
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/41332f3e
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/41332f3e
Branch: refs/heads/KYLIN-2428
Commit: 41332f3ed430b095efe5edefc831f0ec27f4b2ee
Parents: f2e8b69
Author: Hongbin Ma <ma...@apache.org>
Authored: Fri Feb 10 12:34:49 2017 +0800
Committer: Hongbin Ma <ma...@apache.org>
Committed: Fri Feb 10 12:35:14 2017 +0800
----------------------------------------------------------------------
.../rest/controller/EncodingController.java | 3 +-
.../rest/exception/BadRequestException.java | 37 +++++++++-----------
.../kylin/rest/response/EnvelopeResponse.java | 34 ++++--------------
.../kylin/rest/response/ErrorResponse.java | 32 ++++++++++++-----
.../kylin/rest/response/ResponseCode.java | 30 ++++++++++++++++
5 files changed, 79 insertions(+), 57 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kylin/blob/41332f3e/server-base/src/main/java/org/apache/kylin/rest/controller/EncodingController.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/controller/EncodingController.java b/server-base/src/main/java/org/apache/kylin/rest/controller/EncodingController.java
index b95394c..4a8b122 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/controller/EncodingController.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/controller/EncodingController.java
@@ -24,6 +24,7 @@ import java.util.Set;
import org.apache.kylin.metadata.datatype.DataType;
import org.apache.kylin.rest.response.EnvelopeResponse;
+import org.apache.kylin.rest.response.ResponseCode;
import org.apache.kylin.rest.service.EncodingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -65,6 +66,6 @@ public class EncodingController extends BasicController {
datatypeValidEncodings.put(dataTypeStr, encodingService.getValidEncodings(DataType.getType(dataTypeStr)));
}
- return new EnvelopeResponse(EnvelopeResponse.CODE_SUCCESS, datatypeValidEncodings, "");
+ return new EnvelopeResponse(ResponseCode.CODE_SUCCESS, datatypeValidEncodings, "");
}
}
http://git-wip-us.apache.org/repos/asf/kylin/blob/41332f3e/server-base/src/main/java/org/apache/kylin/rest/exception/BadRequestException.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/exception/BadRequestException.java b/server-base/src/main/java/org/apache/kylin/rest/exception/BadRequestException.java
index 42b671b..af1995b 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/exception/BadRequestException.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/exception/BadRequestException.java
@@ -18,6 +18,7 @@
package org.apache.kylin.rest.exception;
+import org.apache.kylin.rest.response.ResponseCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -27,35 +28,31 @@ import org.springframework.web.bind.annotation.ResponseStatus;
*/
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {
- /**
- *
- */
+
private static final long serialVersionUID = -6798154278095441848L;
- public BadRequestException(String s) {
- super(s);
- }
+ private String code;
/**
- *
+ * legacy support, new APIs should not call this. Instead, new APIs should provide return code
*/
- public BadRequestException() {
- super();
+ public BadRequestException(String msg) {
+ super(msg);
+ this.code = ResponseCode.CODE_UNDEFINED;
}
- /**
- * @param arg0
- * @param arg1
- */
- public BadRequestException(String arg0, Throwable arg1) {
- super(arg0, arg1);
+ public BadRequestException(String msg, String code) {
+ super(msg);
+ this.code = code;
}
- /**
- * @param arg0
- */
- public BadRequestException(Throwable arg0) {
- super(arg0);
+ public BadRequestException(String msg, String code, Throwable cause) {
+ super(msg, cause);
+ this.code = code;
+ }
+
+ public String getCode() {
+ return code;
}
}
http://git-wip-us.apache.org/repos/asf/kylin/blob/41332f3e/server-base/src/main/java/org/apache/kylin/rest/response/EnvelopeResponse.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/response/EnvelopeResponse.java b/server-base/src/main/java/org/apache/kylin/rest/response/EnvelopeResponse.java
index 564db70..7855dee 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/response/EnvelopeResponse.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/response/EnvelopeResponse.java
@@ -20,39 +20,17 @@ package org.apache.kylin.rest.response;
public class EnvelopeResponse {
- public final static String CODE_SUCCESS = "000";
+ public String code;
+ public Object data;
+ public String msg;
- private String code;
- private Object data;
- private String msg;
-
- public EnvelopeResponse(String code, Object data, String msg) {
- this.code = code;
- this.data = data;
- this.msg = msg;
- }
-
- public String getCode() {
- return code;
+ //only for child
+ protected EnvelopeResponse() {
}
- public void setCode(String code) {
+ public EnvelopeResponse(String code, Object data, String msg) {
this.code = code;
- }
-
- public Object getData() {
- return data;
- }
-
- public void setData(Object data) {
this.data = data;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
this.msg = msg;
}
}
http://git-wip-us.apache.org/repos/asf/kylin/blob/41332f3e/server-base/src/main/java/org/apache/kylin/rest/response/ErrorResponse.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/response/ErrorResponse.java b/server-base/src/main/java/org/apache/kylin/rest/response/ErrorResponse.java
index 3327cc0..508a35f 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/response/ErrorResponse.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/response/ErrorResponse.java
@@ -18,21 +18,37 @@
package org.apache.kylin.rest.response;
+import org.apache.kylin.rest.exception.BadRequestException;
+
+import com.google.common.base.Throwables;
+
/**
- * @author xduo
- *
+ * response to client when the return HTTP code is not 200
*/
-public class ErrorResponse {
+public class ErrorResponse extends EnvelopeResponse {
- public String url;
+ //stacktrace of the exception
+ public String stacktrace;
+
+ //same as EnvelopeResponse.msg, kept for legacy reasons
public String exception;
- /**
- * @param exception
- */
+ //request URL, kept from legacy codes
+ public String url;
+
public ErrorResponse(String url, Exception exception) {
+ super();
+
this.url = url;
this.exception = exception.getLocalizedMessage();
- }
+ this.msg = exception.getLocalizedMessage();
+ this.stacktrace = Throwables.getStackTraceAsString(exception);
+ this.data = null;
+ if (exception instanceof BadRequestException) {
+ this.code = ((BadRequestException) exception).getCode();
+ } else {
+ this.code = ResponseCode.CODE_UNDEFINED;
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/kylin/blob/41332f3e/server-base/src/main/java/org/apache/kylin/rest/response/ResponseCode.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/response/ResponseCode.java b/server-base/src/main/java/org/apache/kylin/rest/response/ResponseCode.java
new file mode 100644
index 0000000..8c3860a
--- /dev/null
+++ b/server-base/src/main/java/org/apache/kylin/rest/response/ResponseCode.java
@@ -0,0 +1,30 @@
+/*
+ * 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.kylin.rest.response;
+
+/**
+ * It's not HTTP return code!
+ * It represents code for business states
+ * each API can specify different semantics to each code
+ * 000 and 999 are reserved
+ */
+public class ResponseCode {
+ public final static String CODE_SUCCESS = "000";
+ public final static String CODE_UNDEFINED = "999";
+}