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.servlet;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.Locale;
26  import java.util.StringTokenizer;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.pluto.om.entity.PortletApplicationEntity;
33  import org.apache.pluto.om.entity.PortletApplicationEntityList;
34  import org.apache.pluto.om.entity.PortletApplicationEntityListCtrl;
35  import org.apache.pluto.om.entity.PortletEntity;
36  import org.apache.pluto.om.entity.PortletEntityListCtrl;
37  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
38  import org.apache.pluto.om.portlet.PortletApplicationDefinitionList;
39  import org.apache.pluto.om.portlet.PortletDefinition;
40  import org.apache.pluto.portalImpl.aggregation.RootFragment;
41  import org.apache.pluto.portalImpl.om.page.impl.FragmentImpl;
42  import org.apache.pluto.portalImpl.om.page.impl.NavigationImpl;
43  import org.apache.pluto.portalImpl.om.page.impl.PropertyImpl;
44  import org.apache.pluto.portalImpl.services.pageregistry.PageRegistry;
45  import org.apache.pluto.portalImpl.services.portletdefinitionregistry.PortletDefinitionRegistry;
46  import org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistry;
47  
48  /***
49   * TCK test driver, that supports generation of pages via URLs as described in the JSR 168 TCK section.
50   * 
51   */
52  public class TCKdriver extends org.apache.pluto.portalImpl.Servlet {
53  
54  
55      private int testpageNo = 1;
56      private int windowNo = 100;  // start with a high # to avoid conficts with already defined windows in portletEntityRegistry
57      
58      
59      public TCKdriver()
60      {
61      }
62  
63      public final String getServletInfo()
64      {
65          return "portalImpl - Pluto TCK Driver";
66      }
67  
68  
69  
70  
71  
72      /* (non-Javadoc)
73       * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
74       */
75      public void doGet(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
76      throws ServletException, IOException {
77  
78          // check if it is TCK create page command
79  
80          String[] portletNames = servletRequest.getParameterValues("portletName");
81          if ( portletNames != null ) {
82              String pageName = servletRequest.getParameter("pageName");
83              if ( pageName == null ) {
84                  pageName = "TCK_testpage"+testpageNo++;
85              }
86              
87              // create page with given portlets
88              RootFragment root = PageRegistry.getRootFragment();
89              ArrayList fragments = new ArrayList();
90  
91              PortletApplicationEntityList     entityList     = PortletEntityRegistry.getPortletApplicationEntityList();
92              PortletApplicationDefinitionList definitionList = PortletDefinitionRegistry.getPortletApplicationDefinitionList();
93  
94              // put all portlets in one column
95              StringTokenizer tokenizer;
96              Iterator appIt, portletIt;
97              for ( int i=0; i < portletNames.length; ++i) {
98                  tokenizer =  new StringTokenizer(portletNames[i], "/");
99                  String appName = tokenizer.nextToken();
100                 String portletName = tokenizer.nextToken();
101                 appIt = entityList.iterator();
102                 PortletApplicationEntity appEntity = null;
103                 // search for application
104                 boolean found = false;
105                 while ( appIt.hasNext() && ! found ) {            
106                     appEntity = (PortletApplicationEntity) appIt.next();
107                     String displayName = appEntity.getPortletApplicationDefinition().getWebApplicationDefinition().getDisplayName(Locale.ENGLISH).getDisplayName();
108                     if ( (displayName != null) && (displayName.equals(appName)) )
109                       found = true;
110                 }
111                 if (!found) {
112                     for (Iterator iter = definitionList.iterator(); !found && iter.hasNext();) {
113                         PortletApplicationDefinition portletApp = (PortletApplicationDefinition)iter.next();
114                         if (portletApp.getId().toString().equals(appName)) {
115                             //PortletApplicationEntityListCtrl appEntityListCtrl = (PortletApplicationEntityListCtrl)ControllerObjectAccess.get(entityList);
116                             PortletApplicationEntityListCtrl appEntityListCtrl = (PortletApplicationEntityListCtrl)entityList;
117                             appEntity = appEntityListCtrl.add(portletApp.getId().toString());
118                             log("added Portlet Application " + appName + " to PortletEntityRegistry");
119                             found = true;
120                         }
121                     }
122                 }
123                 if ( !found ) {
124                     log("Portlet Application "+appName+" not found!");
125                     throw new ServletException("Portlet Application "+appName+" not found!");
126                 }
127                 portletIt = appEntity.getPortletEntityList().iterator();
128                 PortletEntity portlet = null;
129                 // search for portlet
130                 found = false;
131                 while ( portletIt.hasNext() && ! found ) {
132                     portlet = (PortletEntity) portletIt.next();
133                     PortletDefinition tmpPortletDef = portlet.getPortletDefinition();
134                     if ( tmpPortletDef == null ) {
135     	                log("ERROR: Portlet definition of portlet enity (id="+portlet.getId()+") not found!");
136     	                log("       Portlet may not be defined in portlet.xml!");
137         	            throw new ServletException("Portlet definition of portlet enity (id="+portlet.getId()+") not found!");
138             	    }
139                     String tmpPortletName = tmpPortletDef.getName();
140                     //delete all existing preferences for this portlet entity
141                     if (tmpPortletName != null && tmpPortletName.equals(portletName)) {
142                         for (Iterator iter = portlet.getPreferenceSet().iterator(); iter.hasNext();) {
143                             iter.next();
144                             iter.remove();
145                         }
146                         found = true;
147                     }
148                 }
149                 if (!found) {
150                     for (Iterator appIter = definitionList.iterator(); !found && appIter.hasNext();) {
151                         PortletApplicationDefinition appDef = (PortletApplicationDefinition)appIter.next();
152                         if (appDef.getId().toString().equals(appName)) {
153                             for (Iterator portletIter = appDef.getPortletDefinitionList().iterator(); !found && portletIter.hasNext();) {
154                                 PortletDefinition portletDef = (PortletDefinition)portletIter.next();
155                                 if (portletDef.getId().toString().equals(appName + "." + portletName)) {
156                                     //PortletEntityListCtrl entityListCtrl = (PortletEntityListCtrl)ControllerObjectAccess.get(appEntity.getPortletEntityList());
157                                     PortletEntityListCtrl entityListCtrl = (PortletEntityListCtrl)appEntity.getPortletEntityList();
158                                     portlet = entityListCtrl.add(appEntity, portletDef.getId().toString());
159                                     PortletEntityRegistry.refresh(portlet);
160                                     log("added Portlet " + portletName + " to PortletEntityRegistry");
161                                     found = true;
162                                 }
163                             }
164                         }
165                     }
166                 }
167                 if ( ! found ) {
168                     log("ERROR: Portlet "+portletName+" not found!");
169                     throw new ServletException("Portlet "+portletName+" not found!");
170                 }
171                 FragmentImpl tckPortlet = new FragmentImpl();
172                 tckPortlet.setType("portlet");
173                 tckPortlet.setName("p"+windowNo++);   // set portlet
174                 // property
175                 PropertyImpl property = new PropertyImpl();
176                 property.setName("portlet");
177                 property.setValue(portlet.getId().toString());
178                 tckPortlet.getProperties().add(property);
179                 fragments.add(tckPortlet);
180             }
181             FragmentImpl tckCol = new FragmentImpl();
182             tckCol.setType("column");
183             tckCol.setName("col");
184             tckCol.setFragments(fragments);        
185             ArrayList colFragments = new ArrayList();
186             colFragments.add(tckCol);
187             FragmentImpl tckRow = new FragmentImpl();
188             tckRow.setType("row");
189             tckRow.setName("row");
190             tckRow.setFragments(colFragments);
191             ArrayList rowFragments = new ArrayList();
192             rowFragments.add(tckRow);
193             //page
194             FragmentImpl tckPage = new FragmentImpl();
195             tckPage.setType("page");
196             tckPage.setName(pageName);
197             // navigation
198             NavigationImpl tckNav = new NavigationImpl();
199             tckNav.setTitle(pageName);
200             tckNav.setDescription("dynamically generated TCK test page");
201             tckPage.setNavigation(tckNav);
202             tckPage.setFragments(rowFragments);
203    
204             try {
205                 org.apache.pluto.portalImpl.aggregation.Fragment rootFragment = 
206                     tckPage.build(getServletConfig(), root);
207                 root.addChild(rootFragment);
208             } catch (Exception e) {
209                 log("Exception in building new TCK page occured! "+e.getMessage());
210                 throw new ServletException("Exception in building new TCK page occured!", e);                
211             }
212 
213             // redirect to newly created page
214             StringBuffer path = servletRequest.getRequestURL();
215             path.append("/");
216             path.append(pageName);
217             servletResponse.sendRedirect(path.toString());
218             return;
219         }
220 
221 
222         // normal execution
223         super.doGet(servletRequest, servletResponse);
224         
225 
226     }
227 
228    public void doPost (HttpServletRequest request,
229                               HttpServletResponse response) throws IOException, ServletException
230     {
231         doGet (request, response);
232     }
233 
234 }