Skip to content

Commit

Permalink
#17749 this first effort renders on the absolute path when the contai…
Browse files Browse the repository at this point in the history
…ner is not part of the current host, and also make several fixes to make it work on the edit mode
  • Loading branch information
jdotcms committed Jan 15, 2020
1 parent f710c6b commit 361da97
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 30 deletions.
Expand Up @@ -11,6 +11,8 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.dotmarketing.util.HostUtil;
import org.glassfish.jersey.server.JSONP;
import com.dotcms.rest.InitDataObject;
import com.dotcms.rest.ResponseEntityView;
Expand Down Expand Up @@ -370,9 +372,12 @@ private Container getContainer(final String containerId, final User user, final

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

final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(containerId);
final Host containerHost = hostOpt.isPresent()? hostOpt.get():host;
final String relativePath = FileAssetContainerUtil.getInstance().getPathFromFullPath(containerHost.getHostname(), containerId);
return mode.showLive?
this.containerAPI.getLiveContainerByFolderPath (containerId, host, user, mode.respectAnonPerms):
this.containerAPI.getWorkingContainerByFolderPath(containerId, host, user, mode.respectAnonPerms);
this.containerAPI.getLiveContainerByFolderPath (relativePath, containerHost, user, mode.respectAnonPerms):
this.containerAPI.getWorkingContainerByFolderPath(relativePath, containerHost, user, mode.respectAnonPerms);
}

final ShortyId containerShorty = this.shortyAPI.getShorty(containerId)
Expand Down
Expand Up @@ -25,6 +25,7 @@
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.portlets.templates.model.Template;
import com.dotmarketing.util.Constants;
import com.dotmarketing.util.HostUtil;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.util.WebKeys;
import com.liferay.portal.model.User;
Expand All @@ -49,7 +50,6 @@ public class ContainerAPIImpl extends BaseWebAssetAPI implements ContainerAPI {
protected ContainerFactory containerFactory;
protected HostAPI hostAPI;
protected FolderAPI folderAPI;
private static final String HOST_INDICATOR = "//";

/**
* Constructor
Expand Down Expand Up @@ -290,17 +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 {

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

return Tuple.of(path, null == host? hostAPI.findDefaultHost(user, false): host);
return HostUtil.splitPathHost(containerIdOrPath, user, resourceHost);
}

@CloseDBIfOpened
Expand All @@ -323,7 +313,16 @@ public Container getContainerByFolder(final Folder folder, final User user, fina
@Override
public Container getContainerByFolder(final Folder folder, final Host host, final User user, final boolean showLive) throws DotSecurityException, DotDataException {

return this.containerFactory.getContainerByFolder(host, folder, user, showLive, false);
final String folderHostId = folder.getHostId();
final Optional<Host> currentHostOpt = HostUtil.tryToFindCurrentHost(user);
boolean includeHostOnPath = false;

if (currentHostOpt.isPresent()) {

includeHostOnPath = !folderHostId.equals(currentHostOpt.get().getIdentifier());
}

return this.containerFactory.getContainerByFolder(host, folder, user, showLive, includeHostOnPath);
}

/**
Expand Down
Expand Up @@ -39,11 +39,7 @@
import com.dotmarketing.portlets.folders.business.FolderAPI;
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.util.Constants;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.PaginatedArrayList;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.util.*;
import com.google.common.collect.ImmutableList;
import com.liferay.portal.model.User;
import com.liferay.util.StringPool;
Expand Down Expand Up @@ -179,14 +175,32 @@ public Container find(final String inode) throws DotDataException, DotSecurityEx
public Container getLiveContainerByFolderPath(final String path, final Host host, final User user,
final boolean respectFrontEndPermissions) throws DotSecurityException, DotDataException {

return this.getContainerByFolder(host, this.folderAPI.findFolderByPath(path, host, user, respectFrontEndPermissions), user,true, false);
final String folderHostId = host.getIdentifier();
final Optional<Host> currentHostOpt = HostUtil.tryToFindCurrentHost(user);
boolean includeHostOnPath = false;

if (currentHostOpt.isPresent()) {

includeHostOnPath = !folderHostId.equals(currentHostOpt.get().getIdentifier());
}

return this.getContainerByFolder(host, this.folderAPI.findFolderByPath(path, host, user, respectFrontEndPermissions), user,true, includeHostOnPath);
}

@Override
public Container getWorkingContainerByFolderPath(final String path, final Host host, final User user,
final boolean respectFrontEndPermissions) throws DotSecurityException, DotDataException {

return this.getContainerByFolder(host, this.folderAPI.findFolderByPath(path, host, user, respectFrontEndPermissions), user,false, false);
final String folderHostId = host.getIdentifier();
final Optional<Host> currentHostOpt = HostUtil.tryToFindCurrentHost(user);
boolean includeHostOnPath = false;

if (currentHostOpt.isPresent()) {

includeHostOnPath = !folderHostId.equals(currentHostOpt.get().getIdentifier());
}

return this.getContainerByFolder(host, this.folderAPI.findFolderByPath(path, host, user, respectFrontEndPermissions), user,false, includeHostOnPath);
}


Expand Down Expand Up @@ -575,8 +589,9 @@ private List<Container> getFolderContainers(final Host host, final User user,
for (final Folder subFolder : subFolders) {

try {

final User userFinal = null != user? user: APILocator.systemUser();
final Container container = this.getContainerByFolder(null != host? host:APILocator.getHostAPI().find(subFolder.getHostId(), user, false),
final Container container = this.getContainerByFolder(null != host? host:APILocator.getHostAPI().find(subFolder.getHostId(), user, includeHostOnPath),
subFolder, userFinal, false, includeHostOnPath);
containers.add(container);
} catch (DotSecurityException e) {
Expand Down Expand Up @@ -741,9 +756,17 @@ private List<Container> findFolderAssetContainersForContentType(final String con

for (final Folder folder: containerFolders) {

final Host host = this.hostAPI.find(folder.getHostId(), APILocator.systemUser(), false);
final Host host = this.hostAPI.find(folder.getHostId(), APILocator.systemUser(), false);
final String folderHostId = folder.getHostId();
final Optional<Host> currentHostOpt = HostUtil.tryToFindCurrentHost(APILocator.systemUser());
boolean includeHostOnPath = false;

if (currentHostOpt.isPresent()) {

includeHostOnPath = !folderHostId.equals(currentHostOpt.get().getIdentifier());
}
try {
final Container container = this.getContainerByFolder(host, folder, APILocator.systemUser(), false, false);
final Container container = this.getContainerByFolder(host, folder, APILocator.systemUser(), false, includeHostOnPath);

if (null != container) {

Expand Down
Expand Up @@ -183,7 +183,13 @@ public String getContainerIdFromPath(final String fullPath) throws DotDataExcept
return identifier.getId();
}

private String getPathFromFullPath(final String hostname, final String fullPath) {
/**
* Remove the hostname from the fullPath (if it has the host)
* @param hostname {@link String} host name to remove (must be not null)
* @param fullPath {@link String} full path, could be relative or full (if full, the host will be removed)
* @return returns the relative path
*/
public String getPathFromFullPath(final String hostname, final String fullPath) {

final int indexOf = fullPath.indexOf(hostname);

Expand Down
76 changes: 75 additions & 1 deletion dotCMS/src/main/java/com/dotmarketing/util/HostUtil.java
@@ -1,18 +1,29 @@
package com.dotmarketing.util;

import com.dotcms.api.web.HttpServletRequestThreadLocal;
import com.dotcms.repackage.javax.portlet.ActionRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.liferay.portlet.ActionRequestImpl;
import com.liferay.portal.model.User;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.control.Try;

import java.util.Optional;
import java.util.function.Supplier;

public class HostUtil {


private static final String HOST_INDICATOR = "//";

public static String hostNameUtil(ActionRequest req, User user) throws DotDataException, DotSecurityException {

ActionRequestImpl reqImpl = (ActionRequestImpl) req;
Expand All @@ -27,7 +38,70 @@ public static String hostNameUtil(ActionRequest req, User user) throws DotDataEx


return h.getTitle()!=null?h.getTitle():"default";
}

/**
* Try to find the host on the current thread, empty optional if not able to do it.
* @param user {@link User}
* @return Optional Host
*/
public static Optional<Host> tryToFindCurrentHost (final User user) {

final HttpServletRequest request = HttpServletRequestThreadLocal.INSTANCE.getRequest();

return null != request?
Optional.ofNullable(Try.of(()->WebAPILocator.getHostWebAPI().getCurrentHost(request, user)).getOrNull()):
Optional.empty();
}

private static Host findCurrentHost () {

final Optional<Host> hostOpt = tryToFindCurrentHost(APILocator.systemUser());
return hostOpt.isPresent()? hostOpt.get():null;
}

/**
* 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)
* @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
}

return Optional.empty();
}

/**
* Based on a path tries to split the host and the relative path. If the path is already relative, gets the host from the resourceHost or gets the default one
* @param inputPath {@link String} relative or absolute path
* @param user {@link User} user
* @param resourceHost {@link Supplier}
* @return Tuple2 path and host
* @throws DotSecurityException
* @throws DotDataException
*/
public static Tuple2<String, Host> splitPathHost(final String inputPath, final User user,
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);
final boolean hasHost = hostIndicatorIndex != -1;
final String hostName = hasHost?
inputPath.substring(hostIndicatorIndex+2, applicationContainerFolderStartsIndex):null;
final String path = hasHost?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);
}



}
16 changes: 13 additions & 3 deletions dotCMS/src/main/webapp/html/ng-contentlet-selector.jsp
Expand Up @@ -19,13 +19,23 @@
<%@ page import="com.liferay.portal.language.LanguageUtil"%>
<%@ page import="com.dotmarketing.portlets.containers.business.FileAssetContainerUtil" %>
<%@ page import="com.dotmarketing.business.web.WebAPILocator" %>
<%@ page import="com.dotmarketing.beans.Host" %>
<%@ page import="com.dotmarketing.util.HostUtil" %>

<%
String containerIdentifier = request.getParameter("container_id");
User user = PortalUtil.getUser(request);
Container container = FileAssetContainerUtil.getInstance().isFolderAssetContainerId(containerIdentifier)?
APILocator.getContainerAPI().getWorkingContainerByFolderPath(containerIdentifier, WebAPILocator.getHostWebAPI().getHost(request), APILocator.getUserAPI().getSystemUser(), false):
APILocator.getContainerAPI().getWorkingContainerById(containerIdentifier, APILocator.getUserAPI().getSystemUser(), false);
Container container = null;
if (FileAssetContainerUtil.getInstance().isFolderAssetContainerId(containerIdentifier)) {
final Optional<Host> hostOpt = HostUtil.getHostFromPathOrCurrentHost(containerIdentifier);
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);
} else {
container = APILocator.getContainerAPI().getWorkingContainerById(containerIdentifier, APILocator.getUserAPI().getSystemUser(), false);
}
List<ContentType> contentTypes = null;
String baseTypeToAdd = request.getParameter("add");
Expand Down

0 comments on commit 361da97

Please sign in to comment.