- Direct Known Subclasses:
ConfigAdvancedHandler, ConfigBanHandler, ConfigClientsHandler, ConfigFamilyHandler, ConfigHomeHandler, ConfigKeyringHandler, ConfigLoggingHandler, ConfigNetHandler, ConfigPeerHandler, ConfigReseedHandler, ConfigServiceHandler, ConfigSidebarHandler, ConfigStatsHandler, ConfigTunnelsHandler, ConfigUIHandler, ConfigUpdateHandler, EventLogHelper, GraphHelper, NetDbHelper, TuningFormHandler, WizardHandler
The form is "processed" after the properties are set and the first output property is retrieved - either getAll(), getNotices() or getErrors().
This Handler will only process a single POST. The jsp bean must be declared scope=request.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMessage class to encapsulate the message text and the canClose flag.(package private) static classOne window's POST count.Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringForm action.protected RouterContextRouter context.private final List<FormHandler.Message> The error messages to display.protected LogClass logger.protected StringRequest method.private StringThe form nonce to check.private final List<FormHandler.Message> The notice messages to display.protected WriterOutput writer.private booleanTrue once the form was processed.protected RequestWrapperOnly for multipart/form-data.protected HttpSessionHTTP session for nonce validation.protected MapNot for multipart/form-data, will be null.private booleanTrue when the form input was valid.(package private) static final StringThe single budget key the console charges POSTs to.(package private) static final ConcurrentHashMap<String, FormHandler.RateLimitEntry> Live budgets, keyed by the value passed to checkRateLimit().(package private) static final intPOSTs admitted per window per key before further ones are refused.private static final intAbove this many budgets, drop the idle ones before adding another.(package private) static final intLength of one rate-limit window, in ms.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionTranslate a string.Translate a string with a parameter This is a lot more expensive than _t(s), so use sparingly.Translate a string with two parameters.static StringMark a string for extraction by xgettext and translation.protected voidaddFormError(String errorMsg) Add an error message to display Use if it does not include a link.protected voidaddFormError(String errorMsg, boolean canClose) Add an error message to display Use if it does not include a link.protected voidAdd an error message to display Use if it includes a link or other formatting.protected voidaddFormErrorNoEscape(String msg, boolean canClose) Add an error message to display Use if it includes a link or other formatting.protected voidaddFormNotice(String msg) Add a non-error message to display Use if it does not include a link.protected voidaddFormNotice(String msg, boolean canClose) Add a non-error message to display Use if it does not include a link.protected voidAdd a non-error message to display Use if it includes a link or other formatting.protected voidaddFormNoticeNoEscape(String msg, boolean canClose) Add a non-error message to display Use if it includes a link or other formatting.(package private) static booleancheckRateLimit(String key, long now) Check and record one form submission against the budget for key.(package private) static voidcleanupRateLimits(long now) Remove budgets that have seen no POST for two full windows.private booleancontainsXSS(String val) Check for common XSS patterns.Display everything, wrap it in a div for consistent presentation.Display any error messages (processing the form if it hasn't been yet).protected StringgetJettyString(String key) setSettings(Map)must have been called previously Curses Jetty for returning arrays.Display any non-error messages (processing the form if it hasn't been yet).booleanSame as HelperBase.private voidprocess()Processes the submitted form.protected abstract voidImplement this to perform the final processing (in turn, adding formNotice and formError messages, etc)private static Stringrender(List<FormHandler.Message> source) Renders the messages as html.static StringresolveEffectiveAction(String action, Map<?, ?> settings) Resolve which operation the user actually requested.voidAction value.voidsetContextId(String contextId) Configure this bean to query a particular router context.voidNonce value.voidOnly set by formhandler.jsi for multipart/form-data.voidsetSettings(Map settings) For many forms, it's easiest just to put all the parameters here.voidstoreMethod(String val) Call this to prevent changes using GET.voidstoreSession(HttpSession session) For nonce validation.voidstoreWriter(Writer out) Store the output writer for inline form processing.private voidvalidate()Make sure the nonce was set correctly, otherwise someone could just create a link like /confignet.jsp?hostname=localhost and break the user's node (or worse).
Field Details
_context
Router context._log
Class logger._settings
Not for multipart/form-data, will be null._requestWrapper
Only for multipart/form-data. Warning, parameters are NOT XSS filtered._nonce
The form nonce to check._action
Form action._method
Request method._errors
The error messages to display._notices
The notice messages to display._processed
private boolean _processedTrue once the form was processed._valid
private boolean _validTrue when the form input was valid._out
Output writer._session
HTTP session for nonce validation.RATE_LIMIT_WINDOW_MS
static final int RATE_LIMIT_WINDOW_MSLength of one rate-limit window, in ms.The rate limiter for form submissions - it exists to slow down brute-force nonce guessing on a failed submission. One budget per key; see validate() for what the key is.
- See Also:
RATE_LIMIT_MAX_POSTS
static final int RATE_LIMIT_MAX_POSTSPOSTs admitted per window per key before further ones are refused.- See Also:
RATE_LIMIT_GLOBAL_KEY
The single budget key the console charges POSTs to.- See Also:
RATE_LIMIT_SWEEP_SIZE
private static final int RATE_LIMIT_SWEEP_SIZEAbove this many budgets, drop the idle ones before adding another.- See Also:
RATE_LIMIT_MAP
Live budgets, keyed by the value passed to checkRateLimit().
Constructor Details
FormHandler
public FormHandler()Starts a valid handler with no errors or notices.
Method Details
checkRateLimit
Check and record one form submission against the budget for key.Package-visible, and takes the clock as an argument, so a test can walk a whole window and its rollover without sleeping.
- Parameters:
key- the budget to chargenow- current time in ms- Returns:
- true if the submission is allowed; a null or empty key is allowed unconditionally, because there is nothing to charge it against
cleanupRateLimits
static void cleanupRateLimits(long now) Remove budgets that have seen no POST for two full windows.Driven from the map's own size rather than a thread started in a static initializer: with one budget there is nothing to reclaim, and a thread that outlives every request and never stops is not a price worth paying for it.
- Parameters:
now- current time in ms
storeSession
For nonce validation.- Parameters:
session- the HTTP session
setContextId
Configure this bean to query a particular router context.- Parameters:
contextId- beginning few characters of the routerHash, or null to pick the first one we come across.
setNonce
setAction
setSettings
For many forms, it's easiest just to put all the parameters here.- Parameters:
settings- the form parameters
setRequestWrapper
Only set by formhandler.jsi for multipart/form-data.- Parameters:
rw- the request wrapper
isAdvanced
public boolean isAdvanced()Same as HelperBase.- Returns:
- true if advanced mode
getJettyString
setSettings(Map)must have been called previously Curses Jetty for returning arrays. For nofilter_ prefixed params, validates for XSS patterns.- Parameters:
key- the setting key- Returns:
- trimmed string or null
resolveEffectiveAction
Resolve which operation the user actually requested.Forms commonly carry both a hidden action=blah fallback (so Enter-key submission triggers the default save) and real submit buttons named action with their own values. When a button is clicked, the browser submits BOTH values and the servlet layer exposes only the first one - the hidden blah - to
setAction(String), which would silently run the default save instead of the requested operation. This helper scans the raw parameter array for any real action value before falling back.- Parameters:
action- the single action value seen by setAction(), may be nullsettings- raw parameter map as passed to setSettings(), may be null- Returns:
- the clicked operation if present, else the fallback, else null
containsXSS
Check for common XSS patterns.- Parameters:
val- the value to test; null or empty is not XSS- Returns:
- true if the value contains a script tag, inline event handler or another common XSS pattern
storeMethod
Call this to prevent changes using GET.- Parameters:
val- the request method
storeWriter
Store the output writer for inline form processing.- Parameters:
out- the writer inline form processing writes its output to
processForm
protected abstract void processForm()Implement this to perform the final processing (in turn, adding formNotice and formError messages, etc)Will only be called if _action is non-null and the nonce is valid.
addFormError
Add an error message to display Use if it does not include a link. Escapes '<' and '>' before queueing.- Parameters:
errorMsg- the error message
addFormError
Add an error message to display Use if it does not include a link. Escapes '<' and '>' before queueing.- Parameters:
errorMsg- the error messagecanClose- If true, the message can be closed by the user.
addFormNotice
Add a non-error message to display Use if it does not include a link. Escapes '<' and '>' before queueing.- Parameters:
msg- the notice message
addFormNotice
Add a non-error message to display Use if it does not include a link. Escapes '<' and '>' before queueing.- Parameters:
msg- the notice messagecanClose- If true, the message can be closed by the user.
addFormNoticeNoEscape
Add a non-error message to display Use if it includes a link or other formatting. Does not escape '<' and '>' before queueing.- Parameters:
msg- the notice message
addFormNoticeNoEscape
Add a non-error message to display Use if it includes a link or other formatting. Does not escape '<' and '>' before queueing.- Parameters:
msg- the notice messagecanClose- If true, the message can be closed by the user.
addFormErrorNoEscape
Add an error message to display Use if it includes a link or other formatting. Does not escape '<' and '>' before queueing.- Parameters:
msg- the error message
addFormErrorNoEscape
Add an error message to display Use if it includes a link or other formatting. Does not escape '<' and '>' before queueing.- Parameters:
msg- the error messagecanClose- If true, the message can be closed by the user.
getAllMessages
Display everything, wrap it in a div for consistent presentation.- Returns:
- the queued errors and notices wrapped in a div, or an empty string if there are none
getErrors
Display any error messages (processing the form if it hasn't been yet).- Returns:
- an unordered list of the queued error messages, or an empty string if there are none
getNotices
Display any non-error messages (processing the form if it hasn't been yet).- Returns:
- an unordered list of the queued notice messages, or an empty string if there are none
validate
private void validate()Make sure the nonce was set correctly, otherwise someone could just create a link like /confignet.jsp?hostname=localhost and break the user's node (or worse).process
private void process()Processes the submitted form.render
Renders the messages as html.- Parameters:
source- the messages to render- Returns:
- the html text
_t
_t
Translate a string with a parameter This is a lot more expensive than _t(s), so use sparingly.- Parameters:
s- string to be translated containing {0} The {0} will be replaced by the parameter. Single quotes must be doubled, i.e. ' -> '' in the string.o- parameter, not translated. To translate parameter also, use _t("foo {0} bar", _t("baz")) Do not double the single quotes in the parameter. Use autoboxing to call with ints, longs, floats, etc.- Returns:
- the translated string
_t
_x