Coverage Report - org.apache.tapestry.engine.DirectEventService
 
Classes in this File Line Coverage Branch Coverage Complexity
DirectEventService
0%
0/57
0%
0/30
2.875
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.engine;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.util.Defense;
 19  
 import org.apache.tapestry.*;
 20  
 import org.apache.tapestry.event.BrowserEvent;
 21  
 import org.apache.tapestry.services.LinkFactory;
 22  
 import org.apache.tapestry.services.ResponseRenderer;
 23  
 import org.apache.tapestry.services.ServiceConstants;
 24  
 import org.apache.tapestry.web.WebRequest;
 25  
 import org.apache.tapestry.web.WebSession;
 26  
 
 27  
 import java.io.IOException;
 28  
 import java.util.HashMap;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * Implementation of the direct event service, which encodes the page and component id in the service
 33  
  * context, and passes application-defined parameters as well.
 34  
  * 
 35  
  * @author jkuhnert
 36  
  * @since 4.1
 37  
  */
 38  
 
 39  0
 public class DirectEventService implements IEngineService
 40  
 {   
 41  
     /** @since 4.0 */
 42  
     private ResponseRenderer _responseRenderer;
 43  
 
 44  
     /** @since 4.0 */
 45  
     private LinkFactory _linkFactory;
 46  
 
 47  
     /** @since 4.0 */
 48  
     private WebRequest _request;
 49  
 
 50  
     /** @since 4.0 */
 51  
     private IRequestCycle _requestCycle;
 52  
 
 53  
     public ILink getLink(boolean post, Object parameter)
 54  
     {
 55  0
         Defense.isAssignable(parameter, DirectEventServiceParameter.class, "parameter");
 56  
 
 57  0
         DirectEventServiceParameter dsp = (DirectEventServiceParameter) parameter;
 58  
 
 59  0
         IComponent component = dsp.getDirect();
 60  
 
 61  
         // New since 1.0.1, we use the component to determine
 62  
         // the page, not the cycle. Through the use of tricky
 63  
         // things such as Block/InsertBlock, it is possible
 64  
         // that a component from a page different than
 65  
         // the response page will render.
 66  
         // In 1.0.6, we start to record *both* the render page
 67  
         // and the component page (if different).
 68  
 
 69  0
         IPage activePage = _requestCycle.getPage();
 70  0
         IPage componentPage = component.getPage();
 71  
         
 72  0
         Map parameters = new HashMap();
 73  
         
 74  0
         boolean stateful = _request.getSession(false) != null;
 75  
         
 76  0
         parameters.put(ServiceConstants.PAGE, activePage.getPageName());
 77  0
         parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
 78  0
         parameters.put(ServiceConstants.CONTAINER, componentPage == activePage ? null : componentPage.getPageName());
 79  0
         parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
 80  
 
 81  0
         if (dsp.getUpdateParts() != null && dsp.getUpdateParts().length > 0)
 82  
         {
 83  0
             parameters.put(ServiceConstants.UPDATE_PARTS, dsp.getUpdateParts());
 84  
         }
 85  
 
 86  0
         if (dsp.isJSON())
 87  
         {
 88  0
             parameters.put("json", String.valueOf(dsp.isJSON()));
 89  
         }
 90  
         
 91  0
         parameters.put(ServiceConstants.PARAMETER, dsp.getServiceParameters());
 92  
         
 93  0
         return _linkFactory.constructLink(this, post, parameters, true);
 94  
     }
 95  
 
 96  
     public void service(IRequestCycle cycle) throws IOException
 97  
     {
 98  0
         String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
 99  0
         String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
 100  0
         String activePageName = cycle.getParameter(ServiceConstants.PAGE);
 101  0
         boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
 102  
         
 103  0
         IPage page = cycle.getPage(activePageName);
 104  
 
 105  0
         cycle.activate(page);
 106  
 
 107  0
         IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
 108  
 
 109  0
         IComponent component = componentPage.getNestedComponent(componentId);
 110  
 
 111  0
         IDirectEvent direct = null;
 112  
 
 113  
         try
 114  
         {
 115  0
             direct = (IDirectEvent) component;
 116  
         }
 117  0
         catch (ClassCastException ex)
 118  
         {
 119  0
             throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
 120  
                     component,
 121  
                     IDirectEvent.class), component, null, ex);
 122  0
         }
 123  
 
 124  
         // Check for a StaleSession only when the session was stateful when
 125  
         // the link was created.
 126  
 
 127  0
         if (activeSession && direct.isStateful())
 128  
         {
 129  0
             WebSession session = _request.getSession(false);
 130  
 
 131  0
             if (session == null || session.isNew())
 132  0
                 throw new StaleSessionException(EngineMessages.requestStateSession(direct), componentPage);
 133  
         }
 134  
         
 135  0
         Object[] parameters = _linkFactory.extractListenerParameters(cycle);
 136  
         
 137  0
         triggerComponent(cycle, direct, parameters);
 138  
         
 139  
         // Render the response. This will be the active page
 140  
         // unless the direct component (or its delegate) changes it.
 141  
         
 142  0
         _responseRenderer.renderResponse(cycle);
 143  0
     }
 144  
 
 145  
     /** @since 4.0 */
 146  
 
 147  
     protected void triggerComponent(IRequestCycle cycle, IDirectEvent direct, Object[] parameters)
 148  
     {
 149  0
         if (!BrowserEvent.hasBrowserEvent(cycle))
 150  0
             throw new ApplicationRuntimeException(EngineMessages.noBrowserEvent());
 151  
         
 152  0
         BrowserEvent event = new BrowserEvent(cycle);
 153  
         
 154  0
         Object[] parms = new Object[parameters.length + 1];
 155  0
         System.arraycopy(parameters, 0, parms, 0, parameters.length);
 156  0
         parms[parms.length - 1] = event;
 157  
         
 158  0
         cycle.setListenerParameters(parms);
 159  
         
 160  0
         direct.triggerEvent(cycle, event);
 161  0
     }
 162  
 
 163  
     public String getName()
 164  
     {
 165  0
         return Tapestry.DIRECT_EVENT_SERVICE;
 166  
     }
 167  
 
 168  
     /** @since 4.0 */
 169  
     public void setResponseRenderer(ResponseRenderer responseRenderer)
 170  
     {
 171  0
         _responseRenderer = responseRenderer;
 172  0
     }
 173  
 
 174  
     /** @since 4.0 */
 175  
     public void setLinkFactory(LinkFactory linkFactory)
 176  
     {
 177  0
         _linkFactory = linkFactory;
 178  0
     }
 179  
 
 180  
     /** @since 4.0 */
 181  
     public void setRequest(WebRequest request)
 182  
     {
 183  0
         _request = request;
 184  0
     }
 185  
 
 186  
     /** @since 4.0 */
 187  
     public void setRequestCycle(IRequestCycle requestCycle)
 188  
     {
 189  0
         _requestCycle = requestCycle;
 190  0
     }
 191  
 }