1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.chain.apps.example;
18
19
20 import org.apache.commons.chain.Command;
21 import org.apache.commons.chain.Context;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.commons.chain.web.servlet.ServletWebContext;
25 import javax.servlet.RequestDispatcher;
26
27 /**
28 * <p>Foo Command</p>
29 *
30 * @version $Revision: 532952 $ $Date: 2007-04-27 05:00:53 +0100 (Fri, 27 Apr 2007) $
31 */
32
33 public class ForwardCommand implements Command {
34
35
36 private Log log = LogFactory.getLog(ForwardCommand.class);
37
38 private String forward;
39
40
41 /**
42 * Return the uri to forward to.
43 *
44 * @return The uri to forward to
45 */
46 public String getForward() {
47 return forward;
48 }
49
50
51 /**
52 * Set the uri to forward to.
53 *
54 * @param forward The uri to forward to
55 */
56 public void setForward(String forward) {
57 this.forward = forward;
58 }
59
60 /**
61 * <p>Execute the command.</p>
62 *
63 * @param context The {@link Context} we are operating on
64 * @return <code>false</code> so that processng will continue
65 * @throws Exception If an error occurs during execution.
66 */
67 public boolean execute(Context context) throws Exception {
68
69 String uri = getForward(context);
70 if (uri != null) {
71 if (log.isDebugEnabled()) {
72 log.debug("Forwarding to " + uri);
73 }
74 ServletWebContext swcontext = (ServletWebContext)context;
75 RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri);
76 rd.forward(swcontext.getRequest(), swcontext.getResponse());
77 return true;
78 } else {
79 if (log.isDebugEnabled()) {
80 log.debug("No forward found");
81 }
82 return false;
83 }
84 }
85
86 /**
87 * Return the uri to forward to.
88 *
89 * @param context The {@link Context} we are operating on
90 * @return The uri to forward to
91 */
92 protected String getForward(Context context) {
93 String uri = (String)context.get("forward");
94 if (uri == null) {
95 uri = getForward();
96 }
97 return uri;
98 }
99
100 }