|
| 1 | +package tools.jackson.core; |
| 2 | + |
| 3 | +import tools.jackson.core.exc.StreamConstraintsException; |
| 4 | + |
| 5 | +/** |
| 6 | + * The constraints to use for streaming writes: used to guard against problematic |
| 7 | + * output by preventing processing of "too big" output constructs (values, |
| 8 | + * structures). |
| 9 | + * Constraints are registered with {@code TokenStreamFactory} (such as |
| 10 | + * {@code JsonFactory}); if nothing explicitly specified, default |
| 11 | + * constraints are used. |
| 12 | + *<p> |
| 13 | + * Currently constrained aspects, with default settings, are: |
| 14 | + * <ul> |
| 15 | + * <li>Maximum Nesting depth: default 1000 (see {@link #DEFAULT_MAX_DEPTH}) |
| 16 | + * </li> |
| 17 | + * </ul> |
| 18 | + * |
| 19 | + * @since 2.16 |
| 20 | + */ |
| 21 | +public class StreamWriteConstraints |
| 22 | + implements java.io.Serializable |
| 23 | +{ |
| 24 | + private static final long serialVersionUID = 1L; |
| 25 | + |
| 26 | + /** |
| 27 | + * Default setting for maximum depth: see {@link Builder#maxNestingDepth(int)} for details. |
| 28 | + */ |
| 29 | + public static final int DEFAULT_MAX_DEPTH = 1000; |
| 30 | + |
| 31 | + protected final int _maxNestingDepth; |
| 32 | + |
| 33 | + private static final StreamWriteConstraints DEFAULT = |
| 34 | + new StreamWriteConstraints(DEFAULT_MAX_DEPTH); |
| 35 | + |
| 36 | + public static final class Builder { |
| 37 | + private int maxNestingDepth; |
| 38 | + |
| 39 | + /** |
| 40 | + * Sets the maximum nesting depth. The depth is a count of objects and arrays that have not |
| 41 | + * been closed, `{` and `[` respectively. |
| 42 | + * |
| 43 | + * @param maxNestingDepth the maximum depth |
| 44 | + * |
| 45 | + * @return this builder |
| 46 | + * @throws IllegalArgumentException if the maxNestingDepth is set to a negative value |
| 47 | + */ |
| 48 | + public Builder maxNestingDepth(final int maxNestingDepth) { |
| 49 | + if (maxNestingDepth < 0) { |
| 50 | + throw new IllegalArgumentException("Cannot set maxNestingDepth to a negative value"); |
| 51 | + } |
| 52 | + this.maxNestingDepth = maxNestingDepth; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + Builder() { |
| 57 | + this(DEFAULT_MAX_DEPTH); |
| 58 | + } |
| 59 | + |
| 60 | + Builder(final int maxNestingDepth) { |
| 61 | + this.maxNestingDepth = maxNestingDepth; |
| 62 | + } |
| 63 | + |
| 64 | + Builder(StreamWriteConstraints src) { |
| 65 | + maxNestingDepth = src._maxNestingDepth; |
| 66 | + } |
| 67 | + |
| 68 | + public StreamWriteConstraints build() { |
| 69 | + return new StreamWriteConstraints(maxNestingDepth); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + /********************************************************************** |
| 75 | + /* Life-cycle |
| 76 | + /********************************************************************** |
| 77 | + */ |
| 78 | + |
| 79 | + protected StreamWriteConstraints(final int maxNestingDepth) { |
| 80 | + _maxNestingDepth = maxNestingDepth; |
| 81 | + } |
| 82 | + |
| 83 | + public static Builder builder() { |
| 84 | + return new Builder(); |
| 85 | + } |
| 86 | + |
| 87 | + public static StreamWriteConstraints defaults() { |
| 88 | + return DEFAULT; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return New {@link Builder} initialized with settings of this constraints |
| 93 | + * instance |
| 94 | + */ |
| 95 | + public Builder rebuild() { |
| 96 | + return new Builder(this); |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + /********************************************************************** |
| 101 | + /* Accessors |
| 102 | + /********************************************************************** |
| 103 | + */ |
| 104 | + |
| 105 | + /** |
| 106 | + * Accessor for maximum depth. |
| 107 | + * see {@link Builder#maxNestingDepth(int)} for details. |
| 108 | + * |
| 109 | + * @return Maximum allowed depth |
| 110 | + */ |
| 111 | + public int getMaxNestingDepth() { |
| 112 | + return _maxNestingDepth; |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + /********************************************************************** |
| 117 | + /* Convenience methods for validation, document limits |
| 118 | + /********************************************************************** |
| 119 | + */ |
| 120 | + |
| 121 | + /** |
| 122 | + * Convenience method that can be used to verify that the |
| 123 | + * nesting depth does not exceed the maximum specified by this |
| 124 | + * constraints object: if it does, a |
| 125 | + * {@link StreamConstraintsException} |
| 126 | + * is thrown. |
| 127 | + * |
| 128 | + * @param depth count of unclosed objects and arrays |
| 129 | + * |
| 130 | + * @throws StreamConstraintsException If depth exceeds maximum |
| 131 | + */ |
| 132 | + public void validateNestingDepth(int depth) throws StreamConstraintsException |
| 133 | + { |
| 134 | + if (depth > _maxNestingDepth) { |
| 135 | + throw _constructException( |
| 136 | + "Document nesting depth (%d) exceeds the maximum allowed (%d, from %s)", |
| 137 | + depth, _maxNestingDepth, |
| 138 | + _constrainRef("getMaxNestingDepth")); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /* |
| 143 | + /********************************************************************** |
| 144 | + /* Error reporting |
| 145 | + /********************************************************************** |
| 146 | + */ |
| 147 | + |
| 148 | + // @since 2.16 |
| 149 | + protected StreamConstraintsException _constructException(String msgTemplate, Object... args) throws StreamConstraintsException { |
| 150 | + throw new StreamConstraintsException(String.format(msgTemplate, args)); |
| 151 | + } |
| 152 | + |
| 153 | + // @since 2.16 |
| 154 | + protected String _constrainRef(String method) { |
| 155 | + return "`StreamWriteConstraints."+method+"()`"; |
| 156 | + } |
| 157 | +} |
0 commit comments