View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
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  /* 
17  
18   */
19  
20  package org.apache.pluto.portalImpl.aggregation;
21  
22  import java.io.IOException;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import javax.servlet.RequestDispatcher;
29  import javax.servlet.ServletConfig;
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.pluto.portalImpl.aggregation.navigation.Navigation;
35  import org.apache.pluto.portalImpl.core.PortalURL;
36  import org.apache.pluto.portalImpl.om.page.Property;
37  import org.apache.pluto.portalImpl.services.config.Config;
38  import org.apache.pluto.portalImpl.util.Parameters;
39  import org.apache.pluto.util.StringUtils;
40  
41  public abstract class AbstractFragment 
42  implements org.apache.pluto.portalImpl.aggregation.Fragment {
43  
44      public final static String BASE_ROOT = Config.getParameters().getString(
45                                                 "aggregation.base.dir",
46                                                 "/WEB-INF/aggregation/"
47                                             );
48  
49      private String id;
50      private ServletConfig config;
51      private org.apache.pluto.portalImpl.aggregation.Fragment parent;
52      private org.apache.pluto.portalImpl.om.page.Fragment fragDesc;
53      private Parameters initParameters;
54      private Navigation navigation;
55  
56  
57      public AbstractFragment(String id,
58                              ServletConfig config, 
59                              org.apache.pluto.portalImpl.aggregation.Fragment parent,
60                              org.apache.pluto.portalImpl.om.page.Fragment fragDesc,
61                              org.apache.pluto.portalImpl.aggregation.navigation.Navigation navigation)
62      throws Exception 
63      {
64          StringBuffer compId = new StringBuffer();
65          if (parent != null) {
66              String parentID = parent.getId();
67              if (parentID != null) {
68                  compId.append(parentID);
69                  compId.append("_");
70              }
71  
72          }
73  
74          if (id != null) {
75              compId.append(id);
76              this.id = compId.toString();
77          }
78  
79          this.config = config;
80          this.parent = parent;
81          this.fragDesc = fragDesc;
82          this.navigation = navigation;
83  
84          if (this.fragDesc != null) {
85              // prepare properties - SHESMER:TODO needs to be done better, the additonal Map should not be necessary
86              Map map = new HashMap();
87  
88              Iterator iterator = this.fragDesc.getProperties().iterator();
89  
90              while (iterator.hasNext()) {
91                  Property property = (Property)iterator.next();
92  
93                  map.put(property.getName(), property.getValue());
94              }
95  
96              initParameters = new Parameters(map);
97          }
98  
99          org.apache.pluto.portalImpl.services.pageregistry.PageRegistry.addFragment(this);
100     }
101 
102     // org.apache.pluto.portalImpl.aggregation.Fragment implementation.
103 
104     public void service(HttpServletRequest request, HttpServletResponse response)
105     throws ServletException, IOException
106     {
107         preService(request, response);
108 
109         request.setAttribute(Constants.FRAGMENT, this);
110         String jspName = StringUtils.nameOf(getClass()) + ".jsp";
111         RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher(BASE_ROOT+jspName);
112         rd.include(request, response);
113 
114         postService(request, response);
115     }
116 
117     public org.apache.pluto.portalImpl.aggregation.Fragment getParent()
118     {
119         return parent;
120     }
121 
122     public String getId()
123     {
124         return id;
125     }
126 
127     public Parameters getInitParameters()
128     {
129         return initParameters;
130     }
131 
132     abstract public Collection getChildFragments();
133 
134     abstract public void addChild(org.apache.pluto.portalImpl.aggregation.Fragment child);
135 
136     public Navigation getNavigation()
137     {
138         return navigation;
139     }
140 
141     abstract public void createURL(PortalURL url);
142 
143     abstract public boolean isPartOfURL(PortalURL url);
144 
145     // additional methods.
146     public ServletConfig getServletConfig()
147     {
148         return config;
149     }
150 
151     public String getInitParameterValue(String name)
152     {
153         return initParameters.getString(name);
154     }
155 
156 
157     abstract public void preService(HttpServletRequest request, HttpServletResponse response)
158     throws ServletException, IOException;
159 
160     abstract public void postService(HttpServletRequest request, HttpServletResponse response)
161     throws ServletException, IOException;
162 
163     protected org.apache.pluto.portalImpl.om.page.Fragment getFragmentDescription()
164     {
165         return fragDesc;
166     }
167 }