1 package org.apache.velocity.app.event.implement;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.app.event.IncludeEventHandler;
23 import org.apache.velocity.context.Context;
24 import org.apache.velocity.runtime.RuntimeServices;
25 import org.apache.velocity.util.ContextAware;
26 import org.apache.velocity.util.RuntimeServicesAware;
27 import org.apache.velocity.util.StringUtils;
28
29 /**
30 * Simple event handler that checks to see if an included page is available.
31 * If not, it includes a designated replacement page instead.
32 *
33 * <P>By default, the name of the replacement page is "notfound.vm", however this
34 * page name can be changed by setting the Velocity property
35 * <code>eventhandler.include.notfound</code>, for example:
36 * <code>
37 * <PRE>
38 * eventhandler.include.notfound = error.vm
39 * </PRE>
40 * </code>
41 * </p><p>
42 * The name of the missing resource is put into the Velocity context, under the
43 * key "missingResource", so that the "notfound" template can report the missing
44 * resource with a Velocity reference, like:
45 * <code>$missingResource</code>
46 * </p>
47 *
48 * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
49 * @version $Id: IncludeNotFound.java 809822 2009-09-01 05:30:56Z nbubna $
50 * @since 1.5
51 */
52 public class IncludeNotFound implements IncludeEventHandler,RuntimeServicesAware,ContextAware {
53
54 private static final String DEFAULT_NOT_FOUND = "notfound.vm";
55 private static final String PROPERTY_NOT_FOUND = "eventhandler.include.notfound";
56 private RuntimeServices rs = null;
57 String notfound;
58 Context context;
59
60 /**
61 * Chseck to see if included file exists, and display "not found" page if it
62 * doesn't. If "not found" page does not exist, log an error and return
63 * null.
64 *
65 * @param includeResourcePath
66 * @param currentResourcePath
67 * @param directiveName
68 * @return message.
69 */
70 public String includeEvent(
71 String includeResourcePath,
72 String currentResourcePath,
73 String directiveName)
74 {
75
76 /**
77 * check to see if page exists
78 */
79 boolean exists = (rs.getLoaderNameForResource(includeResourcePath) != null);
80 if (!exists)
81 {
82 context.put("missingResource", includeResourcePath);
83 if (rs.getLoaderNameForResource(notfound) != null)
84 {
85 return notfound;
86 }
87 else
88 {
89 /**
90 * can't find not found, so display nothing
91 */
92 rs.getLog().error("Can't find include not found page: " + notfound);
93 return null;
94 }
95 }
96 else
97 return includeResourcePath;
98 }
99
100
101 /**
102 * @see org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
103 */
104 public void setRuntimeServices(RuntimeServices rs)
105 {
106 this.rs = rs;
107 notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND));
108 }
109
110 /**
111 * @see org.apache.velocity.util.ContextAware#setContext(org.apache.velocity.context.Context)
112 */
113 public void setContext(Context context)
114 {
115 this.context = context;
116 }
117 }