1 /*
2 * Copyright 2023 Web-Legacy
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.tiles.request.jakarta.servlet;
17
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Locale;
23 import java.util.Map;
24
25 import org.apache.tiles.request.ApplicationContext;
26 import org.apache.tiles.request.ApplicationResource;
27 import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
28 import org.apache.tiles.request.collection.ScopeMap;
29 import org.apache.tiles.request.jakarta.servlet.extractor.ApplicationScopeExtractor;
30 import org.apache.tiles.request.jakarta.servlet.extractor.InitParameterExtractor;
31 import org.apache.tiles.request.locale.URLApplicationResource;
32
33 import jakarta.servlet.ServletContext;
34
35 /**
36 * Servlet-based implementation of the TilesApplicationContext interface.
37 *
38 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
39 * Jakarta EE 9.</p>
40 */
41 public class ServletApplicationContext implements ApplicationContext {
42
43 /**
44 * The servlet context to use.
45 */
46 private ServletContext servletContext;
47
48 /**
49 * The lazily instantiated {@code Map} of application scope attributes.
50 */
51 private Map<String, Object> applicationScope = null;
52
53 /**
54 * The lazily instantiated {@code Map} of context initialization parameters.
55 */
56 private Map<String, String> initParam = null;
57
58 /**
59 * Creates a new instance of ServletTilesApplicationContext.
60 *
61 * @param servletContext The servlet context to use.
62 */
63 public ServletApplicationContext(ServletContext servletContext) {
64 this.servletContext = servletContext;
65 }
66
67 /**
68 * Returns the servlet context to use.
69 *
70 * @return the servlet context to use
71 */
72 public Object getContext() {
73 return servletContext;
74 }
75
76 /**
77 * Returns the context map from application scope.
78 *
79 * @return the context map from application scope
80 */
81 public Map<String, Object> getApplicationScope() {
82 if (applicationScope == null && servletContext != null) {
83 applicationScope = new ScopeMap(
84 new ApplicationScopeExtractor(servletContext));
85 }
86
87 return applicationScope;
88 }
89
90 /**
91 * Return an immutable Map that maps context application initialization
92 * parameters to their values.
93 *
94 * @return initialization parameters
95 */
96 public Map<String, String> getInitParams() {
97 if (initParam == null && servletContext != null) {
98 initParam = new ReadOnlyEnumerationMap<String>(
99 new InitParameterExtractor(servletContext));
100 }
101
102 return initParam;
103 }
104
105 /**
106 * Return the application resource mapped to the specified path.
107 *
108 * @param localePath path to the desired resource, including the Locale
109 * suffix.
110 *
111 * @return the first located resource which matches the given path or null
112 * if no such resource exists.
113 */
114 public ApplicationResource getResource(String localePath) {
115 try {
116 URL url = servletContext.getResource(localePath);
117 if (url != null) {
118 return new URLApplicationResource(localePath, url);
119 } else {
120 return null;
121 }
122 } catch (MalformedURLException e) {
123 return null;
124 }
125 }
126
127 /**
128 * Return a localized version of an ApplicationResource.
129 *
130 * @param base the ApplicationResource.
131 * @param locale the desired Locale.
132 *
133 * @return the first located resource which matches the given path or null
134 * if no such resource exists.
135 */
136 public ApplicationResource getResource(ApplicationResource base, Locale locale) {
137 try {
138 URL url = servletContext.getResource(base.getLocalePath(locale));
139 if (url != null) {
140 return new URLApplicationResource(base.getPath(), locale, url);
141 } else {
142 return null;
143 }
144 } catch (MalformedURLException e) {
145 return null;
146 }
147 }
148
149 /**
150 * Return the application resources mapped to the specified path.
151 *
152 * @param path to the desired resource.
153 *
154 * @return all resources which match the given path.
155 */
156 public Collection<ApplicationResource> getResources(String path) {
157 ArrayList<ApplicationResource> resources = new ArrayList<ApplicationResource>();
158 resources.add(getResource(path));
159 return resources;
160 }
161 }