diff --git a/dotCMS/src/main/java/com/dotmarketing/business/ajax/HostAjax.java b/dotCMS/src/main/java/com/dotmarketing/business/ajax/HostAjax.java index 393a4de71a42..2df42a8c7a1c 100644 --- a/dotCMS/src/main/java/com/dotmarketing/business/ajax/HostAjax.java +++ b/dotCMS/src/main/java/com/dotmarketing/business/ajax/HostAjax.java @@ -123,7 +123,8 @@ public Map findHostsForDataStore(String filter, boolean showArch public Map findHostsPaginated(final String filter, final boolean showArchived, int offset, int count) throws DotDataException, DotSecurityException, PortalException, SystemException { final User user = this.getLoggedInUser(); final boolean respectFrontend = !this.userWebAPI.isLoggedToBackend(this.getHttpRequest()); - final List sitesFromDb = this.hostAPI.findAllFromDB(user, false, respectFrontend); + final List sitesFromDb = this.hostAPI.findAllFromDB(user, + false, true, respectFrontend); final List fields = FieldsCache.getFieldsByStructureVariableName(Host.HOST_VELOCITY_VAR_NAME); final List searchableFields = fields.stream().filter(field -> field.isListed() && field .getFieldType().startsWith("text")).collect(Collectors.toList()); diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/ContentletCacheImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/ContentletCacheImpl.java index a53a6a6f3b8b..25d75c471a62 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/ContentletCacheImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/ContentletCacheImpl.java @@ -135,7 +135,7 @@ public void remove(final String key){ Logger.debug(this, "Cache not able to be removed", e); } - final Host host = CacheLocator.getHostCache().getById(key); + final Host host = CacheLocator.getHostCache().getById(key, false); if(host != null){ CacheLocator.getHostCache().remove(host); } diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPI.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPI.java index 3768c8e21980..c71b8c819a56 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPI.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPI.java @@ -251,6 +251,24 @@ Host find(Contentlet contentlet, */ List findAllFromDB(final User user, final boolean includeSystemHost, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException; + /** + * Returns the complete list of Sites in your dotCMS repository retrieved directly from the data source. This + * method allows you to EXCLUDE the System Host from the result list. Additionally, you can specify whether + * you want to retrieve the live version of the Site over its working version if available. + * + * @param user The {@link User} that is calling this method. + * @param includeSystemHost If the System Host must be included in the results, set to {@code true}. + * @param retrieveLiveVersion If the live version of the Site must be retrieved when it's available, set to {@code true}. + * @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this + * operation, set to {@code true}. Otherwise, set to {@code false}. + * + * @return The list of {@link Host} objects. + * @throws DotDataException An error occurred when accessing the data source. + * @throws DotSecurityException The specified User does not have the required permissions to perform this + * operation. + */ + List findAllFromDB(final User user, final boolean includeSystemHost, final boolean retrieveLiveVersion, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException; + /** * Returns the complete list of Sites in your dotCMS repository retrieved from the cache. If no data is currently * available, it will be retrieved from the data source, and put into the respective cache region. @@ -415,7 +433,7 @@ List getHostsWithPermission(final int permissionType, final boolean includ * * @param host */ - public void updateCache(Host host); + public void updateCache(Host host) throws DotDataException, DotSecurityException; /** * Updates the MenuLinks of the host with the new hostname diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPIImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPIImpl.java index b8b8c685ce7f..20cfa39bb5bb 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPIImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostAPIImpl.java @@ -42,6 +42,7 @@ import io.vavr.control.Try; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -101,7 +102,8 @@ public Host findDefaultHost(User user, boolean respectFrontendRoles) throws DotS Host site; try{ - site = (this.hostCache.getDefaultHost()!=null) ? this.hostCache.getDefaultHost() : getOrCreateDefaultHost(); + site = (this.hostCache.getDefaultHost(respectFrontendRoles)!=null) ? + this.hostCache.getDefaultHost(respectFrontendRoles) : getOrCreateDefaultHost(respectFrontendRoles); APILocator.getPermissionAPI().checkPermission(site, PermissionLevel.READ, user); return site; @@ -140,7 +142,7 @@ public Host resolveHostName(String serverName, User user, boolean respectFronten public Optional resolveHostNameWithoutDefault(String serverName, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException { Host host; - final Host cachedHostByAlias = hostCache.getHostByAlias(serverName); + final Host cachedHostByAlias = hostCache.getHostByAlias(serverName, respectFrontendRoles); if (UtilMethods.isSet(() -> cachedHostByAlias.getIdentifier())) { if (HostCache.CACHE_404_HOST.equals(cachedHostByAlias.getIdentifier())) { return Optional.empty(); @@ -220,7 +222,7 @@ public Host findByName(final String siteName, * @return The {@link Host} object that matches the specified name. */ private Host findByNameNotDefault(final String siteName, final User user, final boolean respectFrontendRoles) { - final Host site = this.getHostFactory().bySiteName(siteName); + final Host site = this.getHostFactory().bySiteName(siteName, respectFrontendRoles); if (null != site) { try { checkSitePermission(user, respectFrontendRoles, site); @@ -253,7 +255,7 @@ public Optional findByIdOrKey(final String siteIdOrKey, final User user, @Override @CloseDBIfOpened public Host findByAlias(final String alias, final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { - final Host site = this.getHostFactory().byAlias(alias); + final Host site = this.getHostFactory().byAlias(alias, respectFrontendRoles); if (null != site) { try { checkSitePermission(user, respectFrontendRoles, site); @@ -298,7 +300,7 @@ public Host find(final String id, final User user, } Host site = null; - Host cachedSiteById = hostCache.getById(id); + Host cachedSiteById = hostCache.getById(id, respectFrontendRoles); if (UtilMethods.isSet(() -> cachedSiteById.getIdentifier())) { if (HostCache.CACHE_404_HOST.equals(cachedSiteById.getIdentifier())) { return null; @@ -355,7 +357,15 @@ public List findAllFromDB(final User user, final boolean respectFrontendRo @Override public List findAllFromDB(final User user, final boolean includeSystemHost, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { - return this.findPaginatedSitesFromDB(user, 0, 0, null, includeSystemHost, respectFrontendRoles); + return this.findAllFromDB(user, includeSystemHost, false, respectFrontendRoles); + } + + @Override + public List findAllFromDB(final User user, final boolean includeSystemHost, + final boolean retrieveLiveVersion, + final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { + return this.findPaginatedSitesFromDB(user, 0, 0, null, + includeSystemHost, retrieveLiveVersion, respectFrontendRoles); } /** @@ -380,7 +390,7 @@ public List findAllFromDB(final User user, final boolean includeSystemHost @CloseDBIfOpened private List findPaginatedSitesFromDB(final User user, final int limit, final int offset, final String sortBy, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { - return this.findPaginatedSitesFromDB(user, limit, offset, sortBy, true, respectFrontendRoles); + return this.findPaginatedSitesFromDB(user, limit, offset, sortBy, true, false, respectFrontendRoles); } /** @@ -395,32 +405,48 @@ private List findPaginatedSitesFromDB(final User user, final int limit, fi * @param sortBy Optional sorting criterion, as specified by the available columns in: {@link * com.dotmarketing.common.util.SQLUtil#ORDERBY_WHITELIST} * @param includeSystemHost If the System Host should be included in the result list, set to {@code true}. + * @param retrieveLiveVersion If the live version of the Site should be retrieved, set to {@code true}. * @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this * operation, set to {@code true}. Otherwise, set to {@code false}. - * * @return The list of {@link Host} objects. - * * @throws DotDataException An error occurred when accessing the data source. * @throws DotSecurityException The specified User does not have the required permissions to perform this * operation. */ private List findPaginatedSitesFromDB(final User user, final int limit, final int offset, final String sortBy, final boolean includeSystemHost, - final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { - final List siteList = this.getHostFactory().findAll(limit, offset, sortBy, includeSystemHost); + final boolean retrieveLiveVersion, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { + final List siteList = this.getHostFactory().findAll( + limit, offset, sortBy, includeSystemHost, retrieveLiveVersion); + return filterHostsByPermissions(user, includeSystemHost, respectFrontendRoles, siteList); + } + + /** + * Returns a list of hosts filtered by user read permissions. + * @param user The {@link User} performing this action. + * @param includeSystemHost If the System Host should be included in the result list, set to {@code true}. + * @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this + * operation, set to {@code true}. Otherwise, set to {@code false}. + * @param siteList The list of {@link Host} objects to be filtered. + * @return The list of {@link Host} objects filtered by user read permissions. + */ + private List filterHostsByPermissions(final User user, final boolean includeSystemHost, + final boolean respectFrontendRoles, final List siteList) { if (null != siteList && !siteList.isEmpty()) { return siteList.stream().filter(site -> { try { - if (site.isSystemHost() && !user.isAdmin()){ + if (site.isSystemHost() && !user.isAdmin()) { return includeSystemHost && - APILocator.getPermissionAPI().doesSystemHostHavePermissions(APILocator.systemHost(), user, respectFrontendRoles, Host.class.getCanonicalName()); + APILocator.getPermissionAPI().doesSystemHostHavePermissions( + APILocator.systemHost(), user, respectFrontendRoles, + Host.class.getCanonicalName()); } this.checkSitePermission(user, respectFrontendRoles, site); return true; } catch (final DotDataException | DotSecurityException e) { Logger.warn(this, String.format("An error occurred when checking permissions from User '%s' on " + "Site " + - "'%s': %s", user.getUserId(), site.getInode(), e.getMessage())); + "'%s': %s", user.getUserId(), site.getInode(), e.getMessage())); } return false; }).collect(Collectors.toList()); @@ -431,11 +457,17 @@ private List findPaginatedSitesFromDB(final User user, final int limit, fi @Override public List findAllFromCache(final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { - Set cachedSites = hostCache.getAllSites(); + Set cachedSites = hostCache.getAllSites(respectFrontendRoles); if(null == cachedSites){ - final List allFromDB = findAllFromDB(user, respectFrontendRoles); - hostCache.addAll(allFromDB); - cachedSites = hostCache.getAllSites(); + final List allFromDB = findAllFromDB(APILocator.systemUser(), + true, false,false); + final List allFromDBLive = findAllFromDB(APILocator.systemUser(), + true, true,false); + hostCache.addAll(allFromDB, allFromDBLive); + final List filteredSiteList = filterHostsByPermissions( + user, true, respectFrontendRoles, + new ArrayList<>(hostCache.getAllSites(respectFrontendRoles))); + cachedSites = filteredSiteList != null ? new HashSet<>(filteredSiteList) : new HashSet<>(); } return ImmutableList.copyOf(cachedSites); } @@ -484,6 +516,7 @@ public Host save(final Host hostToBeSaved, User user, boolean respectFrontendRol if(hostToBeSaved.isWorking() || hostToBeSaved.isLive()){ APILocator.getVersionableAPI().setLive(contentletHost); + Logger.info(this, "Host " + hostToBeSaved.getHostname() + " is now live"); } Host savedHost = new Host(contentletHost); @@ -704,8 +737,14 @@ private void sendUnArchiveSiteSystemEvent (final Contentlet contentlet) { } } + /** + * Retrieves the default site if exists or creates a new one if required. + * @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this + * operation, set to {@code true}. Otherwise, set to {@code false}. + * @return The default site. + */ @CloseDBIfOpened - private synchronized Host getOrCreateDefaultHost() throws DotDataException, DotSecurityException { + private synchronized Host getOrCreateDefaultHost(final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { final ContentType siteContentType = hostType(); final List fields = siteContentType.fields(); @@ -716,7 +755,8 @@ private synchronized Host getOrCreateDefaultHost() throws DotDataException, DotS Logger.error(HostAPIImpl.class, message); throw new DotDataException(message); } - final Optional defaultHostOpt = this.getHostFactory().findDefaultHost(siteContentType.inode(), defaultField.get().dbColumn()); + final Optional defaultHostOpt = this.getHostFactory().findDefaultHost( + siteContentType.inode(), defaultField.get().dbColumn(), respectFrontendRoles); if (defaultHostOpt.isPresent()) { return defaultHostOpt.get(); } @@ -794,8 +834,8 @@ public Host DBSearch(String id, User user, boolean respectFrontendRoles) } @Override - public void updateCache(Host host) { - hostCache.remove(host); + public void updateCache(Host host) throws DotDataException, DotSecurityException { + hostCache.remove(host, false); hostCache.clearAliasCache(); hostCache.add(new Host(host)); } diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCache.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCache.java index 780970871eae..5522427f7619 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCache.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCache.java @@ -2,12 +2,16 @@ import com.dotmarketing.beans.Host; import com.dotmarketing.business.Cachable; +import com.dotmarketing.exception.DotDataException; +import com.dotmarketing.exception.DotSecurityException; + import java.util.Set; //This interface should have default package access public abstract class HostCache implements Cachable{ protected static String PRIMARY_GROUP = "HostCache"; protected static String ALIAS_GROUP = "HostAliasCache"; + protected static String HOST_LIVE_GROUP = "HostLiveCache"; protected static String NOT_FOUND_BY_ID_GROUP = "Host404ByIdCache"; protected static String NOT_FOUND_BY_NAME_GROUP = "Host404ByNameCache"; @@ -20,23 +24,25 @@ public String getIdentifier() { } }; - abstract protected Host add(Host host); + abstract protected Host add(Host host) throws DotDataException, DotSecurityException; - abstract protected Host getById(String id); + abstract protected Host getById(String id, boolean retrieveLiveVersion); - abstract protected Host getByName(String name); + abstract protected Host getByName(String name, boolean retrieveLiveVersion); abstract public void clearCache(); abstract protected void remove(Host host); - abstract protected void addAll(final Iterable hosts); + abstract protected void remove(Host host, boolean removeLiveVersion); + + abstract protected void addAll(Iterable hosts, Iterable liveHosts) throws DotDataException, DotSecurityException; - abstract protected Set getAllSites(); + abstract protected Set getAllSites(boolean retrieveLiveVersion); - abstract protected Host getDefaultHost(); + abstract protected Host getDefaultHost(boolean retrieveLiveVersion); - abstract protected Host getHostByAlias(String alias); + abstract protected Host getHostByAlias(String alias, boolean retrieveLiveVersion); abstract protected void addHostAlias(String alias, Host host); diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCacheImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCacheImpl.java index 38d7d363e2c0..d106ca4e1328 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCacheImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostCacheImpl.java @@ -4,9 +4,13 @@ import com.dotmarketing.business.CacheLocator; import com.dotmarketing.business.DotCacheAdministrator; import com.dotmarketing.business.DotCacheException; +import com.dotmarketing.exception.DotDataException; +import com.dotmarketing.exception.DotSecurityException; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; import com.google.common.collect.ImmutableSet; + +import java.util.HashSet; import java.util.Set; import org.apache.commons.lang.StringUtils; @@ -24,7 +28,8 @@ public class HostCacheImpl extends HostCache { // region's name for the cache private final String[] groupNames = { - PRIMARY_GROUP, ALIAS_GROUP, NOT_FOUND_BY_ID_GROUP, NOT_FOUND_BY_NAME_GROUP + PRIMARY_GROUP, ALIAS_GROUP, HOST_LIVE_GROUP, + NOT_FOUND_BY_ID_GROUP, NOT_FOUND_BY_NAME_GROUP }; public HostCacheImpl() { @@ -32,7 +37,7 @@ public HostCacheImpl() { } @Override - protected Host add(Host host) { + protected Host add(final Host host) throws DotDataException, DotSecurityException { if(host == null){ return null; } @@ -43,12 +48,26 @@ protected Host add(Host host) { cache.remove(key2, NOT_FOUND_BY_NAME_GROUP); // Add the key to the cache - cache.put(key, host,PRIMARY_GROUP); - cache.put(key2, host,PRIMARY_GROUP); - + final boolean isLive = host.isLive(); + final boolean onlyLiveVersion = isLive && !host.isWorking(); + + if (!onlyLiveVersion) { + cache.put(key, host, PRIMARY_GROUP); + cache.put(key2, host, PRIMARY_GROUP); + } + + if (isLive) { + cache.put(key, host, HOST_LIVE_GROUP); + cache.put(key2, host, HOST_LIVE_GROUP); + } + if(host.isDefault()){ - String key3 =DEFAULT_HOST; - cache.put(key3,host,PRIMARY_GROUP); + if (!onlyLiveVersion) { + cache.put(DEFAULT_HOST, host, PRIMARY_GROUP); + } + if (isLive) { + cache.put(DEFAULT_HOST, host, HOST_LIVE_GROUP); + } } @@ -56,31 +75,59 @@ protected Host add(Host host) { } - protected void addAll(final Iterable hosts){ - for(final Host host:hosts){ + /** + * Add all the hosts to the cache + * @param hosts the hosts to add to the cache (for hosts with working and live versions + * this list should contain only the working version) + * @param liveHosts the live hosts to add to the cache (for hosts with working and live versions + * this list should contain only the live version) + * @throws DotDataException An error occurred when accessing the data source. + * @throws DotSecurityException The specified user does not have the required permissions + * to perform this operation + */ + protected void addAll(final Iterable hosts, + final Iterable liveHosts) throws DotDataException, DotSecurityException { + + final Set alreadyCached = new HashSet<>(); + for(final Host host : hosts){ add(host); + alreadyCached.add(host.getInode()); } + for (final Host host : liveHosts) { + if (!alreadyCached.contains(host.getInode())) { + add(host); + } + } cache.put(SITES, ImmutableSet.copyOf(hosts), PRIMARY_GROUP); + cache.put(SITES, ImmutableSet.copyOf(liveHosts), HOST_LIVE_GROUP); + } - protected Set getAllSites(){ - return (Set) cache.getNoThrow(SITES, PRIMARY_GROUP); + /** + * Get all the sites + * @param retrieveLiveVersion if true, get the live version of the sites (if available) + * @return all the sites + */ + protected Set getAllSites(boolean retrieveLiveVersion){ + return (Set) cache.getNoThrow(SITES, + retrieveLiveVersion ? HOST_LIVE_GROUP : PRIMARY_GROUP); } private void clearSitesList(){ cache.remove(SITES, PRIMARY_GROUP); + cache.remove(SITES, HOST_LIVE_GROUP); } - protected Host getHostByAlias(String key) { + protected Host getHostByAlias(final String key, final boolean retrieveLiveVersion) { Host host = null; try{ String hostId = (String) cache.get(key,ALIAS_GROUP); if (CACHE_404_HOST.equals(hostId)) { return cache404Contentlet; } - host = get(hostId, NOT_FOUND_BY_ID_GROUP); + host = get(hostId, NOT_FOUND_BY_ID_GROUP, retrieveLiveVersion); if(host == null){ cache.remove(key, ALIAS_GROUP); } @@ -91,18 +138,23 @@ protected Host getHostByAlias(String key) { return host; } - private Host get(String key, String notFoundCacheGroup) { + private Host get(final String key, final String notFoundCacheGroup, final boolean retrieveLiveVersion) { Host host = null; - try{ + try { if (UtilMethods.isSet(notFoundCacheGroup)) { host = (Host) cache.get(key, notFoundCacheGroup); } if (host != null && CACHE_404_HOST.equals(host.getIdentifier())) { return cache404Contentlet; - } else { - host = (Host) cache.get(key, PRIMARY_GROUP); } - }catch (DotCacheException e) { + host = (Host) cache.get(key, PRIMARY_GROUP); + if (host == null || retrieveLiveVersion) { + final Host liveHost = (Host) cache.get(key, HOST_LIVE_GROUP); + if (liveHost != null) { + return liveHost; + } + } + } catch (DotCacheException e) { Logger.debug(this, "Cache Entry not found", e); } @@ -111,24 +163,28 @@ private Host get(String key, String notFoundCacheGroup) { /** * Get a host by id - * @param id the id of the host + * + * @param id the id of the host + * @param retrieveLiveVersion * @return the host or 404 if the host is in the not found cache, * null if the host is not found in the cache */ @Override - protected Host getById(final String id) { - return get(id, NOT_FOUND_BY_ID_GROUP); + protected Host getById(final String id, final boolean retrieveLiveVersion) { + return get(id, NOT_FOUND_BY_ID_GROUP, retrieveLiveVersion); } /** * Get a host by name - * @param name the name of the host + * + * @param name the name of the host + * @param retrieveLiveVersion * @return the host or 404 if the host is in the not found cache, * null if the host is not found in the cache */ @Override - protected Host getByName(final String name) { - return get(name, NOT_FOUND_BY_NAME_GROUP); + protected Host getByName(final String name, boolean retrieveLiveVersion) { + return get(name, NOT_FOUND_BY_NAME_GROUP, retrieveLiveVersion); } /* (non-Javadoc) @@ -138,6 +194,7 @@ public void clearCache() { // clear the cache cache.flushGroup(PRIMARY_GROUP); cache.flushGroup(ALIAS_GROUP); + cache.flushGroup(HOST_LIVE_GROUP); cache.flushGroup(NOT_FOUND_BY_ID_GROUP); cache.flushGroup(NOT_FOUND_BY_NAME_GROUP); } @@ -145,37 +202,54 @@ public void clearCache() { /* (non-Javadoc) * @see com.dotmarketing.business.PermissionCache#remove(java.lang.String) */ - protected void remove(Host host){ + protected void remove(final Host host){ + remove(host, true); + } + + /** + * Remove a host from the cache + * @param host the host to remove + * @param removeLiveVersion if true, remove the live version of the host + */ + @Override + protected void remove(final Host host, final boolean removeLiveVersion) { + if(host == null || StringUtils.isBlank(host.getIdentifier())){ return; } - // always remove default host - String _defaultHost =PRIMARY_GROUP +DEFAULT_HOST; - cache.remove(_defaultHost,PRIMARY_GROUP); + // always remove default host + String _defaultHost =PRIMARY_GROUP +DEFAULT_HOST; + cache.remove(_defaultHost,PRIMARY_GROUP); - String key = host.getIdentifier(); - String key2 = host.getHostname(); + String key = host.getIdentifier(); + String key2 = host.getHostname(); - try{ - cache.remove(key,PRIMARY_GROUP); + try{ + cache.remove(key,PRIMARY_GROUP); + if (removeLiveVersion) { + cache.remove(key,HOST_LIVE_GROUP); + } cache.remove(key,NOT_FOUND_BY_ID_GROUP); - }catch (Exception e) { + }catch (Exception e) { Logger.debug(this, "Cache not able to be removed", e); } - try{ - cache.remove(key2,PRIMARY_GROUP); - cache.remove(key2, NOT_FOUND_BY_NAME_GROUP); - }catch (Exception e) { + try{ + cache.remove(key2,PRIMARY_GROUP); + if (removeLiveVersion) { + cache.remove(key2,HOST_LIVE_GROUP); + } + cache.remove(key2,NOT_FOUND_BY_NAME_GROUP); + }catch (Exception e) { Logger.debug(this, "Cache not able to be removed", e); - } + } - clearAliasCache(); - clearSitesList(); + clearAliasCache(); + clearSitesList(); - } + } - public String[] getGroups() { + public String[] getGroups() { return groupNames; } public String getPrimaryGroup() { @@ -183,8 +257,8 @@ public String getPrimaryGroup() { } - protected Host getDefaultHost(){ - return get(DEFAULT_HOST, null); + protected Host getDefaultHost(final boolean retrieveLiveVersion){ + return get(DEFAULT_HOST, null, retrieveLiveVersion); } protected void addHostAlias(String alias, Host host){ diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactory.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactory.java index addf4da4543c..747e897ee5b0 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactory.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactory.java @@ -27,11 +27,11 @@ public interface HostFactory { * Finds a Site in the repository, based on its name. If it cannot be found or if an error ocurred, the "default" * Site will be returned instead. * - * @param siteName The name of the Site - * + * @param siteName The name of the Site + * @param retrieveLiveVersion * @return The Site with the specified name. */ - Host bySiteName(final String siteName); + Host bySiteName(final String siteName, boolean retrieveLiveVersion); /** * Returns the Site that matches the specified alias. Depending on the existing data, the result may vary: @@ -46,11 +46,11 @@ public interface HostFactory { * * * - * @param alias The alias of the Site. - * + * @param alias The alias of the Site. + * @param retrieveLiveVersion * @return The {@link Host} object matching the alias, the "default" Site, or the first Site from the result set. */ - Host byAlias(final String alias); + Host byAlias(final String alias, boolean retrieveLiveVersion); /** * Returns the list of Sites in your dotCMS repository retrieved directly from the data source matching the @@ -104,6 +104,29 @@ public interface HostFactory { List findAll(final int limit, final int offset, final String orderBy, final boolean includeSystemHost) throws DotDataException, DotSecurityException; + /** + * Returns the list of Sites in your dotCMS repository retrieved directly from the data source matching the + * specified search criteria. This method also allows you to retrieve the live version of the Site, if it exists. + * + * @param limit Limit of results returned in the response, for pagination purposes. If set equal or + * lower than zero, this parameter will be ignored. + * @param offset Expected offset of results in the response, for pagination purposes. If set equal or + * lower than zero, this parameter will be ignored. + * @param orderBy Optional sorting criterion, as specified by the available columns in: {@link + * com.dotmarketing.common.util.SQLUtil#ORDERBY_WHITELIST} . + * @param includeSystemHost If the System Host should be included in the results, set to {@code true}. + * @param retrieveLiveVersion If the live version of the Site should be retrieved, set to {@code true}. + + * @return The list of {@link Host} objects. + * + * @throws DotDataException An error occurred when accessing the data source. + * @throws DotSecurityException The specified User does not have the required permissions to perform this + * operation. + */ + List findAll(final int limit, final int offset, final String orderBy, + final boolean includeSystemHost, final boolean retrieveLiveVersion) + throws DotDataException, DotSecurityException; + /** * Retrieves the System Host object. * @@ -165,17 +188,17 @@ Optional> delete(final Host site, final User deletingUser, final /** * Returns the "default" Site in the current data repository. * - * @param contentTypeId Content Type Inode of the current "Host" type. - * @param columnName For non-JSON databases, the name of the database column that determines whether a Site is - * the default one or not. - * + * @param contentTypeId Content Type Inode of the current "Host" type. + * @param columnName For non-JSON databases, the name of the database column that determines whether a Site is + * the default one or not. + * @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this + * operation, set to {@code true}. Otherwise, set to {@code false}. * @return The Site with the specified name, or the "default" Site. - * * @throws DotDataException An error occurred when accessing the data source. * @throws DotSecurityException The specified User does not have the required permissions to perform this * operation. */ - Optional findDefaultHost(final String contentTypeId, final String columnName) throws DotDataException, DotSecurityException; + Optional findDefaultHost(final String contentTypeId, final String columnName, boolean respectFrontendRoles) throws DotDataException, DotSecurityException; /** * Returns all live Sites in dotCMS. diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImpl.java index dcad50fc2783..2abf97e92462 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImpl.java @@ -48,6 +48,7 @@ import io.vavr.control.Try; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -75,6 +76,7 @@ public class HostFactoryImpl implements HostFactory { private final String WHERE = " WHERE "; private final String AND = " AND "; + private final String OR = " OR "; private final String ORDER_BY = " ORDER BY ? "; private static final String SELECT_SYSTEM_HOST = "SELECT id FROM identifier WHERE id = '"+ Host.SYSTEM_HOST+"' "; @@ -82,36 +84,39 @@ public class HostFactoryImpl implements HostFactory { private static final String FROM_JOINED_TABLES = "INNER JOIN identifier i " + "ON c.identifier = i.id AND i.asset_subtype = '" + Host.HOST_VELOCITY_VAR_NAME + "' " + "INNER JOIN contentlet_version_info cvi " + - "ON c.inode = cvi.working_inode "; + "ON c.inode = cvi.working_inode " + + "LEFT JOIN contentlet clive " + + "ON clive.inode = cvi.live_inode "; - private static final String SELECT_SITE_INODE = "SELECT c.inode FROM contentlet" + - " c " + + private static final String SELECT_SITE_INODE = + "SELECT c.inode, cvi.live_inode FROM contentlet c " + FROM_JOINED_TABLES; - private static final String SELECT_SITE_INODE_AND_ALIASES = "SELECT c.inode, %s" + - " AS aliases FROM contentlet c " + + private static final String SELECT_SITE_INODE_AND_ALIASES = + "SELECT c.inode, cvi.live_inode, " + + "%s AS aliases, %s AS live_aliases FROM contentlet c " + FROM_JOINED_TABLES; - private static final String POSTGRES_ALIASES_COLUMN = ContentletJsonAPI - .CONTENTLET_AS_JSON + + private static final String POSTGRES_ALIASES_COLUMN = "%s." + + ContentletJsonAPI.CONTENTLET_AS_JSON + "->'fields'->'" + Host.ALIASES_KEY + "'->>'value' "; - private static final String MSSQL_ALIASES_COLUMN = "JSON_VALUE(c." + + private static final String MSSQL_ALIASES_COLUMN = "JSON_VALUE(%s." + ContentletJsonAPI.CONTENTLET_AS_JSON + ", '$.fields." + Host.ALIASES_KEY + ".value') "; - private static final String ALIASES_COLUMN = "c.text_area1"; + private static final String ALIASES_COLUMN = "%s.text_area1"; private static final String SITE_NAME_LIKE = "LOWER(%s) LIKE ? "; private static final String SITE_NAME_EQUALS ="LOWER(%s) = ? "; - private static final String POSTGRES_SITENAME_COLUMN = ContentletJsonAPI - .CONTENTLET_AS_JSON + + private static final String POSTGRES_SITENAME_COLUMN = "%s." + + ContentletJsonAPI.CONTENTLET_AS_JSON + "->'fields'->'" + Host.HOST_NAME_KEY + "'->>'value' "; - private static final String MSSQL_SITENAME_COLUMN = "JSON_VALUE(c." + + private static final String MSSQL_SITENAME_COLUMN = "JSON_VALUE(%s." + ContentletJsonAPI.CONTENTLET_AS_JSON + ", '$.fields." + Host.HOST_NAME_KEY + ".value')" + " "; - private static final String SITENAME_COLUMN = "c.text1"; + private static final String SITENAME_COLUMN = "%s.text1"; private static final String ALIAS_LIKE = "LOWER(%s) LIKE ? "; @@ -138,7 +143,7 @@ public class HostFactoryImpl implements HostFactory { // query that Exact matches should be at the top of the search results. private static final String PRIORITIZE_EXACT_MATCHES = - " ORDER BY length(%s), " + POSTGRES_SITENAME_COLUMN; + " ORDER BY length(%s), %s "; /** * Default class constructor. @@ -188,9 +193,9 @@ protected ContentletAPI getContentletAPI() { } @Override - public Host bySiteName(final String siteName) { + public Host bySiteName(final String siteName, boolean retrieveLiveVersion) { Host site; - final Host cachedSiteByName = siteCache.getByName(siteName);; + final Host cachedSiteByName = siteCache.getByName(siteName, retrieveLiveVersion); if (UtilMethods.isSet(() -> cachedSiteByName.getIdentifier())) { if (HostCache.CACHE_404_HOST.equals(cachedSiteByName.getIdentifier())) { return null; @@ -199,10 +204,13 @@ public Host bySiteName(final String siteName) { } else { final DotConnect dc = new DotConnect(); final StringBuilder sqlQuery = new StringBuilder().append(SELECT_SITE_INODE) - .append(WHERE); - sqlQuery.append(getSiteNameColumn(SITE_NAME_EQUALS)); + .append(WHERE) + .append(getSiteNameOrAliasColumn(SITE_NAME_EQUALS, true, "c")) + .append(OR) + .append(getSiteNameOrAliasColumn(SITE_NAME_EQUALS, true, "clive")); dc.setSQL(sqlQuery.toString()); dc.addParam(siteName.toLowerCase()); + dc.addParam(siteName.toLowerCase()); try { final List> dbResults = dc.loadResults(); if (dbResults.isEmpty()) { @@ -223,6 +231,18 @@ public Host bySiteName(final String siteName) { final Contentlet siteAsContentlet = this.contentFactory.find(siteInode); site = new Host(siteAsContentlet); this.siteCache.add(site); + + if (UtilMethods.isSet(() -> dbResults.get(0).get("live_inode"))) { + final String liveInode = dbResults.get(0).get("live_inode"); + if (!siteInode.equals(liveInode)) { + final Host liveHost = new Host(this.contentFactory.find( + dbResults.get(0).get("live_inode"))); + this.siteCache.add(liveHost); + if (retrieveLiveVersion) { + site = liveHost; + } + } + } } catch (final Exception e) { final String errorMsg = String.format("An error occurred when retrieving Site by name '%s': %s", siteName, e.getMessage()); @@ -233,9 +253,9 @@ public Host bySiteName(final String siteName) { } @Override - public Host byAlias(String alias) { + public Host byAlias(String alias, boolean retrieveLiveVersion) { Host site = null; - Host cachedSiteByAlias = this.siteCache.getHostByAlias(alias); + Host cachedSiteByAlias = this.siteCache.getHostByAlias(alias, retrieveLiveVersion); if (UtilMethods.isSet(() -> cachedSiteByAlias.getIdentifier())) { if (HostCache.CACHE_404_HOST.equals(cachedSiteByAlias.getIdentifier())) { return null; @@ -243,20 +263,14 @@ public Host byAlias(String alias) { site = cachedSiteByAlias; } else { final DotConnect dc = new DotConnect(); - final StringBuilder sqlQuery = new StringBuilder().append(SELECT_SITE_INODE_AND_ALIASES) + final StringBuilder sqlQuery = new StringBuilder() + .append(getSiteNameOrAliasColumn(SELECT_SITE_INODE_AND_ALIASES, false, "c", "clive")) .append(WHERE) - .append(ALIAS_LIKE); - String sql = sqlQuery.toString(); - if (APILocator.getContentletJsonAPI().isJsonSupportedDatabase()) { - if (DbConnectionFactory.isPostgres()) { - sql = String.format(sql, POSTGRES_ALIASES_COLUMN, POSTGRES_ALIASES_COLUMN); - } else { - sql = String.format(sql, MSSQL_ALIASES_COLUMN, MSSQL_ALIASES_COLUMN); - } - } else { - sql = String.format(sql, ALIASES_COLUMN, ALIASES_COLUMN); - } - dc.setSQL(sql); + .append(getSiteNameOrAliasColumn(ALIAS_LIKE, false, "c")) + .append(OR) + .append(getSiteNameOrAliasColumn(ALIAS_LIKE, false, "clive")); + dc.setSQL(sqlQuery.toString()); + dc.addParam("%" + alias.toLowerCase() + "%"); dc.addParam("%" + alias.toLowerCase() + "%"); try { final List> dbResults = dc.loadResults(); @@ -264,42 +278,62 @@ public Host byAlias(String alias) { siteCache.addHostAlias(alias, HostCache.cache404Contentlet); return null; } - if (dbResults.size() == 1) { - final Set siteAliases = new HashSet<>(parseSiteAliases(dbResults.get(0).get("aliases"))); + + final List siteAsContentletList = new ArrayList<>(); + final Map liveSiteMap = new HashMap<>(); + for (final Map siteInfo : dbResults) { + final Set siteAliases = new HashSet<>(parseSiteAliases(siteInfo.get("aliases"))); + if (UtilMethods.isSet(() -> siteInfo.get("live_aliases"))) { + siteAliases.addAll(parseSiteAliases(siteInfo.get("live_aliases"))); + } if (siteAliases.contains(alias)) { - site = new Host(this.getContentletFactory().find(dbResults.get(0).get("inode"))); + final String siteInode = siteInfo.get("inode"); + siteAsContentletList.add(this.getContentletFactory().find(siteInode)); + if (UtilMethods.isSet(() -> siteInfo.get("live_inode"))) { + final String liveInode = siteInfo.get("live_inode"); + if (!siteInode.equals(liveInode)) { + liveSiteMap.put(siteInode, this.getContentletFactory().find(liveInode)); + } + } } + } + if (siteAsContentletList.isEmpty()) { + siteCache.addHostAlias(alias, HostCache.cache404Contentlet); + return null; + } else if (siteAsContentletList.size() == 1) { + site = new Host(siteAsContentletList.get(0)); } else { - final List siteAsContentletList = new ArrayList<>(); - for (final Map siteInfo : dbResults) { - final Set siteAliases = new HashSet<>(parseSiteAliases(siteInfo.get("aliases"))); - if (siteAliases.contains(alias)) { - siteAsContentletList.add(this.getContentletFactory().find(siteInfo.get("inode"))); + for (final Contentlet siteAsContentlet : siteAsContentletList) { + if (Boolean.class.cast(siteAsContentlet.get(Host.IS_DEFAULT_KEY))) { + site = new Host(siteAsContentlet); + break; } } - if (siteAsContentletList.size() == 1) { + if (null == site) { site = new Host(siteAsContentletList.get(0)); - } else { - for (final Contentlet siteAsContentlet : siteAsContentletList) { - if (Boolean.class.cast(siteAsContentlet.get(Host.IS_DEFAULT_KEY))) { - site = new Host(siteAsContentlet); - break; - } - } - if (null == site) { - site = new Host(siteAsContentletList.get(0)); - } - final StringBuilder warningMsg = new StringBuilder().append("ERROR: ").append(siteAsContentletList.size()) - .append(" Sites have the same alias '").append(alias).append("':\n"); - for (final Contentlet siteAsContentlet : siteAsContentletList) { - warningMsg.append("-> Inode = ").append(siteAsContentlet.getInode()).append("\n"); - } - warningMsg.append("Defaulting to Site '").append(site.getInode()).append("'"); - Logger.fatal(this, warningMsg.toString()); } + final StringBuilder warningMsg = new StringBuilder().append("ERROR: ").append(siteAsContentletList.size()) + .append(" Sites have the same alias '").append(alias).append("':\n"); + for (final Contentlet siteAsContentlet : siteAsContentletList) { + warningMsg.append("-> Inode = ").append(siteAsContentlet.getInode()).append("\n"); + } + warningMsg.append("Defaulting to Site '").append(site.getInode()).append("'"); + Logger.fatal(this, warningMsg.toString()); } + this.siteCache.add(site); this.siteCache.addHostAlias(alias, site); + + if (!liveSiteMap.isEmpty()) { + final String siteInode = site.getInode(); + if (liveSiteMap.containsKey(siteInode)) { + final Host liveHost = new Host(liveSiteMap.get(siteInode)); + this.siteCache.add(liveHost); + if (retrieveLiveVersion) { + site = liveHost; + } + } + } } catch (final Exception e) { throw new DotRuntimeException(String.format("An error occurred when retrieving Site with alias '%s': " + "%s", alias, e.getMessage()), e); @@ -318,11 +352,18 @@ public List findAll(final int limit, final int offset, final String orderB return findAll(limit,offset, orderBy, true); } - @CloseDBIfOpened @Override public List findAll(final int limit, final int offset, final String orderBy, final boolean includeSystemHost) throws DotDataException, DotSecurityException { + return findAll(limit, offset, orderBy, includeSystemHost, false); + } + + @CloseDBIfOpened + @Override + public List findAll(final int limit, final int offset, final String orderBy, + final boolean includeSystemHost, final boolean retrieveLiveVersion) throws DotDataException, DotSecurityException { final DotConnect dc = new DotConnect(); - final StringBuilder sqlQuery = new StringBuilder().append(SELECT_SITE_INODE) + final StringBuilder sqlQuery = new StringBuilder() + .append(SELECT_SITE_INODE) .append(WHERE) .append(" true "); if (!includeSystemHost) { @@ -344,12 +385,12 @@ public List findAll(final int limit, final int offset, final String orderB dc.setStartRow(offset); } final List> dbResults = dc.loadResults(); - return this.convertDbResultsToSites(dbResults); + return this.convertDbResultsToSites(dbResults, retrieveLiveVersion); } @Override public Host findSystemHost(final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { - Host systemHost = this.siteCache.getById(Host.SYSTEM_HOST); + Host systemHost = this.siteCache.getById(Host.SYSTEM_HOST, false); if (null != systemHost) { return systemHost; } @@ -421,6 +462,18 @@ public Host DBSearch(final String id, final boolean respectFrontendRoles) throws if (siteAsContentlet.getContentType().id().equals(hostContentType.inode())) { site = new Host(siteAsContentlet); this.siteCache.add(site); + + if (UtilMethods.isSet(versionInfo::getLiveInode)) { + final String liveInode = versionInfo.getLiveInode(); + if (!siteInode.equals(liveInode)) { + final Host liveHost = new Host(this.getContentletAPI().find( + liveInode, systemUser, respectFrontendRoles)); + this.siteCache.add(liveHost); + if (respectFrontendRoles) { + site = liveHost; + } + } + } } } if (null == site && !Host.SYSTEM_HOST.equals(id)) { @@ -595,8 +648,11 @@ public void deleteSite(final Host site, final User user, final boolean respectFr "---> (Step 6/%d) Content Type '%s' cannot be deleted, and must be moved to SYSTEM_HOST", steps, type.variable())); //If we can not delete it we need to change the host to SYSTEM_HOST - final ContentType clonedContentType = ContentTypeBuilder.builder(type) - .host(findSystemHost(user, false).getIdentifier()).build(); + final ContentType clonedContentType = ContentTypeBuilder.builder(type).from(type) + .folder(Folder.SYSTEM_FOLDER) + .folderPath(Folder.SYSTEM_FOLDER_PATH) + .host(Host.SYSTEM_HOST) + .siteName(Host.SYSTEM_HOST_NAME).build(); contentTypeAPI.save(clonedContentType); } } @@ -706,26 +762,28 @@ public Boolean call() { } @Override - public Optional findDefaultHost(final String contentTypeId, final String columnName) throws DotDataException, DotSecurityException { - Host defaultHost = this.siteCache.getDefaultHost(); + public Optional findDefaultHost(final String contentTypeId, final String columnName, + final boolean respectFrontendRoles) throws DotDataException, DotSecurityException { + Host defaultHost = this.siteCache.getDefaultHost(respectFrontendRoles); if (null != defaultHost) { return Optional.of(defaultHost); } final DotConnect dotConnect = new DotConnect(); - String inode = null; + String workingInode = null; + String liveInode = null; if (APILocator.getContentletJsonAPI().isPersistContentAsJson()) { String sql = null; if(DbConnectionFactory.isPostgres()) { - sql = "SELECT cvi.working_inode\n" + sql = "SELECT cvi.working_inode,cvi.live_inode\n" + " FROM contentlet_version_info cvi join contentlet c on (c.inode = cvi.working_inode) \n" + " WHERE c.contentlet_as_json @> '{ \"fields\":{\"isDefault\":{ \"value\":true }} }' \n" + " and c.structure_inode = ?"; } if(DbConnectionFactory.isMsSql()){ - sql = "SELECT cvi.working_inode\n" + sql = "SELECT cvi.working_inode,cvi.live_inode\n" + " FROM contentlet_version_info cvi join contentlet c on (c.inode = cvi.working_inode) \n" + " WHERE JSON_VALUE(c.contentlet_as_json, '$.fields.isDefault.value') = 'true' \n" + " and c.structure_inode = ?"; @@ -735,23 +793,37 @@ public Optional findDefaultHost(final String contentTypeId, final String c } dotConnect.setSQL(sql); dotConnect.addParam(contentTypeId); - inode = Try.of(()->dotConnect.getString("working_inode")).onFailure(throwable -> { - Logger.warnAndDebug(HostAPIImpl.class,"An Error occurred while fetching the default host. ", throwable); + workingInode = Try.of(()->dotConnect.getString("working_inode")).onFailure(throwable -> { + Logger.warnAndDebug(HostAPIImpl.class,"An Error occurred while fetching the working inode for the default host. ", throwable); + }).getOrNull(); + liveInode = Try.of(()->dotConnect.getString("live_inode")).onFailure(throwable -> { + Logger.warnAndDebug(HostAPIImpl.class,"An Error occurred while fetching the live inode for the default host. ", throwable); }).getOrNull(); } - if (UtilMethods.isNotSet(inode)) { + if (UtilMethods.isNotSet(workingInode)) { dotConnect - .setSQL("select working_inode from contentlet_version_info join contentlet on (contentlet.inode = contentlet_version_info.working_inode) " + .setSQL("select working_inode,live_inode" + + " from contentlet_version_info join contentlet" + + " on (contentlet.inode = contentlet_version_info.working_inode) " + " where " + columnName + " = ? and structure_inode =?"); dotConnect.addParam(true); dotConnect.addParam(contentTypeId); - inode = dotConnect.getString("working_inode"); + workingInode = dotConnect.getString("working_inode"); + liveInode = dotConnect.getString("live_inode"); } - if (UtilMethods.isNotSet(inode)) { + if (UtilMethods.isNotSet(workingInode)) { return Optional.empty(); } - defaultHost = new Host(APILocator.getContentletAPI().find(inode, APILocator.systemUser(), false)); + defaultHost = new Host(APILocator.getContentletAPI().find(workingInode, APILocator.systemUser(), false)); this.siteCache.add(defaultHost); + if (UtilMethods.isSet(liveInode) && !workingInode.equals(liveInode)) { + final Host liveDefaultHost = new Host(APILocator.getContentletAPI().find( + liveInode, APILocator.systemUser(), false)); + this.siteCache.add(liveDefaultHost); + if (respectFrontendRoles) { + defaultHost = liveDefaultHost; + } + } return Optional.of(defaultHost); } @@ -866,7 +938,7 @@ protected Optional> search(final String siteNameFilter, final String sqlQuery.append("cvi.identifier = i.id"); if (UtilMethods.isSet(siteNameFilter)) { sqlQuery.append(AND); - sqlQuery.append(getSiteNameColumn(SITE_NAME_LIKE)); + sqlQuery.append(getSiteNameOrAliasColumn(SITE_NAME_LIKE, true, "c")); } if (UtilMethods.isSet(condition)) { sqlQuery.append(AND); @@ -877,7 +949,7 @@ protected Optional> search(final String siteNameFilter, final String sqlQuery.append(EXCLUDE_SYSTEM_HOST); } if (UtilMethods.isSet(siteNameFilter)) { - sqlQuery.append(getSiteNameColumn(PRIORITIZE_EXACT_MATCHES)); + sqlQuery.append(getSiteNameOrAliasColumn(PRIORITIZE_EXACT_MATCHES, true, "c", "c")); } dc.setSQL(sqlQuery.toString()); @@ -897,13 +969,15 @@ protected Optional> search(final String siteNameFilter, final String if (dbResults.isEmpty()) { return Optional.of(hostList); } - final List siteList = convertDbResultsToSites(dbResults); + final List siteList = convertDbResultsToSites(dbResults, respectFrontendRoles); if(user.isAdmin()){ return Optional.of(siteList); } hostList.addAll(APILocator.getPermissionAPI().filterCollection(siteList, PermissionAPI .PERMISSION_READ, respectFrontendRoles, user)); - hostList = hostList.stream().limit(limit).collect(Collectors.toList()); + if (limit > 0) { + hostList = hostList.stream().limit(limit).collect(Collectors.toList()); + } //We want to include the system host in the list of hosts final Host systemHost = APILocator.systemHost(); @@ -916,10 +990,11 @@ protected Optional> search(final String siteNameFilter, final String hostList.add(systemHost); } - if(hostList.size()==limit || siteList.size() < limit){//user is admin or reach the amount of sites requested or there is no anymore sites + if (limit == 0 || hostList.size() == limit || siteList.size() < limit) {//reached the amount of sites requested or there is no anymore sites return Optional.of(hostList); - }else{ - return search(siteNameFilter,condition,showSystemHost,limit,offset+limit,user,respectFrontendRoles,hostList); + } else { + return search(siteNameFilter, condition, showSystemHost, + limit,offset+limit, user, respectFrontendRoles, hostList); } } catch (final Exception e) { Logger.error(this, String.format("An error occurred when searching for Sites based on the following " + @@ -933,14 +1008,17 @@ protected Optional> search(final String siteNameFilter, final String /** * Utility method used to convert Site data from the data source into a list of Sites. * - * @param dbResults Data representing a Site. - * + * @param dbResults Data representing a Site. + * @param retrieveLiveVersion If the live version of the Site must be retrieved. * @return The list of {@link Host} objects. */ - private List convertDbResultsToSites(final List> dbResults) { + private List convertDbResultsToSites(final List> dbResults, + final boolean retrieveLiveVersion) { return dbResults.stream().map(siteData -> { try { - final Contentlet contentlet = this.getContentletFactory().find(siteData.get("inode")); + final String inode = retrieveLiveVersion && UtilMethods.isSet(() -> siteData.get("live_inode")) ? + siteData.get("live_inode") : siteData.get("inode"); + final Contentlet contentlet = this.getContentletFactory().find(inode); return new Host(contentlet); } catch (final DotDataException | DotSecurityException e) { Logger.warn(this, String.format("Contentlet with Inode '%s' could not be retrieved from Content " + @@ -969,25 +1047,34 @@ private List parseSiteAliases(final String aliases) { } /** - * Returns the appropriate column for the {@code Site Name} field depending on the database that dotCMS is running - * on. That is, if the value is inside the "Content as JSON" column, or the legacy "text" column. - * - * @param baseQuery The base SQL query whose column name will be replaced. + * Returns the appropriate column for the {@code Site Name} or {@code Alias} field depending + * on the database that dotCMS is running on. That is, if the value is inside + * the "Content as JSON" column, or the legacy "text" column. * - * @return The appropriate database column for the Site Name field. + * @param baseQuery The base SQL query whose column name will be replaced. + * @param getSiteColumn If the column is for the Site Name field, set to {@code true}. Otherwise, set to {@code false}. + * @param tableAliases The aliases of the tables where the Site Name or Alias fields are located. + * @return The appropriate database column for the Site Name or Alias field. */ - private static String getSiteNameColumn(final String baseQuery) { - String sql = baseQuery; - if (APILocator.getContentletJsonAPI().isJsonSupportedDatabase()) { - if (DbConnectionFactory.isPostgres()) { - sql = String.format(sql, POSTGRES_SITENAME_COLUMN); + private static String getSiteNameOrAliasColumn(final String baseQuery, + final boolean getSiteColumn, final String... tableAliases) { + + final Object [] fields = new Object[tableAliases.length]; + for (int i = 0; i < tableAliases.length; i++) { + if (APILocator.getContentletJsonAPI().isJsonSupportedDatabase()) { + if (DbConnectionFactory.isPostgres()) { + fields[i] = String.format(getSiteColumn ? + POSTGRES_SITENAME_COLUMN : POSTGRES_ALIASES_COLUMN, tableAliases[i]); + } else { + fields[i] = String.format(getSiteColumn ? + MSSQL_SITENAME_COLUMN : MSSQL_ALIASES_COLUMN, tableAliases[i]); + } } else { - sql = String.format(sql, MSSQL_SITENAME_COLUMN); + fields[i] = String.format(getSiteColumn ? + SITENAME_COLUMN : ALIASES_COLUMN, tableAliases[i]); } - } else { - sql = String.format(sql, SITENAME_COLUMN); } - return sql; + return String.format(baseQuery, fields); } /** diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/web/UpdateContainersPathsJob.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/web/UpdateContainersPathsJob.java index 98f886edf0af..e0bc0cc88c90 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/web/UpdateContainersPathsJob.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/business/web/UpdateContainersPathsJob.java @@ -90,7 +90,7 @@ public void run(final JobExecutionContext jobContext) throws JobExecutionExcepti Logger.debug(UpdateContainersPathsJob.class, () -> "Finished UpdateContainersPathsJob"); } - private void cleanCache(final List> templates, final Host host) throws DotDataException { + private void cleanCache(final List> templates, final Host host) throws DotDataException, DotSecurityException { APILocator.getHostAPI().updateCache(host); for (final Map template : templates) { diff --git a/dotcms-integration/src/test/java/com/dotcms/MainSuite1b.java b/dotcms-integration/src/test/java/com/dotcms/MainSuite1b.java index 44fb1c8ea2a1..6f9ee04ee22a 100644 --- a/dotcms-integration/src/test/java/com/dotcms/MainSuite1b.java +++ b/dotcms-integration/src/test/java/com/dotcms/MainSuite1b.java @@ -78,7 +78,8 @@ com.dotcms.analytics.metrics.QueryParameterValuesTransformerTest.class, QuartzUtilsTest.class, DotConnectTest.class, - com.dotcms.contenttype.model.field.layout.FieldUtilTest.class + com.dotcms.contenttype.model.field.layout.FieldUtilTest.class, + com.dotmarketing.portlets.contentlet.business.HostAPITest.class, }) public class MainSuite1b { diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostAPITest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostAPITest.java index 7b4e4561469b..27ed560ee831 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostAPITest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostAPITest.java @@ -25,15 +25,12 @@ import com.dotmarketing.business.FactoryLocator; import com.dotmarketing.business.PermissionAPI; import com.dotmarketing.business.Role; -import com.dotmarketing.business.RoleAPI; -import com.dotmarketing.business.UserAPI; import com.dotmarketing.business.VersionableAPI; import com.dotmarketing.common.db.DotConnect; import com.dotmarketing.db.HibernateUtil; import com.dotmarketing.exception.DotDataException; import com.dotmarketing.exception.DotHibernateException; import com.dotmarketing.exception.DotSecurityException; -import com.dotmarketing.init.DotInitScheduler; import com.dotmarketing.portlets.contentlet.model.Contentlet; import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo; import com.dotmarketing.portlets.contentlet.model.IndexPolicy; @@ -71,6 +68,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -89,9 +87,7 @@ public class HostAPITest extends IntegrationTestBase { public static void prepare() throws Exception { //Setting web app environment IntegrationTestInitService.getInstance().init(); - LicenseTestUtil.getLicense(); - DotInitScheduler.start(); } /** @@ -123,8 +119,6 @@ public void delete_host_with_system_content_type() throws Exception { @Test public void testDeleteHostCleanUpTemplates() throws Exception { - - final User user = APILocator.getUserAPI().getSystemUser(); final Host host = new SiteDataGen().nextPersisted(); @@ -199,77 +193,87 @@ public void testDeleteSiteCleanUpSiteVariables() throws Exception { public void testDeleteHost() throws Exception { User user = APILocator.getUserAPI().getSystemUser(); + Host sourceHost = null; - Host source = new SiteDataGen().nextPersisted(); - final ContentType blogContentType = TestDataUtils - .getBlogLikeContentType("Blog" + System.currentTimeMillis(), source); - final ContentType employeeContentType = TestDataUtils - .getEmployeeLikeContentType("Employee" + System.currentTimeMillis(), source); - final ContentType newsContentType = TestDataUtils - .getNewsLikeContentType("News" + System.currentTimeMillis(), source); - - TestDataUtils.getBlogContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), - blogContentType.id(), source); - TestDataUtils.getBlogContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), - blogContentType.id(), source); - TestDataUtils - .getEmployeeContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), - employeeContentType.id(), source); - TestDataUtils - .getEmployeeContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), - employeeContentType.id(), source); - TestDataUtils.getNewsContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), - newsContentType.id(), source); - TestDataUtils.getNewsContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), - newsContentType.id(), source); - - TestDataUtils.getGenericContentContent(true, - APILocator.getLanguageAPI().getDefaultLanguage().getId(), - source); - TestDataUtils.getGenericContentContent(true, - APILocator.getLanguageAPI().getDefaultLanguage().getId(), - source); - TestDataUtils.getGenericContentContent(true, - APILocator.getLanguageAPI().getDefaultLanguage().getId(), - source); - - //Create a new test host - Host host = createHost("copy" + System.currentTimeMillis() + ".demo.dotcms.com", user); - - Thread.sleep(5000); - String newHostIdentifier = host.getIdentifier(); - String newHostName = host.getHostname(); - - // mocking JobExecutionContext to execute HostAssetsJobProxy - final JobDetail jobDetail = mock(JobDetail.class); - final JobExecutionContext jobExecutionContext = mock(JobExecutionContext.class); - final Trigger trigger = mock(Trigger.class); - when(jobExecutionContext.getJobDetail()).thenReturn(jobDetail); - when(jobExecutionContext.getJobDetail().getName()) - .thenReturn("setup-host-" + host.getIdentifier()); - when(jobExecutionContext.getJobDetail().getGroup()).thenReturn("setup-host-group"); - - final Map dataMap = new HashMap<>(); - dataMap.put(HostAssetsJobProxy.USER_ID, user.getUserId()); - dataMap.put(HostAssetsJobProxy.SOURCE_HOST_ID, source.getIdentifier()); - dataMap.put(HostAssetsJobProxy.DESTINATION_HOST_ID, host.getIdentifier()); - dataMap.put(HostAssetsJobProxy.COPY_OPTIONS, new HostCopyOptions(true)); - when(jobExecutionContext.getTrigger()).thenReturn(trigger); - - HostAssetsJobProxy hostAssetsJobProxy = Mockito.spy(new HostAssetsJobProxy()); - doReturn(dataMap).when(hostAssetsJobProxy).getExecutionData(trigger); - hostAssetsJobProxy.execute(jobExecutionContext); - - Thread.sleep(600); // wait a bit for the index - - //Archive the just created host in order to be able to delete it - archiveHost(host, user); - - //Delete the just created host - deleteHost(host, user); - - //Make sure the host was deleted properly - hostDoesNotExistCheck(newHostIdentifier, newHostName, user); + try { + sourceHost = new SiteDataGen().nextPersisted(); + final ContentType blogContentType = TestDataUtils + .getBlogLikeContentType("Blog" + System.currentTimeMillis(), sourceHost); + final ContentType employeeContentType = TestDataUtils + .getEmployeeLikeContentType("Employee" + System.currentTimeMillis(), sourceHost); + final ContentType newsContentType = TestDataUtils + .getNewsLikeContentType("News" + System.currentTimeMillis(), sourceHost); + + TestDataUtils.getBlogContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), + blogContentType.id(), sourceHost); + TestDataUtils.getBlogContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), + blogContentType.id(), sourceHost); + TestDataUtils + .getEmployeeContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), + employeeContentType.id(), sourceHost); + TestDataUtils + .getEmployeeContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), + employeeContentType.id(), sourceHost); + TestDataUtils.getNewsContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), + newsContentType.id(), sourceHost); + TestDataUtils.getNewsContent(true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), + newsContentType.id(), sourceHost); + + TestDataUtils.getGenericContentContent(true, + APILocator.getLanguageAPI().getDefaultLanguage().getId(), + sourceHost); + TestDataUtils.getGenericContentContent(true, + APILocator.getLanguageAPI().getDefaultLanguage().getId(), + sourceHost); + TestDataUtils.getGenericContentContent(true, + APILocator.getLanguageAPI().getDefaultLanguage().getId(), + sourceHost); + + TestDataUtils.waitForEmptyQueue(); + + //Create a new test host + Host host = createHost("copy" + System.currentTimeMillis() + ".demo.dotcms.com", user); + String newHostIdentifier = host.getIdentifier(); + String newHostName = host.getHostname(); + + // mocking JobExecutionContext to execute HostAssetsJobProxy + final JobDetail jobDetail = mock(JobDetail.class); + final JobExecutionContext jobExecutionContext = mock(JobExecutionContext.class); + final Trigger trigger = mock(Trigger.class); + when(jobExecutionContext.getJobDetail()).thenReturn(jobDetail); + when(jobExecutionContext.getJobDetail().getName()) + .thenReturn("setup-host-" + host.getIdentifier()); + when(jobExecutionContext.getJobDetail().getGroup()).thenReturn("setup-host-group"); + + final Map dataMap = new HashMap<>(); + dataMap.put(HostAssetsJobProxy.USER_ID, user.getUserId()); + dataMap.put(HostAssetsJobProxy.SOURCE_HOST_ID, sourceHost.getIdentifier()); + dataMap.put(HostAssetsJobProxy.DESTINATION_HOST_ID, host.getIdentifier()); + dataMap.put(HostAssetsJobProxy.COPY_OPTIONS, new HostCopyOptions(true)); + when(jobExecutionContext.getTrigger()).thenReturn(trigger); + + HostAssetsJobProxy hostAssetsJobProxy = Mockito.spy(new HostAssetsJobProxy()); + doReturn(dataMap).when(hostAssetsJobProxy).getExecutionData(trigger); + hostAssetsJobProxy.execute(jobExecutionContext); + + TestDataUtils.waitForEmptyQueue(); // wait a bit for the index + + //Archive the just created host in order to be able to delete it + archiveHost(host, user); + + //Delete the just created host + deleteHost(host, user); + + //Make sure the host was deleted properly + hostDoesNotExistCheck(newHostIdentifier, newHostName, user); + } finally { + // Cleanup + if (sourceHost != null) { + unpublishHost(sourceHost, user); + archiveHost(sourceHost, user); + deleteHost(sourceHost, user); + } + } } @Test @@ -350,6 +354,7 @@ public void givenSearch_whenNewHost_thenFindsNewHost() throws Exception { .nextPersisted(); final String newHostName = "demo.test" + System.currentTimeMillis() + ".dotcms.com"; + //Create a new test host Host host = createHost(newHostName, user); final String newHostIdentifier = host.getIdentifier(); @@ -622,7 +627,7 @@ private void deleteHost(final Host host, final User user) } if (hostDeleteResult.isEmpty()) { - Thread.sleep(6000); // wait a bit for the index + TestDataUtils.waitForEmptyQueue(); // wait a bit for the index } else { hostDeleteResult.get().get(); } @@ -648,15 +653,34 @@ private void hostDoesNotExistCheck(final String identifier, final String name, f * Should return it */ @Test - public void shouldReturnExistingHost() throws DotSecurityException, DotDataException { - final Host host = new SiteDataGen().nextPersisted(); - final Role role = new RoleDataGen().nextPersisted(); - final User user = new UserDataGen().roles(role).nextPersisted(); + public void shouldReturnExistingHost() throws Exception { + Host host = null; + Role role = null; + User user = null; + try { + host = new SiteDataGen().nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); - this.addPermission(role, host); + this.addPermission(role, host); - final Host hostReturned = APILocator.getHostAPI().resolveHostNameWithoutDefault(host.getHostname(), user, false).get(); - assertEquals(host, hostReturned); + final Host hostReturned = APILocator.getHostAPI().resolveHostNameWithoutDefault(host.getHostname(), user, false).get(); + assertEquals(host, hostReturned); + } finally { + // Cleanup + final User systemUser = APILocator.systemUser(); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } } /** @@ -665,12 +689,31 @@ public void shouldReturnExistingHost() throws DotSecurityException, DotDataExcep * Should throw a {@link DotSecurityException} */ @Test(expected = DotSecurityException.class) - public void shouldThrowDotSecurityExceptionWhenUserNotHavePermission() throws DotSecurityException, DotDataException { - final Host host = new SiteDataGen().nextPersisted(); - final Role role = new RoleDataGen().nextPersisted(); - final User user = new UserDataGen().roles(role).nextPersisted(); + public void shouldThrowDotSecurityExceptionWhenUserNotHavePermission() throws Exception { + Host host = null; + Role role = null; + User user = null; + try { + host = new SiteDataGen().nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); - APILocator.getHostAPI().resolveHostNameWithoutDefault(host.getHostname(), user, false); + APILocator.getHostAPI().resolveHostNameWithoutDefault(host.getHostname(), user, false); + } finally { + // Cleanup + final User systemUser = APILocator.systemUser(); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } } /** @@ -680,7 +723,6 @@ public void shouldThrowDotSecurityExceptionWhenUserNotHavePermission() throws Do */ @Test public void shouldReturnNull() throws DotSecurityException, DotDataException { - final Optional optional = APILocator.getHostAPI().resolveHostNameWithoutDefault( "not_exists_host", APILocator.systemUser(), false); assertFalse(optional.isPresent()); @@ -689,20 +731,20 @@ public void shouldReturnNull() throws DotSecurityException, DotDataException { /** * Method to test: {@link HostAPI#resolveHostName(String, User, boolean)} * When the host does not exist - * Should return the default host and store it into host cache + * Should return the default host and store 404 into host cache */ @Test - public void shouldStoreDefaultHostIntoCache() throws DotSecurityException, DotDataException { + public void shouldReturnDefaultHostForNonExistingHost() throws DotSecurityException, DotDataException { final String hostName = "not_exists_host"; final Host notExistsHost = APILocator.getHostAPI().resolveHostName(hostName , APILocator.systemUser(), false); final Host defaultHost = APILocator.getHostAPI().findDefaultHost(APILocator.systemUser(), true); - assertEquals(notExistsHost.getIdentifier(), defaultHost.getIdentifier()); + assertEquals(defaultHost.getIdentifier(), notExistsHost.getIdentifier()); final HostCache hostCache = CacheLocator.getHostCache(); - final Host hostByAlias = hostCache.getHostByAlias(hostName); + final Host hostByAlias = hostCache.getHostByAlias(hostName, false); - assertEquals(hostByAlias.getIdentifier(), defaultHost.getIdentifier()); + assertEquals(HostCache.CACHE_404_HOST, hostByAlias.getIdentifier()); } /** @@ -711,13 +753,92 @@ public void shouldStoreDefaultHostIntoCache() throws DotSecurityException, DotDa * Should return the host */ @Test() - public void shouldReturnHostWhenRespectFrontendRolesIsTrue() throws DotSecurityException, DotDataException { - final Host host = new SiteDataGen().nextPersisted(); - final Role role = new RoleDataGen().nextPersisted(); - final User user = new UserDataGen().roles(role).nextPersisted(); + public void shouldReturnHostWhenRespectFrontendRolesIsTrue() throws Exception { + Host host = null; + Role role = null; + User user = null; + try { + host = new SiteDataGen().nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); - final Host hostReturned = APILocator.getHostAPI().resolveHostNameWithoutDefault(host.getHostname(), user, true).get(); - assertEquals(host, hostReturned); + final Host hostReturned = APILocator.getHostAPI().resolveHostNameWithoutDefault(host.getHostname(), user, true).get(); + assertEquals(host, hostReturned); + } finally { + // Cleanup + final User systemUser = APILocator.systemUser(); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } + } + + /** + * Method to test: {@link HostAPI#resolveHostNameWithoutDefault(String, User, boolean)} + * When a host has live and working versions and the user has a front end role + * Should return the live version + */ + @Test + public void shouldReturnLiveVersionWhenFrontendRolesIsTrue() throws Exception { + Host host = null; + Role role = null; + User user = null; + try { + host = new SiteDataGen().nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); + + // Create a working version + final Host workingHost = new Host(APILocator.getContentletAPI().checkout( + host.getInode(), APILocator.systemUser(), false)); + final String workingHostAlias = "working." + workingHost.getHostname(); + workingHost.setAliases(workingHostAlias); + final Host savedHost = new Host(APILocator.getContentletAPI().checkin( + workingHost, APILocator.systemUser(), false)); + assertNotEquals(host.getInode(), savedHost.getInode()); + + // Resolve host by name + final Optional optionalHost = APILocator.getHostAPI().resolveHostNameWithoutDefault( + host.getHostname(), user, true); + assertTrue(optionalHost.isPresent()); + final Host resolvedHost = optionalHost.get(); + + assertTrue(resolvedHost.isLive()); + assertNull(resolvedHost.getAliases()); + assertEquals(host, resolvedHost); + + // Resolve host by alias + final Optional optionalHostByAlias = APILocator.getHostAPI() + .resolveHostNameWithoutDefault(workingHostAlias, user, true); + assertTrue(optionalHostByAlias.isPresent()); + final Host resolvedHostByAlias = optionalHostByAlias.get(); + + assertTrue(resolvedHostByAlias.isLive()); + assertEquals(host, resolvedHostByAlias); + + } finally { + // Cleanup + final User systemUser = APILocator.systemUser(); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } } private void addPermission(final Role role, final Host host) @@ -740,19 +861,48 @@ private void addPermission(final Role role, final Host host) * Should return the first one */ @Test - public void shouldReturnHostByAlias() throws DotSecurityException, DotDataException { - final Host host = new SiteDataGen().aliases("demo.dotcms.com").nextPersisted(); - final Host host_2 = new SiteDataGen().aliases("not-demo.dotcms.com").nextPersisted(); - - final Role role = new RoleDataGen().nextPersisted(); - final User user = new UserDataGen().roles(role).nextPersisted(); - - this.addPermission(role, host); - this.addPermission(role, host_2); - - final Host hostReturned = APILocator.getHostAPI().findByAlias("demo.dotcms.com", user, false); - assertEquals(host, hostReturned); - assertNotEquals(host_2, hostReturned); + public void shouldReturnHostByAlias() throws Exception { + Host host = null; + Host host_2 = null; + Role role = null; + User user = null; + try { + final long currentTime = System.currentTimeMillis(); + final String demoAlias = "demo-" + currentTime + ".dotcms.com"; + host = new SiteDataGen().aliases(demoAlias).nextPersisted(); + final String notDemoAlias = "not-demo-" + currentTime + ".dotcms.com"; + host_2 = new SiteDataGen().aliases(notDemoAlias).nextPersisted(); + + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); + + this.addPermission(role, host); + this.addPermission(role, host_2); + + final Host hostReturned = APILocator.getHostAPI().findByAlias( + demoAlias, user, false); + assertEquals(host, hostReturned); + assertNotEquals(host_2, hostReturned); + } finally { + // Cleanup + final User systemUser = APILocator.systemUser(); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (host_2 != null) { + unpublishHost(host_2, systemUser); + archiveHost(host_2, systemUser); + deleteHost(host_2, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } } /** @@ -761,16 +911,39 @@ public void shouldReturnHostByAlias() throws DotSecurityException, DotDataExcept * Should return thehost by alias */ @Test - public void whenHostHasMultipleAliasshouldReturnHostByAlias() throws DotSecurityException, DotDataException { - final Host host = new SiteDataGen().aliases("demo.dotcms.com\r\ntest.dotcms.com").nextPersisted(); + public void whenHostHasMultipleAliasshouldReturnHostByAlias() throws Exception { + Host host = null; + Role role = null; + User user = null; + try { + final long currentTime = System.currentTimeMillis(); + final String demoAlias = "demo-" + currentTime + ".dotcms.com"; + final String testAlias = "test-" + currentTime + ".dotcms.com"; + host = new SiteDataGen().aliases(String.format("%s%n%s", + demoAlias, testAlias)).nextPersisted(); - final Role role = new RoleDataGen().nextPersisted(); - final User user = new UserDataGen().roles(role).nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); - this.addPermission(role, host); + this.addPermission(role, host); - final Host hostReturned = APILocator.getHostAPI().findByAlias("test.dotcms.com", user, false); - assertEquals(host, hostReturned); + final Host hostReturned = APILocator.getHostAPI().findByAlias(testAlias, user, false); + assertEquals(host, hostReturned); + } finally { + // Cleanup + final User systemUser = APILocator.systemUser(); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } } /** @@ -779,23 +952,51 @@ public void whenHostHasMultipleAliasshouldReturnHostByAlias() throws DotSecurity * Should return the right host by alias */ @Test - public void whenBothAliasStartByProd() throws DotSecurityException, DotDataException { - final Host host = new SiteDataGen().aliases("prod-client.dotcms.com").nextPersisted(); - final Host host_2 = new SiteDataGen().aliases("prod-anotherclient.dotcms.com").nextPersisted(); + public void whenBothAliasStartByProd() throws Exception { + Host host = null; + Host host_2 = null; + User user = null; + Role role = null; + final User systemUser = APILocator.systemUser(); + try { + final long currentTime = System.currentTimeMillis(); + final String prodAlias = "prod-client-" + currentTime + ".dotcms.com"; + host = new SiteDataGen().aliases(prodAlias).nextPersisted(); + final String prodAlias_2 = "prod-anotherclient-" + currentTime + ".dotcms.com"; + host_2 = new SiteDataGen().aliases(prodAlias_2).nextPersisted(); - final Role role = new RoleDataGen().nextPersisted(); - final User user = new UserDataGen().roles(role).nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); - this.addPermission(role, host); - this.addPermission(role, host_2); + this.addPermission(role, host); + this.addPermission(role, host_2); - final Host hostReturned = APILocator.getHostAPI().findByAlias("prod-client.dotcms.com", user, false); - assertEquals(host, hostReturned); - assertNotEquals(host_2, hostReturned); + final Host hostReturned = APILocator.getHostAPI().findByAlias(prodAlias, user, false); + assertEquals(host, hostReturned); + assertNotEquals(host_2, hostReturned); - final Host hostReturned2 = APILocator.getHostAPI().findByAlias("prod-anotherclient.dotcms.com", user, false); - assertNotEquals(host, hostReturned2); - assertEquals(host_2, hostReturned2); + final Host hostReturned2 = APILocator.getHostAPI().findByAlias(prodAlias_2, user, false); + assertNotEquals(host, hostReturned2); + assertEquals(host_2, hostReturned2); + } finally { + // Cleanup + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (host_2 != null) { + unpublishHost(host_2, systemUser); + archiveHost(host_2, systemUser); + deleteHost(host_2, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } } /** @@ -810,8 +1011,10 @@ public void Test_findAllCache() throws DotSecurityException, DotDataException { final HostAPI hostAPI = APILocator.getHostAPI(); final List allFromDB1 = hostAPI.findAllFromDB(systemUser, false); final List allFromCache1 = hostAPI.findAllFromCache(systemUser, false); - Assert.assertEquals(allFromDB1, allFromCache1); - final Host host1 = new SiteDataGen().aliases("any.client.dotcms.com").nextPersisted(); + Assert.assertTrue( allFromDB1.size() == allFromCache1.size() && + allFromDB1.containsAll(allFromCache1) && allFromCache1.containsAll(allFromDB1)); + final Host host1 = new SiteDataGen().aliases("any.client" + + System.currentTimeMillis() + ".dotcms.com").nextPersisted(); final List allFromCache2 = hostAPI.findAllFromCache(systemUser, false); assertTrue(allFromCache1.size() < allFromCache2.size()); hostAPI.archive(host1, systemUser, false); @@ -827,12 +1030,10 @@ public void Test_findAllCache() throws DotSecurityException, DotDataException { * This Test is meant to verify a changed introduced in DBSearch to find ContentletVersionInfo regardless of the language * Also it verifies there is always only one entry on version-info for every contentlet of type Host. * Regardless of the operation we perform. - * @throws DotDataException - * @throws DotSecurityException */ @Test public void Test_Host_With_Multiple_Lang_Versions_Return_Default_Lang_OtherWise_First_Occurrence() - throws DotDataException, DotSecurityException { + throws Exception { final LanguageAPI languageAPI = APILocator.getLanguageAPI(); final ContentletAPI contentletAPI = APILocator.getContentletAPI(); @@ -840,56 +1041,63 @@ public void Test_Host_With_Multiple_Lang_Versions_Return_Default_Lang_OtherWise_ final User systemUser = APILocator.systemUser(); final VersionableAPI versionableAPI = APILocator.getVersionableAPI(); - final SiteDataGen siteDataGen = new SiteDataGen(); - - Host host = siteDataGen.name("xyx" + System.currentTimeMillis()) - .aliases("xyz.dotcms.com").next(); + Host host = null; + try { + final SiteDataGen siteDataGen = new SiteDataGen(); + host = siteDataGen.name("xyx" + System.currentTimeMillis()) + .aliases("xyz" + System.currentTimeMillis() + ".dotcms.com").next(); - host.setLanguageId(0); - host = siteDataGen.persist(host, false); + host.setLanguageId(0); + host = siteDataGen.persist(host, false); - //Host are created by default under the default language. - Assert.assertEquals(languageAPI.getDefaultLanguage().getId(), host.getLanguageId()); + //Host are created by default under the default language. + Assert.assertEquals(languageAPI.getDefaultLanguage().getId(), host.getLanguageId()); - List verInfos = versionableAPI - .findContentletVersionInfos(host.getIdentifier()); + List verInfos = versionableAPI + .findContentletVersionInfos(host.getIdentifier()); - Assert.assertEquals("There should be only one entry.", 1, verInfos.size()); + Assert.assertEquals("There should be only one entry.", 1, verInfos.size()); - Host dbSearch = hostAPI.DBSearch(host.getIdentifier(), systemUser, false); - Assert.assertNotNull(dbSearch); + Host dbSearch = hostAPI.DBSearch(host.getIdentifier(), systemUser, false); + Assert.assertNotNull(dbSearch); - Assert.assertEquals(verInfos.get(0).getWorkingInode(), dbSearch.getInode()); + Assert.assertEquals(verInfos.get(0).getWorkingInode(), dbSearch.getInode()); - Assert.assertNull("There shouldn't be a live version yet.", verInfos.get(0).getLiveInode()); + Assert.assertNull("There shouldn't be a live version yet.", verInfos.get(0).getLiveInode()); - contentletAPI.publish(host, systemUser, false); + contentletAPI.publish(host, systemUser, false); - verInfos = versionableAPI.findContentletVersionInfos(host.getIdentifier()); + verInfos = versionableAPI.findContentletVersionInfos(host.getIdentifier()); - Assert.assertNotNull("There should be a live version now.", verInfos.get(0).getLiveInode()); + Assert.assertNotNull("There should be a live version now.", verInfos.get(0).getLiveInode()); - //This should create another version of the host in a different language. - final Language newLanguage = new LanguageDataGen().languageName("ES").nextPersisted(); + //This should create another version of the host in a different language. + final Language newLanguage = new LanguageDataGen().languageName("ES").nextPersisted(); - host.setLanguageId(newLanguage.getId()); - hostAPI.save(host, systemUser, false); + host.setLanguageId(newLanguage.getId()); + hostAPI.save(host, systemUser, false); - verInfos = versionableAPI - .findContentletVersionInfos(host.getIdentifier()); - Assert.assertEquals("There should be two entries one for each language.", 2, verInfos.size()); + verInfos = versionableAPI + .findContentletVersionInfos(host.getIdentifier()); + Assert.assertEquals("There should be two entries one for each language.", 2, verInfos.size()); - final long newRandomDefaultLangId = -1; - final Language mockDefaultLang = mock(Language.class); - when(mockDefaultLang.getId()).thenReturn(newRandomDefaultLangId); + final long newRandomDefaultLangId = -1; + final Language mockDefaultLang = mock(Language.class); + when(mockDefaultLang.getId()).thenReturn(newRandomDefaultLangId); - final HostAPI hostAPIWithMockedLangAPI = new HostAPIImpl(APILocator.getSystemEventsAPI()); - final Host dbSearchNoDefaultLang = hostAPIWithMockedLangAPI.DBSearch(host.getIdentifier(), systemUser, false); + final HostAPI hostAPIWithMockedLangAPI = new HostAPIImpl(APILocator.getSystemEventsAPI()); + final Host dbSearchNoDefaultLang = hostAPIWithMockedLangAPI.DBSearch(host.getIdentifier(), systemUser, false); - //Since Default language is changed now we still get something here. - assertNotNull(dbSearchNoDefaultLang); - //And System-host should still be system host - assertNotNull(hostAPIWithMockedLangAPI.findSystemHost()); + //Since Default language is changed now we still get something here. + assertNotNull(dbSearchNoDefaultLang); + //And System-host should still be system host + assertNotNull(hostAPIWithMockedLangAPI.findSystemHost()); + } finally { + // Cleanup + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } } @@ -1027,6 +1235,67 @@ public void getAllSites() throws DotSecurityException, DotDataException, Executi } } + /** + * Method to test: {@link HostAPI#findAllFromCache(User, boolean)} + * + * Given Scenario: For a Frontend User, get the list of all Sites in the repository + * when a given host has a live and a working version + * Expected Result: The API should return the live version of the host + */ + @Test + public void getAllSitesShouldReturnLiveVersionForFrontendUser() throws Exception { + // Initialization + final HostAPI hostAPI = APILocator.getHostAPI(); + final User systemUser = APILocator.systemUser(); + Host host = null; + Role role = null; + User user = null; + + try { + // Test data generation + host = new SiteDataGen().nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); + + // Create a working version of the host + final Host workingHost = new Host(APILocator.getContentletAPI().checkout( + host.getInode(), APILocator.systemUser(), false)); + final String workingHostAlias = "working." + workingHost.getHostname(); + workingHost.setAliases(workingHostAlias); + final Host savedHost = new Host(APILocator.getContentletAPI().checkin( + workingHost, systemUser, false)); + assertNotEquals(host.getInode(), savedHost.getInode()); + + final List allSites = hostAPI.findAllFromCache(user, true); + + // Assertions + assertFalse("No Sites were returned", allSites.isEmpty()); + final String hostId = host.getIdentifier(); + + Optional optionalHost = allSites.stream().filter( + h -> h.getIdentifier().equals(hostId)).findFirst(); + assertTrue("Host was NOT returned", optionalHost.isPresent()); + final Host returnedHost = optionalHost.get(); + + assertTrue("Host is NOT live", returnedHost.isLive()); + assertNull("Host has aliases", returnedHost.getAliases()); + assertEquals("Host does NOT match", host, returnedHost); + } finally { + // Cleanup + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } + } + /** * Method to test: {@link HostAPI#findDefaultHost(User, boolean)} * @@ -1060,6 +1329,62 @@ public void updateDefaultSite() throws DotSecurityException, DotDataException, E } } + /** + * Method to test: {@link HostAPI#findDefaultHost(User, boolean)} + * Given scenario: When the default host has live and working versions and the user has a front end role + * Expected result: The API should return the live version of the host + */ + @Test + public void findDefaultHostShouldReturnLiveVersionForFrontedUser() throws Exception { + // Initialization + final HostAPI hostAPI = APILocator.getHostAPI(); + final User systemUser = APILocator.systemUser(); + final Host originalDefaultSite = hostAPI.findDefaultHost(systemUser, false); + + Host host = null; + Role role = null; + User user = null; + + try { + // Test data generation + host = new SiteDataGen().setDefault(true).nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); + hostAPI.updateDefaultHost(host, systemUser, false); + + // Save working version of the default host + final Host workingHost = new Host(APILocator.getContentletAPI().checkout( + host.getInode(), APILocator.systemUser(), false)); + final String workingHostAlias = "working." + workingHost.getHostname(); + workingHost.setAliases(workingHostAlias); + final Host savedHost = new Host(APILocator.getContentletAPI().checkin( + workingHost, systemUser, false)); + assertNotEquals(host.getInode(), savedHost.getInode()); + + final Host defaultHost = hostAPI.findDefaultHost(user, true); + + // Assertions + assertTrue("Default Host is NOT live", defaultHost.isLive()); + assertNull("Default Host has aliases", defaultHost.getAliases()); + assertEquals("Default Host does NOT match", host, defaultHost); + + } finally { + // Cleanup + hostAPI.updateDefaultHost(originalDefaultSite, systemUser, false); + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } + } + /** * Method to test: {@link HostAPI#getHostsWithPermission(int, User, boolean)} * @@ -1073,11 +1398,9 @@ public void getSitesWithPermission() throws DotSecurityException, DotDataExcepti // Initialization final HostAPI hostAPI = APILocator.getHostAPI(); final User systemUser = APILocator.systemUser(); - final RoleAPI roleAPI = APILocator.getRoleAPI(); - final UserAPI userAPI = APILocator.getUserAPI(); - Host testSite = new Host(); - Role testRole = new Role(); - User dummyUser = new User(); + Host testSite = null; + Role testRole = null; + User dummyUser = null; try { // Test data generation @@ -1093,11 +1416,17 @@ public void getSitesWithPermission() throws DotSecurityException, DotDataExcepti assertTrue("Permissioned Site ID does not match!", testSite.getIdentifier().equals(permissionedSites.get(0).getIdentifier())); } finally { // Cleanup - unpublishHost(testSite, systemUser); - archiveHost(testSite, systemUser); - deleteHost(testSite, systemUser); - userAPI.delete(dummyUser, systemUser, false); - roleAPI.delete(testRole); + if (testSite != null) { + unpublishHost(testSite, systemUser); + archiveHost(testSite, systemUser); + deleteHost(testSite, systemUser); + } + if (dummyUser != null) { + UserDataGen.remove(dummyUser); + } + if (testRole != null) { + RoleDataGen.remove(testRole); + } } } @@ -1115,11 +1444,9 @@ public void getArchivedSitesWithPermission() throws DotSecurityException, DotDat // Initialization final HostAPI hostAPI = APILocator.getHostAPI(); final User systemUser = APILocator.systemUser(); - final RoleAPI roleAPI = APILocator.getRoleAPI(); - final UserAPI userAPI = APILocator.getUserAPI(); - Host testSite = new Host(); - Role testRole = new Role(); - User dummyUser = new User(); + Host testSite = null; + Role testRole = null; + User dummyUser = null; try { // Test data generation @@ -1136,9 +1463,15 @@ public void getArchivedSitesWithPermission() throws DotSecurityException, DotDat assertEquals("Non-live/archived Sites cannot be verified for permissions", 0, permissionedSites.size()); } finally { // Cleanup - deleteHost(testSite, systemUser); - userAPI.delete(dummyUser, systemUser, false); - roleAPI.delete(testRole); + if (testSite != null) { + deleteHost(testSite, systemUser); + } + if (dummyUser != null) { + UserDataGen.remove(dummyUser); + } + if (testRole != null) { + RoleDataGen.remove(testRole); + } } } @@ -1172,10 +1505,8 @@ public void findSystemHost() throws DotDataException { public void findSystemHostWithLimitedUser() throws DotDataException, DotSecurityException { // Initialization final HostAPI hostAPI = APILocator.getHostAPI(); - final RoleAPI roleAPI = APILocator.getRoleAPI(); - final UserAPI userAPI = APILocator.getUserAPI(); - Role testRole = new Role(); - User dummyUser = new User(); + Role testRole = null; + User dummyUser = null; try { // Test data generation @@ -1187,8 +1518,12 @@ public void findSystemHostWithLimitedUser() throws DotDataException, DotSecurity assertEquals("System Host object NOT found!", "SYSTEM_HOST", systemHost.getIdentifier()); } finally { // Cleanup - userAPI.delete(dummyUser, APILocator.systemUser(), false); - roleAPI.delete(testRole); + if (dummyUser != null) { + UserDataGen.remove(dummyUser); + } + if (testRole != null) { + RoleDataGen.remove(testRole); + } } } @@ -1294,7 +1629,7 @@ public void retrieveHostsPerTagStorage() throws DotHibernateException, Execution * archive it. Finally, compare the total count of stopped Sites. *
  • Expected Result: When compared to the initial stopped Sites count, after stopping the new Site, * the count must be increased by one. After stopping AND archiving the second Site, the total count difference - * must be 2 because archived Sites are also considered "stopped Sites" as well.
  • + * must still be 1 because archived Sites are not returned by searchByStopped method. * */ @Test @@ -1328,7 +1663,7 @@ public void searchByStopped() throws DotHibernateException, ExecutionException, false, 0, 0, systemUser, false); // Assertions #2 - assertEquals("Stopped and Archived Sites count difference MUST be two", 2, updatedStoppedAndArchivedSites.size() - stoppedSites.size()); + assertEquals("After create and archive a site, Stopped Sites count difference MUST be one", 1, updatedStoppedAndArchivedSites.size() - stoppedSites.size()); } finally { // Cleanup archiveHost(testSite, systemUser); @@ -1404,6 +1739,64 @@ public void searchByFilter() throws DotHibernateException, ExecutionException, I } } + /** + * Method to test: {@link HostAPI#search(String, boolean, boolean, boolean, int, int, User, boolean)} + * Given Scenario: look for a site when there is a live and a working version. + * Expected Result: The API should return the live version of the site for a front-end user. + */ + @Test + public void searchShouldReturnLiveVersionForFrontendUser() throws Exception { + // Initialization + final HostAPI hostAPI = APILocator.getHostAPI(); + final User systemUser = APILocator.systemUser(); + + Host host = null; + Role role = null; + User user = null; + + try { + // Test data generation + final String siteName = "returnliveversion.dotcms.com" + System.currentTimeMillis(); + host = new SiteDataGen().name(siteName).nextPersisted(); + role = new RoleDataGen().nextPersisted(); + user = new UserDataGen().roles(role).nextPersisted(); + + // Save working version of the host + final Host workingHost = new Host(APILocator.getContentletAPI().checkout( + host.getInode(), APILocator.systemUser(), false)); + final String workingHostAlias = "working." + workingHost.getHostname(); + workingHost.setAliases(workingHostAlias); + final Host savedHost = new Host(APILocator.getContentletAPI().checkin( + workingHost, systemUser, false)); + assertNotEquals(host.getInode(), savedHost.getInode()); + + final List returnedSites = hostAPI.search(siteName, + false, true, false, 0, 0, + user, true); + assertFalse("No Sites were returned", returnedSites.isEmpty()); + final Host returnedHost = returnedSites.get(0); + + // Assertions + assertTrue("Returned Host is NOT live", returnedHost.isLive()); + assertNull("Returned Host has aliases", returnedHost.getAliases()); + assertEquals("Returned Host does NOT match", host, returnedHost); + + } finally { + // Cleanup + if (host != null) { + unpublishHost(host, systemUser); + archiveHost(host, systemUser); + deleteHost(host, systemUser); + } + if (user != null) { + UserDataGen.remove(user); + } + if (role != null) { + RoleDataGen.remove(role); + } + } + } + /** * Method to test: {@link HostAPI#count(User, boolean)} * diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImplTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImplTest.java index aea178adff04..a170b15b0f7a 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImplTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/contentlet/business/HostFactoryImplTest.java @@ -158,7 +158,7 @@ public void test_search_shouldIncludeSystemHost() throws DotDataException, DotSe } /** - * Method to test: {@link HostFactoryImpl#bySiteName(String)} + * Method to test: {@link HostFactory#bySiteName(String, boolean)} * Given Scenario: get host by name for a non-existing host * ExpectedResult: host cache should return 404 for not existing host by name */ @@ -185,8 +185,8 @@ public void test_get_404_for_not_existing_host_by_name() throws DotDataException // Check 404 after deletion of test host final HostCache hostCache = new HostCacheImpl(); - final Host nonExistingHost = hostFactory.bySiteName(hostName); - final Host cached404Host = hostCache.getByName(hostName); + final Host nonExistingHost = hostFactory.bySiteName(hostName, false); + final Host cached404Host = hostCache.getByName(hostName, false); assertNull(nonExistingHost); assertNotNull(cached404Host);