Class StandardLinkBuilder
- java.lang.Object
-
- org.thymeleaf.linkbuilder.AbstractLinkBuilder
-
- org.thymeleaf.linkbuilder.StandardLinkBuilder
-
- All Implemented Interfaces:
ILinkBuilder
public class StandardLinkBuilder extends AbstractLinkBuilder
Standard implementation of
ILinkBuilder.This class will build link URLs using (by default) the Java Servlet API when the specified URLs are context-relative, given the need to obtain the context path and add it to the URL. Also, when an
IWebContextimplementation is used as context, URLs will be passed to the standardHttpSerlvetResponse.encodeURL(...)method before returning.Note however that the Servlet-API specific part of this behaviour is configurable and confined to a set of
protectedmethods that can be overwritten by subclasses that want to offer a link building behaviour very similar to the standard one, but without any dependencies on the Servlet API (e.g. extracting URL context paths from a framework artifact other thanHttpServletRequest).This implementation will only return
nullatbuildLink(IExpressionContext, String, Map)if the specifiedbaseargument isnull.- Since:
- 3.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classStandardLinkBuilder.LinkType
-
Field Summary
Fields Modifier and Type Field Description private static charURL_TEMPLATE_DELIMITER_PREFIXprivate static java.lang.StringURL_TEMPLATE_DELIMITER_SEGMENT_PREFIXprivate static charURL_TEMPLATE_DELIMITER_SUFFIX
-
Constructor Summary
Constructors Constructor Description StandardLinkBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringbuildLink(IExpressionContext context, java.lang.String base, java.util.Map<java.lang.String,java.lang.Object> parameters)Build a link, returningnullif not possible.protected java.lang.StringcomputeContextPath(IExpressionContext context, java.lang.String base, java.util.Map<java.lang.String,java.lang.Object> parameters)Compute the context path to be applied to URLs that have been determined to be context-relative (and therefore need a context path to be inserted at their beginning).private static voidfilterOutJavaScriptLinks(java.lang.CharSequence linkBase)private static intfindCharInSequence(java.lang.CharSequence seq, char character)private static java.lang.StringformatParameterValueAsUnescapedVariableTemplate(java.lang.Object parameterValue)private static booleanisLinkBaseAbsolute(java.lang.CharSequence linkBase)private static booleanisLinkBaseContextRelative(java.lang.CharSequence linkBase)private static booleanisLinkBaseServerRelative(java.lang.CharSequence linkBase)private static voidprocessAllRemainingParametersAsQueryParams(java.lang.StringBuilder strBuilder, java.util.Map<java.lang.String,java.lang.Object> parameters)protected java.lang.StringprocessLink(IExpressionContext context, java.lang.String link)Process an already-built URL just before returning it.private static java.lang.StringBuilderreplaceTemplateParamsInBase(java.lang.StringBuilder linkBase, java.util.Map<java.lang.String,java.lang.Object> parameters)-
Methods inherited from class org.thymeleaf.linkbuilder.AbstractLinkBuilder
getName, getOrder, setName, setOrder
-
-
-
-
Field Detail
-
URL_TEMPLATE_DELIMITER_PREFIX
private static final char URL_TEMPLATE_DELIMITER_PREFIX
- See Also:
- Constant Field Values
-
URL_TEMPLATE_DELIMITER_SUFFIX
private static final char URL_TEMPLATE_DELIMITER_SUFFIX
- See Also:
- Constant Field Values
-
URL_TEMPLATE_DELIMITER_SEGMENT_PREFIX
private static final java.lang.String URL_TEMPLATE_DELIMITER_SEGMENT_PREFIX
- See Also:
- Constant Field Values
-
-
Method Detail
-
buildLink
public final java.lang.String buildLink(IExpressionContext context, java.lang.String base, java.util.Map<java.lang.String,java.lang.Object> parameters)
Description copied from interface:ILinkBuilderBuild a link, returning
nullif not possible.- Parameters:
context- theIExpressionContextobject being used for template processing. Cannot be null.base- the base of the link URL to be built, i.e. its path. Can be null.parameters- the (optional) URL parameters.- Returns:
- the built URL.
-
findCharInSequence
private static int findCharInSequence(java.lang.CharSequence seq, char character)
-
filterOutJavaScriptLinks
private static void filterOutJavaScriptLinks(java.lang.CharSequence linkBase)
-
isLinkBaseAbsolute
private static boolean isLinkBaseAbsolute(java.lang.CharSequence linkBase)
-
isLinkBaseContextRelative
private static boolean isLinkBaseContextRelative(java.lang.CharSequence linkBase)
-
isLinkBaseServerRelative
private static boolean isLinkBaseServerRelative(java.lang.CharSequence linkBase)
-
replaceTemplateParamsInBase
private static java.lang.StringBuilder replaceTemplateParamsInBase(java.lang.StringBuilder linkBase, java.util.Map<java.lang.String,java.lang.Object> parameters)
-
formatParameterValueAsUnescapedVariableTemplate
private static java.lang.String formatParameterValueAsUnescapedVariableTemplate(java.lang.Object parameterValue)
-
processAllRemainingParametersAsQueryParams
private static void processAllRemainingParametersAsQueryParams(java.lang.StringBuilder strBuilder, java.util.Map<java.lang.String,java.lang.Object> parameters)
-
computeContextPath
protected java.lang.String computeContextPath(IExpressionContext context, java.lang.String base, java.util.Map<java.lang.String,java.lang.Object> parameters)
Compute the context path to be applied to URLs that have been determined to be context-relative (and therefore need a context path to be inserted at their beginning).
By default, this method will obtain the context path from
HttpServletRequest.getContextPath(), throwing an exception ifcontextis not an instance ofIWebContextgiven context-relative URLs are (by default) only allowed in web contexts.This method can be overridden by any subclasses that want to change this behaviour (e.g. in order to avoid using the Servlet API for resolving context path or to allow context-relative URLs in non-web contexts).
- Parameters:
context- the execution context.base- the URL base specified.parameters- the URL parameters specified.- Returns:
- the context path.
-
processLink
protected java.lang.String processLink(IExpressionContext context, java.lang.String link)
Process an already-built URL just before returning it.
By default, this method will apply the
HttpServletResponse.encodeURL(url)mechanism, as standard when using the Java Servlet API. Note however that this will only be applied ifcontextis an implementation ofIWebContext(i.e. the Servlet API will only be applied in web environments).This method can be overridden by any subclasses that want to change this behaviour (e.g. in order to avoid using the Servlet API).
- Parameters:
context- the execution context.link- the already-built URL.- Returns:
- the processed URL, ready to be used.
-
-