Skip to content

Commit

Permalink
Include TTL parameters in dotParse Velocity directive (#29628)
Browse files Browse the repository at this point in the history
### Proposed Changes
* Include a second parameters in the dotParse directive to set a TTL
just like the dotCache directive

- Include methods to process something after render any Template using
the dotDirective class


https://github.com/dotCMS/core/pull/29628/files#diff-5bb38e461f0bcc492aaf6c13d9e7116ed14e533c727550ecd62e63599c85ea69R136

- Create a method to allow retrieve from cache any value before do the
render of a Template, this methos is going to be define on the
dotDirective class and be override on the dotParse class


https://github.com/dotCMS/core/pull/29628/files#diff-5bb38e461f0bcc492aaf6c13d9e7116ed14e533c727550ecd62e63599c85ea69R83


https://github.com/dotCMS/core/pull/29628/files#diff-046d4b8541bc8f8e5ad86fc004d0134b74c30dace321de05009be56225611d85R211
  • Loading branch information
freddyDOTCMS authored Aug 16, 2024
1 parent 1950037 commit c8f8051
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.dotcms.rendering.velocity.directive;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import com.dotmarketing.business.CacheLocator;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapter;
Expand All @@ -18,7 +22,6 @@
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;

import com.dotcms.rendering.velocity.directive.RenderParams;
import com.dotcms.rendering.velocity.services.VelocityType;
import com.dotcms.rendering.velocity.util.VelocityUtil;

Expand Down Expand Up @@ -70,8 +73,17 @@ final Template loadTemplate(InternalContextAdapter context, String templatePath
}
}




/**
* Return the value from Cache if the Directive is using some kind of Cache level
* the default implementation return a Empty Optional.
*
* @return Value from cache
*/
public Optional<String> getFromCache(final String[] arguments) {
return Optional.empty();
}

final public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {

Expand All @@ -84,11 +96,12 @@ final public boolean render(InternalContextAdapter context, Writer writer, Node
arguments[i]= (value == null) ? null : value.toString();
}





final Optional<String> fromCache = getFromCache(arguments);

if (fromCache.isPresent()) {
writer.write(fromCache.get());
return true;
}

RenderParams params = new RenderParams(request);

Expand All @@ -98,7 +111,13 @@ final public boolean render(InternalContextAdapter context, Writer writer, Node
throw new ResourceNotFoundException("null template");
}
Template t = loadTemplate(context, templatePath);
return this.renderTemplate(context, writer, t, templatePath);

final Writer innerWriter = new StringWriter();
final boolean result = this.renderTemplate(context, innerWriter, t, templatePath);
this.afterRender(innerWriter.toString(), arguments);
writer.write(innerWriter.toString());

return result;
} catch(ParseErrorException|ResourceNotFoundException rnfe){
context.remove("ContentIdentifier");
postRender(context);
Expand All @@ -107,6 +126,16 @@ final public boolean render(InternalContextAdapter context, Writer writer, Node

}

/**
* Call after render the Template, it allow you to do something before the return of
* the {@link DotDirective#render(InternalContextAdapter, Writer, Node)}
*
* @param render Template content after render
* @param arguments
*/
void afterRender(final String render, String[] arguments) {

}


final boolean renderTemplate(InternalContextAdapter context, final Writer writer, final Template t, final String templatePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.dotcms.rendering.velocity.directive;

import com.dotcms.variant.business.web.VariantWebAPI.RenderContext;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.web.WebAPILocator;
import java.io.File;
import java.io.Serializable;
import java.io.Writer;
import java.util.Map;
import java.util.Optional;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.ResourceNotFoundException;
import com.dotcms.contenttype.model.type.DotAssetContentType;
import com.dotcms.util.ConversionUtils;
Expand All @@ -31,10 +34,12 @@
import com.liferay.util.StringPool;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import org.apache.velocity.runtime.parser.node.Node;


public class DotParse extends DotDirective {

private final static String RENDER = "render";
private static final long serialVersionUID = 1L;

private final String hostIndicator = "//";
Expand Down Expand Up @@ -190,7 +195,29 @@ private void throwNotResourceNotFoundException(final RenderParams params, final
throw new ResourceNotFoundException(errorMessage);
}

/**
* Call after render the Template, it allow you to do something before the return of
* the {@link DotDirective#render(InternalContextAdapter, Writer, Node)}
*
* @param render Template content after render
* @param arguments
*/
void afterRender(final String render, String[] arguments) {
if (arguments.length > 1) {
CacheLocator.getBlockDirectiveCache().add(getTTLCacheKey(arguments), Map.of(RENDER, render), Integer.parseInt(arguments[1]));
}
}

public Optional<String> getFromCache(final String[] arguments) {
final Map<String, Serializable> valueFromCache =
CacheLocator.getBlockDirectiveCache().get(getTTLCacheKey(arguments));
return UtilMethods.isSet(valueFromCache.get(RENDER)) ? Optional.of(valueFromCache.get(RENDER).toString()) :
Optional.empty();
}

private String getTTLCacheKey(String[] arguments) {
return getName() + "_" + arguments[0];
}

private Optional<Tuple2<Identifier, String>> resolveDotAsset(final RenderParams params,
final String templatePath) throws DotDataException, DotSecurityException {
Expand Down Expand Up @@ -229,8 +256,6 @@ private Optional<Tuple2<Identifier, String>> resolveDotAsset(final RenderParams

final Identifier identifier = APILocator.getIdentifierAPI().find(conOpt.get().getIdentifier());
return Optional.of(Tuple.of(identifier, fieldVar));


}

private Optional<Tuple2<Identifier, String>> resolveFileAsset(final RenderParams params,
Expand Down

0 comments on commit c8f8051

Please sign in to comment.