001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.settings;
018
019import java.lang.ref.WeakReference;
020
021import org.apache.wicket.Page;
022import org.apache.wicket.application.DefaultClassResolver;
023import org.apache.wicket.application.IClassResolver;
024import org.apache.wicket.feedback.DefaultCleanupFeedbackMessageFilter;
025import org.apache.wicket.feedback.IFeedbackMessageFilter;
026import org.apache.wicket.request.cycle.RequestCycle;
027import org.apache.wicket.util.lang.Args;
028import org.apache.wicket.util.lang.Bytes;
029
030/**
031 * * Settings class for application settings.
032 * <p>
033 * <i>internalErrorPage </i>- You can override this with your own page class to display internal
034 * errors in a different way.
035 * <p>
036 * <i>pageExpiredErrorPage </i>- You can override this with your own bookmarkable page class to
037 * display expired page errors in a different way. You can set property homePageRenderStrategy to
038 * choose from different ways the home page url shows up in your browser.
039 * <p>
040 * <b>A Converter Factory </b>- By overriding getConverterFactory(), you can provide your own
041 * factory which creates locale sensitive Converter instances.
042 *
043 * @author Jonathan Locke
044 * @author Chris Turner
045 * @author Eelco Hillenius
046 * @author Juergen Donnerstag
047 * @author Johan Compagner
048 * @author Igor Vaynberg (ivaynberg)
049 * @author Martijn Dashorst
050 * @author James Carman
051 */
052public class ApplicationSettings
053{
054        private WeakReference<Class<? extends Page>> accessDeniedPage;
055
056        private IClassResolver classResolver = new DefaultClassResolver();
057
058        private WeakReference<Class<? extends Page>> internalErrorPage;
059
060        private WeakReference<Class<? extends Page>> pageExpiredErrorPage;
061
062        private Bytes defaultMaximumUploadSize = Bytes.MAX;
063
064        private boolean uploadProgressUpdatesEnabled = false;
065
066        private IFeedbackMessageFilter feedbackMessageCleanupFilter = new DefaultCleanupFeedbackMessageFilter();
067
068        /**
069         * Gets the access denied page class.
070         *
071         * @return Returns the accessDeniedPage.
072         */
073        public Class<? extends Page> getAccessDeniedPage()
074        {
075                return accessDeniedPage.get();
076        }
077
078        /**
079         * Gets the default resolver to use when finding classes and resources.
080         *
081         * @return Default class resolver
082         */
083        public IClassResolver getClassResolver()
084        {
085                return classResolver;
086        }
087
088        /**
089         * Gets the default maximum size for uploads. This is used by {@link org.apache.wicket.markup.html.form.Form#getMaxSize()} if no
090         * value is explicitly set through {@link org.apache.wicket.markup.html.form.Form#setMaxSize(Bytes)}.
091         *
092         * @return the default maximum size for uploads
093         */
094        public Bytes getDefaultMaximumUploadSize()
095        {
096                return defaultMaximumUploadSize;
097        }
098
099        /**
100         * Gets internal error page class.
101         *
102         * @return Returns the internalErrorPage.
103         */
104        public Class<? extends Page> getInternalErrorPage()
105        {
106                return internalErrorPage.get();
107        }
108
109        /**
110         * Gets the page expired page class.
111         *
112         * @return Returns the pageExpiredErrorPage.
113         */
114        public Class<? extends Page> getPageExpiredErrorPage()
115        {
116                return pageExpiredErrorPage.get();
117        }
118
119        /**
120         * Gets whether wicket is providing updates about the upload progress or not.
121         *
122         * @return if true upload progress monitoring is enabled
123         */
124        public boolean isUploadProgressUpdatesEnabled()
125        {
126                return uploadProgressUpdatesEnabled;
127        }
128
129        /**
130         * Sets the access denied page class. The class must be bookmarkable and must extend Page.
131         *
132         * @param accessDeniedPage
133         *            The accessDeniedPage to set.
134         * @return {@code this} object for chaining
135         */
136        public ApplicationSettings setAccessDeniedPage(Class<? extends Page> accessDeniedPage)
137        {
138                if (accessDeniedPage == null)
139                {
140                        throw new IllegalArgumentException("Argument accessDeniedPage may not be null");
141                }
142                checkPageClass(accessDeniedPage);
143
144                this.accessDeniedPage = new WeakReference<Class<? extends Page>>(accessDeniedPage);
145                return this;
146        }
147
148        /**
149         * Sets the default class resolver to use when finding classes and resources.
150         *
151         * @param defaultClassResolver
152         *            The default class resolver
153         * @return {@code this} object for chaining
154         */
155        public ApplicationSettings setClassResolver(final IClassResolver defaultClassResolver)
156        {
157                classResolver = defaultClassResolver;
158                return this;
159        }
160
161        /**
162         * Sets the default maximum size for uploads. This is used by {@link org.apache.wicket.markup.html.form.Form#getMaxSize()} if no
163         * value is explicitly set through {@link org.apache.wicket.markup.html.form.Form#setMaxSize(Bytes)}.
164         *
165         * @param defaultMaximumUploadSize
166         *            the default maximum size for uploads
167         * @return {@code this} object for chaining
168         */
169        public ApplicationSettings setDefaultMaximumUploadSize(Bytes defaultMaximumUploadSize)
170        {
171                this.defaultMaximumUploadSize = defaultMaximumUploadSize;
172                return this;
173        }
174
175        /**
176         * Sets internal error page class. The class must be bookmarkable and must extend {@link Page}.
177         *
178         * <p>Consider using custom
179         * {@link org.apache.wicket.request.cycle.IRequestCycleListener#onException(RequestCycle, Exception) request
180         * cycle listener} if you need to pass some extra information to the error page. By using
181         * {@link org.apache.wicket.request.cycle.IRequestCycleListener} the application has more flexibility in
182         * the instantiation of the error page.</p>
183         *
184         * @param internalErrorPage
185         *            The internalErrorPage to set.
186         * @return {@code this} object for chaining
187         * @see org.apache.wicket.request.cycle.IRequestCycleListener#onException(RequestCycle, Exception)
188         */
189        public ApplicationSettings setInternalErrorPage(final Class<? extends Page> internalErrorPage)
190        {
191                Args.notNull(internalErrorPage, "internalErrorPage");
192                checkPageClass(internalErrorPage);
193
194                this.internalErrorPage = new WeakReference<>(internalErrorPage);
195                return this;
196        }
197
198        /**
199         * Sets the page expired page class. The class must be bookmarkable and must extend Page.
200         *
201         * @param pageExpiredErrorPage
202         *            The pageExpiredErrorPage to set.
203         * @return {@code this} object for chaining
204         */
205        public ApplicationSettings setPageExpiredErrorPage(final Class<? extends Page> pageExpiredErrorPage)
206        {
207                Args.notNull(pageExpiredErrorPage, "pageExpiredErrorPage");
208                checkPageClass(pageExpiredErrorPage);
209
210                this.pageExpiredErrorPage = new WeakReference<>(pageExpiredErrorPage);
211                return this;
212        }
213
214        /**
215         * Sets whether wicket should provide updates about the upload progress or not.
216         *
217         * @param uploadProgressUpdatesEnabled
218         *            if true upload progress monitoring is enabled
219         * @return {@code this} object for chaining
220         */
221        public ApplicationSettings setUploadProgressUpdatesEnabled(boolean uploadProgressUpdatesEnabled)
222        {
223                this.uploadProgressUpdatesEnabled = uploadProgressUpdatesEnabled;
224                return this;
225        }
226
227        /**
228         * Throws an IllegalArgumentException if the given class is not a subclass of Page.
229         * 
230         * @param <C>
231         * @param pageClass
232         *            the page class to check
233         */
234        private <C extends Page> void checkPageClass(final Class<C> pageClass)
235        {
236                // NOTE: we can't really check on whether it is a bookmarkable page
237                // here, as - though the default is that a bookmarkable page must
238                // either have a default constructor and/or a constructor with a
239                // PageParameters object, this could be different for another
240                // IPageFactory implementation
241                if (!Page.class.isAssignableFrom(pageClass))
242                {
243                        throw new IllegalArgumentException("argument " + pageClass +
244                                " must be a subclass of Page");
245                }
246        }
247
248        /**
249         * Sets the cleanup feedback message filter. see {@link #getFeedbackMessageCleanupFilter()} for
250         * more details.
251         *
252         * @param filter
253         * @return {@code this} object for chaining
254         */
255        public ApplicationSettings setFeedbackMessageCleanupFilter(IFeedbackMessageFilter filter)
256        {
257                Args.notNull(filter, "filter");
258                feedbackMessageCleanupFilter = filter;
259                return this;
260        }
261
262        /**
263         * Returns the cleanup feedback message filter. At the end of request all messages are ran
264         * through this filter, and the ones accepted are removed. The default implementation accepts
265         * (and therefore removes) all rendered messages.
266         *
267         * @return feedback message filter
268         */
269        public IFeedbackMessageFilter getFeedbackMessageCleanupFilter()
270        {
271                return feedbackMessageCleanupFilter;
272        }
273}