diff options
Diffstat (limited to 'dcae_dmaapbc_webapp/src/main/java/org')
33 files changed, 4421 insertions, 0 deletions
diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DataBusHomeController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DataBusHomeController.java new file mode 100644 index 0000000..830c1fa --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DataBusHomeController.java @@ -0,0 +1,57 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +import javax.servlet.ServletException; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +/** + * This controller maps requests for the DMaaP-BC-App's landing page, which is + * an Angular single-page application. + */ +@Controller +@RequestMapping("/") +public class DataBusHomeController extends DbcappRestrictedBaseController { + + public static final String APP_NAME = "dmaap-bc-app"; + public static final DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + + /** + * At one time I published the landing page as simply "/dbc", but it can + * also be accessed with a default suffix; e.g., "/dbc.htm". + * + * @return View name key, which is resolved to a file using an Apache tiles + * "definitions.xml" file. + * @throws ServletException + */ + @RequestMapping(value = { "/dbc" }, method = RequestMethod.GET) + public ModelAndView dbcDefaultController() throws ServletException { + // a model is only useful for JSP; this app is angular. + return new ModelAndView("dbc_home_tdkey"); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DataRouterController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DataRouterController.java new file mode 100644 index 0000000..27f5dd6 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DataRouterController.java @@ -0,0 +1,315 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.util.Date; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.slf4j.MDC; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * Data Router controller: serves Ajax requests made by Angular scripts on pages + * that show feeds, publishers and subscribers. + */ +@Controller +@RequestMapping("/") +public class DataRouterController extends DbcappRestrictedBaseController { + + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DataRouterController.class); + + private static final String FEED_PATH = "/dr_feed"; + private static final String PUB_PATH = "/dr_pub"; + private static final String SUB_PATH = "/dr_sub"; + + public DataRouterController() { + } + + /** + * Answers a request for one page of data router feeds. + * + * @param request + * @return Result of + * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)} + * @throws ServletException + */ + @RequestMapping(value = { FEED_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDRFeedsByPage(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = getItemListForPageWrapper(request, DmaapDataItem.DR_FEED); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Answers a request for one page of data router publishers. + * + * @param request + * + * @return Result of + * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)} + * @throws ServletException + */ + @RequestMapping(value = { PUB_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDRPubsByPage(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = getItemListForPageWrapper(request, DmaapDataItem.DR_PUB); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Answers a request for one page of data router subscribers. + * + * @param request + * + * @return Result of + * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)} + * @throws ServletException + */ + @RequestMapping(value = { SUB_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDRSubsByPage(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = getItemListForPageWrapper(request, DmaapDataItem.DR_SUB); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Adds a feed with the specified information. Expects a JSON block in the + * request body - a Feed object. + * + * @param request + * @return a JSON object; on success it has a "status" and possibly a "data" + * item; on failure, also has an "error" item. + * @throws ServletException + */ + @RequestMapping(value = { FEED_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String addDRFeed(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = addItem(request, DmaapDataItem.DR_FEED, null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Adds a publisher with the specified information. Expects a JSON block in + * the request body - a DR_Pub object. + * + * @return a JSON object; on success it has a "status" and possibly a "data" + * item; on failure, also has an "error" item. + * + * @param request + * @throws ServletException + */ + @RequestMapping(value = { PUB_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String addDRPub(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = addItem(request, DmaapDataItem.DR_PUB, HttpServletResponse.SC_CREATED); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Adds a subscriber with the specified information. Expects a JSON block in + * the request body - a DR_Sub object. + * + * @return a JSON object; on success it has a "status" and possibly a "data" + * item; on failure, also has an "error" item. + * + * @param request + * @throws ServletException + */ + @RequestMapping(value = { SUB_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String addDRSub(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = addItem(request, DmaapDataItem.DR_SUB, HttpServletResponse.SC_CREATED); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Updates a feed with the specified information. Expects a JSON block in + * the request body - a Feed object. + * + * @param id + * Path parameter with object ID + * @param request + * @return a JSON object; on success it has a "status" and possibly a "data" + * item; on failure, also has an "error" item. + * @throws ServletException + */ + @RequestMapping(value = { FEED_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String updateFeed(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = updateItem(request, DmaapDataItem.DR_FEED, Long.toString(id), null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Updates a publisher with the specified information. Expects a JSON block + * in the request body - a DR_Pub object. + * + * The pubId may have a dot in it. Spring, in its infinite wisdom, truncates + * extensions on dotted path parameters; e.g., "foo.json" becomes "foo". + * Avoid truncation here with the extra ":.+" incantation at the end. + * + * @param id + * Path parameter with object ID + * @param request + * @return a JSON object; on success it has a "status" and possibly a "data" + * item; on failure, also has an "error" item. + * @throws ServletException + */ + @RequestMapping(value = { PUB_PATH + "/{id:.+}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String updateDRPub(@PathVariable("id") String id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = updateItem(request, DmaapDataItem.DR_PUB, id, null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Updates a subscriber with the specified information. Expects a JSON block + * in the request body - a DR_Sub object. + * + * @param id + * Path parameter with object ID + * @param request + * @return a JSON object; on success it has a "status" and possibly a "data" + * item; on failure, also has an "error" item. + * @throws ServletException + */ + @RequestMapping(value = { SUB_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String updateDRSub(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = updateItem(request, DmaapDataItem.DR_SUB, Long.toString(id), null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Deletes a feed with the ID specified as a path parameter. On successful + * delete the endpoint returns 204 (confusingly). + * + * Writes a JSON object as an HTTP response; on success it only has "status" + * item; on failure, also has an "error" item. + * + * @param id + * Path parameter with object ID + * @param request + * @return JSON object + * @throws ServletException + */ + @RequestMapping(value = { FEED_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteDRFeed(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = deleteItem(request, DmaapDataItem.DR_FEED, Long.toString(id), 204); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Deletes the publisher with the ID specified as a path parameter. + * + * The pubId may have a dot in it. Spring, in its infinite wisdom, truncates + * extensions on dotted path parameters; e.g., "foo.json" becomes "foo". + * Avoid truncation here with the extra ":.+" incantation at the end. + * + * @param id + * Path parameter with object ID + * @param request + * @return a JSON object; on success it only has "status" item; on failure, + * also has an "error" item. + * @throws ServletException + */ + @RequestMapping(value = { PUB_PATH + "/{id:.+}" }, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteDRPub(@PathVariable("id") String id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = deleteItem(request, DmaapDataItem.DR_PUB, id, null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Deletes the subscriber with the ID specified as a path parameter. + * + * @param id + * Path parameter with object ID + * @param request + * @return Writes a JSON object; on success it only has "status" item; on + * failure, also has an "error" item. + * @throws ServletException + */ + @RequestMapping(value = { SUB_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteDRSub(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = deleteItem(request, DmaapDataItem.DR_SUB, Long.toString(id), 204); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DbcappRestrictedBaseController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DbcappRestrictedBaseController.java new file mode 100644 index 0000000..a527127 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DbcappRestrictedBaseController.java @@ -0,0 +1,974 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.openecomp.dcae.dmaap.bcapi.client.DmaapBcRestClient; +import org.openecomp.dcae.dmaap.bcapi.client.HttpStatusAndResponse; +import org.openecomp.dcae.dmaapbc.model.DR_Pub; +import org.openecomp.dcae.dmaapbc.model.DR_Sub; +import org.openecomp.dcae.dmaapbc.model.DcaeLocation; +import org.openecomp.dcae.dmaapbc.model.Dmaap; +import org.openecomp.dcae.dmaapbc.model.DmaapObject; +import org.openecomp.dcae.dmaapbc.model.ErrorResponse; +import org.openecomp.dcae.dmaapbc.model.Feed; +import org.openecomp.dcae.dmaapbc.model.MR_Client; +import org.openecomp.dcae.dmaapbc.model.Topic; +import org.openecomp.dmaapbc.dbcapp.domain.DmaapAccess; +import org.openecomp.dmaapbc.dbcapp.rest.DbcUsvcRestClient; +import org.openecomp.dmaapbc.dbcapp.service.DmaapAccessService; +import org.openecomp.dmaapbc.dbcapp.util.DbcappProperties; +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.openecomp.portalsdk.core.domain.User; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.util.CipherUtil; +import org.openecomp.portalsdk.core.web.support.UserUtils; +import org.springframework.beans.factory.annotation.Autowired; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * This base class provides utility methods to child controllers. All of the + * requests are forwarded on to a remote REST API, so there's a large degree of + * commonality among the implementations. Combining them kept the lines-of-code + * count down, at the expense of some complexity. + */ +public class DbcappRestrictedBaseController extends RestrictedBaseController { + + /** + * Query parameter for desired page number + */ + protected static final String PAGE_NUM_QUERY_PARAM = "pageNum"; + + /** + * Query parameter for desired items per page + */ + protected static final String VIEW_PER_PAGE_QUERY_PARAM = "viewPerPage"; + + /** + * Tag for status code in JSON responses - ALWAYS PRESENT. + */ + protected static final String STATUS_RESPONSE_KEY = "status"; + + /** + * Tag for data in JSON responses. + */ + protected static final String DATA_RESPONSE_KEY = "data"; + + /** + * Tag for error message in JSON responses; absent on success. + */ + protected static final String ERROR_RESPONSE_KEY = "error"; + + /** + * Tag for response integer, pages required to display complete result list + */ + protected static final String TOTAL_PAGES_RESPONSE_KEY = "totalPages"; + + /** + * Tag for DMaaP name obtained from REST client. + */ + protected static final String PROFILE_NAME_RESPONSE_KEY = "profileName"; + + /** + * Tag for DMaaP name obtained from REST client. + */ + protected static final String DMAAP_NAME_RESPONSE_KEY = "dmaapName"; + + /** + * Tag for DCAE location name list obtained from REST client. + */ + protected static final String DCAE_LOCATIONS_RESPONSE_KEY = "dcaeLocations"; + + /** + * Logger that conforms with ECOMP guidelines + */ + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DbcappRestrictedBaseController.class); + + /** + * For general use in these methods and subclasses + */ + protected final ObjectMapper mapper = new ObjectMapper(); + + /** + * DAO accesses the profiles via a local database. REST accesses the + * profiles via a remote REST service. + */ + public enum AccessMethod { + DAO, REST + }; + + /** + * Enum for selecting an item type. + */ + public enum DmaapDataItem { + DR_FEED, DR_PUB, DR_SUB, MR_TOPIC, MR_CLIENT; + } + + /** + * Application properties - NOT available to constructor. + */ + @Autowired + private DbcappProperties appProperties; + + /** + * Database access - which might not be used. + */ + @Autowired + private DmaapAccessService dmaapAccessDaoServiceAuto; + + /** + * Read from application properties. + */ + private String mechIdName, mechIdPass; + + /** + * This is set by {@link #getDmaapAccessService()} to the DAO or REST + * implementation as configured in properties. + */ + private DmaapAccessService dmaapAccessService; + + /** + * Hello Spring, here's your no-arg constructor. + */ + public DbcappRestrictedBaseController() { + // Do not serialize null values + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + /** + * Access method for subclasses. + * + * @return DbcappProperties object that was autowired by Spring. + */ + protected DbcappProperties getAppProperties() { + return appProperties; + } + + /** + * Lazy initialization. As a side effect, caches mech ID and password. + * + * @return Either DAO or REST client that implements the access service + * interface. + */ + protected DmaapAccessService getDmaapAccessService() { + if (dmaapAccessService != null) + return dmaapAccessService; + + // Get the application's mechid + mechIdName = appProperties.getProperty(DbcappProperties.DMAAP_MECHID_NAME); + // This is encrypted + String cipher = appProperties.getProperty(DbcappProperties.DMAAP_MECHID_PASSWORD); + if (mechIdName == null || cipher == null) + throw new RuntimeException("Failed to get MECH_ID name and/or password from properties"); + try { + mechIdPass = CipherUtil.decrypt(cipher); + } catch (Exception ex) { + throw new RuntimeException("Failed to decrypt password from config file", ex); + } + + String accessMethod = appProperties.getProperty(DbcappProperties.PROFILE_ACCESS_METHOD); + if (accessMethod == null) + throw new RuntimeException("Failed to get property " + DbcappProperties.PROFILE_ACCESS_METHOD); + AccessMethod profileAccessMethod = AccessMethod.valueOf(accessMethod.toUpperCase()); + if (AccessMethod.DAO == profileAccessMethod) { + // Spring auto-wired this field + dmaapAccessService = dmaapAccessDaoServiceAuto; + } else { + String url = appProperties.getProperty(DbcappProperties.PROFILE_USVC_URL); + String user = appProperties.getProperty(DbcappProperties.PROFILE_USVC_USER); + String pass = appProperties.getProperty(DbcappProperties.PROFILE_USVC_PASS); + if (url == null || user == null || pass == null) + throw new RuntimeException("getDmaapAccessService: missing property: one of url, user, pass"); + String clearText = null; + try { + clearText = CipherUtil.decrypt(pass); + } catch (Exception ex) { + throw new RuntimeException("getDmaapAccessService: failed to decrypt password from config"); + } + dmaapAccessService = new DbcUsvcRestClient(url, user, clearText); + } + return dmaapAccessService; + } + + /** + * Creates a REST client with appropriate credentials, the user/pass from + * the access profile if present, otherwise with the default mech ID and + * password. + * + * @param dmaapAccess + * @return REST client + */ + protected DmaapBcRestClient getDmaapBcRestClient(DmaapAccess dmaapAccess) { + DmaapBcRestClient restClient = null; + if (dmaapAccess.getMechId() == null || dmaapAccess.getMechId().length() == 0) + restClient = new DmaapBcRestClient(dmaapAccess.getDmaapUrl(), mechIdName, mechIdPass); + else + restClient = new DmaapBcRestClient(dmaapAccess.getDmaapUrl(), dmaapAccess.getMechId(), + dmaapAccess.getPassword()); + return restClient; + } + + /** + * Pulls out of the specified list the appropriate items for the page of + * results specified by the page number and view-per-page parameters. + * + * @param pageNum + * Page number requested by user + * @param viewPerPage + * Number of items per page + * @param itemList + * List of items available + * @return List of items to display + */ + @SuppressWarnings("rawtypes") + private static List shrinkListToPage(final int pageNum, final int viewPerPage, final List itemList) { + // user-friendly page numbers index from 1 + int firstIndexOnThisPage = viewPerPage * (pageNum - 1); + int firstIndexOnNextPage = viewPerPage * pageNum; + int fromIndex = firstIndexOnThisPage < itemList.size() ? firstIndexOnThisPage : itemList.size(); + int toIndex = firstIndexOnNextPage < itemList.size() ? firstIndexOnNextPage : itemList.size(); + // answers empty list if from==to + return itemList.subList(fromIndex, toIndex); + } + + /** + * Gets the body of a HTTP request assuming UTF-8 encoding. + * + * @param request + * @return String version of request body + * @throws IOException If the read fails + */ + protected static String getBody(HttpServletRequest request) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + BufferedReader bufferedReader = null; + try { + InputStream inputStream = request.getInputStream(); + if (inputStream != null) { + bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); + char[] charBuffer = new char[512]; + int bytesRead = -1; + while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { + stringBuilder.append(charBuffer, 0, bytesRead); + } + } else { + stringBuilder.append(""); + } + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException ex) { + throw ex; + } + } + + } + return stringBuilder.toString(); + } + + /** + * Builds a JSON success response from the specified inputs. + * + * @param statusCode + * e.g., 200 for OK + * @param dataPojo + * Plain old Java object to serialize as JSON; ignored if null. + * @throws JsonProcessingException + * If the POJO cannot be serialized + * @return JSON block with items "status" : 200 and "data" : (data..) + */ + protected String buildJsonSuccess(int statusCode, Object dataPojo) throws JsonProcessingException { + Map<String, Object> model = new HashMap<String, Object>(); + model.put(STATUS_RESPONSE_KEY, statusCode); + if (dataPojo != null) + model.put(DATA_RESPONSE_KEY, dataPojo); + String json = mapper.writeValueAsString(model); + return json; + } + + /** + * Builds a JSON error response from the specified inputs. + * + * @param statusCode + * e.g., 500 for internal server error + * @param errMsg + * Information about the operation that failed + * @param exception + * Converted to string; ignored if null. + * @return JSON block with tags "status" and "error". + */ + protected String buildJsonError(int statusCode, String errMsg, Exception exception) { + Map<String, Object> model = new HashMap<String, Object>(); + model.put(STATUS_RESPONSE_KEY, new Integer(500)); + if (exception == null) { + model.put(ERROR_RESPONSE_KEY, errMsg); + } else { + final int enough = 512; + String exString = exception.toString(); + String exceptionMsg = exString.length() > enough ? exString.substring(0, enough) : exString; + model.put(ERROR_RESPONSE_KEY, errMsg + ": " + exceptionMsg); + } + String json = null; + try { + json = mapper.writeValueAsString(model); + } catch (JsonProcessingException ex) { + // serializing the trivial map should never fail + String err = "buildJsonError: failed to serialize"; + logger.error(EELFLoggerDelegate.errorLogger, err, ex); + throw new RuntimeException(err, ex); + } + return json; + } + + /** + * Gets a list of DMaaP access profiles for this user from the database. The + * profiles have passwords in the clear - this method decrypts the database + * entries. + * + * Initializes the list for new users and/or configuration changes. Checks + * the database list against the configured list of URLs, and creates new + * rows for any configured URLs not present for the user. Most environments + * are expected to have exactly one valid URL, and the webapp uses a fixed + * MechID to authenticate itself to the DMaaP bus controller, so this + * approach means new users can start without any setup of URLs. + * + * @param userId + * @return List of DmaapAccess objects + * @throws Exception + */ + protected List<DmaapAccess> getOrInitDmaapAccessList(String userId) throws Exception { + String[] configUrls = getAppProperties().getCsvListProperty(DbcappProperties.DMAAP_REST_URL_LIST); + if (configUrls == null || configUrls.length == 0) + throw new Exception("getOrInitDmaapAccessList: Failed to get DMAAP REST URL list"); + // Update this list to track which URLs are in the database. + List<String> configUrlList = new ArrayList<String>(configUrls.length); + for (String c : configUrls) { + // Validate URL to detect config botches + URL url = new URL(c); + configUrlList.add(url.toExternalForm()); + } + + List<DmaapAccess> dbAccessList = getDmaapAccessService().getDmaapAccessList(userId); + + // Check the database entries against the configuration. Also + // build a list of non-DAO objects with clear-text passwords. + List<DmaapAccess> clearList = new ArrayList<DmaapAccess>(dbAccessList.size()); + for (DmaapAccess dmaapAccess : dbAccessList) { + // drop this URL from the list. + // If it's not known to config, complain because that's a bogus row. + if (!configUrlList.remove(dmaapAccess.getDmaapUrl())) + logger.warn(EELFLoggerDelegate.errorLogger, "getOrInitDmaapAccessList: detected extra URL {}", + dmaapAccess.getDmaapUrl()); + // Return cleartext in JSON + DmaapAccess clone = new DmaapAccess(dmaapAccess); + clone.setPassword(clone.decryptPassword()); + clearList.add(clone); + } + + // Create new rows for any configured URLs not found for this user. + for (int i = 0; i < configUrlList.size(); ++i) { + String missUrl = configUrlList.get(i); + logger.debug(EELFLoggerDelegate.debugLogger, "getOrInitDmaapAccessList: adding missing URL {}", missUrl); + DmaapAccess newDmaapAccess = new DmaapAccess(); + // Create a semi-reasonable name for the table + newDmaapAccess.setName("dmaap-" + Integer.toString(i + 1)); + newDmaapAccess.setUserId(userId); + newDmaapAccess.setDmaapUrl(missUrl); + // Write to db. + getDmaapAccessService().saveDmaapAccess(newDmaapAccess); + // Add to response, which assumes the write was successful. + clearList.add(newDmaapAccess); + } + + return clearList; + } + + /** + * Gets the user's selected DMaaP access profile. + * + * @param userId + * User's UID + * @return DmaapAccess object that is currently selected, or the first one + * found if none are selected; null if no access profiles are + * configured. + * @throws ServletException + * If the user's UID cannot be extracted from request. + */ + protected DmaapAccess getSelectedDmaapAccess(String userId) throws Exception { + List<DmaapAccess> profiles = getOrInitDmaapAccessList(userId); + if (profiles.size() == 0) { + logger.debug("getSelectedDmaapAccess: no rows found, returning null"); + return null; + } + + // Return the first one by default if nothing is selected. + DmaapAccess selected = profiles.get(0); + for (DmaapAccess da : profiles) + if (da.getSelected()) + selected = da; + + return selected; + } + + /** + * Supports sorting a list of feeds by the first column displayed: ID + */ + private static Comparator<DmaapObject> feedComparator = new Comparator<DmaapObject>() { + @Override + public int compare(DmaapObject o1, DmaapObject o2) { + Feed f1 = (Feed) o1; + Feed f2 = (Feed) o2; + // sort these numbers lexicographically, same as the front end + // table. + return f1.getFeedId().compareTo(f2.getFeedId()); + } + }; + + /** + * Supports sorting a list of publishers by the first column displayed: pub + * ID + */ + private static Comparator<DmaapObject> pubComparator = new Comparator<DmaapObject>() { + @Override + public int compare(DmaapObject o1, DmaapObject o2) { + DR_Pub p1 = (DR_Pub) o1; + DR_Pub p2 = (DR_Pub) o2; + return p1.getPubId().compareTo(p2.getPubId()); + } + }; + + /** + * Supports sorting a list of subscribers by the first column displayed: sub + * ID + */ + private static Comparator<DmaapObject> subComparator = new Comparator<DmaapObject>() { + @Override + public int compare(DmaapObject o1, DmaapObject o2) { + DR_Sub s1 = (DR_Sub) o1; + DR_Sub s2 = (DR_Sub) o2; + // sort these numbers lexicographically, same as the front end + // table. + return s1.getSubId().compareTo(s2.getSubId()); + } + }; + + /** + * Supports sorting a list of topics by the first column displayed: FQTN + */ + private static Comparator<DmaapObject> topicComparator = new Comparator<DmaapObject>() { + @Override + public int compare(DmaapObject o1, DmaapObject o2) { + Topic t1 = (Topic) o1; + Topic t2 = (Topic) o2; + return t1.getFqtn().compareTo(t2.getFqtn()); + } + }; + + /** + * Supports sorting a list of clients by the first column displayed: client + * ID. + */ + private static Comparator<DmaapObject> clientComparator = new Comparator<DmaapObject>() { + @Override + public int compare(DmaapObject o1, DmaapObject o2) { + MR_Client c1 = (MR_Client) o1; + MR_Client c2 = (MR_Client) o2; + // sort these numbers lexicographically, same as the front end + // table. + return c1.getMrClientId().compareTo(c2.getMrClientId()); + } + }; + + /** + * Gets one page of DMaaP objects and supporting information via the Bus + * Controller REST client. On success, returns a JSON object as String with + * the following tags: + * <UL> + * <LI>status: Integer; HTTP status code 200. + * <LI>dmaapName: String, name returned by the remote DMaaP instance. + * <LI>dcaeLocations: Array of string, locations returned by the remote + * DMaaP instance. + * <LI>data: Array of the desired items; e.g., data router feeds. + * <LI>totalPages: Integer, the number of pages required to display the + * complete list of items using the submitted page size + * </UL> + * + * This duplicates all of {@link #buildJsonSuccess(int, Object)}. + * + * @param dmaapAccess + * Access details for the DMaaP REST API + * @param option + * Specifies which item list type to get: data router feeds, etc. + * @param pageNum + * Page number of results + * @param viewPerPage + * Number of items per page + * @return JSON block as String, see above. + * @throws Exception + */ + private String getItemListForPage(DmaapAccess dmaapAccess, DmaapDataItem option, int pageNum, int viewPerPage) + throws Exception { + DmaapBcRestClient restClient = getDmaapBcRestClient(dmaapAccess); + // Get the instance so the page can display its name + DmaapObject dmaap = restClient.getDmaap(); + if (dmaap instanceof ErrorResponse) { + // Bad password is caught here. + ErrorResponse err = (ErrorResponse) dmaap; + throw new Exception(err.getMessage()); + } + // Get locations for editing + List<DmaapObject> dcaeLocations = restClient.getDcaeLocations(); + if (dcaeLocations.size() == 1 && dcaeLocations.get(0) instanceof ErrorResponse) { + // Should never happen - bad password is caught right above - but be + // careful. + ErrorResponse err = (ErrorResponse) dcaeLocations.get(0); + throw new Exception(err.getMessage()); + } + // Pass them back as String array + String[] dcaeLocs = new String[dcaeLocations.size()]; + for (int i = 0; i < dcaeLocs.length; ++i) { + DcaeLocation dcaeLoc = (DcaeLocation) dcaeLocations.get(i); + dcaeLocs[i] = dcaeLoc.getDcaeLocationName(); + } + // Get the requested item list + List<DmaapObject> itemList = null; + switch (option) { + case DR_FEED: + itemList = restClient.getFeeds(); + Collections.sort(itemList, feedComparator); + break; + case DR_PUB: + itemList = restClient.getDRPubs(); + Collections.sort(itemList, pubComparator); + break; + case DR_SUB: + itemList = restClient.getDRSubs(); + Collections.sort(itemList, subComparator); + break; + case MR_TOPIC: + itemList = restClient.getTopics(); + Collections.sort(itemList, topicComparator); + break; + case MR_CLIENT: + itemList = restClient.getMRClients(); + Collections.sort(itemList, clientComparator); + break; + default: + throw new Exception("getItemListForPage: pgmr error, unimplemented case: " + option.name()); + } + + logger.debug("getItemListForPage: list size is {}", itemList.size()); + int pageCount = (int) Math.ceil((double) itemList.size() / viewPerPage); + @SuppressWarnings("unchecked") + List<DmaapObject> subList = shrinkListToPage(pageNum, viewPerPage, itemList); + itemList = subList; + // Build response here + Map<String, Object> model = new HashMap<String, Object>(); + model.put(STATUS_RESPONSE_KEY, new Integer(200)); + model.put(PROFILE_NAME_RESPONSE_KEY, dmaapAccess.getName()); + model.put(DMAAP_NAME_RESPONSE_KEY, ((Dmaap) dmaap).getDmaapName()); + model.put(DCAE_LOCATIONS_RESPONSE_KEY, dcaeLocs); + model.put(DATA_RESPONSE_KEY, itemList); + model.put(TOTAL_PAGES_RESPONSE_KEY, pageCount); + + // build the response + String outboundJson = null; + try { + outboundJson = mapper.writeValueAsString(model); + } catch (Exception ex) { + // should never happen + logger.error("getItemListForPage: failed to serialize model: ", ex); + throw new Exception("sendItemListForPage", ex); + } + + return outboundJson; + } + + /** + * Gets a page of the specified DMaaP items. This method traps errors and + * constructs an appropriate JSON block if an error happens. + * + * See {@link #getItemListForPage(DmaapAccess, DmaapDataItem, int, int)}. + * + * @param request + * Inbound request + * @param option + * DMaaP item type to get + * @return JSON with list of serialized objects, or an error. + */ + protected String getItemListForPageWrapper(HttpServletRequest request, DmaapDataItem option) + throws ServletException { + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("getItemListForPageWrapper: Failed to get UID"); + DmaapAccess selected = getSelectedDmaapAccess(appUser.getLoginId()); + if (selected == null) // leap into exception handler + throw new Exception("No DMaaP access profiles are configured."); + int pageNum = Integer.parseInt(request.getParameter(PAGE_NUM_QUERY_PARAM)); + int viewPerPage = Integer.parseInt(request.getParameter(VIEW_PER_PAGE_QUERY_PARAM)); + outboundJson = getItemListForPage(selected, option, pageNum, viewPerPage); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to get DMaaP item type " + option.name(), ex); + } + return outboundJson; + } + + /** + * Adds an item of the specified type with the specified content. Constructs + * an object by deserializing the JSON block, but ignores any ID field that + * is supplied. + * + * On success, returns a JSON block as String with any data returned by the + * REST client. Throws an exception on any failure. + * + * @param dmaapAccess + * Access details for the DMaaP REST API + * @param userId + * The UID of the user making the request + * @param itemType + * DMaaP item type to add + * @param itemContent + * JSON block to deserialize as an object + * @param scAddlStatus + * HTTP status code 200 is always accepted. If this parameter is + * not null, the value is also considered a valid HTTP status + * code on success; e.g., 204. + * @return JSON object with result of the operation + * @throws Exception + * on any problem + */ + private String addDmaapItem(DmaapAccess dmaapAccess, String userId, DmaapDataItem itemType, String itemContent, + Integer scAddlStatus) throws Exception { + DmaapBcRestClient restClient = getDmaapBcRestClient(dmaapAccess); + HttpStatusAndResponse<Object> hsr = null; + switch (itemType) { + case DR_FEED: + Feed feed = mapper.readValue(itemContent, Feed.class); + logger.debug("addDmaapItem: received feed: {} ", feed); + // Null out any ID to get an auto-generated ID + feed.setFeedId(null); + // Assign the owner to be the webapp user + feed.setOwner(userId); + hsr = restClient.postFeed(feed); + break; + case DR_PUB: + DR_Pub pub = mapper.readValue(itemContent, DR_Pub.class); + logger.debug("addDmaapItem: received pub: {} ", pub); + // Null out any ID to get an auto-generated ID + pub.setPubId(null); + hsr = restClient.postDRPub(pub); + break; + case DR_SUB: + DR_Sub sub = mapper.readValue(itemContent, DR_Sub.class); + logger.debug("addDmaapItem: received sub: {} ", sub); + // Null out any ID to get an auto-generated ID + sub.setSubId(null); + // Assign the owner to be the webapp user + sub.setOwner(userId); + hsr = restClient.postDRSub(sub); + break; + case MR_TOPIC: + Topic topic = mapper.readValue(itemContent, Topic.class); + logger.debug("addDmaapItem: received topic: {} ", topic); + // No ID on topic + topic.setOwner(userId); + hsr = restClient.postTopic(topic); + break; + case MR_CLIENT: + MR_Client client = mapper.readValue(itemContent, MR_Client.class); + logger.debug("addDmaapItem: received client: {} ", client); + client.setMrClientId(null); + hsr = restClient.postMRClient(client); + break; + default: + throw new Exception("addDmaapItem: pgmr error, unimplemented case: " + itemType.name()); + } + + // Build result here + String outboundJson = null; + if (hsr.getStatusCode() == HttpServletResponse.SC_OK + || (scAddlStatus != null && hsr.getStatusCode() == scAddlStatus)) { + outboundJson = buildJsonSuccess(hsr.getStatusCode(), hsr.getResponseString()); + } else { + throw new Exception("Unexpected HTTP response code " + Integer.toString(hsr.getStatusCode()) + + " with content " + hsr.getResponseString()); + } + return outboundJson; + } + + /** + * Adds the specified DMaaP item that is read from the request body. This + * method traps errors and constructs an appropriate JSON block if an error + * happens. + * + * @param request + * Used to obtain user info from the active session + * @param itemType + * DMaaP item type to add + * @param scAddlStatus + * HTTP status code 200 is always accepted. If this parameter is + * not null, the value is also considered a valid HTTP status + * code on success; e.g., 204. + * @return JSON block with success or failure object + */ + protected String addItem(HttpServletRequest request, DmaapDataItem itemType, Integer scAddlStatus) + throws ServletException { + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("addDmaapItem: Failed to get UID"); + + DmaapAccess access = getSelectedDmaapAccess(appUser.getLoginId()); + if (access == null) // leap into exception handler + throw new Exception("No DMaaP access profiles are configured."); + String jsonContent = getBody(request); + outboundJson = addDmaapItem(access, appUser.getLoginId(), itemType, jsonContent, scAddlStatus); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to add DMaaP item " + itemType.name(), ex); + } + + return outboundJson; + } + + /** + * Updates an item of the specified type with the specified content. + * Constructs an object by deserializing the JSON block. + * + * On success, returns a JSON block as String with any data returned by the + * REST client. Throws an exception on any failure. + * + * @param dmaapAccess + * Access details for the DMaaP REST API + * @param userId + * The UID of the user making the request + * @param itemType + * DMaaP item type to update + * @param itemId + * Item identification + * @param itemContent + * JSON block to deserialize as an object + * @param scAddlStatus + * HTTP status code 200 is always accepted. If this parameter is + * not null, the value is also considered a valid HTTP status + * code on success; e.g., 204. + * @return JSON object with result of the operation + * @throws Exception + * on any problem + */ + private String updateDmaapItem(DmaapAccess dmaapAccess, String userId, DmaapDataItem itemType, String itemId, + String itemContent, Integer scAddlStatus) throws Exception { + DmaapBcRestClient restClient = getDmaapBcRestClient(dmaapAccess); + HttpStatusAndResponse<Object> hsr = null; + switch (itemType) { + case DR_FEED: + Feed feed = mapper.readValue(itemContent, Feed.class); + logger.debug("updateDmaapItem: received feed: {} ", feed); + // Ensure the owner is the webapp user + feed.setOwner(userId); + hsr = restClient.putFeed(feed); + break; + case DR_PUB: + DR_Pub pub = mapper.readValue(itemContent, DR_Pub.class); + logger.debug("updateDmaapItem: received pub: {} ", pub); + hsr = restClient.putDRPub(pub); + break; + case DR_SUB: + DR_Sub sub = mapper.readValue(itemContent, DR_Sub.class); + logger.debug("updateDmaapItem: received sub: {} ", sub); + // Ensure the owner is the webapp user + sub.setOwner(userId); + hsr = restClient.putDRSub(sub); + break; + case MR_TOPIC: + Topic topic = mapper.readValue(itemContent, Topic.class); + logger.debug("updateDmaapItem: received topic: {} ", topic); + // Ensure the owner is the webapp user + topic.setOwner(userId); + if (true && userId != null) + throw new UnsupportedOperationException("put topic"); + // TODO: someday the backend may implement PUT + break; + case MR_CLIENT: + MR_Client client = mapper.readValue(itemContent, MR_Client.class); + logger.debug("updateDmaapItem: received client: {} ", client); + hsr = restClient.putMRClient(client); + break; + default: + throw new Exception("updateDmaapItem: pgmr error, unimplemented case: " + itemType.name()); + } + + // Build result here + String outboundJson = null; + if (hsr.getStatusCode() == HttpServletResponse.SC_OK + || (scAddlStatus != null && hsr.getStatusCode() == scAddlStatus)) { + outboundJson = buildJsonSuccess(hsr.getStatusCode(), hsr.getResponseString()); + } else { + throw new Exception("Unexpected HTTP response code " + Integer.toString(hsr.getStatusCode()) + + " with content " + hsr.getResponseString()); + } + return outboundJson; + } + + /** + * Updates the specified DMaaP item that is read from the request body. This + * method traps errors and constructs an appropriate JSON block if an error + * happens. + * + * @param request + * Used to obtain user info from the active session + * @param itemType + * DMaaP item type to update + * @param itemId + * Item identification to update + * @param scUpdatelStatus + * HTTP status code 200 is always accepted. If this parameter is + * not null, the value is also considered a valid HTTP status + * code on success; e.g., 204. + * @return JSON object with success or error information. + */ + protected String updateItem(HttpServletRequest request, DmaapDataItem itemType, String itemId, + Integer scUpdatelStatus) throws ServletException { + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("updateItem: Failed to get UID"); + DmaapAccess access = getSelectedDmaapAccess(appUser.getLoginId()); + if (access == null) // leap into exception handler + throw new Exception("No DMaaP access profiles are configured."); + String jsonContent = getBody(request); + outboundJson = updateDmaapItem(access, appUser.getLoginId(), itemType, itemId, jsonContent, + scUpdatelStatus); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to update DMaaP item " + itemType.name(), ex); + } + + return outboundJson; + } + + /** + * Deletes an item of the specified type with the specified ID. + * + * @param dmaapAccess + * Access details for the DMaaP REST API + * @param itemType + * DMaaP item type to delete + * @param itemId + * Item identification + * @param scAddlStatus + * HTTP status code 200 is always accepted. If this parameter is + * not null, the value is also considered a valid HTTP status + * code on success; e.g., 204. + * @return On success, returns a JSON block as String with any data returned + * by the REST client. + * @throws Exception + * Throws an exception on any failure. + */ + private String deleteDmaapItem(DmaapAccess dmaapAccess, DmaapDataItem itemType, String itemId, Integer scAddlStatus) + throws Exception { + DmaapBcRestClient restClient = getDmaapBcRestClient(dmaapAccess); + HttpStatusAndResponse<Object> hsr = null; + switch (itemType) { + case DR_FEED: + hsr = restClient.deleteFeed(itemId); + break; + case DR_PUB: + hsr = restClient.deleteDRPub(itemId); + break; + case DR_SUB: + hsr = restClient.deleteDRSub(itemId); + break; + case MR_TOPIC: + hsr = restClient.deleteTopic(itemId); + break; + case MR_CLIENT: + hsr = restClient.deleteMRClient(itemId); + break; + default: + throw new Exception("deleteDmaapItem: pgmr error, unimplemented case: " + itemType.name()); + } + + // Build result here + String outboundJson = null; + if (hsr.getStatusCode() == HttpServletResponse.SC_OK + || (scAddlStatus != null && hsr.getStatusCode() == scAddlStatus)) { + outboundJson = buildJsonSuccess(hsr.getStatusCode(), hsr.getResponseString()); + } else { + throw new Exception("Unexpected HTTP response code " + Integer.toString(hsr.getStatusCode()) + + " with content " + hsr.getResponseString()); + } + return outboundJson; + } + + /** + * Deletes the specified DMaaP item. This method traps errors and constructs + * an appropriate JSON block if an error happens. + * + * @param request + * Used to obtain user info from the active session + * @param itemType + * DMaaP item type to delete + * @param itemId + * item ID to delete + * @param scAddlStatus + * HTTP status code 200 is always accepted. If this parameter is + * not null, the value is also considered a valid HTTP status + * code on success; e.g., 204. + * @return JSON object with success or error information. + */ + protected String deleteItem(HttpServletRequest request, DmaapDataItem itemType, String itemId, Integer scAddlStatus) + throws ServletException { + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("deleteItem: Failed to get UID"); + DmaapAccess selected = getSelectedDmaapAccess(appUser.getLoginId()); + if (selected == null) // leap into exception handler + throw new Exception("No DMaaP access profiles are configured."); + outboundJson = deleteDmaapItem(selected, itemType, itemId, scAddlStatus); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to delete DMaaP item " + itemType.name() + " ID " + itemId, ex); + } + return outboundJson; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DmaapAccessController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DmaapAccessController.java new file mode 100644 index 0000000..9b36c8b --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/DmaapAccessController.java @@ -0,0 +1,374 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.dcae.dmaap.bcapi.client.DmaapBcRestClient; +import org.openecomp.dcae.dmaapbc.model.Dmaap; +import org.openecomp.dcae.dmaapbc.model.DmaapObject; +import org.openecomp.dcae.dmaapbc.model.ErrorResponse; +import org.openecomp.dmaapbc.dbcapp.domain.DmaapAccess; +import org.openecomp.portalsdk.core.domain.User; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.openecomp.portalsdk.core.web.support.UserUtils; +import org.slf4j.MDC; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * DMaaP Access controller: serves Ajax requests made by Angular on pages where + * the user adds, edits and deletes DMaaP access profiles. This controller must + * defend the database against rogue requests including SQL injection attacks. + */ +@Controller +@RequestMapping("/") +public class DmaapAccessController extends DbcappRestrictedBaseController { + + /** + * Logger that conforms with ECOMP guidelines + */ + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DmaapAccessController.class); + + private static final String DMAAP_ACCESS_PATH = "/dmaap_access"; + private static final String SELECT_DMAAP_ACCESS_PATH = "/select_dmaap_access"; + + /** + * For general use in these methods + */ + private final ObjectMapper mapper; + + /** + * Never forget that Spring autowires fields AFTER the constructor is + * called. + */ + public DmaapAccessController() { + mapper = new ObjectMapper(); + } + + /** + * Gets a list of DMaaP access profiles for this user from the database and + * returns them in a JSON array nested within a response object. + * + * See {@link #getOrInitDmaapAccessList(String)}. + * + * @param request + * @return JSON with access profiles, or an error JSON if the request fails. + * @throws ServletException + */ + @RequestMapping(value = { DMAAP_ACCESS_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDmaapAccessList(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("getDmaapAccessList: Failed to get UID from request"); + List<DmaapAccess> dbAccessList = getOrInitDmaapAccessList(appUser.getLoginId()); + // Wrap the list in the status indicator. + Map<String, Object> model = new HashMap<String, Object>(); + model.put(STATUS_RESPONSE_KEY, new Integer(200)); + model.put(DATA_RESPONSE_KEY, dbAccessList); + outboundJson = mapper.writeValueAsString(model); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to get DMaaP access profile list", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + + /** + * Adds a DMaaP access profile for the requesting user ID; ignores any + * values for row ID and user ID in the body. + * + * @param request + * @return Trivial JSON object indicating success or failure. + * @throws ServletException + */ + @RequestMapping(value = { DMAAP_ACCESS_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String addDmaapAccess(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("addDmaapAccess: Failed to get UID"); + + DmaapAccess dmaapAccess = mapper.readValue(request.getReader(), DmaapAccess.class); + logger.debug("addDmaapAccess: received object: {} ", dmaapAccess); + + // Null out ID to get an auto-generated ID + dmaapAccess.setId(null); + // Overwrite any submitted user id + dmaapAccess.setUserId(appUser.getLoginId()); + // Encrypt password + if (dmaapAccess.getPassword() != null) + dmaapAccess.encryptPassword(dmaapAccess.getPassword()); + + // Create a new row + getDmaapAccessService().saveDmaapAccess(dmaapAccess); + + // Answer success + outboundJson = buildJsonSuccess(200, null); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to add DMaaP access profile", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + + /** + * Updates a DMaaP access profile if the row user ID matches the requesting + * user ID. + * + * @param id + * Path parameter with ID of the DMaaP access profile + * @param request + * @return Trivial JSON object indicating success or failure. + * @throws ServletException + * in case of unrecoverable failure + */ + @RequestMapping(value = { DMAAP_ACCESS_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String updateDmaapAccess(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("updateDmaapAccess: Failed to get UID"); + + DmaapAccess domainObj = getDmaapAccessService().getDmaapAccess(id); + if (!appUser.getLoginId().equals(domainObj.getUserId())) + throw new Exception("updateDmaapAccess: mismatch of appUser and row user ID"); + + DmaapAccess dmaapAccess = mapper.readValue(request.getReader(), DmaapAccess.class); + logger.debug("updateDmaapAccess: received object: {} ", dmaapAccess); + + // Use the path-parameter id; don't trust the one in the object + dmaapAccess.setId(id); + // Encrypt password if present + if (dmaapAccess.getPassword() != null) + dmaapAccess.encryptPassword(dmaapAccess.getPassword()); + + // Update the existing row + getDmaapAccessService().saveDmaapAccess(dmaapAccess); + + // Answer "OK" + outboundJson = buildJsonSuccess(200, null); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to update DMaaP access profile", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + + /** + * Deletes a DMaaP access profile if the row user ID matches the requesting + * user ID. + * + * @param id + * Path parameter with ID of the DMaaP access profile + * @param request + * @return Trivial JSON object indicating success or failure (altho this is + * slightly contrary to conventions for a DELETE method) + * @throws ServletException + * in case of unrecoverable failure + */ + @RequestMapping(value = { + DMAAP_ACCESS_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteDmaapAccess(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("deleteDmaapAccess: Failed to get UID"); + // Validate that this user owns the row with the specified ID + DmaapAccess domainObj = getDmaapAccessService().getDmaapAccess(id); + if (!appUser.getLoginId().equals(domainObj.getUserId())) + throw new Exception("deleteDmaapAccess: mismatch of appUser and row user ID"); + + getDmaapAccessService().deleteDmaapAccess(id); + + // Answer "OK" + outboundJson = buildJsonSuccess(200, null); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to delete DMaaP access profile", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + + /** + * Gets the selected DMaaP access row for the requesting user. + * + * See {@link #getSelectedDmaapAccess(String)} + * + * @param request + * @return JSON object with one DmaapAccessProfile, or an error + * @throws ServletException + * in case of unrecoverable failure + */ + @RequestMapping(value = { SELECT_DMAAP_ACCESS_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getSelectedDmaapAccessProfile(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("getSelectedDmaapAccessProfile: Failed to get UID"); + DmaapAccess selected = super.getSelectedDmaapAccess(appUser.getLoginId()); + // clone and decrypt + DmaapAccess clear = new DmaapAccess(selected); + try { + clear.setPassword(clear.decryptPassword()); + } catch (Exception ex) { + throw new ServletException("getSelectedDmaapAccessProfile: Failed to decrypt password", ex); + } + outboundJson = buildJsonSuccess(200, clear); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to get selected DMaaP access profile", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + + /** + * Marks the DMaaP access row as selected (first choice) for the requesting + * user if the row user ID matches the requesting user ID. As a side effect, + * removes selected marking from all other user rows. Returns status, + * additionally an error message on failure. + * + * Choice of PUT is fairly arbitrary - there is no body, but GET is for + * actions that do not change data. + * + * @param id + * Path parameter with ID of the DMaaP access profile + * @param request + * @return Trivial JSON object indicating success or failure. + * @throws ServletException + * in case of unrecoverable failure + */ + @RequestMapping(value = { + SELECT_DMAAP_ACCESS_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String selectDmaapAccess(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new Exception("selectDmaapAccess: Failed to get UID"); + // A little inefficient in that it requires 3 database accesses; + // probably could be done in 1 with some sophisticated SQL. + List<DmaapAccess> dmaapAccessList = getDmaapAccessService().getDmaapAccessList(appUser.getLoginId()); + for (DmaapAccess dmaap : dmaapAccessList) { + // Only write out the changed rows. + boolean changed = false; + if (id == dmaap.getId()) { + changed = !dmaap.getSelected(); + dmaap.setSelected(true); + } else { + changed = dmaap.getSelected(); + dmaap.setSelected(false); + } + if (changed) + getDmaapAccessService().saveDmaapAccess(dmaap); + } + + // Answer OK + outboundJson = buildJsonSuccess(200, null); + } catch (Exception ex) { + outboundJson = buildJsonError(500, "Failed to select a DMaaP access profile", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + + /** + * Tests the URL in the DMaaP access profile object. + * + * @param request + * @return JSON with a Dmaap object (which has name etc.) on success, error + * on failure. + * @throws ServletException + * in case of unrecoverable failure + */ + @RequestMapping(value = { "test_dmaap_access" }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String testDmaapAccess(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String outboundJson = null; + try { + DmaapAccess dmaapAccess = mapper.readValue(request.getReader(), DmaapAccess.class); + logger.debug("testDmaapAccess: received object: {} ", dmaapAccess); + if (dmaapAccess.getDmaapUrl() == null || dmaapAccess.getDmaapUrl().trim().length() == 0) + throw new Exception("Null or empty URL"); + + DmaapBcRestClient restClient = getDmaapBcRestClient(dmaapAccess); + // Get the instance so the page can display its name + DmaapObject dmaap = restClient.getDmaap(); + if (dmaap instanceof Dmaap) { + outboundJson = buildJsonSuccess(200, dmaap); + } else { + // Bad credentials lands here. + ErrorResponse err = (ErrorResponse) dmaap; + outboundJson = buildJsonError(500, "Test failed: " + err.getMessage(), null); + } + } catch (Exception ex) { + // This is entirely likely; e.e., unknown host exception on typo. + outboundJson = buildJsonError(500, "Invalid DMaaP URL", ex); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return outboundJson; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/HealthCheckController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/HealthCheckController.java new file mode 100644 index 0000000..90a67a1 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/HealthCheckController.java @@ -0,0 +1,157 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.util.Date; + +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.dmaapbc.dbcapp.service.DmaapAccessService; +import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * This controller responds to probes for application health, returning a JSON + * body to indicate current status. + */ +@RestController +@Configuration +@EnableAspectJAutoProxy +@RequestMapping("/") +public class HealthCheckController extends UnRestrictedBaseController { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class); + + private static final String HEALTH_CHECK_PATH = "/healthCheck"; + + @Autowired + private DmaapAccessService dmaapAccessService; + + /** + * Model for JSON response with health-check results. + */ + public class HealthStatus { + // Either 200 or 500 + public int statusCode; + // Additional detail in case of error, empty in case of success. + public String message; + + public HealthStatus(int code, String msg) { + this.statusCode = code; + this.message = msg; + } + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int code) { + this.statusCode = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String msg) { + this.message = msg; + } + } + + /** + * Checks application health by making a trivial query to the database. + * + * @param request + * @return 200 if database access succeeds, 500 if it fails. + */ + @RequestMapping(value = { HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus healthCheck(HttpServletRequest request) { + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + HealthStatus healthStatus = null; + try { + logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); + dmaapAccessService.getDmaapAccessCount(); + healthStatus = new HealthStatus(200, "health check succeeded"); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); + healthStatus = new HealthStatus(500, "health check failed: " + ex.toString()); + } + return healthStatus; + } + + /** + * This implementation does not support suspend/resume. + * + * @param request + * @return Trivial success + */ + @RequestMapping(value = { + HEALTH_CHECK_PATH + "/suspend" }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus healthCheckSuspend(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + HealthStatus response = new HealthStatus(200, "suspend not implemented"); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * This implementation does not support suspend/resume. + * + * @param request + * @return Trivial success + */ + @RequestMapping(value = { + HEALTH_CHECK_PATH + "/resume" }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus healthCheckResume(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + HealthStatus response = new HealthStatus(200, "resume not implemented"); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Answers ping request without checking the application health. + * + * @param request + * @return Trivial success + */ + @RequestMapping(value = { HEALTH_CHECK_PATH + "/ping" }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus ping(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + HealthStatus response = new HealthStatus(200, "ping received"); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/ManifestController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/ManifestController.java new file mode 100644 index 0000000..a2986f9 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/ManifestController.java @@ -0,0 +1,113 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +/** + * This controller responds to a request for the web application manifest, + * returning a JSON with the information that was created at build time. + * + * Manifest entries have names with hyphens, which means Javascript code can't + * simply use the shorthand object.key; instead use object['key']. + */ +@RestController +@Configuration +@EnableAspectJAutoProxy +@RequestMapping("/") +public class ManifestController extends RestrictedBaseController { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ManifestController.class); + + /** + * Required to obtain the webapp manifest. + */ + @Autowired + ServletContext context; + + /** + * Gets the content of the webapp manifest file META-INF/MANIFEST.MF. + * + * @return Attributes object with key-value pairs from the manifest + * @throws IOException + */ + private Attributes getWebappManifest() throws IOException { + // Path to resource on classpath + final String MANIFEST_RESOURCE_PATH = "/META-INF/MANIFEST.MF"; + // Manifest is formatted as Java-style properties + InputStream inputStream = context.getResourceAsStream(MANIFEST_RESOURCE_PATH); + if (inputStream == null) + throw new IOException("getWebappManifest: failed to get resource at path " + MANIFEST_RESOURCE_PATH); + Manifest manifest = new Manifest(inputStream); + inputStream .close(); + return manifest.getMainAttributes(); + } + + /** + * Gets the webapp manifest contents as a JSON object. + * + * @param request + * @return A map of key-value pairs. On success: + * + * <pre> + * { "manifest" : { "key1": "value1", "key2": "value2" } } + * </pre> + * + * On failure: + * + * <pre> + * { "error": "message" } + * </pre> + */ + @RequestMapping(value = { "/manifest" }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Map<String, Object> getManifest(HttpServletRequest request) { + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + Map<String, Object> response = new HashMap<String, Object>(); + try { + Attributes attributes = getWebappManifest(); + response.put("manifest", attributes); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getManifest: failed to read manifest", ex); + response.put("error", "failed to get manifest: " + ex.toString()); + } + return response; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/MessageRouterController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/MessageRouterController.java new file mode 100644 index 0000000..1513e17 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/controller/MessageRouterController.java @@ -0,0 +1,226 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.controller; + +import java.util.Date; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.slf4j.MDC; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * Message Router controller: serves Ajax requests made by Angular scripts on + * pages that show topics and clients. + */ +@Controller +@RequestMapping("/") +public class MessageRouterController extends DbcappRestrictedBaseController { + + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MessageRouterController.class); + + private static final String TOPIC_PATH = "/mr_topic"; + private static final String CLIENT_PATH = "/mr_client"; + + public MessageRouterController() { + } + + /** + * Answers a request for one page of message router topics. See + * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)} + * + * @param request + * @return Item list for the specified page + * @throws ServletException + */ + @RequestMapping(value = { TOPIC_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getMRTopicsByPage(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = getItemListForPageWrapper(request, DmaapDataItem.MR_TOPIC); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Answers a request for one page of message router clients. See + * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)} + * + * @param request + * @return Item list for the specified page + * @throws ServletException + */ + @RequestMapping(value = { CLIENT_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getMRClientsByPage(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = getItemListForPageWrapper(request, DmaapDataItem.MR_CLIENT); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Adds a topic with the specified information. Expects a JSON block in the + * request body - a Topic object. + * + * @param request + * @return Item list for the specified page + * @throws ServletException + */ + @RequestMapping(value = { TOPIC_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String addTopic(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = addItem(request, DmaapDataItem.MR_TOPIC, HttpServletResponse.SC_CREATED); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Adds a client with the specified information. Expects a JSON block in the + * request body - a MR_Client object. + * + * @param request + * @return Success / failure JSON + * @throws ServletException + */ + @RequestMapping(value = { CLIENT_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String addMRClient(HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = addItem(request, DmaapDataItem.MR_CLIENT, HttpServletResponse.SC_CREATED); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Updates a topic with the specified information. Expects a JSON block in + * the request body - a Topic object. + * + * Writes a JSON object as an HTTP response; on success it has a "status" + * and possibly a "data" item; on failure, also has an "error" item. + * + * @param id + * @param request + * @return Success / failure JSON + * @throws ServletException + */ + @RequestMapping(value = { TOPIC_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String updateTopic(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = updateItem(request, DmaapDataItem.MR_TOPIC, Long.toString(id), null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Updates a client with the specified information. Expects a JSON block in + * the request body - a MR_Client object. + * + * Writes a JSON object as an HTTP response; on success it has a "status" + * and possibly a "data" item; on failure, also has an "error" item. + * + * @param id + * + * @param request + * @return Success / failure JSON + * @throws ServletException + */ + @RequestMapping(value = { CLIENT_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json") + @ResponseBody + public String updateMRClient(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = updateItem(request, DmaapDataItem.MR_CLIENT, Long.toString(id), null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Deletes a topic with the FQTN ID specified as a path parameter. + * + * FQTN is a string of dot-separated names. Spring, in its infinite wisdom, + * truncates extensions on dotted path parameters; e.g., "foo.json" becomes + * "foo". Avoid truncation here with the extra ":.+" incantation at the end. + * + * Writes a JSON object as an HTTP response; on success it only has "status" + * item; on failure, also has an "error" item. + * + * @param id + * Path parameter with object ID + * @param request + * @return Success / failure JSON + * @throws ServletException + */ + @RequestMapping(value = { "/mr_topic/{id:.+}" }, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteTopic(@PathVariable("id") String id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = deleteItem(request, DmaapDataItem.MR_TOPIC, id, 204); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + + /** + * Deletes a client with the mrClientId specified as a path parameter. + * + * Writes a JSON object as an HTTP response; on success it only has "status" + * item; on failure, also has an "error" item. + * + * @param id + * Path parameter with object ID + * @param request + * @return Success / failure JSON + * @throws ServletException + */ + @RequestMapping(value = { "/mr_client/{id}" }, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteMRClient(@PathVariable("id") long id, HttpServletRequest request) throws ServletException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME); + String response = deleteItem(request, DmaapDataItem.MR_CLIENT, Long.toString(id), null); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return response; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/domain/DmaapAccess.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/domain/DmaapAccess.java new file mode 100644 index 0000000..bb11dfd --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/domain/DmaapAccess.java @@ -0,0 +1,167 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.domain; + +import org.openecomp.portalsdk.core.domain.support.DomainVo; +import org.openecomp.portalsdk.core.util.CipherUtil; + +/** + * Hold an access profile for a DMaaP REST endpoint. Represents one row in the + * DBCA_DMAAP table. + */ +public class DmaapAccess extends DomainVo { + + private static final long serialVersionUID = 6443219375733216340L; + + // parent class defines these fields: + // ID, created, modified, created_id, modified_id + + /** UID for user who owns this row */ + private String userId; + /** Nickname for this row */ + private String name; + /** REST API endpoint */ + private String dmaapUrl; + /** Credentials */ + private String mechId; + /** Credentials */ + private String password; + /** User's preferred access profile */ + private boolean selected; + + /** + * Standard POJO no-arg constructor + */ + public DmaapAccess() { + } + + /** + * Copy constructor + * @param copy + */ + public DmaapAccess(final DmaapAccess copy) { + // Unfortunately DomainVo doesn't provide a copy constructor; + // only the ID field is needed. + this.id = copy.id; + // Our fields + this.userId = copy.userId; + this.name = copy.name; + this.dmaapUrl = copy.dmaapUrl; + this.mechId = copy.mechId; + this.password = copy.password; + this.selected = copy.selected; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDmaapUrl() { + return dmaapUrl; + } + + public void setDmaapUrl(String dmaapUrl) { + this.dmaapUrl = dmaapUrl; + } + + public String getMechId() { + return mechId; + } + + public void setMechId(String mechId) { + this.mechId = mechId; + } + + /** + * Gets the encrypted password. Applications should use + * {@link #decryptPassword()}! + * + * @return The encrypted password + */ + public String getPassword() { + return password; + } + + /** + * Sets the encrypted password. Applications should use + * {@link #encryptPassword(String)}! + * + * @param password + * The encrypted password + */ + public void setPassword(String password) { + this.password = password; + } + + public boolean getSelected() { + return selected; + } + + public void setSelected(boolean selected) { + this.selected = selected; + } + + /** + * A getter that decrypts the value read from the database and returns the + * clear text. Has no side effects. + * + * @return Clear-text password. + * @throws Exception + */ + public String decryptPassword() throws Exception { + if (password == null) + return null; + return CipherUtil.decrypt(password); + } + + /** + * A setter that encrypts the clear-text in preparation for storing in the + * database. + * + * @param clearText + * The clear-text password to be encrypted + * @throws Exception + */ + public void encryptPassword(String clearText) throws Exception { + if (clearText == null) { + password = null; + return; + } + password = CipherUtil.encrypt(clearText); + } + + @Override + public String toString() { + return "DmaapAccess[id=" + id + ", url=" + dmaapUrl + ", ...]"; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/DbcUsvcRestClient.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/DbcUsvcRestClient.java new file mode 100644 index 0000000..43542b2 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/DbcUsvcRestClient.java @@ -0,0 +1,184 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.rest; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.openecomp.dmaapbc.dbcapp.domain.DmaapAccess; +import org.openecomp.dmaapbc.dbcapp.service.DmaapAccessService; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +/** + * Provides methods for accessing the DBC microservice via REST using basic HTTP + * authentication. + * + */ +public class DbcUsvcRestClient implements DmaapAccessService { + + public static final String endpointPath = "/dmaap_access"; + private final String baseUrl; + private final RestTemplate restTemplate; + + /** + * Builds a restTemplate that uses basic HTTP authentication for use by all + * methods in this class. + * + * @param webapiUrl + * @param user + * @param pass + */ + public DbcUsvcRestClient(String webapiUrl, String user, String pass) { + if (webapiUrl == null || user == null || pass == null) + throw new IllegalArgumentException("Nulls not permitted"); + + baseUrl = webapiUrl; + URL url = null; + try { + url = new URL(baseUrl); + } catch (MalformedURLException ex) { + throw new RuntimeException("Failed to parse URL", ex); + } + final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort()); + + // Build a client with a credentials provider + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass)); + HttpClientBuilder clientBuilder = HttpClientBuilder.create(); + CloseableHttpClient httpClient = clientBuilder.setDefaultCredentialsProvider(credsProvider).build(); + + // Create request factory with our superpower client + HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth( + httpHost); + requestFactory.setHttpClient(httpClient); + + // Put the factory in the template + this.restTemplate = new RestTemplate(); + restTemplate.setRequestFactory(requestFactory); + } + + /** + * Gets the count of DMaaP access profiles. + * + * @return Number of access profiles in the database. + */ + public int getDmaapAccessCount() { + String url = baseUrl + "/count_dmaap_access"; + ResponseEntity<DbcUsvcRestResponse> daResponse = restTemplate.exchange(url, HttpMethod.GET, null, + DbcUsvcRestResponse.class); + DbcUsvcRestResponse response = daResponse.getBody(); + return response.getStatus(); + } + + /** + * Gets the DMaaP access profiles for the specified userId. + * + * @param userId + * @return List of DmaapAccess items + */ + @Override + public List<DmaapAccess> getDmaapAccessList(final String userId) { + String url = baseUrl + endpointPath + "?userId=" + userId; + // ResponseEntity<Object[]> responseEntity = + // restTemplate.getForEntity(url, Object[].class); + // MediaType contentType = responseEntity.getHeaders().getContentType(); + // HttpStatus statusCode = responseEntity.getStatusCode(); + // Object[] objects = responseEntity.getBody(); + ResponseEntity<List<DmaapAccess>> daResponse = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference<List<DmaapAccess>>() { + }); + List<DmaapAccess> daList = daResponse.getBody(); + return daList; + } + + /** + * Gets the specified DMaaP access profile. + */ + @Override + public DmaapAccess getDmaapAccess(Long dmaapId) { + String url = baseUrl + endpointPath + "?dmaapId=" + dmaapId; + ResponseEntity<DmaapAccess> daResponse = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference<DmaapAccess>() { + }); + DmaapAccess da = daResponse.getBody(); + return da; + } + + /** + * POSTs or PUTs the DMaaP access profile as needed, based on whether the + * object's ID field is set. If not set it creates a new row; if set, it + * updates a row in the remote service table. + * + * @param dmaapAccess + */ + @Override + public void saveDmaapAccess(final DmaapAccess dmaapAccess) { + if (dmaapAccess.getId() == null) { + String url = baseUrl + endpointPath; + restTemplate.postForObject(url, dmaapAccess, String.class); + } else { + String url = baseUrl + endpointPath + "/" + Long.toString(dmaapAccess.getId()); + restTemplate.put(url, dmaapAccess); + } + } + + /** + * Deletes the new DMaaP access profile row in the remote service table. + * + * @param id + */ + @Override + public void deleteDmaapAccess(final Long id) { + String url = baseUrl + endpointPath + "/" + Long.toString(id); + restTemplate.delete(url); + } + + /** + * Simple test + * + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + if (args.length != 1) + throw new IllegalArgumentException("Single argument expected: userid"); + DbcUsvcRestClient client = new DbcUsvcRestClient("http://localhost:8081/dbus", "dbus_user", "dbus_pass"); + final String userId = args[0]; + System.out.println("Requesting profiles for user " + userId); + List<DmaapAccess> access = client.getDmaapAccessList(userId); + if (access == null) + System.err.println("Received null"); + else + for (DmaapAccess da : access) + System.out.println(da); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/DbcUsvcRestResponse.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/DbcUsvcRestResponse.java new file mode 100644 index 0000000..50e37d8 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/DbcUsvcRestResponse.java @@ -0,0 +1,61 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.rest; + +/** + * Models the responses sent by the micro service in JSON format. + */ +public class DbcUsvcRestResponse { + + private int status; + private String data, error, exception; + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + public String getException() { + return exception; + } + + public void setException(String exception) { + this.exception = exception; + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/HttpComponentsClientHttpRequestFactoryBasicAuth.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/HttpComponentsClientHttpRequestFactoryBasicAuth.java new file mode 100644 index 0000000..dd70d48 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/rest/HttpComponentsClientHttpRequestFactoryBasicAuth.java @@ -0,0 +1,65 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.rest; + +import java.net.URI; + +import org.apache.http.HttpHost; +import org.apache.http.client.AuthCache; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +/** + * Utility class to enable Basic HTTP Authentication with Spring REST templates. + * + * From: + * http://www.baeldung.com/2012/04/16/how-to-use-resttemplate-with-basic-authentication-in-spring-3-1/ + */ +public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory { + + private HttpHost host; + + public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) { + super(); + this.host = host; + } + + protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { + return createHttpContext(); + } + + private HttpContext createHttpContext() { + // Create AuthCache instance + AuthCache authCache = new BasicAuthCache(); + // Generate BASIC scheme object and add it to the local auth cache + BasicScheme basicAuth = new BasicScheme(); + authCache.put(host, basicAuth); + + // Add AuthCache to the execution context + BasicHttpContext localcontext = new BasicHttpContext(); + localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache); + return localcontext; + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/service/DmaapAccessService.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/service/DmaapAccessService.java new file mode 100644 index 0000000..e0bf0b2 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/service/DmaapAccessService.java @@ -0,0 +1,72 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.service; + +import java.util.List; + +import org.openecomp.dmaapbc.dbcapp.domain.DmaapAccess; + +/** + * Defines methods to manipulate the database table with DmaapAccess domain + * objects. No method throws a checked exception, in keeping with the Spring + * philosophy of throwing unchecked exceptions. + */ +public interface DmaapAccessService { + + /** + * Gets the number of Dmaap Access entries. + * + * @return Number of rows in the table. + */ + int getDmaapAccessCount(); + + /** + * Gets all DMaaP access rows in the table for the specified user. + * + * @param userId + * UID of the user + * @return List of DMaaP instance objects, which may be empty. + */ + List<DmaapAccess> getDmaapAccessList(String userId); + + /** + * Gets the DMaaP access object with the specified row ID. + * + * @param dmaapId + * @return DMaap instance; null if none exists. + */ + DmaapAccess getDmaapAccess(Long dmaapId); + + /** + * Creates a new managed object (a new row in the table). + * + * @param dmaap + * DMaaP instance to create. + */ + void saveDmaapAccess(DmaapAccess dmaap); + + /** + * Deletes the DMaaP access row with the specified ID. + * + * @param dmaapId + */ + void deleteDmaapAccess(Long dmaapId); + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/service/DmaapAccessServiceImpl.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/service/DmaapAccessServiceImpl.java new file mode 100644 index 0000000..a7e5185 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/service/DmaapAccessServiceImpl.java @@ -0,0 +1,86 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.service; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; +import org.openecomp.dmaapbc.dbcapp.domain.DmaapAccess; +import org.openecomp.portalsdk.core.service.DataAccessService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Hibernate-assisted methods to manipulate the DBCA_DMAAP table. + */ +@Service("dmaapAccessService") +@Transactional +public class DmaapAccessServiceImpl implements DmaapAccessService { + + @Autowired + private DataAccessService dataAccessService; + + public DataAccessService getDataAccessService() { + return dataAccessService; + } + + public void setDataAccessService(DataAccessService dataAccessService) { + this.dataAccessService = dataAccessService; + } + + @SuppressWarnings("unchecked") + @Override + public int getDmaapAccessCount() { + List<DmaapAccess> accesses = (List<DmaapAccess>) getDataAccessService().getList(DmaapAccess.class, null); + return accesses.size(); + } + + @SuppressWarnings("unchecked") + @Override + public List<DmaapAccess> getDmaapAccessList(String userId) { + List<Criterion> restrictionsList = new ArrayList<Criterion>(); + Criterion criterion1 = Restrictions.eq("userId", userId); + restrictionsList.add(criterion1); + List<DmaapAccess> accesses = (List<DmaapAccess>) getDataAccessService().getList(DmaapAccess.class, null, + restrictionsList, null); + return accesses; + } + + @Override + public DmaapAccess getDmaapAccess(Long dmaapId) { + return (DmaapAccess) getDataAccessService().getDomainObject(DmaapAccess.class, dmaapId, null); + } + + @Override + public void saveDmaapAccess(DmaapAccess dmaap) { + getDataAccessService().saveDomainObject(dmaap, null); + } + + @Override + public void deleteDmaapAccess(Long dmaapId) { + DmaapAccess dmaapAccess = getDmaapAccess(dmaapId); + if (dmaapAccess != null) + getDataAccessService().deleteDomainObject(dmaapAccess, null); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/util/DbcappProperties.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/util/DbcappProperties.java new file mode 100644 index 0000000..d2acf3e --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/dmaapbc/dbcapp/util/DbcappProperties.java @@ -0,0 +1,88 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dmaapbc.dbcapp.util; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; + +/** + * Publishes a list of constants and methods to access the properties that are + * read by Spring from the specified configuration file(s). + * + * Should be used like this (and never in a constructor): + * + * <pre> + * @Autowired + * DbcappProperties properties; + * </pre> + */ +@Configuration +@PropertySource(value = { "${container.classpath:}/WEB-INF/dbcapp/dbcapp.properties" }) +public class DbcappProperties { + + public static final String DMAAP_REST_URL_LIST = "dmaap.rest.url.list"; + public static final String DMAAP_MECHID_NAME = "dmaap.mechid.name"; + public static final String DMAAP_MECHID_PASSWORD = "dmaap.mechid.password"; + public static final String PROFILE_ACCESS_METHOD = "profile.access.method"; + public static final String PROFILE_USVC_URL = "profile.microservice.url"; + public static final String PROFILE_USVC_USER = "profile.microservice.user.name"; + public static final String PROFILE_USVC_PASS = "profile.microservice.user.password"; + + private Environment environment; + + public DbcappProperties() { + } + + protected Environment getEnvironment() { + return environment; + } + + @Autowired + public void setEnvironment(final Environment environment) { + this.environment = environment; + } + + public boolean containsProperty(String key) { + return environment.containsProperty(key); + } + + public String getProperty(String key) { + return environment.getRequiredProperty(key); + } + + /** + * Gets the values for a comma-separated list property value as a String + * array. + * + * @param key + * @return Array of values with leading and trailing whitespace removed; + * null if key is not found. + */ + public String[] getCsvListProperty(final String key) { + String listVal = getProperty(key); + if (listVal == null) + return null; + String[] vals = listVal.split("\\s*,\\s*"); + return vals; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/ExternalAppConfig.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/ExternalAppConfig.java new file mode 100644 index 0000000..fc0a54a --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/ExternalAppConfig.java @@ -0,0 +1,203 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.conf; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.PostConstruct; + +import org.openecomp.fusionapp.uebhandler.InitUebHandler; +import org.openecomp.fusionapp.uebhandler.MainUebHandler; +import org.openecomp.fusionapp.uebhandler.WidgetNotificationHandler; +import org.openecomp.portalsdk.core.conf.AppConfig; +import org.openecomp.portalsdk.core.conf.Configurable; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager; +import org.openecomp.portalsdk.core.service.DataAccessService; +import org.openecomp.portalsdk.core.util.CacheManager; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.PropertySource; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; + +/** + * Configures Spring features in the DMaaP Bus Control web application. + * Subclasses the ECOMP Portal SDK core AppConfig class to reuse interceptors, + * view resolvers and other features defined there. + */ +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = { "org.openecomp" } ) +@PropertySource(value = { "${container.classpath:}/WEB-INF/conf/app/test.properties" }, ignoreResourceNotFound = true) +@Profile("src") +@EnableAsync +@EnableScheduling +public class ExternalAppConfig extends AppConfig implements Configurable { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppConfig.class); + + // private RegistryAdapter schedulerRegistryAdapter; + + @Configuration + @Import(SystemProperties.class) + static class InnerConfiguration { + } + + /** + * @see org.openecomp.portalsdk.core.conf.AppConfig#viewResolver() + */ + public ViewResolver viewResolver() { + return super.viewResolver(); + } + + /** + * @see org.openecomp.portalsdk.core.conf.AppConfig#addResourceHandlers(ResourceHandlerRegistry) + * + * @param registry + */ + public void addResourceHandlers(ResourceHandlerRegistry registry) { + super.addResourceHandlers(registry); + } + + /** + * @see org.openecomp.portalsdk.core.conf.AppConfig#dataAccessService() + */ + public DataAccessService dataAccessService() { + return super.dataAccessService(); + } + + /** + * Creates a new list with entries that are external app + * definitions.xml paths. + * + * @return List of String + */ + public List<String> addTileDefinitions() { + List<String> definitions = new ArrayList<String>(); + definitions.add("/WEB-INF/defs/definitions.xml"); + definitions.add("/WEB-INF/dbcapp/dbcapp-definitions.xml"); + if (logger.isDebugEnabled()) + logger.debug(EELFLoggerDelegate.debugLogger, "addTileDefinitions: list is " + definitions); + return definitions; + } + + /** + * Adds request interceptors to the specified registry by calling + * {@link AppConfig#addInterceptors(InterceptorRegistry)}, but excludes + * certain paths from the session timeout interceptor. + */ + @Override + public void addInterceptors(InterceptorRegistry registry) { + super.setExcludeUrlPathsForSessionTimeout("/login_external", "*/login_external.htm", "login", "/login.htm", + "/api*","/single_signon.htm","/single_signon"); + super.addInterceptors(registry); + } + + /** + * Creates and returns a new instance of a {@link CacheManager} class. + * + * @return New instance of {@link CacheManager} + */ + @Bean + public AbstractCacheManager cacheManager() { + return new CacheManager(); + } + + @PostConstruct + public void initGlobalLocalContext() { + // logger.init(); + } + + + /** + * Creates and returns a new instance of a {@link MainUebHandler}. + * + * @return New instance of {@link MainUebHandler}. + */ + @Bean + public MainUebHandler mainUebHandler() { + + return new MainUebHandler(); + } + + /** + * Creates and returns a new instance of a {@link InitUebHandler}. + * + * @return New instance of {@link InitUebHandler}. + */ + @Bean + public InitUebHandler initUebHandler() { + + return new InitUebHandler(); + } + + /** + * Creates and returns a new instance of a {@link WidgetNotificationHandler} + * . + * + * @return New instance of {@link WidgetNotificationHandler}. + */ + @Bean + public WidgetNotificationHandler widgetNotificationHandler() { + return new WidgetNotificationHandler(); + } + + /** + * Creates and returns a new instance of a {@link SchedulerFactoryBean} and + * populates it with triggers. + * + * @return New instance of {@link SchedulerFactoryBean} + * @throws Exception + */ + // APPLICATIONS REQUIRING QUARTZ SHOULD RESTORE ANNOTATION + /* + @Bean // ANNOTATION COMMENTED OUT + public SchedulerFactoryBean schedulerFactoryBean() throws Exception { + SchedulerFactoryBean scheduler = new SchedulerFactoryBean(); + scheduler.setTriggers(schedulerRegistryAdapter.getTriggers()); + scheduler.setConfigLocation(appApplicationContext.getResource("WEB-INF/conf/quartz.properties")); + scheduler.setDataSource(dataSource()); + return scheduler; + } + */ + + + /** + * Sets the scheduler registry adapter. + * + * @param schedulerRegistryAdapter + */ + /* + @Autowired + public void setSchedulerRegistryAdapter(final RegistryAdapter schedulerRegistryAdapter) { + this.schedulerRegistryAdapter = schedulerRegistryAdapter; + } + */ +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/ExternalAppInitializer.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/ExternalAppInitializer.java new file mode 100644 index 0000000..f9d5065 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/ExternalAppInitializer.java @@ -0,0 +1,60 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.conf; + +import java.util.Arrays; + +import org.openecomp.portalsdk.core.conf.AppInitializer; + +public class ExternalAppInitializer extends AppInitializer{ + + + @Override + protected Class<?>[] getRootConfigClasses() { + return super.getRootConfigClasses(); + } + + @Override + protected Class<?>[] getServletConfigClasses() { +// Class<?>[] configClasses = super.getServletConfigClasses(); +// Class<?>[] additionalConfigClasses = Arrays.copyOf(configClasses, configClasses.length); +// addConfigClass(additionalConfigClasses, ExternalAppConfig.class); +// return additionalConfigClasses; +// + return new Class[] {ExternalAppConfig.class}; + } + + static Class<?>[] addConfigClass(Class<?>[] a, Class<?> e) { + a = Arrays.copyOf(a, a.length + 1); + a[a.length - 1] = e; + return a; + } + + /* + * URL request will direct to the Spring dispatcher for processing + */ + @Override + protected String[] getServletMappings() { + return super.getServletMappings(); + } + +} + + diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/HibernateMappingLocations.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/HibernateMappingLocations.java new file mode 100644 index 0000000..bac61a6 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/conf/HibernateMappingLocations.java @@ -0,0 +1,45 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.conf; + +import org.openecomp.portalsdk.core.conf.HibernateMappingLocatable; +import org.springframework.context.annotation.Profile; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Component; + +@Component +@Profile("src") +public class HibernateMappingLocations implements HibernateMappingLocatable { + + public Resource[] getMappingLocations() { + return new Resource[] { + // Path is relative to WEB-INF/conf; + // a leading slash gets stripped so don't bother. + new ClassPathResource("../fusion/orm/Fusion.hbm.xml"), + // DBC does not use workflow nor RCloud + // new ClassPathResource("../fusion/orm/Workflow.hbm.xml"), + // new ClassPathResource("../fusion/orm/RNoteBookIntegration.hbm.xml"), + new ClassPathResource("../dbcapp/dbcapp.hbm.xml") + }; + } + + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/AngularSinglePageController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/AngularSinglePageController.java new file mode 100644 index 0000000..2a4c6d5 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/AngularSinglePageController.java @@ -0,0 +1,48 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +/** + * Controller for a single-page application sample. The view is restricted to + * authenticated users. The named view resolves to page singlePageSample.html, + * which uses Angular. + */ +@Controller +@RequestMapping("/") +public class AngularSinglePageController extends RestrictedBaseController { + + @RequestMapping(value = { "/singlePageSample" }, method = RequestMethod.GET) + public ModelAndView view(HttpServletRequest request) { + Map<String, Object> model = new HashMap<String, Object>(); + return new ModelAndView("single_page_sample", "model", model); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/CallflowController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/CallflowController.java new file mode 100644 index 0000000..371284c --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/CallflowController.java @@ -0,0 +1,44 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +/** + * Controller for a message sequence chart view. The view is restricted to + * authenticated users. The view name defaults to the request name with no + * suffix, "callflow", which resolves to page details.jsp. That page is an + * iframe around page details.html. + */ +@Controller +@RequestMapping("/") +public class CallflowController extends RestrictedBaseController { + + @RequestMapping(value = { "/callflow" }, method = RequestMethod.GET) + public ModelAndView plot() { + final String defaultViewName = null; + return new ModelAndView(defaultViewName); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/ElasticSearchController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/ElasticSearchController.java new file mode 100644 index 0000000..5b59aa9 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/ElasticSearchController.java @@ -0,0 +1,128 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import java.io.IOException; + +import org.json.JSONObject; +import org.openecomp.fusionapp.model.Result; +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +import io.searchbox.client.JestClient; +import io.searchbox.client.JestClientFactory; +import io.searchbox.client.config.HttpClientConfig; +import io.searchbox.core.Search; +import io.searchbox.core.SearchResult; +import io.searchbox.core.Suggest; +import io.searchbox.core.SuggestResult; +import io.searchbox.params.Parameters; + +/** + * Controller for views that demonstrate Elastic Search features. + */ +@RestController +public class ElasticSearchController extends RestrictedBaseController{ + + @RequestMapping(value = {"/es_search_demo" }, method = RequestMethod.GET) + public ModelAndView search() { + return new ModelAndView("es_search_demo"); + } + + @RequestMapping(value = {"/es_suggest_demo" }, method = RequestMethod.GET) + public ModelAndView suggest() { + return new ModelAndView("es_suggest_demo"); + } + + @RequestMapping(value="/es_suggest/{task}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Result> doSuggest(@PathVariable("task") String task) throws IOException { + JSONObject obj = new JSONObject(task); + String searchTerm = obj.getString("data"); + String searchSize = obj.getString("size"); + String searchFuzzy = obj.getString("fuzzy"); + String resultName = obj.getString("resultname"); + + JestClientFactory factory = new JestClientFactory(); + factory.setHttpClientConfig(new HttpClientConfig + .Builder("http://host:9200") + .multiThreaded(true) + .build()); + JestClient client = factory.getObject(); + + + Suggest suggest = new Suggest.Builder("{\n" + +"\"" + resultName +"\" : {\n" + +"\"text\" : \""+ searchTerm +"\",\n" + +"\"completion\" : {\n" + +"\"field\" : \"suggest\",\n" + +"\"size\" : " + searchSize + ",\n" + +"\"fuzzy\" : \"" + searchFuzzy + "\"\n" + +"}\n" + +"}\n" + +"}").addIndex("customer").build(); + + SuggestResult result = client.execute(suggest); + System.err.println(result.getJsonObject().toString()); + return new ResponseEntity<Result>(new Result(result.getJsonObject().toString()),HttpStatus.OK); + } + + @RequestMapping(value="/es_search/{task}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Result> doSearch(@PathVariable("task") String task) throws IOException { + JSONObject obj = new JSONObject(task); + String searchTerm = obj.getString("data"); + String searchSize = obj.getString("size"); + // String searchFuzzy = obj.getString("fuzzy"); + + JestClientFactory factory = new JestClientFactory(); + factory.setHttpClientConfig(new HttpClientConfig + .Builder("http://host:9200") + .multiThreaded(true) + .build()); + JestClient client = factory.getObject(); + + Search search = new Search.Builder("{\n" + +"\"query\" : {\n" + +"\"query_string\" : {\n" + +"\"query\" : \"name:"+ searchTerm +"\"\n" + +"}\n" + +"}\n" + +"}").addIndex("customer").setParameter(Parameters.SIZE,Integer.valueOf(searchSize)).build(); + + SearchResult result = client.execute(search); + System.err.println(result.getJsonObject().toString()); + return new ResponseEntity<Result>(new Result(result.getJsonObject().toString()),HttpStatus.OK); + } + + public ResponseEntity<Result> sendResult(Result result) { + return new ResponseEntity<Result>(result, HttpStatus.OK); + } + + @Override + public boolean isRESTfulCall() { + return true; + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/LeafletMapContoller.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/LeafletMapContoller.java new file mode 100644 index 0000000..3c461be --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/LeafletMapContoller.java @@ -0,0 +1,43 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +/** + * Controller for geographic map view. The view is restricted to authenticated + * users. The view name defaults to the request name with no suffix, + * "leafletMap", which resolves to page leafletMap.jsp. + */ +@Controller +@RequestMapping("/") +public class LeafletMapContoller extends RestrictedBaseController { + + @RequestMapping(value = { "/leafletMap" }, method = RequestMethod.GET) + public ModelAndView plot() { + final String defaultViewName = null; + return new ModelAndView(defaultViewName); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/NetMapController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/NetMapController.java new file mode 100644 index 0000000..fedd95f --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/NetMapController.java @@ -0,0 +1,57 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +/** + * Controller for a network map view that uses iframes. The view is restricted + * to authenticated users. + */ +@Controller +@RequestMapping("/") +public class NetMapController extends RestrictedBaseController { + + @RequestMapping(value = { "/net_map" }, method = RequestMethod.GET) + public ModelAndView plot(HttpServletRequest request) { + Map<String, Object> model = new HashMap<String, Object>(); + model.put("frame_int", "net_map_int"); + // This view resolves to page frame_insert.jsp + return new ModelAndView("frame_insert", model); + } + + @RequestMapping(value = { "/net_map_int" }, method = RequestMethod.GET) + public ModelAndView plot2() { + // The view name defaults to the request without any suffix. + final String defaultViewName = null; + // This view resolves to page net_map.jsp + return new ModelAndView(defaultViewName); + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/PostDroolsController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/PostDroolsController.java new file mode 100644 index 0000000..0fd902e --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/PostDroolsController.java @@ -0,0 +1,138 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.JSONObject; +import org.openecomp.portalsdk.core.command.PostDroolsBean; +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.openecomp.portalsdk.core.service.PostDroolsService; +import org.openecomp.portalsdk.core.web.support.JsonMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Controller +@RequestMapping("/") +public class PostDroolsController extends RestrictedBaseController{ + + @Autowired + private PostDroolsService postDroolsService; + + @RequestMapping(value = {"/drools" }, method = RequestMethod.GET) + public ModelAndView drools(HttpServletRequest request) { + + return new ModelAndView(getViewName()); + } + + + @RequestMapping(value = {"/getDrools" }, method = RequestMethod.GET) + public void getDrools(HttpServletRequest request, HttpServletResponse response) { + // Map<String, Object> model = new HashMap<String, Object>(); + + ObjectMapper mapper = new ObjectMapper(); + try { + List<PostDroolsBean> beanList = postDroolsService.fetchDroolBeans(); + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(beanList)); + JSONObject j = new JSONObject(msg); + response.getWriter().write(j.toString()); + + } catch (JsonGenerationException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @RequestMapping(value = {"/getDroolDetails" }, method = RequestMethod.GET) + public void getDroolDetails(HttpServletRequest request, HttpServletResponse response) { + + ObjectMapper mapper = new ObjectMapper(); + try { + + PostDroolsBean postDroolsBean = new PostDroolsBean(); + String selectedFile = request.getParameter("selectedFile"); + postDroolsBean.setDroolsFile(selectedFile);//sample populated + //postDroolsBean.setSelectedRules("[\"NJ\",\"NY\",\"KY\"]"); + postDroolsBean.setClassName(postDroolsService.retrieveClass(selectedFile)); + + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(postDroolsBean)); + JSONObject j = new JSONObject(msg); + response.getWriter().write(j.toString()); + + } catch (JsonGenerationException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @RequestMapping(value = {"/post_drools/execute" }, method = RequestMethod.POST) + public ModelAndView search(HttpServletRequest request, + HttpServletResponse response) throws Exception { + try { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JsonNode root = mapper.readTree(request.getReader()); + PostDroolsBean postDroolsBean = mapper.readValue(root.get("postDroolsBean").toString(), PostDroolsBean.class); + + String resultsString = postDroolsService.execute(postDroolsBean.getDroolsFile(), postDroolsBean.getClassName(), postDroolsBean.getSelectedRules()); + + response.setCharacterEncoding("UTF-8"); + response.setContentType("application / json"); + request.setCharacterEncoding("UTF-8"); + + PrintWriter out = response.getWriter(); + //String responseString = mapper.writeValueAsString(resultsString); + JSONObject j = new JSONObject("{resultsString: "+resultsString+"}"); + + out.write(j.toString()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/UserProfileController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/UserProfileController.java new file mode 100644 index 0000000..cf5feb8 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/UserProfileController.java @@ -0,0 +1,73 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.openecomp.portalsdk.core.domain.Profile; +import org.openecomp.portalsdk.core.service.ProfileService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Controller for user profile view. The view is restricted to authenticated + * users. The view name resolves to page user_profile.jsp which uses Angular. + */ + +@Controller +@RequestMapping("/") +public class UserProfileController extends RestrictedBaseController { + + @Autowired + ProfileService service; + + @RequestMapping(value = { "/user_profile" }, method = RequestMethod.GET) + public ModelAndView ProfileSearch(HttpServletRequest request) { + Map<String, Object> model = new HashMap<String, Object>(); + ObjectMapper mapper = new ObjectMapper(); + List<Profile> profileList = service.findAll(); + try { + model.put("customerInfo", mapper.writeValueAsString(profileList)); + } catch (JsonGenerationException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return new ModelAndView("user_profile", "model", model); + } + +} + diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/WelcomeController.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/WelcomeController.java new file mode 100644 index 0000000..510d154 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/controller/WelcomeController.java @@ -0,0 +1,44 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.controller; + +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.portalsdk.core.controller.RestrictedBaseController; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +/** + * Controller for welcome view. The view is restricted to authenticated users. + * If no view name is provided, the view name defaults to the request name with + * no suffix, "welcome", which is resolved by consulting a definitions.xml file. + */ +@Controller +@RequestMapping("/") +public class WelcomeController extends RestrictedBaseController { + @RequestMapping(value = { "/welcome" }, method = RequestMethod.GET) + public ModelAndView welcome(HttpServletRequest request) { + // The ECOMP SDK controller process_csp always redirects + // to welcome.htm, so map it to something useful. + return new ModelAndView("dbc_home_tdkey"); + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/model/Result.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/model/Result.java new file mode 100644 index 0000000..9dfd546 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/model/Result.java @@ -0,0 +1,37 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.model; + +public class Result { + private String result; + + public Result(String result) { + this.result = result; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/service/AdminAuthExtension.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/service/AdminAuthExtension.java new file mode 100644 index 0000000..d6e5193 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/service/AdminAuthExtension.java @@ -0,0 +1,34 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.service; + +import org.openecomp.portalsdk.core.domain.User; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service("adminAuthExtension") +@Transactional +public class AdminAuthExtension { + + public void saveUserExtension(User user){ + //app's developer implement their own logic here, like updating app's related tables + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/InitUebHandler.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/InitUebHandler.java new file mode 100644 index 0000000..1b50693 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/InitUebHandler.java @@ -0,0 +1,73 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.uebhandler; + +import java.util.concurrent.ConcurrentLinkedQueue; + +import javax.annotation.PostConstruct; + +import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.onboarding.crossapi.PortalApiConstants; +import org.openecomp.portalsdk.core.onboarding.crossapi.PortalApiProperties; +import org.openecomp.portalsdk.core.onboarding.ueb.UebManager; +import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; + +// +// Adding this class for the sole purpose of insuring that the MainUebHandler really +// honors @Async and kicks off a thread. For more info google @Async and read about +// @Async only working if called from different class. +// +@Configuration +public class InitUebHandler { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(InitUebHandler.class); + + + @Autowired + MainUebHandler mainUebHandler; + + public InitUebHandler() { + + } + + @PostConstruct + public void initUeb() { + + try { + String enableListenerThread = PortalApiProperties.getProperty(PortalApiConstants.UEB_LISTENERS_ENABLE); + if (enableListenerThread.equalsIgnoreCase("true")) { + ConcurrentLinkedQueue<UebMsg> inboxQueue = new ConcurrentLinkedQueue<UebMsg>(); + UebManager.getInstance().initListener(inboxQueue); + mainUebHandler.runHandler(inboxQueue); + logger.info(EELFLoggerDelegate.debugLogger, ("Returned from initiating mainUebHandler...")); + } + else { + logger.info(EELFLoggerDelegate.debugLogger, ("Not starting UEB listening thread because ueb_listeners_enable is not set to true in the properties file.")); + } + } + catch (Exception e) { + logger.error(EELFLoggerDelegate.debugLogger, ("Not starting UEB listening thread because property could not be read " + PortalApiConstants.UEB_LISTENERS_ENABLE),AlarmSeverityEnum.MAJOR); + } + + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/MainUebHandler.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/MainUebHandler.java new file mode 100644 index 0000000..604880b --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/MainUebHandler.java @@ -0,0 +1,104 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.uebhandler; + +import java.util.concurrent.ConcurrentLinkedQueue; + +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg; +import org.openecomp.portalsdk.core.onboarding.ueb.UebMsgTypes; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +//------------------------------------------------------------------------- +// Listens for received UEB messages and handles the messages +// +// Note: To implement a synchronous reply call getMsgId on the request +// and putMsgId on the reply (echoing the request MsgId). +// +//------------------------------------------------------------------------- +@Component("MainUebHandler") +public class MainUebHandler { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MainUebHandler.class); + + + ConcurrentLinkedQueue<UebMsg> inboxQueue = null; + + @Autowired + WidgetNotificationHandler widgetNotificationHandler; + + @Async + public void runHandler(ConcurrentLinkedQueue<UebMsg> queue) { + inboxQueue = queue; + + logger.info(EELFLoggerDelegate.debugLogger, ("==> MainUebHandler started")); + + while (true) { + UebMsg msg = null; + while ((msg = inboxQueue.poll()) != null) { + if (msg.getMsgType() != null) { + logger.debug(EELFLoggerDelegate.debugLogger, ("<== Received UEB message : " + msg.toString())); + + switch (msg.getMsgType()) { + /* + * Add your own defined handler objects, use @Component for + * the class. See WidgetNotificationHandler as an example. + * + * Use @Async on methods for performance + * + * For syncronous replies use UebManager publishReply and + * echo back the msgId in your response ie + * msg.putMsgId(requestMsg.getMsgId()) + * + * case UebMsgTypes.UEB_MSG_TYPE_XYZ: { + * XYZHandler.handleMsg(msg); break; } + */ + case UebMsgTypes.UEB_MSG_TYPE_WIDGET_NOTIFICATION: { + widgetNotificationHandler.handleWidgetNotification(msg); + break; + } + default: { + + logger.info(EELFLoggerDelegate.debugLogger, ("Unknown message type [" + msg.getMsgType() + "] from " + msg.getSourceTopicName())); + + break; + } + } + } + } + + if (Thread.interrupted()) { + + logger.info(EELFLoggerDelegate.debugLogger, ("==> UebMainHandler exiting")); + + break; + } + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + logger.info(EELFLoggerDelegate.debugLogger, ("UebMainHandler interrupted during sleep" + e.getMessage())); + + } + } + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/WidgetNotificationHandler.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/WidgetNotificationHandler.java new file mode 100644 index 0000000..9b0ff58 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/uebhandler/WidgetNotificationHandler.java @@ -0,0 +1,46 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.uebhandler; + +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +@Component +public class WidgetNotificationHandler { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetNotificationHandler.class); + + + public WidgetNotificationHandler() { + } + + @Async + public void handleWidgetNotification(UebMsg requestMsg) { + logger.debug(EELFLoggerDelegate.debugLogger, ("handleWidgetNotification received notification: " + requestMsg.toString())); + /* + * Here the notification msg can be handled + * + * requestMsg.getPayload() - returns string that contains the + * Application defined content + */ + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/CustomLoggingFilter.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/CustomLoggingFilter.java new file mode 100644 index 0000000..760460f --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/CustomLoggingFilter.java @@ -0,0 +1,54 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.util; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.filter.Filter; +import ch.qos.logback.core.spi.FilterReply; + +/** + * Custom Filter class bind with logback.xml + * configuration file to strip out certain log messages + * coming out of special packages or classes. + * + */ +public class CustomLoggingFilter extends Filter<ILoggingEvent> { + + /** + * Custom Filter is added to strip out the continuous U-EB logging messages + * But make sure we log the ERROR & WARNING Level messages. + */ + @Override + public FilterReply decide(ILoggingEvent event) { + try { + if ((event.getLevel() != Level.ERROR || event.getLevel() != Level.WARN) && + (event.getThreadName().equalsIgnoreCase("UEBConsumerThread")) && + (event.getLoggerName().contains("com.att.nsa") || event.getLoggerName().contains("org.apache.http")) + ) { + return FilterReply.DENY; + } else { + return FilterReply.NEUTRAL; + } + } catch(Exception e) { + return FilterReply.NEUTRAL; + } + } +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/LoggerDemo.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/LoggerDemo.java new file mode 100644 index 0000000..9bdff16 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/LoggerDemo.java @@ -0,0 +1,48 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.LoggerContext; + +public class LoggerDemo { + + private static final Logger log = LoggerFactory.getLogger(LoggerDemo.class); + + public static void main(String[] args) { + new LoggerDemo().new Parent().hello(); + new LoggerDemo().new Child().hello(); + // Flush messages before shutting down async logger + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + loggerContext.stop(); + } + + public class Parent { + public void hello() { + log.info("Hello from " + getClass().getSimpleName()); + } + } + + public class Child extends Parent { + } + +} diff --git a/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/ValidateEncodingApp.java b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/ValidateEncodingApp.java new file mode 100644 index 0000000..cd327b2 --- /dev/null +++ b/dcae_dmaapbc_webapp/src/main/java/org/openecomp/fusionapp/util/ValidateEncodingApp.java @@ -0,0 +1,203 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller Web Application + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.fusionapp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; + +/** + * Reads bytes from file using a decoder that throws an exception if the bytes + * cannot be decoded as UTF-8. Can be used to validate a single file or a + * directory of files with full recursion. Includes a feature to create a file + * with a non-UTF-8 byte sequence for testing. + */ +public class ValidateEncodingApp { + + private final String charsetName; + private final CharsetDecoder decoder; + private int filesPassed = 0, filesFailed = 0; + + /** + * Obtains a decoder and configures it to blow up on problems. + * + * @param charsetName + * Name of character set to use; e.g., UTF-8 + */ + public ValidateEncodingApp(String charsetName) { + this.charsetName = charsetName; + Charset charset = Charset.forName(charsetName); + decoder = charset.newDecoder(); + decoder.onMalformedInput(CodingErrorAction.REPORT); + decoder.onUnmappableCharacter(CodingErrorAction.REPORT); + } + + /** + * @return The character set name supplied to the constructor + */ + public String getCharsetName() { + return charsetName; + } + + /** + * @return The number of files that could not be validated for whatever + * reason. + */ + public int getFilesFailed() { + return filesFailed; + } + + /** + * @return The number of files successfully validated. + */ + public int getFilesPassed() { + return filesPassed; + } + + /** + * Reads and decodes bytes from the specified file, which will find byte + * sequences that cannot be decoded using the current system. Traps all + * exceptions and reports to System.err. + * + * @param file + * @return True on success, false on IOException or decoding error + */ + private boolean validateFile(File file) { + boolean result = true; + int lineNr = 1; + BufferedReader br = null; + try { + br = new BufferedReader(new InputStreamReader(new FileInputStream(file), decoder)); + while (br.readLine() != null) + ++lineNr; + ++filesPassed; + } catch (CharacterCodingException ex) { + ++filesFailed; + System.err.println("Failed at line " + lineNr + ", file " + file.getPath() + ": " + ex.toString()); + result = false; + } catch (IOException ex) { + ++filesFailed; + System.err.println("Failed to read file " + file.getPath() + ": " + ex.toString()); + result = false; + } finally { + if (br != null) + try { + br.close(); + } catch (IOException ex) { + System.err.println("Failed to close file: " + ex.toString()); + } + } + return result; + } + + /** + * Validates all files in the specified directory. Optionally recurses into + * subdirectories. Prints message to stdout when it starts and again when it + * finishes traversing a directory. + * + * @param dir + * Directory to traverse. + * @param isRecurse + * If true, will call itself to process subdirectories. + * @throws Exception + */ + private void validateDirectory(File dir, boolean isRecurse) throws Exception { + if (!dir.exists() || !dir.isDirectory()) + throw new IllegalArgumentException("Not found or not a directory: " + dir.getPath()); + System.out.println("Entering directory " + dir.getPath()); + File[] contents = dir.listFiles(); + for (File f : contents) { + if (f.isFile()) + validateFile(f); + else if (f.isDirectory() && isRecurse) + validateDirectory(f, isRecurse); + } + System.out.println("Leaving directory " + dir.getPath()); + } + + /** + * Prints message(s) and exits + * + * @param msg + */ + private static void usage(String msg) { + if (msg != null) + System.err.println(msg); + System.err.println("Usage: ValidateEncodingApp -validate (file|dir)"); + System.err.println(" ValidateEncodingApp -create filename"); + System.exit(0); + } + + public static void main(String[] args) throws Exception { + + // TODO: extend to accept encoding system name + // and recurse option as arguments. + String charsetName = "UTF-8"; + boolean recurse = true; + + if (args.length != 2) + usage("Unexpected number of arguments"); + + String cmd = args[0]; + File file = new File(args[1]); + + if ("-create".equals(cmd)) { + if (file.exists()) + usage("Will not overwrite existing file: " + file.getPath()); + System.out.println("Creating file with ISO-8859-1 content: " + file.getPath()); + try { + // The word "dolt" has an o with a diagonal slash thru it. + // The slash-o is valid in ISO-8859-1 but not in UTF-8. + final byte[] invalid = "d\u00f8lt\n".getBytes("iso-8859-1"); + FileOutputStream fis = new FileOutputStream(file); + fis.write(invalid); + fis.close(); + } catch (IOException ex) { + System.err.println("Failed to write file: " + ex.toString()); + } + } else if ("-validate".equals(cmd)) { + if (!file.exists()) + usage("Not found: " + file.getPath()); + + ValidateEncodingApp validator = new ValidateEncodingApp(charsetName); + if (file.isFile()) { + System.out.println("Validating decoding of bytes as " + charsetName + " for file " + file.getPath()); + validator.validateFile(file); + } else { + System.out.println("Validating decoding of bytes as " + charsetName + " for files in/below directory " + + file.getPath()); + validator.validateDirectory(file, recurse); + } + System.out.println("Validation success count is " + validator.getFilesPassed()); + System.out.println("Validation failure count is " + validator.getFilesFailed()); + } else { + usage("Unexpected command: " + cmd); + } + + } + +} |