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;
018
019import java.util.function.Supplier;
020
021import org.apache.wicket.core.request.mapper.BookmarkableMapper;
022import org.apache.wicket.core.request.mapper.BufferedResponseMapper;
023import org.apache.wicket.core.request.mapper.HomePageMapper;
024import org.apache.wicket.core.request.mapper.PageInstanceMapper;
025import org.apache.wicket.core.request.mapper.ResourceReferenceMapper;
026import org.apache.wicket.request.IRequestMapper;
027import org.apache.wicket.request.component.IRequestablePage;
028import org.apache.wicket.request.mapper.CompoundRequestMapper;
029import org.apache.wicket.request.mapper.parameter.PageParametersEncoder;
030import org.apache.wicket.request.resource.caching.IResourceCachingStrategy;
031
032/**
033 * Mapper that encapsulates mappers that are necessary for Wicket to function.
034 * 
035 * @author igor.vaynberg
036 */
037public class SystemMapper extends CompoundRequestMapper
038{
039        private final Application application;
040
041        /**
042         * Constructor
043         * 
044         * @param application
045         */
046        public SystemMapper(final Application application)
047        {
048                this.application = application;
049
050                add(newPageInstanceMapper());
051                add(newBookmarkableMapper());
052                add(newHomePageMapper(new HomePageProvider(application)));
053                add(newResourceReferenceMapper(new PageParametersEncoder(),
054                        new ParentFolderPlaceholderProvider(application), getResourceCachingStrategy()));
055                add(newUrlResourceReferenceMapper());
056                add(RestartResponseAtInterceptPageException.MAPPER);
057                add(newBufferedResponseMapper());
058        }
059
060        protected IRequestMapper newBufferedResponseMapper()
061        {
062                return new BufferedResponseMapper();
063        }
064
065        protected IRequestMapper newUrlResourceReferenceMapper()
066        {
067                return new UrlResourceReferenceMapper();
068        }
069
070        private IRequestMapper newResourceReferenceMapper(PageParametersEncoder pageParametersEncoder,
071                                                          ParentFolderPlaceholderProvider parentFolderPlaceholderProvider,
072                                                          Supplier<IResourceCachingStrategy> resourceCachingStrategy)
073        {
074                return new ResourceReferenceMapper(pageParametersEncoder, parentFolderPlaceholderProvider,resourceCachingStrategy);
075        }
076
077        protected IRequestMapper newBookmarkableMapper()
078        {
079                return new BookmarkableMapper();
080        }
081
082        protected IRequestMapper newPageInstanceMapper()
083        {
084                return new PageInstanceMapper();
085        }
086
087        protected IRequestMapper newHomePageMapper(Supplier<Class<? extends IRequestablePage>> homePageProvider)
088        {
089                return new HomePageMapper(homePageProvider);
090        }
091
092        protected Supplier<IResourceCachingStrategy> getResourceCachingStrategy()
093        {
094                return () -> application.getResourceSettings().getCachingStrategy();
095        }
096
097        protected static class ParentFolderPlaceholderProvider implements Supplier<String>
098        {
099                private final Application application;
100
101                protected ParentFolderPlaceholderProvider(Application application)
102                {
103                        this.application = application;
104                }
105
106                @Override
107                public String get()
108                {
109                        return application.getResourceSettings().getParentFolderPlaceholder();
110                }
111        }
112
113        protected static class HomePageProvider<C extends Page> implements Supplier<Class<C>>
114        {
115                private final Application application;
116
117                protected HomePageProvider(final Application application)
118                {
119                        this.application = application;
120                }
121
122                @Override
123                public Class<C> get()
124                {
125                        return (Class<C>) application.getHomePage();
126                }
127        }
128}