@Test public void testGetContextUrlWithMapEscaping() throws Exception { RequestContext context = new RequestContext(this.exchange, this.model, this.applicationContext); Map<String, Object> map = new HashMap<>(); map.put("foo", "bar baz"); map.put("spam", "&bucket="); assertEquals("/foo/bar%20baz?spam=%26bucket%3D", context.getContextUrl("{foo}?spam={spam}", map)); }
/** * Return a context-aware URl for the given relative URL. * @param relativeUrl the relative URL part * @return a URL that points back to the current web application with an * absolute path also URL-encoded accordingly */ public String getContextUrl(String relativeUrl) { String url = StringUtils.applyRelativePath(getContextPath() + "/", relativeUrl); return getExchange().transformUrl(url); }
/** * Retrieve the Errors instance for the given bind object, using the * "defaultHtmlEscape" setting. * @param name name of the bind object * @return the Errors instance, or {@code null} if not found */ @Nullable public Errors getErrors(String name) { return getErrors(name, isDefaultHtmlEscape()); }
/** * Retrieve the message for the given code, using the "defaultHtmlEscape" setting. * @param code code of the message * @return the message * @throws org.springframework.context.NoSuchMessageException if not found */ public String getMessage(String code) throws NoSuchMessageException { return getMessage(code, null, isDefaultHtmlEscape()); }
/** * Extract the error messages from the ObjectError list. */ private String[] initErrorMessages() throws NoSuchMessageException { if (this.errorMessages == null) { if (this.objectErrors != null) { this.errorMessages = new String[this.objectErrors.size()]; for (int i = 0; i < this.objectErrors.size(); i++) { ObjectError error = this.objectErrors.get(i); this.errorMessages[i] = this.requestContext.getMessage(error, this.htmlEscape); } } else { this.errorMessages = new String[0]; } } return this.errorMessages; }
this.errors = requestContext.getErrors(beanName, false); Object target = requestContext.getModelObject(beanName); if (target == null) { throw new IllegalStateException(
/** * Create a BindStatus for the given bind object using the * "defaultHtmlEscape" setting. * @param path the bean and property path for which values and errors will * be resolved (e.g. "person.age") * @return the new BindStatus instance * @throws IllegalStateException if no corresponding Errors object found */ public BindStatus getBindStatus(String path) throws IllegalStateException { return new BindStatus(this, path, isDefaultHtmlEscape()); }
/** * Create a RequestContext to expose under the specified attribute name. * <p>The default implementation creates a standard RequestContext instance * for the given request and model. Can be overridden in subclasses for * custom instances. * @param exchange current exchange * @param model combined output Map (never {@code null}), * with dynamic values taking precedence over static attributes * @return the RequestContext instance * @see #setRequestContextAttribute */ protected RequestContext createRequestContext(ServerWebExchange exchange, Map<String, Object> model) { return new RequestContext(exchange, model, obtainApplicationContext(), getRequestDataValueProcessor()); }
@Override public Optional<Errors> getErrors(final String name, final boolean htmlEscape) { return Optional.ofNullable(this.requestContext.getErrors(name, htmlEscape)); }
@Override public String getContextPath() { return this.requestContext.getContextPath(); }
/** * Retrieve the Errors instance for the given bind object. * @param name name of the bind object * @param htmlEscape create an Errors instance with automatic HTML escaping? * @return the Errors instance, or {@code null} if not found */ @Nullable public Errors getErrors(String name, boolean htmlEscape) { if (this.errorsMap == null) { this.errorsMap = new HashMap<>(); } Errors errors = this.errorsMap.get(name); if (errors == null) { errors = getModelObject(BindingResult.MODEL_KEY_PREFIX + name); if (errors == null) { return null; } } if (errors instanceof BindException) { errors = ((BindException) errors).getBindingResult(); } if (htmlEscape && !(errors instanceof EscapedErrors)) { errors = new EscapedErrors(errors); } else if (!htmlEscape && errors instanceof EscapedErrors) { errors = ((EscapedErrors) errors).getSource(); } this.errorsMap.put(name, errors); return errors; }
@Override public String getContextUrl(final String relativeUrl) { return this.requestContext.getContextUrl(relativeUrl); }
@Override public void changeLocale(final Locale locale) { this.requestContext.changeLocale(locale); }
/** * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the "defaultHtmlEscape" setting. * @param resolvable the MessageSourceResolvable * @return the message * @throws org.springframework.context.NoSuchMessageException if not found */ public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException { return getMessage(resolvable, isDefaultHtmlEscape()); }
@Override public String getMessage(final String code, final Object[] args, final boolean htmlEscape) throws NoSuchMessageException { return this.requestContext.getMessage(code, args, htmlEscape); }
this.errors = requestContext.getErrors(beanName, false).orElse(null); Object target = requestContext.getModelObject(beanName); if (target == null) { throw new IllegalStateException(
@Override public boolean isDefaultHtmlEscape() { return this.requestContext.isDefaultHtmlEscape(); }
/** * Create a RequestContext to expose under the specified attribute name. * <p>The default implementation creates a standard RequestContext instance for the * given request and model. Can be overridden in subclasses for custom instances. * @param exchange current exchange * @param model combined output Map (never {@code null}), * with dynamic values taking precedence over static attributes * @return the RequestContext instance * @see #setRequestContextAttribute */ protected RequestContext createRequestContext(ServerWebExchange exchange, Map<String, Object> model) { return new RequestContext(exchange, model, getApplicationContext(), getRequestDataValueProcessor()); }
@Override public Optional<Errors> getErrors(final String name) { return Optional.ofNullable(this.requestContext.getErrors(name)); }
optional = optional.isPresent() ? optional : getModelObject(BindingResult.MODEL_KEY_PREFIX + name);