Spring Webflux 源码阅读之 result包

Package org.springframework.web.reactive.result

支持各种编程模型样式,包括调用不同类型的handles

handlerResultSupport.png

HandlerResultHandlerSupport

HandlerResultHandler的基类,支持内容协商和访问ReactiveAdapter注册表。

    public abstract class HandlerResultHandlerSupport implements Ordered {

private static final MediaType MEDIA_TYPE_APPLICATION_ALL = new MediaType("application");


private final RequestedContentTypeResolver contentTypeResolver;

private final ReactiveAdapterRegistry adapterRegistry;

private int order = LOWEST_PRECEDENCE;


protected HandlerResultHandlerSupport(RequestedContentTypeResolver contentTypeResolver,
        ReactiveAdapterRegistry adapterRegistry) {

    Assert.notNull(contentTypeResolver, "RequestedContentTypeResolver is required");
    Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required");
    this.contentTypeResolver = contentTypeResolver;
    this.adapterRegistry = adapterRegistry;
}


/**
 * Return the configured {@link ReactiveAdapterRegistry}.
 */
public ReactiveAdapterRegistry getAdapterRegistry() {
    return this.adapterRegistry;
}

/**
 * Return the configured {@link RequestedContentTypeResolver}.
 */
public RequestedContentTypeResolver getContentTypeResolver() {
    return this.contentTypeResolver;
}

/**
 * Set the order for this result handler relative to others.
 * <p>By default set to {@link Ordered#LOWEST_PRECEDENCE}, however see
 * Javadoc of sub-classes which may change this default.
 * @param order the order
 */
public void setOrder(int order) {
    this.order = order;
}

@Override
public int getOrder() {
    return this.order;
}


/**
 * Get a {@code ReactiveAdapter} for the top-level return value type.
 * @return the matching adapter or {@code null}
 */
@Nullable
protected ReactiveAdapter getAdapter(HandlerResult result) {
    Class<?> returnType = result.getReturnType().getRawClass();
    return getAdapterRegistry().getAdapter(returnType, result.getReturnValue());
}

/**
 * Select the best media type for the current request through a content
 * negotiation algorithm.
 * @param exchange the current request
 * @param producibleTypesSupplier the media types that can be produced for the current request
 * @return the selected media type or {@code null}
 */
@Nullable
protected MediaType selectMediaType(ServerWebExchange exchange,
        Supplier<List<MediaType>> producibleTypesSupplier) {

    List<MediaType> acceptableTypes = getAcceptableTypes(exchange);
    List<MediaType> producibleTypes = getProducibleTypes(exchange, producibleTypesSupplier);

    Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
    for (MediaType acceptable : acceptableTypes) {
        for (MediaType producible : producibleTypes) {
            if (acceptable.isCompatibleWith(producible)) {
                compatibleMediaTypes.add(selectMoreSpecificMediaType(acceptable, producible));
            }
        }
    }

    List<MediaType> result = new ArrayList<>(compatibleMediaTypes);
    MediaType.sortBySpecificityAndQuality(result);

    for (MediaType mediaType : result) {
        if (mediaType.isConcrete()) {
            return mediaType;
        }
        else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) {
            return MediaType.APPLICATION_OCTET_STREAM;
        }
    }

    return null;
}

private List<MediaType> getAcceptableTypes(ServerWebExchange exchange) {
    List<MediaType> mediaTypes = getContentTypeResolver().resolveMediaTypes(exchange);
    return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes);
}

@SuppressWarnings("unchecked")
private List<MediaType> getProducibleTypes(ServerWebExchange exchange,
        Supplier<List<MediaType>> producibleTypesSupplier) {

    Set<MediaType> mediaTypes = exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
    return (mediaTypes != null ? new ArrayList<>(mediaTypes) : producibleTypesSupplier.get());
}

private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
    producible = producible.copyQualityValue(acceptable);
    Comparator<MediaType> comparator = MediaType.SPECIFICITY_COMPARATOR;
    return (comparator.compare(acceptable, producible) <= 0 ? acceptable : producible);
}

}

接口 View的Diagram

view.png

View

通过视图解析支持结果处理。

将HandlerResult呈现给HTTP响应的约定。

与Encoder相比,Encoder是一个单实例对象,并对给定类型的任何对象进行编码,因此,视图通常是通过名称来选择的,并使用ViewResolver来解析,例如将其与HTML模板匹配。此外,视图可以基于模型中包含的多个属性呈现。

视图还可以选择从模型中选择一个属性,使用任何现有的编码器来呈现替代媒体类型。

返回此视图支持的媒体类型列表,或空列表。
List<MediaType> getSupportedMediaTypes();

这个视图是否通过执行重定向来呈现。
default boolean isRedirectView() {
    return false;
}

根据给定的HandlerResult呈现视图。实现可以访问和使用模型,或者仅在其中使用一个特定的属性。
Mono<Void> render(@Nullable Map<String, ?> model, @Nullable MediaType contentType, ServerWebExchange exchange);

AbstractView

View实现的基类。


public abstract class AbstractView implements View, ApplicationContextAware {

    /**  在 bean factory 有名的RequestDataValueProcessor */
    public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";


    /** 可用于子类的日志记录器 */
    protected final Log logger = LogFactory.getLog(getClass());

    private static final Object NO_VALUE = new Object();


    private final List<MediaType> mediaTypes = new ArrayList<>(4);

    private final ReactiveAdapterRegistry adapterRegistry;

    private Charset defaultCharset = StandardCharsets.UTF_8;

    @Nullable
    private String requestContextAttribute;

    @Nullable
    private ApplicationContext applicationContext;


    public AbstractView() {
        this(new ReactiveAdapterRegistry());
    }

    public AbstractView(ReactiveAdapterRegistry registry) {
        this.mediaTypes.add(ViewResolverSupport.DEFAULT_CONTENT_TYPE);
        this.adapterRegistry = registry;
    }


    /**
     * Set the supported media types for this view.
     * Default is "text/html;charset=UTF-8".
     */
    public void setSupportedMediaTypes(@Nullable List<MediaType> supportedMediaTypes) {
        Assert.notEmpty(supportedMediaTypes, "MediaType List must not be empty");
        this.mediaTypes.clear();
        if (supportedMediaTypes != null) {
            this.mediaTypes.addAll(supportedMediaTypes);
        }
    }

    /**
     * Return the configured media types supported by this view.
     */
    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return this.mediaTypes;
    }

    /**
     * Set the default charset for this view, used when the
     * {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
     * Default is {@linkplain StandardCharsets#UTF_8 UTF 8}.
     */
    public void setDefaultCharset(Charset defaultCharset) {
        Assert.notNull(defaultCharset, "'defaultCharset' must not be null");
        this.defaultCharset = defaultCharset;
    }

    /**
     * Return the default charset, used when the
     * {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
     */
    public Charset getDefaultCharset() {
        return this.defaultCharset;
    }

    /**
     * Set the name of the RequestContext attribute for this view.
     * Default is none.
     */
    public void setRequestContextAttribute(@Nullable String requestContextAttribute) {
        this.requestContextAttribute = requestContextAttribute;
    }

    /**
     * Return the name of the RequestContext attribute, if any.
     */
    @Nullable
    public String getRequestContextAttribute() {
        return this.requestContextAttribute;
    }

    @Override
    public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Nullable
    public ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }

    /**
     * Obtain the ApplicationContext for actual use.
     * @return the ApplicationContext (never {@code null})
     * @throws IllegalStateException in case of no ApplicationContext set
     */
    protected final ApplicationContext obtainApplicationContext() {
        ApplicationContext applicationContext = getApplicationContext();
        Assert.state(applicationContext != null, "No ApplicationContext");
        return applicationContext;
    }


    /**
     * Prepare the model to render.
     * @param model Map with name Strings as keys and corresponding model
     * objects as values (Map can also be {@code null} in case of empty model)
     * @param contentType the content type selected to render with which should
     * match one of the {@link #getSupportedMediaTypes() supported media types}.
     * @param exchange the current exchange
     * @return {@code Mono} to represent when and if rendering succeeds
     */
    @Override
    public Mono<Void> render(@Nullable Map<String, ?> model, @Nullable MediaType contentType,
            ServerWebExchange exchange) {

        if (logger.isTraceEnabled()) {
            logger.trace("Rendering view with model " + model);
        }

        if (contentType != null) {
            exchange.getResponse().getHeaders().setContentType(contentType);
        }

        return getModelAttributes(model, exchange).flatMap(mergedModel -> {
            // Expose RequestContext?
            if (this.requestContextAttribute != null) {
                mergedModel.put(this.requestContextAttribute, createRequestContext(exchange, mergedModel));
            }
            return renderInternal(mergedModel, contentType, exchange);
        });
    }

    /**
     * Prepare the model to use for rendering.
     * <p>The default implementation creates a combined output Map that includes
     * model as well as static attributes with the former taking precedence.
     */
    protected Mono<Map<String, Object>> getModelAttributes(@Nullable Map<String, ?> model,
            ServerWebExchange exchange) {

        int size = (model != null ? model.size() : 0);

        Map<String, Object> attributes = new LinkedHashMap<>(size);
        if (model != null) {
            attributes.putAll(model);
        }

        return resolveAsyncAttributes(attributes).then(Mono.just(attributes));
    }

    /**
     * By default, resolve async attributes supported by the
     * {@link ReactiveAdapterRegistry} to their blocking counterparts.
     * <p>View implementations capable of taking advantage of reactive types
     * can override this method if needed.
     * @return {@code Mono} for the completion of async attributes resolution
     */
    protected Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {

        List<String> names = new ArrayList<>();
        List<Mono<?>> valueMonos = new ArrayList<>();

        for (Map.Entry<String, ?> entry : model.entrySet()) {
            Object value =  entry.getValue();
            if (value == null) {
                continue;
            }
            ReactiveAdapter adapter = this.adapterRegistry.getAdapter(null, value);
            if (adapter != null) {
                names.add(entry.getKey());
                if (adapter.isMultiValue()) {
                    Flux<Object> fluxValue = Flux.from(adapter.toPublisher(value));
                    valueMonos.add(fluxValue.collectList().defaultIfEmpty(Collections.emptyList()));
                }
                else {
                    Mono<Object> monoValue = Mono.from(adapter.toPublisher(value));
                    valueMonos.add(monoValue.defaultIfEmpty(NO_VALUE));
                }
            }
        }

        if (names.isEmpty()) {
            return Mono.empty();
        }

        return Mono.zip(valueMonos,
                values -> {
                    for (int i=0; i < values.length; i++) {
                        if (values[i] != NO_VALUE) {
                            model.put(names.get(i), values[i]);
                        }
                        else {
                            model.remove(names.get(i));
                        }
                    }
                    return NO_VALUE;
                })
                .then();
    }

    /**
     * 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());
    }

    /**
     * Return the {@link RequestDataValueProcessor} to use.
     * <p>The default implementation looks in the {@link #getApplicationContext()
     * Spring configuration} for a {@code RequestDataValueProcessor} bean with
     * the name {@link #REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME}.
     * @return the RequestDataValueProcessor, or null if there is none at the
     * application context.
     */
    @Nullable
    protected RequestDataValueProcessor getRequestDataValueProcessor() {
        ApplicationContext context = getApplicationContext();
        if (context != null && context.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
            return context.getBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
        }
        return null;
    }

    /**
     * Subclasses must implement this method to actually render the view.
     * @param renderAttributes combined output Map (never {@code null}),
     * with dynamic values taking precedence over static attributes
     * @param contentType the content type selected to render with which should
     * match one of the {@link #getSupportedMediaTypes() supported media types}.
     *@param exchange current exchange  @return {@code Mono} to represent when
     * and if rendering succeeds
     */
    protected abstract Mono<Void> renderInternal(Map<String, Object> renderAttributes,
            @Nullable MediaType contentType, ServerWebExchange exchange);


    @Override
    public String toString() {
        return getClass().getName();
    }

}

ViewResolver

ViewResolver.png

Rendering

reanding.jpg

SimpleHandlerAdapter

HandlerAdapter,它允许使用普通的WebHandler与一般的DispatcherHandler一起使用。



public class SimpleHandlerAdapter implements HandlerAdapter {

    private static final MethodParameter RETURN_TYPE;

    static {
        try {
            Method method = WebHandler.class.getMethod("handle", ServerWebExchange.class);
            RETURN_TYPE = new MethodParameter(method, -1);
        }
        catch (NoSuchMethodException ex) {
            throw new IllegalStateException(
                    "Failed to initialize the return type for WebHandler: " + ex.getMessage());
        }
    }

        //这个HandlerAdapter是否支持给定的handler。
    @Override
    public boolean supports(Object handler) {
        return WebHandler.class.isAssignableFrom(handler.getClass());
    }

      //鼓励实现处理由调用handler所产生的异常,并在必要时返回代表错误响应的替代结果。
    @Override
    public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
        WebHandler webHandler = (WebHandler) handler;
        Mono<Void> mono = webHandler.handle(exchange);
        return mono.then(Mono.empty());
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容