Skip to content

Commit

Permalink
#31442 move methods to utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinogiardino committed Feb 24, 2025
1 parent 34efcdc commit 42f60fa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
26 changes: 8 additions & 18 deletions dotCMS/src/main/java/com/dotcms/rest/api/v1/page/PageResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.dotcms.util.ConversionUtils;
import com.dotcms.util.HttpRequestDataUtil;
import com.dotcms.util.PaginationUtil;
import com.dotcms.util.TimeMachineUtil;
import com.dotcms.util.pagination.ContentTypesPaginator;
import com.dotcms.util.pagination.OrderDirection;
import com.dotcms.vanityurl.business.VanityUrlAPI;
Expand Down Expand Up @@ -78,7 +79,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import io.vavr.control.Try;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -351,6 +351,12 @@ private PageRenderParams optionalRenderParams(final String modeParam,
if (null != deviceInode){
builder.deviceInode(deviceInode);
}
TimeMachineUtil.parseTimeMachineDate(timeMachineDateAsISO8601).ifPresentOrElse(
builder::timeMachineDate,
() -> Logger.debug(this, () -> String.format(
"Date %s is not older than the grace window. Skipping Time Machine setup.",
timeMachineDateAsISO8601))
);
if (null != timeMachineDateAsISO8601) {
final Date date;
try {
Expand Down Expand Up @@ -475,7 +481,7 @@ private Response getPageRender(final PageRenderParams renderParams) throws DotDa
*/
private void setUpTimeMachineIfPresent(final PageRenderParams renderParams) {
final Optional<Instant> timeMachineDate = renderParams.timeMachineDate();
if(timeMachineDate.isPresent() && isOlderThanGraceWindow(timeMachineDate.get())){
if(timeMachineDate.isPresent()){
final String timeMachineEpochMillis = String.valueOf(timeMachineDate.get().toEpochMilli());
final Optional<Host> host = currentHost(renderParams);
if(host.isEmpty()){
Expand All @@ -497,22 +503,6 @@ private void setUpTimeMachineIfPresent(final PageRenderParams renderParams) {
}
}


/**
* Determines if the FTM logic should be applied based on the given timeMachineDate.
* It checks if the date is older than the grace window (not too recent),
* using a configurable time limit.
*
* @param timeMachineDate The Time Machine date from the request.
* @return true if the timeMachineDate is older than the grace window, meaning FTM logic should be applied,
* false otherwise (if within the grace window).
*/
private boolean isOlderThanGraceWindow(final Instant timeMachineDate) {
final int graceWindowMinutes = Config.getIntProperty("FTM_GRACE_WINDOW_LIMIT", 5);
final Instant graceWindowTime = Instant.now().plus(Duration.ofMinutes(graceWindowMinutes));
return timeMachineDate.isAfter(graceWindowTime);
}

/**
* Removes the Time Machine attributes from the session.
* @param request The current instance of the {@link HttpServletRequest}.
Expand Down
48 changes: 47 additions & 1 deletion dotCMS/src/main/java/com/dotcms/util/TimeMachineUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
import com.dotcms.api.web.HttpServletRequestThreadLocal;

import com.dotcms.rest.api.v1.page.PageResource;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.DateUtil;
import io.vavr.Lazy;
import io.vavr.control.Try;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

public final class TimeMachineUtil {

private TimeMachineUtil(){}

private static final Lazy<Integer> FTM_GRACE_WINDOW_LIMIT =
Lazy.of(() -> Config.getIntProperty("FTM_GRACE_WINDOW_LIMIT", 5));
/**
* If Time Machine is running return the timestamp of the Time Machine date
* Running Time Machine is determined by the presence of the attribute PageResource.TM_DATE in the request or session
Expand Down Expand Up @@ -46,4 +56,40 @@ public static boolean isRunning(){
public static boolean isNotRunning(){
return !isRunning();
}

/**
* Parses and validates the given date string in ISO 8601 format.
*
* @param dateAsISO8601 The date string in ISO 8601 format. If null, an empty {@link Optional} is returned.
* @return An {@link Optional} containing a valid {@link Instant} if parsing is successful and the date meets the validation criteria.
* Returns an empty {@link Optional} if the date is invalid or does not meet the validation criteria.
* @throws IllegalArgumentException If the date string cannot be parsed.
*/
public static Optional<Instant> parseTimeMachineDate(final String dateAsISO8601) {
if (Objects.isNull(dateAsISO8601)) {
return Optional.empty();
}
Instant instant = Try.of(() -> DateUtil.convertDate(dateAsISO8601))
.map(Date::toInstant)
.getOrElseThrow(e ->
new IllegalArgumentException(
String.format("Error Parsing date: %s", dateAsISO8601), e)
);
return isOlderThanGraceWindow(instant) ? Optional.of(instant) : Optional.empty();
}


/**
* Determines if the FTM logic should be applied based on the given timeMachineDate.
* It checks if the date is older than the grace window (not too recent),
* using a configurable time limit.
*
* @param timeMachineDate The Time Machine date from the request.
* @return true if the timeMachineDate is older than the grace window, meaning FTM logic should be applied,
* false otherwise (if within the grace window).
*/
public static boolean isOlderThanGraceWindow(final Instant timeMachineDate) {
final Instant graceWindowTime = Instant.now().plus(Duration.ofMinutes(FTM_GRACE_WINDOW_LIMIT.get()));
return timeMachineDate.isAfter(graceWindowTime);
}
}

0 comments on commit 42f60fa

Please sign in to comment.