001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.reactive.result.view;
018
019import java.util.Locale;
020
021import org.springframework.beans.factory.InitializingBean;
022import org.springframework.lang.Nullable;
023
024/**
025 * Abstract base class for URL-based views. Provides a consistent way of
026 * holding the URL that a View wraps, in the form of a "url" bean property.
027 *
028 * @author Rossen Stoyanchev
029 * @since 5.0
030 */
031public abstract class AbstractUrlBasedView extends AbstractView implements InitializingBean {
032
033        @Nullable
034        private String url;
035
036
037        /**
038         * Constructor for use as a bean.
039         */
040        protected AbstractUrlBasedView() {
041        }
042
043        /**
044         * Create a new AbstractUrlBasedView with the given URL.
045         */
046        protected AbstractUrlBasedView(String url) {
047                this.url = url;
048        }
049
050
051        /**
052         * Set the URL of the resource that this view wraps.
053         * The URL must be appropriate for the concrete View implementation.
054         */
055        public void setUrl(@Nullable String url) {
056                this.url = url;
057        }
058
059        /**
060         * Return the URL of the resource that this view wraps.
061         */
062        @Nullable
063        public String getUrl() {
064                return this.url;
065        }
066
067
068        @Override
069        public void afterPropertiesSet() throws Exception {
070                if (getUrl() == null) {
071                        throw new IllegalArgumentException("Property 'url' is required");
072                }
073        }
074
075        /**
076         * Check whether the resource for the configured URL actually exists.
077         * @param locale the desired Locale that we're looking for
078         * @return {@code false} if the resource exists
079         * {@code false} if we know that it does not exist
080         * @throws Exception if the resource exists but is invalid (e.g. could not be parsed)
081         */
082        public abstract boolean checkResourceExists(Locale locale) throws Exception;
083
084
085        @Override
086        public String toString() {
087                return super.toString() + "; URL [" + getUrl() + "]";
088        }
089
090}