|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ExternalService.java | - | 100% | 100% | 100% |
|
||||||||||||||
| 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 java.io.IOException; | |
| 18 | import java.util.HashMap; | |
| 19 | import java.util.Map; | |
| 20 | ||
| 21 | import org.apache.hivemind.ApplicationRuntimeException; | |
| 22 | import org.apache.hivemind.util.Defense; | |
| 23 | import org.apache.tapestry.IExternalPage; | |
| 24 | import org.apache.tapestry.IPage; | |
| 25 | import org.apache.tapestry.IRequestCycle; | |
| 26 | import org.apache.tapestry.Tapestry; | |
| 27 | import org.apache.tapestry.services.LinkFactory; | |
| 28 | import org.apache.tapestry.services.ResponseRenderer; | |
| 29 | import org.apache.tapestry.services.ServiceConstants; | |
| 30 | ||
| 31 | /** | |
| 32 | * The external service enables external applications to reference Tapestry pages via a URL. Pages | |
| 33 | * which can be referenced by the external service must implement the {@link IExternalPage} | |
| 34 | * interface. The external service enables the bookmarking of pages. | |
| 35 | * <p> | |
| 36 | * The external service may also be used by the Tapestry JSP taglibrary ( | |
| 37 | * {@link org.apache.tapestry.jsp.ExternalURLTag}and {@link org.apache.tapestry.jsp.ExternalTag}). | |
| 38 | * <p> | |
| 39 | * You can try and second guess the URL format used by Tapestry. The default URL format for the | |
| 40 | * external service is: <blockquote> | |
| 41 | * <tt>http://localhost/app?service=external/<i>[Page Name]</i>&sp=[Param 0]&sp=[Param 1]...</tt> | |
| 42 | * </blockquote> For example to view the "ViewCustomer" page the service parameters 5056 (customer | |
| 43 | * ID) and 309 (company ID) the external service URL would be: <blockquote> | |
| 44 | * <tt>http://localhost/myapp?service=external&context=<b>ViewCustomer</b>&sp=<b>5056</b>&sp=<b>302</b></tt> | |
| 45 | * </blockquote> In this example external service will get a "ViewCustomer" page and invoke the | |
| 46 | * {@link IExternalPage#activateExternalPage(Object[], IRequestCycle)}method with the parameters: | |
| 47 | * Object[] { new Integer(5056), new Integer(302) }. | |
| 48 | * <p> | |
| 49 | * Note service parameters (sp) need to be prefixed by valid | |
| 50 | * {@link org.apache.tapestry.util.io.DataSqueezerImpl}adaptor char. These adaptor chars are | |
| 51 | * automatically provided in URL's created by the <tt>buildGesture()</tt> method. However if you | |
| 52 | * hand coded an external service URL you will need to ensure valid prefix chars are present. | |
| 53 | * <p> | |
| 54 | * <table border="1" cellpadding="2"> | |
| 55 | * <tr> | |
| 56 | * <th>Prefix char(s)</th> | |
| 57 | * <th>Mapped Java Type</th> | |
| 58 | * </tr> | |
| 59 | * <tr> | |
| 60 | * <td> TF</td> | |
| 61 | * <td> boolean</td> | |
| 62 | * </tr> | |
| 63 | * <tr> | |
| 64 | * <td> b</td> | |
| 65 | * <td> byte</td> | |
| 66 | * </tr> | |
| 67 | * <tr> | |
| 68 | * <td> c</td> | |
| 69 | * <td> char</td> | |
| 70 | * </tr> | |
| 71 | * <tr> | |
| 72 | * <td> d</td> | |
| 73 | * <td> double</td> | |
| 74 | * </tr> | |
| 75 | * <tr> | |
| 76 | * <td> -0123456789</td> | |
| 77 | * <td> integer</td> | |
| 78 | * </tr> | |
| 79 | * <tr> | |
| 80 | * <td> l</td> | |
| 81 | * <td> long</td> | |
| 82 | * </tr> | |
| 83 | * <tr> | |
| 84 | * <td> S</td> | |
| 85 | * <td> String</td> | |
| 86 | * </tr> | |
| 87 | * <tr> | |
| 88 | * <td> s</td> | |
| 89 | * <td> short</td> | |
| 90 | * </tr> | |
| 91 | * <tr> | |
| 92 | * <td> other chars</td> | |
| 93 | * <td> <tt>String</tt> without truncation of first char</td> | |
| 94 | * </tr> | |
| 95 | * <table> | |
| 96 | * <p> | |
| 97 | * <p> | |
| 98 | * A good rule of thumb is to keep the information encoded in the URL short and simple, and restrict | |
| 99 | * it to just Strings and Integers. Integers can be encoded as-is. Prefixing all Strings with the | |
| 100 | * letter 'S' will ensure that they are decoded properly. Again, this is only relevant if an | |
| 101 | * {@link org.apache.tapestry.IExternalPage}is being referenced from static HTML or JSP and the URL | |
| 102 | * must be assembled in user code ... when the URL is generated by Tapestry, it is automatically | |
| 103 | * created with the correct prefixes and encodings (as with any other service). | |
| 104 | * | |
| 105 | * @see org.apache.tapestry.IExternalPage | |
| 106 | * @see org.apache.tapestry.jsp.ExternalTag | |
| 107 | * @see org.apache.tapestry.jsp.ExternalURLTag | |
| 108 | * @author Howard Lewis Ship | |
| 109 | * @author Malcolm Edgar | |
| 110 | * @since 2.2 | |
| 111 | */ | |
| 112 | ||
| 113 | public class ExternalService implements IEngineService | |
| 114 | { | |
| 115 | /** @since 4.0 */ | |
| 116 | ||
| 117 | private ResponseRenderer _responseRenderer; | |
| 118 | ||
| 119 | /** @since 4.0 */ | |
| 120 | private LinkFactory _linkFactory; | |
| 121 | ||
| 122 | /** | |
| 123 | * {@inheritDoc} | |
| 124 | * | |
| 125 | * @return The URL for the service. The URL will always be encoded when it is returned. | |
| 126 | */ | |
| 127 | 12 | public ILink getLink(boolean post, Object parameter) |
| 128 | { | |
| 129 | 12 | Defense.isAssignable(parameter, ExternalServiceParameter.class, "parameter"); |
| 130 | ||
| 131 | 12 | ExternalServiceParameter esp = (ExternalServiceParameter) parameter; |
| 132 | ||
| 133 | 12 | Map parameters = new HashMap(); |
| 134 | ||
| 135 | 12 | parameters.put(ServiceConstants.PAGE, esp.getPageName()); |
| 136 | 12 | parameters.put(ServiceConstants.PARAMETER, esp.getServiceParameters()); |
| 137 | ||
| 138 | 12 | return _linkFactory.constructLink(this, post, parameters, true); |
| 139 | } | |
| 140 | ||
| 141 | 6 | public void service(IRequestCycle cycle) throws IOException |
| 142 | { | |
| 143 | 6 | String pageName = cycle.getParameter(ServiceConstants.PAGE); |
| 144 | 6 | IPage rawPage = cycle.getPage(pageName); |
| 145 | ||
| 146 | 6 | IExternalPage page = null; |
| 147 | ||
| 148 | 6 | try |
| 149 | { | |
| 150 | 6 | page = (IExternalPage) rawPage; |
| 151 | } | |
| 152 | catch (ClassCastException ex) | |
| 153 | { | |
| 154 | 3 | throw new ApplicationRuntimeException(EngineMessages.pageNotCompatible( |
| 155 | rawPage, | |
| 156 | IExternalPage.class), rawPage, null, ex); | |
| 157 | } | |
| 158 | ||
| 159 | 3 | Object[] parameters = _linkFactory.extractListenerParameters(cycle); |
| 160 | ||
| 161 | 3 | cycle.setListenerParameters(parameters); |
| 162 | ||
| 163 | 3 | cycle.activate(page); |
| 164 | ||
| 165 | 3 | page.activateExternalPage(parameters, cycle); |
| 166 | ||
| 167 | 3 | _responseRenderer.renderResponse(cycle); |
| 168 | } | |
| 169 | ||
| 170 | 15 | public String getName() |
| 171 | { | |
| 172 | 15 | return Tapestry.EXTERNAL_SERVICE; |
| 173 | } | |
| 174 | ||
| 175 | /** @since 4.0 */ | |
| 176 | ||
| 177 | 9 | public void setResponseRenderer(ResponseRenderer responseRenderer) |
| 178 | { | |
| 179 | 9 | _responseRenderer = responseRenderer; |
| 180 | } | |
| 181 | ||
| 182 | /** @since 4.0 */ | |
| 183 | 12 | public void setLinkFactory(LinkFactory linkFactory) |
| 184 | { | |
| 185 | 12 | _linkFactory = linkFactory; |
| 186 | } | |
| 187 | } |
|
||||||||||