diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/AppraisalResultSerializer.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/AppraisalResultSerializer.java similarity index 96% rename from HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/AppraisalResultSerializer.java rename to HIRS_AttestationCA/src/main/java/hirs/attestationca/AppraisalResultSerializer.java index 9744d70b..dd639e48 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/AppraisalResultSerializer.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/AppraisalResultSerializer.java @@ -1,4 +1,4 @@ -package hirs.attestationca.portal; +package hirs.attestationca; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/entity/AppraisalResult.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/entity/AppraisalResult.java index 89f51cb3..2c152368 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/entity/AppraisalResult.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/entity/AppraisalResult.java @@ -1,8 +1,8 @@ package hirs.attestationca.entity; import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import hirs.AppraisalResultSerializer; import hirs.appraiser.Appraiser; +import hirs.attestationca.AppraisalResultSerializer; import hirs.data.persist.AbstractEntity; import hirs.data.persist.AppraisalStatus; diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBDeviceManager.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBDeviceManager.java deleted file mode 100644 index bf3cdfb8..00000000 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBDeviceManager.java +++ /dev/null @@ -1,312 +0,0 @@ -package hirs.attestationca.servicemanager; - -import hirs.FilteredRecordsList; -import hirs.attestationca.entity.Device; -import hirs.persist.CriteriaModifier; -import hirs.persist.DBManagerException; -import hirs.persist.DeviceManagerException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.hibernate.Criteria; -import org.hibernate.HibernateException; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.query.Query; -import org.springframework.stereotype.Service; - -import javax.persistence.EntityManager; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.JoinType; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * This class defines a DeviceManager that stores the devices - * in a database. - */ -@Service -public class DBDeviceManager extends DBManager implements - DeviceManager { - - private static final Logger LOGGER = LogManager.getLogger(); - - /** - * Creates a new DBDeviceManager that uses the default - * database. The default database is used to store all of the - * Devices. - * - * @param em entity manager used to access database connections - */ - public DBDeviceManager(final EntityManager em) { - super(Device.class, em); - } - - /** - * Saves the Device in the database. This creates a new - * database session and saves the device. If the Device had - * previously been saved then a DeviceManagerException is - * thrown. - * - * @param device - * device to save - * @return reference to saved device - * @throws hirs.persist.DeviceManagerException - * if device has previously been saved or an error occurs - * while trying to save it to the database - */ - @Override - public final Device saveDevice(final Device device) - throws DeviceManagerException { - LOGGER.debug("saving device: {}", device); - try { - return super.save(device); - } catch (DBManagerException e) { - throw new DeviceManagerException(e); - } - } - - /** - * Updates a Device. This updates the database entries to - * reflect the new values that should be set. - * - * @param device - * device - * @throws DeviceManagerException - * if device has not previously been saved or an error occurs - * while trying to save it to the database - */ - @Override - public final void updateDevice(final Device device) - throws DeviceManagerException { - LOGGER.debug("updating device: {}", device); - try { - super.update(device); - } catch (DBManagerException e) { - throw new DeviceManagerException(e); - } - } - - /** - * Updates list of Devices. This updates the database entries - * to reflect the new values that should be set. Commonly used when - * deleting a DeviceGroup. - * - * @param deviceList - * list of devices that should be updated in single transaction - * @throws DeviceManagerException - * if device has not previously been saved or an error occurs - * while trying to save it to the database - */ - @Override - public final void updateDeviceList(final Set deviceList) - throws DeviceManagerException { - LOGGER.debug("updating all devices in list"); - - Session session = getSession(); - Transaction tx = session.beginTransaction(); - try { - for (final Device device : deviceList) { - session.merge(device); - } - session.getTransaction().commit(); - } catch (Exception e) { - final String msg = "unable to update all devices in list"; - LOGGER.error(msg, e); - LOGGER.debug("rolling back transaction"); - tx.rollback(); - throw new DBManagerException(msg, e); - } - } - - /** - * Returns a list of all Devices. This searches through - * the database for this information. - * - * @return list of Devices - * @throws DeviceManagerException - * if unable to search the database - */ - @Override - public final Set getDeviceList() throws DeviceManagerException { - LOGGER.debug("getting device list"); - - try { - final List devices = super.getList(Device.class); - return new HashSet<>(devices); - } catch (DBManagerException e) { - throw new DeviceManagerException(e); - } - } - - /** - * Returns a list of all Device names. This searches through - * the database for this information. - * - * @return list of Device names - * @throws DeviceManagerException - * if unable to search the database - */ - @Override - public final List getDeviceNameList() - throws DeviceManagerException { - LOGGER.debug("getting device list"); - List deviceNames = new LinkedList<>(); - try { - final List devices = super.getList(Device.class); - for (Device b : devices) { - deviceNames.add(b.getName()); - } - } catch (DBManagerException e) { - throw new DeviceManagerException(e); - } - return deviceNames; - } - - /** - * Returns a list of all Devices that are ordered by a column - * and direction (ASC, DESC) that is provided by the user. This method - * helps support the server-side processing in the JQuery DataTables. - * - * @param columnToOrder Column to be ordered - * @param ascending direction of sort - * @param firstResult starting point of first result in set - * @param maxResults total number we want returned for display in table - * @param search string of criteria to be matched to visible columns - * - * @return FilteredRecordsList object with fields for DataTables - * @throws DeviceManagerException - * if unable to create the list - */ - @Override - public final FilteredRecordsList getOrderedDeviceList( - final String columnToOrder, final boolean ascending, final int firstResult, - final int maxResults, final String search) - throws DeviceManagerException { - - if (columnToOrder == null) { - LOGGER.debug("null object argument"); - throw new NullPointerException("object"); - } - - //Maps object types and their ability to be searched by Hibernate - //without modification - Map searchableColumns = new HashMap<>(); - searchableColumns.put("name", true); - searchableColumns.put("group.name", true); - searchableColumns.put("last_report_timestamp", false); - - CriteriaModifier modifier = new CriteriaModifier() { - @Override - public void modify(final Criteria criteria) { - criteria.createAlias("deviceGroup", "group"); - } - }; - - try { - LOGGER.debug("Getting baseline list"); - return super.getOrderedList(Device.class, columnToOrder, ascending, firstResult, - maxResults, search, searchableColumns, modifier); - } catch (DBManagerException e) { - LOGGER.error(e); - return null; - } - } - /** - * Retrieves the Device from the database. This searches the - * database for an entry whose name matches name. It then - * reconstructs a Device object from the database entry - * - * @param name - * name of the device - * @return device if found, otherwise null. - * @throws DeviceManagerException - * if unable to search the database or recreate the - * Device - */ - @Override - public final Device getDevice(final String name) - throws DeviceManagerException { - LOGGER.debug("getting device: {}", name); - try { - return super.get(name); - } catch (DBManagerException e) { - throw new DeviceManagerException(e); - } - } - - /** - * Used to produce a list of all Devices associated with the Default Group. - * - * @return list of Devices that are part of the Default Group - * @throws DeviceManagerException - * if unable to find the device or delete it from the database - */ - @Override - public final List getDefaultDevices() throws DeviceManagerException { - Transaction tx = null; - - Session session = getSession(); - List devices = new ArrayList<>(); - try { - LOGGER.debug("retrieving defaults devices from db"); - tx = session.beginTransaction(); - - CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); - CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Device.class); - Root root = criteriaQuery.from(Device.class); - root.join("group.name", JoinType.LEFT).alias("group"); - Predicate recordPredicate = criteriaBuilder - .and(criteriaBuilder.equal(root.get("group.name"), "Default Group")); - criteriaQuery.select(root).where(recordPredicate).distinct(true); - Query query = session.createQuery(criteriaQuery); - List results = query.getResultList(); - if (results != null) { - devices.addAll(results); - } -// List list = session.createCriteria(Device.class).createAlias("deviceGroup", "group") -// .add(Restrictions.eq("group.name", DeviceGroup.DEFAULT_GROUP)) -// .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) -// .list(); - - tx.commit(); - } catch (HibernateException e) { - final String msg = "unable to retrieve default devices"; - LOGGER.error(msg, e); - if (tx != null) { - LOGGER.debug("rolling back transaction"); - tx.rollback(); - } - throw e; - } - - return devices; - } - - /** - * Deletes the Device from the database. This removes all - * of the database entries that stored information with regards to the - * Device with a foreign key relationship. - * - * @param name of the device to be deleted - * @return true if successfully found and deleted, false if otherwise - */ - @Override - public final boolean deleteDevice(final String name) - throws DeviceManagerException { - LOGGER.debug("deleting device: {}", name); - try { - return super.delete(name); - } catch (DBManagerException e) { - throw new DeviceManagerException(e); - } - } - -} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBPortalInfoManager.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBPortalInfoManager.java deleted file mode 100644 index 034b6e11..00000000 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBPortalInfoManager.java +++ /dev/null @@ -1,159 +0,0 @@ -package hirs.attestationca.servicemanager; - -import hirs.data.persist.enums.PortalScheme; -import hirs.data.persist.info.PortalInfo; -import hirs.persist.DBManagerException; -import hirs.persist.PortalInfoManagerException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.stereotype.Service; - -import javax.persistence.EntityManager; -import java.net.URI; -import java.net.URISyntaxException; - -/** - * A DBPortalInfoManager is a service (extends DBManager) that - * implements the PortalInfoManager that stores and retrieves Portal Info objects. - */ -@Service -public class DBPortalInfoManager extends DBManager implements PortalInfoManager { - - private static final Logger LOGGER = LogManager.getLogger(DBPortalInfoManager.class); - - /** - * Creates a new DBPortalInfoManager. The optional SessionFactory parameter is - * used to manage sessions with a hibernate db. - * - * @param em a hibernate session - */ - public DBPortalInfoManager(final EntityManager em) { - super(PortalInfo.class, em); - } - - /** - * Saves the PortalInfo in the database. This creates a new database - * session and saves the PortalInfo. If the PortalInfo had previously - * been saved then a PortalInfoManagerException is thrown. - * - * @param info PortalInfo to save - * @return reference to saved PortalInfo - * @throws hirs.persist.PortalInfoManagerException if PortalInfo has previously been saved or an - * error occurs while trying to save it to the database - */ - @Override - public final PortalInfo savePortalInfo(final PortalInfo info) - throws PortalInfoManagerException { - LOGGER.debug("saving Portal Info {}", info); - try { - return super.save(info); - } catch (DBManagerException e) { - throw new PortalInfoManagerException(e); - } - } - - /** - * Updates a PortalInfo. This updates the database entries to reflect the new - * values that should be set. - * - * @param info PortalInfo - * @throws PortalInfoManagerException if PortalInfo has not previously been saved or - * an error occurs while trying to save it to the database - */ - @Override - public final void updatePortalInfo(final PortalInfo info) - throws PortalInfoManagerException { - LOGGER.debug("updating Portal Info: {}", info); - try { - super.update(info); - } catch (DBManagerException e) { - throw new PortalInfoManagerException(e); - } - } - - /** - * Retrieves the PortalInfo from the database. This searches the database for an - * entry whose name matches name. It then reconstructs a PortalInfo - * object from the database entry. - * - * @param scheme PortalInfo.Scheme of the PortalInfo - * @return PortalInfo if found, otherwise null. - * @throws PortalInfoManagerException if unable to search the database or recreate the - * PortalInfo - */ - @Override - public final PortalInfo getPortalInfo(final PortalScheme scheme) - throws PortalInfoManagerException { - LOGGER.debug("getting Portal Info: {}", scheme.name()); - try { - return super.get(scheme.name()); - } catch (DBManagerException e) { - throw new PortalInfoManagerException(e); - } - } - - /** - * Deletes the PortalInfo from the database. This removes all of the database - * entries that stored information with regards to the this PortalInfo. - * Currently, iterates over Policy entries and removes the selected - * PortalInfo from them if it exists. This needs to be fixed as this should not - * be performed without user action. Update is expected soon. - * - * @param scheme PortalInfo.Scheme of the PortalInfo to delete - * @return true if successfully found and deleted PortalInfo - * @throws PortalInfoManagerException if unable to find the PortalInfo or delete it - * from the database - */ - @Override - public final boolean deletePortalInfo(final PortalScheme scheme) - throws PortalInfoManagerException { - LOGGER.debug("deleting Portal Info: {}", scheme.name()); - try { - return super.delete(scheme.name()); - } catch (DBManagerException e) { - throw new PortalInfoManagerException(e); - } - } - - /** - * Retrieve the PortalInfo object stored into the repo - * and return the url it represents. - * - * @return the URL represented by the PortalInfo object. - */ - @Override - public final String getPortalUrlBase() { - PortalInfo info; - - try { - // Prefer HIRS to use HTTPS, but check HTTP if needed - info = getPortalInfo(PortalScheme.HTTPS); - if (info == null) { - info = getPortalInfo(PortalScheme.HTTP); - } - } catch (Exception e) { - info = null; - } - - // The default base url - String url = "Your_HIRS_Portal/"; - - try { - if (info != null && info.getIpAddress() != null) { - String context = "/"; - if (info.getContextName() != null) { - context += info.getContextName() + "/"; - } - URI uri = new URI(info.getSchemeName().toLowerCase(), null, - info.getIpAddress().getHostName(), info.getPort(), - context, null, null); - url = uri.toString(); - } - } catch (URISyntaxException e) { - LOGGER.error("DBPortalInfoManager.getPortalUrlBase():" - + " Could not create the URI. Returning the default."); - } - - return url; - } -} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBReportManager.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBReportManager.java deleted file mode 100755 index 6d9f7bf3..00000000 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBReportManager.java +++ /dev/null @@ -1,195 +0,0 @@ -package hirs.attestationca.servicemanager; - -import hirs.data.persist.Report; -import hirs.persist.DBManagerException; -import hirs.persist.ReportManagerException; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.Logger; -import org.hibernate.criterion.Conjunction; -import org.hibernate.criterion.Disjunction; -import org.hibernate.criterion.MatchMode; -import org.hibernate.criterion.Restrictions; -import org.springframework.stereotype.Service; - -import javax.persistence.EntityManager; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import static org.apache.logging.log4j.LogManager.getLogger; -import static org.hibernate.criterion.Restrictions.ilike; - -/** - * This class defines a ReportManager that stores the reports in a - * database. - */ -@Service -public class DBReportManager extends DBManager implements ReportManager { - private static final Logger LOGGER = getLogger(DBReportManager.class); - - /** - * Creates a new DBReportManager that uses the provided sessionFactory - * to interact with a database. - * - * @param em entity manager used to access database connections - */ - public DBReportManager(final EntityManager em) { - super(Report.class, em); - } - - /** - * Saves the Report in the database and returns it. - * - * @param report - * report to save - * @return Report that was saved - * @throws hirs.persist.DBManagerException - * if Report has previously been saved or an error occurs while - * trying to save it to the database - */ - @Override - public final Report saveReport(final Report report) - throws DBManagerException { - LOGGER.debug("Saving report: {}", report); - try { - return super.save(report); - } catch (DBManagerException e) { - throw new ReportManagerException(e); - } - } - - /** - * Returns a list of all Reports of type clazz. - * This searches through the database for this information. - * - * All Reports will be returned without measurement records as they are - * lazily loaded for performance. If the records of a report are necessary, - * a method will need to be written to return the records inside of a - * transaction. - * - * @param clazz - * class type of Reports to return (may be null) - * @return list of Reports - * @throws ReportManagerException - * if unable to search the database - */ - @Override - public final List getReportList(final Class clazz) - throws ReportManagerException { - LOGGER.debug("getting report list"); - try { - //super.getList(Report); - return null; - } catch (DBManagerException e) { - throw new ReportManagerException(e); - } - } - - /** - * Retrieves the Report from the database. This searches the - * database for an entry whose id matches id. It then - * reconstructs a Report object from the database entry. - * - * Note: IMAMeasurementRecords are lazily loaded so the object - * returned will not contain them for performance purposes. If the whole - * report needs to be retrieved a method will need to be written to return - * the records inside of a transaction. - * - * @param id id of the report - * @return report - * @throws ReportManagerException - * if unable to search the database or recreate the Report - */ - @Override - public final Report getReport(final UUID id) throws ReportManagerException { - LOGGER.debug("getting report: {}", id); - try { - return super.get(id); - } catch (DBManagerException e) { - throw new ReportManagerException(e); - } - } - - @Override - public final Report getCompleteReport(final UUID id) throws ReportManagerException { - LOGGER.debug("getting full report: {}", id); - try { - return super.getAndLoadLazyFields(id, true); - } catch (DBManagerException e) { - throw new ReportManagerException(e); - } - } - - /** - * Updates a Report. This updates the database entries - * to reflect the new values that should be set. - * - * @param report - * report to be updated - * @throws ReportManagerException - * if Report an error occurs while updating the report or - * while trying to save it to the database - */ - @Override - public void updateReport(final Report report) throws ReportManagerException { - LOGGER.debug("updating report: {}", report); - try { - super.update(report); - } catch (DBManagerException e) { - throw new ReportManagerException(e); - } - } - - /** - * Deletes the Report from the database. This removes all of - * the database entries that stored information with regards to the this - * Report. - *

- * If the Report is referenced by any other tables then this - * will throw a ReportManagerException. - * - * @param id - * id of the Report to delete - * @return true if successfully found and deleted the Report - * @throws ReportManagerException - * if unable to find the baseline or delete it from the - * database - */ - @Override - public final boolean deleteReport(final UUID id) - throws ReportManagerException { - LOGGER.debug("deleting baseline: {}", id); - try { - return false; //super.delete(id); - } catch (DBManagerException e) { - throw new ReportManagerException(e); - } - } - - private Conjunction buildImaRecordSearchFilter(final String search, final - Map searchableColumns) { - // Search for all words in all searchable columns - Conjunction and = Restrictions.conjunction(); - String[] searchWords = StringUtils.split(search); - for (String word : searchWords) { - // Every word must be in at least one column - Disjunction or = Restrictions.disjunction(); - for (Map.Entry entry - : searchableColumns.entrySet()) { - if (entry.getValue()) { - if (entry.getKey().equals("digest")) { - or.add(ilikeHex("digest", word)); - } else { - or.add(ilike(entry.getKey(), word, - MatchMode.ANYWHERE)); - } - } else { - or.add(ilikeCast(entry.getKey(), word)); - } - } - and.add(or); - } - return and; - } -} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBReportRequestStateManager.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBReportRequestStateManager.java deleted file mode 100644 index 74e6511b..00000000 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/servicemanager/DBReportRequestStateManager.java +++ /dev/null @@ -1,101 +0,0 @@ -package hirs.attestationca.servicemanager; - -import hirs.attestationca.entity.Device; -import hirs.attestationca.entity.ReportRequestState; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.stereotype.Service; - -import javax.persistence.EntityManager; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -/** - * This class defines a ReportRequestStateManager that stores ReportRequestStates in a - * database. - */ -@Service -public class DBReportRequestStateManager extends DBManager - implements ReportRequestStateManager { - private static final Logger LOGGER = LogManager.getLogger(DBReportRequestStateManager.class); - - /** - * Creates a new DBReportRequestStateManager that uses the default database. The - * default database is used to store all of the ReportRequestStates. - * - * @param em entity manager used to access database connections - */ - public DBReportRequestStateManager(final EntityManager em) { - super(ReportRequestState.class, em); - } - - /** - * Retrieves the state of a device, if the device and state exists. - * - * @param device the Device whose state should be retrieved - * @return the associated ReportRequestState, or null if no associated state was found - */ - @Override - public final ReportRequestState getState(final Device device) { - CriteriaBuilder builder = this.getSession().getCriteriaBuilder(); - Root root = builder.createQuery(ReportRequestState.class) - .from(ReportRequestState.class); - - Predicate predicate = builder.equal(root.get("device"), device); - List results = getWithCriteria(Collections.singletonList(predicate)); - if (results.isEmpty()) { - return null; - } else { - LOGGER.debug("Retrieved ReportRequestState: {}", results.get(0)); - return results.get(0); - } - } - - /** - * Return a Collection of all persisted ReportRequestStates in the database. - * - * @return the Collection of all persisted ReportRequestStates - */ - @Override - public final List getLateDeviceStates() { - CriteriaBuilder builder = this.getSession().getCriteriaBuilder(); - Root root = builder.createQuery(ReportRequestState.class) - .from(ReportRequestState.class); - - Predicate predicate = builder.lessThanOrEqualTo(root.get("dueDate"), new Date()); - return getWithCriteria(Collections.singletonList(predicate)); - } - - /** - * Saves the given state to the database. The associated Device must be saved prior to saving - * the state. - * - * @param state the state to save - * @return the saved copy of the state - */ - @Override - public final ReportRequestState saveState(final ReportRequestState state) { - if (state.getId() == null) { - return save(state); - } else { - update(state); - ReportRequestState updatedState = getState(state.getDevice()); - LOGGER.debug("Updated ReportRequestState: {}", updatedState); - return updatedState; - } - } - - /** - * Deletes the given {@link ReportRequestState} from the database. - * - * @param state the ReportRequestState instance to delete - */ - @Override - public final void deleteState(final ReportRequestState state) { - delete(state.toString()); // cyrus-dev - } -}