aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/restapi
diff options
context:
space:
mode:
authorPawelSzalapski <pawel.szalapski@nokia.com>2018-06-21 12:12:30 +0200
committerPawelSzalapski <pawel.szalapski@nokia.com>2018-06-24 14:18:02 +0200
commit943a47187dbb1393d720b2fdf0019d48270edb4d (patch)
treefef76ed7b3034597853ab9735f5052cab2f0d6d5 /src/main/java/org/onap/dcae/restapi
parentb4d5236a7164b166d49cf8c0a5c15bb848e04cd6 (diff)
Remove dead code from VESCollector
Many things there are unused or have inproper modifiers, spelling etc. I run static analysis tool (Intellij code inspect) and clear those things up. It will be easier to maintain now. No actual behavior changes were done. Issue-ID: DCAEGEN2-526 Signed-off-by: PawelSzalapski <pawel.szalapski@nokia.com> Change-Id: I1a4ad0c896bd32165cba654344ffc5245648c615
Diffstat (limited to 'src/main/java/org/onap/dcae/restapi')
-rw-r--r--src/main/java/org/onap/dcae/restapi/ApiException.java6
-rw-r--r--src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java66
-rw-r--r--src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java102
3 files changed, 57 insertions, 117 deletions
diff --git a/src/main/java/org/onap/dcae/restapi/ApiException.java b/src/main/java/org/onap/dcae/restapi/ApiException.java
index 3feeacfc..0f922678 100644
--- a/src/main/java/org/onap/dcae/restapi/ApiException.java
+++ b/src/main/java/org/onap/dcae/restapi/ApiException.java
@@ -33,9 +33,9 @@ public enum ApiException {
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;
+ private final ExceptionType type;
+ private final String code;
+ private final String details;
public final int httpStatusCode;
ApiException(ExceptionType type, String code, String details, int httpStatusCode) {
diff --git a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java
index d664b137..0d9df155 100644
--- a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java
+++ b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java
@@ -34,8 +34,6 @@ import org.slf4j.LoggerFactory;
import com.att.nsa.apiServer.CommonServlet;
import com.att.nsa.configs.ConfigDbException;
-import com.att.nsa.drumlin.service.framework.DrumlinErrorHandler;
-import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext;
import com.att.nsa.drumlin.service.framework.routing.DrumlinRequestRouter;
import com.att.nsa.drumlin.service.framework.routing.playish.DrumlinPlayishRoutingFileSource;
import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
@@ -50,12 +48,15 @@ import com.att.nsa.security.db.simple.NsaSimpleApiKey;
public class RestfulCollectorServlet extends CommonServlet
{
- public static String authlist;
+ private static final long serialVersionUID = 1L;
+ private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );
+
+ private static String authCredentialsList;
public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting
{
super ( settings, "collector", false );
- authlist = settings.getString(CommonStartup.KSETTING_AUTHLIST,null);
+ authCredentialsList = settings.getString(CommonStartup.KSETTING_AUTHLIST, null);
}
@@ -69,8 +70,7 @@ public class RestfulCollectorServlet extends CommonServlet
{
super.servletSetup ();
- try
- {
+ try {
// the base class provides a bunch of things like API authentication and ECOMP compliant
// logging. The Restful Collector likely doesn't need API authentication, so for now,
// we init the base class services with an in-memory (and empty!) config DB.
@@ -82,14 +82,8 @@ public class RestfulCollectorServlet extends CommonServlet
final DrumlinRequestRouter drr = getRequestRouter ();
// you can tell the request router what to do when a particular kind of exception is thrown.
- drr.setHandlerForException( IllegalArgumentException.class, new DrumlinErrorHandler()
- {
- @Override
- public void handle ( DrumlinRequestContext ctx, Throwable cause )
- {
- sendJsonReply ( ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() );
- }
- });
+ drr.setHandlerForException(IllegalArgumentException.class,
+ (ctx, cause) -> sendJsonReply (ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() ));
// load the routes from the config file
final URL routes = findStream ( "routes.conf" );
@@ -99,52 +93,34 @@ public class RestfulCollectorServlet extends CommonServlet
if (CommonStartup.authflag > 0) {
NsaAuthenticator<NsaSimpleApiKey> NsaAuth;
- NsaAuth = AuthlistHandler(authlist);
+ NsaAuth = createAuthenticator(authCredentialsList);
this.getSecurityManager().addAuthenticator(NsaAuth);
}
log.info ( "Restful Collector Servlet is up." );
}
- catch ( SecurityException e )
- {
- throw new ServletException ( e );
- }
- catch ( IOException e )
- {
- throw new ServletException ( e );
- }
- catch ( ConfigDbException e )
- {
+ catch ( SecurityException | IOException | ConfigDbException e ) {
throw new ServletException ( e );
}
}
- public NsaAuthenticator<NsaSimpleApiKey> AuthlistHandler (String authlist)
- {
- NsaAuthenticator<NsaSimpleApiKey> NsaAuth = new SimpleAuthenticator ();
- if (authlist != null)
- {
- String authpair[] = authlist.split("\\|");
- for (String pair: authpair) {
+ public NsaAuthenticator<NsaSimpleApiKey> createAuthenticator(String authCredentials) {
+ NsaAuthenticator<NsaSimpleApiKey> authenticator = new SimpleAuthenticator();
+ if (authCredentials != null) {
+ String authpair[] = authCredentials.split("\\|");
+ for (String pair : authpair) {
String lineid[] = pair.split(",");
- String listauthid = lineid[0];
- String listauthpwd = new String(Base64.decodeBase64(lineid[1]));
- ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd);
+ String listauthid = lineid[0];
+ String listauthpwd = new String(Base64.decodeBase64(lineid[1]));
+ ((SimpleAuthenticator) authenticator).add(listauthid, listauthpwd);
}
+ } else {
+ ((SimpleAuthenticator) authenticator).add("admin", "collectorpasscode");
}
- else
- {
- //add a default test account
- ((SimpleAuthenticator) NsaAuth).add("admin","collectorpasscode");
- }
- return NsaAuth;
-
+ return authenticator;
}
-
- private static final long serialVersionUID = 1L;
- private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );
}
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 24bd96ea..d028a957 100644
--- a/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java
+++ b/src/main/java/org/onap/dcae/restapi/endpoints/EventReceipt.java
@@ -52,8 +52,6 @@ public class EventReceipt extends NsaBaseEndpoint {
private static final Logger log = LoggerFactory.getLogger(EventReceipt.class);
private static final String MESSAGE = " Message:";
- static String valresult;
- static JSONObject customerror;
public static void receiveVESEvent(DrumlinRequestContext ctx) {
// the request body carries events. assume for now it's an array
@@ -64,7 +62,6 @@ public class EventReceipt extends NsaBaseEndpoint {
JSONObject jsonObject;
- FileReader fr = null;
InputStream istr = null;
int arrayFlag = 0;
String vesVersion = null;
@@ -83,9 +80,6 @@ public class EventReceipt extends NsaBaseEndpoint {
if (m.find()) {
log.info("VES version:" + m.group());
vesVersion = m.group();
- m = null;
- p = null;
-
}
final UUID uuid = UUID.randomUUID();
@@ -118,10 +112,7 @@ public class EventReceipt extends NsaBaseEndpoint {
return;
}
- Boolean ErrorStatus = false;
- ErrorStatus = schemaCheck( retkey, arrayFlag, jsonObject, vesVersion, ctx, uuid);
- if (ErrorStatus)
- {
+ if (schemaCheck(retkey, arrayFlag, jsonObject, vesVersion, ctx, uuid)) {
return;
}
@@ -137,10 +128,6 @@ public class EventReceipt extends NsaBaseEndpoint {
respondWithCustomMsginJson(ctx, ApiException.NO_SERVER_RESOURCES);
return;
} finally {
- if (fr != null) {
- safeClose(fr);
- }
-
if (istr != null) {
safeClose(istr);
}
@@ -150,28 +137,28 @@ public class EventReceipt extends NsaBaseEndpoint {
}
- public static String getUser( DrumlinRequestContext ctx){
- String authorization = null;
- authorization = ctx.request().getFirstHeader("Authorization");
- if (authorization != null && authorization.startsWith("Basic")) {
- // Authorization: Basic base64credentials
+ private static String getUser(DrumlinRequestContext ctx){
+ String authorization = ctx.request().getFirstHeader("Authorization");
+ if (authorization != null && authorization.startsWith("Basic")) {
String base64Credentials = authorization.substring("Basic".length()).trim();
String credentials = new String(Base64.getDecoder().decode(base64Credentials),
Charset.forName("UTF-8"));
- // credentials = username:password
final String[] values = credentials.split(":",2);
- log.debug("User:" + values[0].toString() + " Pwd:" + values[1].toString());
- return values[0].toString();
+ log.debug("User:" + values[0] + " Pwd:" + values[1]);
+ return values[0];
}
return null;
}
- public static Boolean schemaCheck(NsaSimpleApiKey retkey, int arrayFlag,JSONObject jsonObject, String vesVersion, DrumlinRequestContext ctx, UUID uuid) throws JSONException, QueueFullException, IOException
- {
+
+ private static Boolean schemaCheck(NsaSimpleApiKey retkey, int arrayFlag,
+ JSONObject jsonObject, String vesVersion,
+ DrumlinRequestContext ctx, UUID uuid)
+ throws JSONException, QueueFullException, IOException {
+
JSONArray jsonArray;
JSONArray jsonArrayMod = new JSONArray();
JSONObject event;
- Boolean ErrorStatus=false;
FileReader fr;
if (retkey != null || CommonStartup.authflag == 0) {
if (CommonStartup.schemaValidatorflag > 0) {
@@ -180,25 +167,24 @@ public class EventReceipt extends NsaBaseEndpoint {
fr = new FileReader(schemaFileVersion(vesVersion));
String schema = new JsonParser().parse(fr).toString();
- valresult = CommonStartup.schemavalidate(jsonObject.toString(), schema);
- if (valresult.equals("true")) {
- log.info("Validation successful");
- } else if (valresult.equals("false")) {
- log.info("Validation failed");
- respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);
- ErrorStatus=true;
- return ErrorStatus;
- } else {
- log.error("Validation errored" + valresult);
- respondWithCustomMsginJson(ctx, ApiException.INVALID_JSON_INPUT);
- ErrorStatus=true;
- return ErrorStatus;
+ String valresult = CommonStartup.validateAgainstSchema(jsonObject.toString(), schema);
+ switch (valresult) {
+ case "true":
+ log.info("Validation successful");
+ break;
+ case "false":
+ log.info("Validation failed");
+ respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);
+ return true;
+ default:
+ log.error("Validation errored" + valresult);
+ respondWithCustomMsginJson(ctx, ApiException.INVALID_JSON_INPUT);
+ return true;
}
} else {
log.info("Validation failed");
respondWithCustomMsginJson(ctx, ApiException.SCHEMA_VALIDATION_FAILED);
- ErrorStatus=true;
- return ErrorStatus;
+ return true;
}
if (arrayFlag == 1) {
jsonArray = jsonObject.getJSONArray("eventList");
@@ -222,8 +208,7 @@ public class EventReceipt extends NsaBaseEndpoint {
log.info(String.format("Rejecting request with content type %s Message:%s",
ctx.request().getContentType(), jsonObject));
respondWithCustomMsginJson(ctx, ApiException.INVALID_CONTENT_TYPE);
- ErrorStatus=true;
- return ErrorStatus;
+ return true;
}
CommonStartup.handleEvents(jsonArrayMod);
@@ -231,30 +216,18 @@ public class EventReceipt extends NsaBaseEndpoint {
log.info(String.format("Unauthorized request %s FROM %s %s %s %s", getUser(ctx), ctx.request().getRemoteAddress(), ctx.request().getContentType(), MESSAGE,
jsonObject));
respondWithCustomMsginJson(ctx, ApiException.UNAUTHORIZED_USER);
- ErrorStatus=true;
- return ErrorStatus;
+ return true;
}
- return ErrorStatus;
+ return false;
}
- public static void respondWithCustomMsginJson(DrumlinRequestContext ctx, ApiException apiException) {
+ private static void respondWithCustomMsginJson(DrumlinRequestContext ctx, ApiException apiException) {
ctx.response()
.sendErrorAndBody(apiException.httpStatusCode,
apiException.toJSON().toString(), MimeTypes.kAppJson);
}
- public static void safeClose(FileReader fr) {
- if (fr != null) {
- try {
- fr.close();
- } catch (IOException e) {
- log.error("Error closing file reader stream : " + e);
- }
- }
-
- }
-
- public static void safeClose(InputStream is) {
+ private static void safeClose(InputStream is) {
if (is != null) {
try {
is.close();
@@ -266,17 +239,8 @@ public class EventReceipt extends NsaBaseEndpoint {
}
public static String schemaFileVersion(String version) {
- String filename = null;
-
- if (CommonStartup.schemaFileJson.has(version)) {
- filename = CommonStartup.schemaFileJson.getString(version);
- } else {
- filename = CommonStartup.schemaFileJson.getString("v5");
-
- }
- log.info(String.format("VESversion: %s Schema File:%s", version, filename));
- return filename;
-
+ return CommonStartup.schemaFileJson.has(version) ?
+ CommonStartup.schemaFileJson.getString(version) : CommonStartup.schemaFileJson.getString("v5");
}
}