Skip to content

Commit

Permalink
#17749 adding fixes and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Jan 16, 2020
1 parent 98a1f6c commit a2cd2c4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
Expand Up @@ -93,4 +93,64 @@ public void tryToFindCurrentHost_with_request_on_threadlocal_expected_global_hos
assertTrue( hostOpt.isPresent() );
assertEquals("Should return the default one", hostOpt.get().getIdentifier(), host.getIdentifier());
}

@Test
public void getHostFromPathOrCurrentHost_null_expected_empty () {

final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(null, Constants.CONTAINER_FOLDER_PATH);
assertNotNull( hostOpt );
assertFalse( hostOpt.isPresent() );
}

@Test
public void getHostFromPathOrCurrentHost_empty_expected_empty () {

final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(" ", Constants.CONTAINER_FOLDER_PATH);
assertNotNull( hostOpt );
assertFalse( hostOpt.isPresent() );
}

@Test
public void getHostFromPathOrCurrentHost_no_request_on_threadlocal_relative_path () {

HttpServletRequestThreadLocal.INSTANCE.setRequest(null);
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost("/application/containers/custom-container", Constants.CONTAINER_FOLDER_PATH);
assertNotNull( hostOpt );
assertTrue( hostOpt.isPresent() );
assertEquals("should return the default one", hostOpt.get().getIdentifier(), defaultHost.getIdentifier());
}

@Test
public void getHostFromPathOrCurrentHost_custom_host_request_on_threadlocal_relative_custom_host_path () {

HttpServletRequestThreadLocal.INSTANCE.setRequest(null);
final String hostName = "custom" + System.currentTimeMillis() + ".dotcms.com";
final Host host = new SiteDataGen().name(hostName).nextPersisted(true);
final HttpServletRequest request = mock(HttpServletRequest.class);
final HttpSession session = mock(HttpSession.class);
when(request.getSession(false)).thenReturn(session);
when(session.getAttribute(WebKeys.CMS_SELECTED_HOST_ID)).thenReturn(host.getIdentifier());
HttpServletRequestThreadLocal.INSTANCE.setRequest(request);
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost("/application/containers/custom-container", Constants.CONTAINER_FOLDER_PATH);
assertNotNull( hostOpt );
assertTrue( hostOpt.isPresent() );
assertEquals("should return the custom one", hostOpt.get().getIdentifier(), host.getIdentifier());
}

@Test
public void getHostFromPathOrCurrentHost_default_host_request_on_threadlocal_absolute_custom_host_path () {

HttpServletRequestThreadLocal.INSTANCE.setRequest(null);
final String hostName = "custom" + System.currentTimeMillis() + ".dotcms.com";
final Host host = new SiteDataGen().name(hostName).nextPersisted(true);
final HttpServletRequest request = mock(HttpServletRequest.class);
final HttpSession session = mock(HttpSession.class);
when(request.getSession(false)).thenReturn(session);
when(session.getAttribute(WebKeys.CMS_SELECTED_HOST_ID)).thenReturn(defaultHost.getIdentifier());
HttpServletRequestThreadLocal.INSTANCE.setRequest(request);
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost("//" + hostName + "/application/containers/custom-container", Constants.CONTAINER_FOLDER_PATH);
assertNotNull( hostOpt );
assertTrue( hostOpt.isPresent() );
assertEquals("should return the custom one", hostOpt.get().getIdentifier(), host.getIdentifier());
}
}
Expand Up @@ -12,7 +12,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.dotmarketing.util.HostUtil;
import com.dotmarketing.util.*;
import org.glassfish.jersey.server.JSONP;
import com.dotcms.rest.InitDataObject;
import com.dotcms.rest.ResponseEntityView;
Expand Down Expand Up @@ -42,9 +42,6 @@
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.form.business.FormAPI;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.PageMode;
import com.dotmarketing.util.UtilMethods;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.liferay.portal.model.User;
Expand Down Expand Up @@ -372,7 +369,7 @@ private Container getContainer(final String containerId, final User user, final

if (FileAssetContainerUtil.getInstance().isFolderAssetContainerId(containerId)) {

final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(containerId);
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(containerId, Constants.CONTAINER_FOLDER_PATH);
final Host containerHost = hostOpt.isPresent()? hostOpt.get():host;
final String relativePath = FileAssetContainerUtil.getInstance().getPathFromFullPath(containerHost.getHostname(), containerId);
return mode.showLive?
Expand Down
Expand Up @@ -290,7 +290,7 @@ public Container getWorkingContainerByFolderPath(final String fullContainerPathW
private Tuple2<String, Host> getContainerPathHost(final String containerIdOrPath, final User user,
final Supplier<Host> resourceHost) throws DotSecurityException, DotDataException {

return HostUtil.splitPathHost(containerIdOrPath, user, resourceHost);
return HostUtil.splitPathHost(containerIdOrPath, user, Constants.CONTAINER_FOLDER_PATH, resourceHost);
}

@CloseDBIfOpened
Expand Down
27 changes: 16 additions & 11 deletions dotCMS/src/main/java/com/dotmarketing/util/HostUtil.java
Expand Up @@ -63,15 +63,18 @@ private static Host findCurrentHost () {
/**
* Tries to recover the host from the path, or the current host (if not possible returns the default one)
* @param path {@link String} path (could be relative or full)
* @param postHostToken {@link String} is the token that become immediate to the host, for instance if getting the host from a container postHostToken could be "/application/containers"
* @return Optional Host
*/
public static Optional<Host> getHostFromPathOrCurrentHost (final String path) {

try {
final Tuple2<String, Host> pathHostTuple = splitPathHost(path, APILocator.systemUser(), HostUtil::findCurrentHost);
return Optional.ofNullable(pathHostTuple._2);
} catch (DotSecurityException | DotDataException e) {
// quiet
public static Optional<Host> getHostFromPathOrCurrentHost (final String path, final String postHostToken) {

if (UtilMethods.isSet(path)) {
try {
final Tuple2<String, Host> pathHostTuple = splitPathHost(path, APILocator.systemUser(), postHostToken, HostUtil::findCurrentHost);
return Optional.ofNullable(pathHostTuple._2);
} catch (DotSecurityException | DotDataException e) {
// quiet
}
}

return Optional.empty();
Expand All @@ -82,21 +85,23 @@ public static Optional<Host> getHostFromPathOrCurrentHost (final String path) {
* @param inputPath {@link String} relative or absolute path
* @param user {@link User} user
* @param resourceHost {@link Supplier}
* @param posHostToken {@link String} is the token that become immediate to the host, for instance if getting the host from a container postHostToken could be "/application/containers"
* @return Tuple2 path and host
* @throws DotSecurityException
* @throws DotDataException
*/
public static Tuple2<String, Host> splitPathHost(final String inputPath, final User user,
public static Tuple2<String, Host> splitPathHost(final String inputPath, final User user, final String posHostToken,
final Supplier<Host> resourceHost) throws DotSecurityException, DotDataException {

final HostAPI hostAPI = APILocator.getHostAPI();
final int hostIndicatorIndex = inputPath.indexOf(HOST_INDICATOR);
final int applicationContainerFolderStartsIndex =
inputPath.indexOf(Constants.CONTAINER_FOLDER_PATH);
inputPath.indexOf(posHostToken);
final boolean hasHost = hostIndicatorIndex != -1;
final String hostName = hasHost?
final boolean hasPos = applicationContainerFolderStartsIndex != -1;
final String hostName = hasHost && hasPos?
inputPath.substring(hostIndicatorIndex+2, applicationContainerFolderStartsIndex):null;
final String path = hasHost?inputPath.substring(applicationContainerFolderStartsIndex):inputPath;
final String path = hasHost && hasPos?inputPath.substring(applicationContainerFolderStartsIndex):inputPath;
final Host host = hasHost?hostAPI.findByName(hostName, user, false):resourceHost.get();

return Tuple.of(path, null == host? hostAPI.findDefaultHost(user, false): host);
Expand Down
3 changes: 2 additions & 1 deletion dotCMS/src/main/webapp/html/ng-contentlet-selector.jsp
Expand Up @@ -21,14 +21,15 @@
<%@ page import="com.dotmarketing.business.web.WebAPILocator" %>
<%@ page import="com.dotmarketing.beans.Host" %>
<%@ page import="com.dotmarketing.util.HostUtil" %>
<%@ page import="com.dotmarketing.util.Constants" %>

<%
String containerIdentifier = request.getParameter("container_id");
User user = PortalUtil.getUser(request);
Container container = null;
if (FileAssetContainerUtil.getInstance().isFolderAssetContainerId(containerIdentifier)) {
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(containerIdentifier);
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(containerIdentifier, Constants.CONTAINER_FOLDER_PATH);
final Host host = hostOpt.isPresent()? hostOpt.get():WebAPILocator.getHostWebAPI().getHost(request);
final String relativePath = FileAssetContainerUtil.getInstance().getPathFromFullPath(host.getHostname(), containerIdentifier);
container = APILocator.getContainerAPI().getWorkingContainerByFolderPath(relativePath, host, APILocator.getUserAPI().getSystemUser(), false);
Expand Down

0 comments on commit a2cd2c4

Please sign in to comment.