aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/restapi
diff options
context:
space:
mode:
authorVijay Venkatesh Kumar <vv770d@att.com>2018-06-15 21:33:21 +0000
committerGerrit Code Review <gerrit@onap.org>2018-06-15 21:33:21 +0000
commitcea2e184ff50e91833efd23408d647ccb295e847 (patch)
tree9c1e90b6ae4783e089bfdbd54a7580c3847e009b /src/main/java/org/onap/dcae/restapi
parent0421f6b4f00cf4657d3aee35946d5ebb8b1c2132 (diff)
parentb48ea0e307f5977b5d2b9dcc4ebb22a7bc3f1710 (diff)
Merge "Fix bug about custom exceptions not being used"
Diffstat (limited to 'src/main/java/org/onap/dcae/restapi')
-rw-r--r--src/main/java/org/onap/dcae/restapi/ApiException.java70
-rw-r--r--src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java69
2 files changed, 91 insertions, 48 deletions
diff --git a/src/main/java/org/onap/dcae/restapi/ApiException.java b/src/main/java/org/onap/dcae/restapi/ApiException.java
new file mode 100644
index 00000000..3feeacfc
--- /dev/null
+++ b/src/main/java/org/onap/dcae/restapi/ApiException.java
@@ -0,0 +1,70 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcae.restapi;
+
+import com.google.common.base.CaseFormat;
+import org.json.JSONObject;
+
+/**
+ * @author Pawel Szalapski (pawel.szalapski@nokia.com)
+ */
+public enum ApiException {
+
+ INVALID_JSON_INPUT(ExceptionType.SERVICE_EXCEPTION, "SVC0002", "Incorrect JSON payload", 400),
+ SCHEMA_VALIDATION_FAILED(ExceptionType.SERVICE_EXCEPTION, "SVC0002", "Bad Parameter (JSON does not conform to schema)", 400),
+ INVALID_CONTENT_TYPE(ExceptionType.SERVICE_EXCEPTION, "SVC0002", "Bad Parameter (Incorrect request Content-Type)", 400),
+ UNAUTHORIZED_USER(ExceptionType.POLICY_EXCEPTION, "POL2000", "Unauthorized user", 401),
+ NO_SERVER_RESOURCES(ExceptionType.SERVICE_EXCEPTION, "SVC1000", "No server resources (internal processing queue full)", 503);
+
+ public final ExceptionType type;
+ public final String code;
+ public final String details;
+ public final int httpStatusCode;
+
+ ApiException(ExceptionType type, String code, String details, int httpStatusCode) {
+ this.type = type;
+ this.code = code;
+ this.details = details;
+ this.httpStatusCode = httpStatusCode;
+ }
+
+ public enum ExceptionType {
+ SERVICE_EXCEPTION, POLICY_EXCEPTION;
+
+ @Override
+ public String toString() {
+ return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, this.name());
+ }
+ }
+
+ public JSONObject toJSON() {
+ JSONObject exceptionTypeNode = new JSONObject();
+ exceptionTypeNode.put("messageId", code );
+ exceptionTypeNode.put("text", details);
+
+ JSONObject requestErrorNode = new JSONObject();
+ requestErrorNode.put(type.toString(), exceptionTypeNode);
+
+ JSONObject rootNode = new JSONObject();
+ rootNode.put("requestError", requestErrorNode);
+ return rootNode;
+ }
+
+}
diff --git a/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java b/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java
index f5a5e6d8..24bd96ea 100644
--- a/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java
+++ b/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java
@@ -29,27 +29,25 @@ import com.att.nsa.logging.LoggingContext;
import com.att.nsa.logging.log4j.EcompFields;
import com.att.nsa.security.db.simple.NsaSimpleApiKey;
import com.google.gson.JsonParser;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+import java.util.Base64;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.onap.dcae.commonFunction.CommonStartup;
import org.onap.dcae.commonFunction.CommonStartup.QueueFullException;
-import org.onap.dcae.commonFunction.CustomExceptionLoader;
import org.onap.dcae.commonFunction.VESLogger;
+import org.onap.dcae.restapi.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.util.Base64;
-import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
public class EventReceipt extends NsaBaseEndpoint {
private static final Logger log = LoggerFactory.getLogger(EventReceipt.class);
@@ -116,7 +114,7 @@ public class EventReceipt extends NsaBaseEndpoint {
//log.info("Invalid user request :" + userId + " FROM " + ctx.request().getRemoteAddress() + " " + ctx.request().getContentType() + MESSAGE + jsonObject);
log.info(String.format("Unauthorized request %s FROM %s %s %s %s", getUser(ctx), ctx.request().getRemoteAddress(), ctx.request().getContentType(), MESSAGE, jsonObject));
CommonStartup.eplog.info("EVENT_RECEIPT_FAILURE: Unauthorized user" + userId + x);
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k401_unauthorized, "Invalid user");
+ respondWithCustomMsginJson(ctx, ApiException.UNAUTHORIZED_USER);
return;
}
@@ -131,12 +129,12 @@ public class EventReceipt extends NsaBaseEndpoint {
log.error(String.format("Couldn't parse JSON Array - HttpStatusCodes.k400_badRequest%d%s%s",
HttpStatusCodes.k400_badRequest, MESSAGE, x.getMessage()));
CommonStartup.eplog.info("EVENT_RECEIPT_FAILURE: Invalid user request " + x);
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k400_badRequest, "Couldn't parse JSON object");
+ respondWithCustomMsginJson(ctx, ApiException.INVALID_JSON_INPUT);
return;
} catch (QueueFullException e) {
log.error("Collector internal queue full :" + e.getMessage(), e);
CommonStartup.eplog.info("EVENT_RECEIPT_FAILURE: QueueFull" + e);
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k503_serviceUnavailable, "Queue full");
+ respondWithCustomMsginJson(ctx, ApiException.NO_SERVER_RESOURCES);
return;
} finally {
if (fr != null) {
@@ -187,20 +185,18 @@ public class EventReceipt extends NsaBaseEndpoint {
log.info("Validation successful");
} else if (valresult.equals("false")) {
log.info("Validation failed");
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k400_badRequest,
- "Schema validation failed");
+ respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);
ErrorStatus=true;
return ErrorStatus;
} else {
log.error("Validation errored" + valresult);
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k400_badRequest,
- "Couldn't parse JSON object");
+ respondWithCustomMsginJson(ctx, ApiException.INVALID_JSON_INPUT);
ErrorStatus=true;
return ErrorStatus;
}
} else {
log.info("Validation failed");
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k400_badRequest, "Schema validation failed");
+ respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);
ErrorStatus=true;
return ErrorStatus;
}
@@ -225,8 +221,7 @@ public class EventReceipt extends NsaBaseEndpoint {
if (!ctx.request().getContentType().equalsIgnoreCase("application/json")) {
log.info(String.format("Rejecting request with content type %s Message:%s",
ctx.request().getContentType(), jsonObject));
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k400_badRequest,
- "Incorrect message content-type; only accepts application/json messages");
+ respondWithCustomMsginJson(ctx, ApiException.INVALID_CONTENT_TYPE);
ErrorStatus=true;
return ErrorStatus;
}
@@ -235,39 +230,17 @@ public class EventReceipt extends NsaBaseEndpoint {
} else {
log.info(String.format("Unauthorized request %s FROM %s %s %s %s", getUser(ctx), ctx.request().getRemoteAddress(), ctx.request().getContentType(), MESSAGE,
jsonObject));
- respondWithCustomMsginJson(ctx, HttpStatusCodes.k401_unauthorized, "Unauthorized user");
+ respondWithCustomMsginJson(ctx, ApiException.UNAUTHORIZED_USER);
ErrorStatus=true;
return ErrorStatus;
}
return ErrorStatus;
}
- public static void respondWithCustomMsginJson(DrumlinRequestContext ctx, int sc, String msg) {
- String[] str;
- String exceptionType = "GeneralException";
-
- str = CustomExceptionLoader.LookupMap(String.valueOf(sc), msg);
- log.info("Post CustomExceptionLoader.LookupMap" + str);
-
- if (str != null) {
-
- if (str[0].matches("SVC")) {
- exceptionType = "ServiceException";
- } else if (str[1].matches("POL")) {
- exceptionType = "PolicyException";
- }
-
- JSONObject jb = new JSONObject().put("requestError",
- new JSONObject().put(exceptionType, new JSONObject().put("MessagID", str[0]).put("text", str[1])));
-
- log.debug("Constructed json error : " + jb);
- ctx.response().sendErrorAndBody(sc, jb.toString(), MimeTypes.kAppJson);
- } else {
- JSONObject jb = new JSONObject().put("requestError",
- new JSONObject().put(exceptionType, new JSONObject().put("Status", sc).put("Error", msg)));
- ctx.response().sendErrorAndBody(sc, jb.toString(), MimeTypes.kAppJson);
- }
-
+ public static void respondWithCustomMsginJson(DrumlinRequestContext ctx, ApiException apiException) {
+ ctx.response()
+ .sendErrorAndBody(apiException.httpStatusCode,
+ apiException.toJSON().toString(), MimeTypes.kAppJson);
}
public static void safeClose(FileReader fr) {