View Javadoc
1   /*
2    * $Id$
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  package org.apache.struts.tiles2.preparer;
23  
24  import java.io.IOException;
25  
26  import org.apache.tiles.AttributeContext;
27  import org.apache.tiles.preparer.PreparerException;
28  import org.apache.tiles.preparer.ViewPreparer;
29  import org.apache.tiles.request.Request;
30  import org.apache.tiles.request.jakarta.servlet.ServletRequest;
31  
32  import jakarta.servlet.RequestDispatcher;
33  import jakarta.servlet.ServletException;
34  import jakarta.servlet.http.HttpServletRequest;
35  import jakarta.servlet.http.HttpServletResponse;
36  
37  /**
38   * @version $Rev$ $Date$
39   */
40  public class UrlPreparer implements ViewPreparer {
41  
42      /**
43       * The URL to be used as a preparer.
44       */
45      private String url;
46  
47      /**
48       * Constructor.
49       *
50       * @param url The URL to be used as a preparer.
51       */
52      public UrlPreparer(String url) {
53          this.url = url;
54      }
55  
56      /** {@inheritDoc} */
57      public void execute(Request tilesContext,
58              AttributeContext attributeContext) throws PreparerException {
59  
60          if (tilesContext instanceof ServletRequest) {
61              ServletRequest servletTilesContext =
62                  (ServletRequest) tilesContext;
63              HttpServletRequest request = servletTilesContext.getRequest();
64              HttpServletResponse response = servletTilesContext.getResponse();
65              RequestDispatcher rd = request.getSession().getServletContext()
66                      .getRequestDispatcher(url);
67              if (rd == null) {
68                  throw new PreparerException(
69                      "Controller can't find url '" + url + "'.");
70              }
71  
72              try {
73                  rd.include(request, response);
74              } catch (ServletException e) {
75                  throw new PreparerException(
76                          "The request dispatcher threw an exception", e);
77              } catch (IOException e) {
78                  throw new PreparerException(
79                          "The request dispatcher threw an I/O exception", e);
80              }
81          } else {
82              throw new PreparerException("Cannot dispatch url '" + url
83                      + "' since this preparer has not been called under a servlet environment");
84          }
85      }
86  
87  }