/** * Wraps the provided executor service to make submitted tasks traceable with a fresh {@link Trace trace} * for each execution, see {@link #wrapWithNewTrace(String, ExecutorService)}. This method should not be used to * wrap a ScheduledExecutorService that has already been {@link #wrap(ExecutorService) wrapped}. If this is * done, a new trace will be generated for each execution, effectively bypassing the intent of the previous * wrapping. The given {@link String operation} is used to create the initial span. */ public static ExecutorService wrapWithNewTrace(@CompileTimeConstant String operation, ExecutorService executorService) { return new WrappingExecutorService(executorService) { @Override protected <T> Callable<T> wrapTask(Callable<T> callable) { return wrapWithNewTrace(operation, callable); } }; }
/** * Wraps the provided scheduled executor service to make submitted tasks traceable with a fresh {@link Trace trace} * for each execution, see {@link #wrapWithNewTrace(String, ScheduledExecutorService)}. This method should not be * used to wrap a ScheduledExecutorService that has already been {@link #wrap(ScheduledExecutorService) wrapped}. * If this is done, a new trace will be generated for each execution, effectively bypassing the intent of the * previous wrapping. The given {@link String operation} is used to create the initial span. */ public static ScheduledExecutorService wrapWithNewTrace(@CompileTimeConstant String operation, ScheduledExecutorService executorService) { return new WrappingScheduledExecutorService(executorService) { @Override protected <T> Callable<T> wrapTask(Callable<T> callable) { return wrapWithNewTrace(operation, callable); } }; }
/** * Creates a new builder, with an underlying URL set to the given compile-time constant * {@code string}. * * <p>No runtime validation or sanitization is performed on {@code string}; being under * application control, it is simply assumed to comply with the TrustedResourceUrl contract. */ public TrustedResourceUrlBuilder(@CompileTimeConstant final String string) { url.append(string); }
/** * Appends the compile-time constant {@code string} to the URL being built. * * <p>No runtime validation or sanitization is performed on {@code string}; being under * application control, it is simply assumed comply with the TrustedResourceUrl contract. */ public TrustedResourceUrlBuilder append(@CompileTimeConstant final String string) { url.append(string); return this; }
/** * Wraps an assumed-safe URI constant. * * <p>This only accepts compile-time constants, based on the assumption that URLs that are * controlled by the application (and not user input) are considered safe. */ public static SanitizedContent constantUri(@CompileTimeConstant final String constant) { return fromConstant(constant, ContentKind.URI, Dir.LTR); }
/** * Wraps an assumed-safe constant string that specifies an attribute. * * <p>This only accepts compile-time constants, based on the assumption that attributes that are * controlled by the application (and not user input) are considered safe. */ public static SanitizedContent constantAttributes(@CompileTimeConstant final String constant) { return fromConstant(constant, ContentKind.ATTRIBUTES, Dir.LTR); }
/** * Wraps an assumed-safe constant string that specifies a safe, balanced, document fragment. * * <p>This only accepts compile-time constants, based on the assumption that HTML snippets that * are controlled by the application (and not user input) are considered safe. */ public static SanitizedContent constantHtml(@CompileTimeConstant final String constant) { return fromConstant(constant, ContentKind.HTML, null); }
/** * Wraps an assumed-safe CSS constant. * * <p>This only accepts compile-time constants, based on the assumption that URLs that are * controlled by the application (and not user input) are considered safe. */ public static SanitizedContent constantCss(@CompileTimeConstant final String constant) { return fromConstant(constant, ContentKind.CSS, Dir.LTR); }
/** * Wraps an assumed-safe constant string that specifies a safe, balanced, document fragment. * * <p>This only accepts compile-time constants, based on the assumption that HTML snippets that * are controlled by the application (and not user input) are considered safe. */ public static SanitizedContent constantHtml(@CompileTimeConstant final String constant) { return fromConstant(constant, ContentKind.HTML, null); }
/** * Wraps an assumed-safe URI constant. * * <p>This only accepts compile-time constants, based on the assumption that URLs that are * controlled by the application (and not user input) are considered safe. */ public static SanitizedContent constantUri(@CompileTimeConstant final String constant) { return fromConstant(constant, ContentKind.URI, Dir.LTR); }
/** * Sets the {@code for} attribute for this element to a {@link CompileTimeConstant} {@code prefix} * and a {@code value} joined by a hyphen. * * @throws IllegalArgumentException if {@code prefix} is an empty string */ public SafeHtmlBuilder setForWithPrefix(@CompileTimeConstant final String prefix, String value) { if (prefix.trim().length() == 0) { throw new IllegalArgumentException("Prefix cannot be empty string"); } return setAttribute("for", prefix + "-" + value); }
/** * Sets the {@code list} attribute for this element to a {@link CompileTimeConstant} {@code * prefix} and a {@code value} joined by a hyphen. * * @throws IllegalArgumentException if {@code prefix} is an empty string */ public SafeHtmlBuilder setListWithPrefix(@CompileTimeConstant final String prefix, String value) { if (prefix.trim().length() == 0) { throw new IllegalArgumentException("Prefix cannot be empty string"); } return setAttribute("list", prefix + "-" + value); }
/** * Sets the {@code id} attribute for this element to a {@link CompileTimeConstant} {@code prefix} * and a {@code value} joined by a hyphen. * * @throws IllegalArgumentException if {@code prefix} is an empty string */ public SafeHtmlBuilder setIdWithPrefix(@CompileTimeConstant final String prefix, String value) { if (prefix.trim().length() == 0) { throw new IllegalArgumentException("Prefix cannot be empty string"); } return setAttribute("id", prefix + "-" + value); }
/** * Sets the {@code name} attribute for this element to a {@link CompileTimeConstant} {@code * prefix} and a {@code value} joined by a hyphen. * * @throws IllegalArgumentException if {@code prefix} is an empty string */ public SafeHtmlBuilder setNameWithPrefix(@CompileTimeConstant final String prefix, String value) { if (prefix.trim().length() == 0) { throw new IllegalArgumentException("Prefix cannot be empty string"); } return setAttribute("name", prefix + "-" + value); }
/** * Creates a SafeUrl from the given compile-time constant string {@code url}. * * <p>No runtime validation or sanitization is performed on {@code url}; being under application * control, it is simply assumed to comply with the SafeUrl contract. */ public static SafeUrl fromConstant(@CompileTimeConstant final String url) { return create(url); }
/** * Creates a TrustedResourceUrl from the given compile-time constant string {@code url}. * * <p>No runtime validation or sanitization is performed on {@code url}; being under application * control, it is simply assumed to comply with the TrustedResourceUrl contract. */ public static TrustedResourceUrl fromConstant(@CompileTimeConstant final String url) { return create(url); }
private SafeHtmlBuilder setAttribute(@CompileTimeConstant final String name, String value) { if (value == null) { throw new NullPointerException("setAttribute requires a non-null value."); } attributes.put(name, coerceToInterchangeValid(value)); return this; } }
/** * Creates a SafeScript from the given compile-time constant string {@code script}. */ public static SafeScript fromConstant(@CompileTimeConstant final String script) { if (script.length() == 0) { return SafeScript.EMPTY; } return create(script); }
/** Sets the {@code for} attribute for this element. */ public SafeHtmlBuilder setFor(@CompileTimeConstant final String value) { return setAttribute("for", value); }
/** Sets the {@code id} attribute for this element. */ public SafeHtmlBuilder setId(@CompileTimeConstant final String value) { return setAttribute("id", value); }