Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Apr 10, 2024
1 parent f82bbfd commit 7cf2c5e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dotcms.rest.api.v1.site;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

@JsonSerialize(as = ImmutableSiteVarView.class)
@JsonDeserialize(as = ImmutableSiteVarView.class)
@Value.Immutable
public interface AbstractSiteVarView {

String id();
String name();
String key();
String value();

}
76 changes: 68 additions & 8 deletions dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteHelper.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.dotcms.rest.api.v1.site;

import static com.dotmarketing.util.Logger.debug;
import static com.dotmarketing.util.Logger.error;

import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.AlreadyExistException;
import com.dotmarketing.exception.DoesNotExistException;
Expand All @@ -11,20 +15,19 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.DotContentletStateException;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.hostvariable.model.HostVariable;
import com.dotmarketing.util.HostUtil;
import com.dotmarketing.util.UtilMethods;
import com.liferay.portal.model.User;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.liferay.util.StringPool;
import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Future;

import static com.dotmarketing.util.Logger.debug;
import static com.dotmarketing.util.Logger.error;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;

/**
* Provides all the utility methods used by the {@link SiteResource}
Expand Down Expand Up @@ -171,10 +174,64 @@ public boolean makeDefault(Host site, User user, boolean respectFrontendRoles) t
return true;
}

static SiteView toView(final Host host) throws DotStateException, DotDataException, DotSecurityException{
return toView(host, null);
}

static SiteView toView(final Host host, final User user) throws DotStateException, DotDataException, DotSecurityException {
final SiteView.Builder builder = SiteView.Builder.builder();
builder.withIdentifier(host.getIdentifier())
.withInode(host.getInode())
.withAliases(host.getAliases())
.withSiteName(host.getHostname())
.withTagStorage(host.getTagStorage())
.withSiteThumbnail(null != host.getHostThumbnail() ? host.getHostThumbnail().getName(): StringPool.BLANK)
.withRunDashboard(host.getBoolProperty(SiteResource.RUN_DASHBOARD))
.withKeywords(host.getStringProperty(SiteResource.KEYWORDS))
.withDescription(host.getStringProperty(SiteResource.DESCRIPTION))
.withGoogleMap(host.getStringProperty(SiteResource.GOOGLE_MAP))
.withGoogleAnalytics(host.getStringProperty(SiteResource.GOOGLE_ANALYTICS))
.withAddThis(host.getStringProperty(SiteResource.ADD_THIS))
.withProxyUrlForEditMode(host.getStringProperty(SiteResource.PROXY_EDIT_MODE_URL))
.withEmbeddedDashboard(host.getStringProperty(SiteResource.EMBEDDED_DASHBOARD))
.withLanguageId(host.getLanguageId())
.withIsSystemHost(host.isSystemHost())
.withIsDefault(host.isDefault())
.withIsArchived(host.isArchived())
.withIsLive(host.isLive())
.withIsLocked(host.isLocked())
.withIsWorking(host.isWorking())
.withModDate(host.getModDate())
.withModUser(host.getModUser());
if(null != user){
final List<HostVariable> variablesForHost = getVariablesForHost(host, user);
final List<ImmutableSiteVarView> siteVariableViews = variablesForHost.stream()
.map(variable -> {
ImmutableSiteVarView.Builder siteVarBuilder = ImmutableSiteVarView.builder();
return siteVarBuilder.name(variable.getName())
.id(variable.getId())
.key(variable.getKey())
.value(variable.getValue())
.build();
}).collect(Collectors.toList());
builder.withVariables(siteVariableViews);
}
return builder.build();
}

static List<HostVariable> getVariablesForHost(final Host host, final User user)
throws DotDataException, DotSecurityException {
if (null == user){
return List.of();
}
return APILocator.getHostVariableAPI()
.getVariablesForHost(host.getIdentifier(), user, false);
}

private static class SingletonHolder {

private static final SiteHelper INSTANCE = new SiteHelper();
}

/**
* Get the instance.
* @return JsonWebTokenFactory
Expand Down Expand Up @@ -399,4 +456,7 @@ public Host switchToDefaultHost(final HttpServletRequest req, final User user)
this.switchSite(req, defaultSite.getIdentifier());
return defaultSite;
}



}
57 changes: 14 additions & 43 deletions dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.liferay.portal.language.LanguageException;
import com.liferay.portal.language.LanguageUtil;
import com.liferay.portal.model.User;
import com.liferay.util.StringPool;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -77,6 +76,7 @@
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import static com.dotcms.rest.api.v1.site.SiteHelper.toView;
import static com.dotcms.util.CollectionsUtils.map;

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public final Response currentSite(@Context final HttpServletRequest httpServletR
.init().getUser();

Host currentSite = siteHelper.getCurrentSite(httpServletRequest, user);
response = Response.ok( new ResponseEntityView(currentSite) ).build();
response = Response.ok( new ResponseEntityView<>(currentSite) ).build();
} catch (Exception e) {
if (ExceptionUtil.causedBy(e, DotSecurityException.class)) {
throw new ForbiddenException(e);
Expand Down Expand Up @@ -188,7 +188,7 @@ public final Response defaultSite(@Context final HttpServletRequest httpServletR

final Host currentSite = APILocator.getHostAPI().findDefaultHost(user, PageMode.get(httpServletRequest).respectAnonPerms);
response = Response.ok(
new ResponseEntityView(currentSite)
new ResponseEntityView<>(currentSite)
).build();
} catch (Exception e) {
if (ExceptionUtil.causedBy(e, DotSecurityException.class)) {
Expand Down Expand Up @@ -294,7 +294,7 @@ public final Response switchSite(
}

response = (switchDone) ?
Response.ok(new ResponseEntityView(map("hostSwitched",
Response.ok(new ResponseEntityView<>(map("hostSwitched",
switchDone))).build(): // 200
Response.status(Response.Status.NOT_FOUND).build();

Expand Down Expand Up @@ -335,7 +335,7 @@ public final Response switchSite(

try {
final Host host = siteHelper.switchToDefaultHost(request, user);
return Response.ok(new ResponseEntityView(host)).build();
return Response.ok(new ResponseEntityView<>(host)).build();

} catch (DotSecurityException e) {
Logger.error(this.getClass(), "Exception on switch site exception message: " + e.getMessage(), e);
Expand Down Expand Up @@ -436,7 +436,7 @@ public Response publishSite(@Context final HttpServletRequest httpServletRequest
throw new IllegalArgumentException(String.format(SITE_DOESNT_EXIST_ERR_MSG, siteId));
}
this.siteHelper.publish(site, user, pageMode.respectAnonPerms);
return Response.ok(new ResponseEntityView<>(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site, user))).build();
}

/**
Expand Down Expand Up @@ -479,7 +479,7 @@ public Response unpublishSite(@Context final HttpServletRequest httpServletReque
}

this.siteHelper.unpublish(site, user, pageMode.respectAnonPerms);
return Response.ok(new ResponseEntityView<>(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site, user))).build();
}

/**
Expand Down Expand Up @@ -526,7 +526,7 @@ public Response archiveSite(@Context final HttpServletRequest httpServletRequest
}

this.archive(user, pageMode, site);
return Response.ok(new ResponseEntityView<>(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site))).build();
}

@WrapInTransaction
Expand All @@ -539,7 +539,7 @@ private Response archive(final User user, final PageMode pageMode,
}

this.siteHelper.archive(site, user, pageMode.respectAnonPerms);
return Response.ok(new ResponseEntityView(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site, user))).build();
}

/**
Expand Down Expand Up @@ -582,7 +582,7 @@ public Response unarchiveSite(@Context final HttpServletRequest httpServletReque
}

this.siteHelper.unarchive(site, user, pageMode.respectAnonPerms);
return Response.ok(new ResponseEntityView<>(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site))).build();
}

/**
Expand Down Expand Up @@ -756,7 +756,7 @@ public Response findHostByIdentifier(@Context final HttpServletRequest httpServl
throw new NotFoundException(String.format(SITE_DOESNT_EXIST_ERR_MSG, siteId));
}

return Response.ok(new ResponseEntityView<>(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site,user))).build();
}

/**
Expand Down Expand Up @@ -804,7 +804,7 @@ public Response findHostByName(@Context final HttpServletRequest httpServletRequ
throw new NotFoundException(String.format(SITE_DOESNT_EXIST_ERR_MSG, hostname));
}

return Response.ok(new ResponseEntityView<>(this.toView(site))).build();
return Response.ok(new ResponseEntityView<>(toView(site,user))).build();
}

/**
Expand Down Expand Up @@ -858,7 +858,7 @@ public Response createNewSite(@Context final HttpServletRequest httpServletReque
copySitePropertiesFromForm(newSiteForm, newSite);

return Response.ok(new ResponseEntityView<>(
this.toView(this.siteHelper.save(newSite, user, pageMode.respectAnonPerms)))).build();
toView(this.siteHelper.save(newSite, user, pageMode.respectAnonPerms),user))).build();
}

/**
Expand Down Expand Up @@ -1187,7 +1187,7 @@ public Response updateSite(@Context final HttpServletRequest httpServletRequest,
copySitePropertiesFromForm(newSiteForm, site);

return Response.ok(new ResponseEntityView<>(
this.toView(this.siteHelper.update(site, user, pageMode.respectAnonPerms)))).build();
toView(this.siteHelper.update(site, user, pageMode.respectAnonPerms),user))).build();
}

/**
Expand Down Expand Up @@ -1250,33 +1250,4 @@ public Response copySite(@Context final HttpServletRequest httpServletRequest,
return Response.ok(new ResponseEntityView<>(newSite)).build();
}

private SiteView toView (final Host host) throws DotStateException, DotDataException, DotSecurityException {

return SiteView.Builder.builder()
.withIdentifier(host.getIdentifier())
.withInode(host.getInode())
.withAliases(host.getAliases())
.withSiteName(host.getHostname())
.withTagStorage(host.getTagStorage())
.withSiteThumbnail(null != host.getHostThumbnail() ? host.getHostThumbnail().getName(): StringPool.BLANK)
.withRunDashboard(host.getBoolProperty(RUN_DASHBOARD))
.withKeywords(host.getStringProperty(KEYWORDS))
.withDescription(host.getStringProperty(DESCRIPTION))
.withGoogleMap(host.getStringProperty(GOOGLE_MAP))
.withGoogleAnalytics(host.getStringProperty(GOOGLE_ANALYTICS))
.withAddThis(host.getStringProperty(ADD_THIS))
.withProxyUrlForEditMode(host.getStringProperty(PROXY_EDIT_MODE_URL))
.withEmbeddedDashboard(host.getStringProperty(EMBEDDED_DASHBOARD))
.withLanguageId(host.getLanguageId())
.withIsSystemHost(host.isSystemHost())
.withIsDefault(host.isDefault())
.withIsArchived(host.isArchived())
.withIsLive(host.isLive())
.withIsLocked(host.isLocked())
.withIsWorking(host.isWorking())
.withModDate(host.getModDate())
.withModUser(host.getModUser())
.build();

}
} // E:O:F:SiteBrowserResource.
13 changes: 13 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.rest.api.v1.site;

import java.util.Date;
import java.util.List;

public class SiteView {

Expand Down Expand Up @@ -50,6 +51,8 @@ public class SiteView {

private final String modUser;

private final List<ImmutableSiteVarView> variables;

private SiteView(Builder builder) {
identifier = builder.identifier;
inode = builder.inode;
Expand All @@ -74,6 +77,7 @@ private SiteView(Builder builder) {
isWorking = builder.isWorking;
modDate = builder.modDate;
modUser = builder.modUser;
variables = builder.variables;
}

public String getIdentifier() {
Expand Down Expand Up @@ -168,6 +172,9 @@ public String getModUser() {
return modUser;
}

public List<ImmutableSiteVarView> getVariables() {
return variables;
}

public static final class Builder {
private String identifier;
Expand All @@ -193,6 +200,7 @@ public static final class Builder {
private boolean isWorking;
private Date modDate;
private String modUser;
private List<ImmutableSiteVarView> variables;

private Builder() {
}
Expand Down Expand Up @@ -316,6 +324,11 @@ public Builder withModUser(String val) {
return this;
}

public Builder withVariables(List<ImmutableSiteVarView> val) {
variables = val;
return this;
}

public SiteView build() {
return new SiteView(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,13 @@ ResponseEntityView<List<Site>> getSites(
)
ResponseEntityView<SiteView> findByName(final GetSiteByNameRequest request);

@GET
@Path("/currentSite")
@Operation(
summary = " Returns the Site that is currently selected"
)
ResponseEntityView<Site> current();

@GET
@Path("/defaultSite")
@Operation(
summary = " Returns the Site marked as default"
)
ResponseEntityView<Site>defaultSite();

@PUT
@Path ("/switch/{id}")
@Operation(
summary = " Returns the Site that is currently selected"
)
ResponseEntityView<Map<String,String>>switchSite(@PathParam("id") String id);

@POST
@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ void Test_Find_Non_Existing_Host_By_Name() {
}
}

@Test
void Test_Current_Site() {
ResponseEntityView<Site> currentSiteResponse = clientFactory.getClient(SiteAPI.class).current();
Assertions.assertNotNull(currentSiteResponse);
}

@Test
void Test_Default_Site() {
ResponseEntityView<Site> defaultSiteResponse = clientFactory.getClient(SiteAPI.class).defaultSite();
Expand Down

0 comments on commit 7cf2c5e

Please sign in to comment.