KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > encoders > DirectServiceEncoder


1 // Copyright 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.encoders;
16
17 import org.apache.tapestry.INamespace;
18 import org.apache.tapestry.Tapestry;
19 import org.apache.tapestry.engine.ServiceEncoder;
20 import org.apache.tapestry.engine.ServiceEncoding;
21 import org.apache.tapestry.services.ServiceConstants;
22
23 /**
24  * A specialized encoder for the {@link org.apache.tapestry.engine.DirectService direct service}
25  *  that encodes the page name and component id path into the servlet path, and encodes the
26  * stateful flag by choosing one of two extensions.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class DirectServiceEncoder implements ServiceEncoder
32 {
33     private String JavaDoc _statelessExtension;
34
35     private String JavaDoc _statefulExtension;
36
37     public void encode(ServiceEncoding encoding)
38     {
39         String JavaDoc service = encoding.getParameterValue(ServiceConstants.SERVICE);
40         if (!service.equals(Tapestry.DIRECT_SERVICE))
41             return;
42
43         String JavaDoc pageName = encoding.getParameterValue(ServiceConstants.PAGE);
44
45         // Only handle pages in the application namespace (not from a library).
46

47         if (pageName.indexOf(INamespace.SEPARATOR) >= 0)
48             return;
49
50         String JavaDoc stateful = encoding.getParameterValue(ServiceConstants.SESSION);
51         String JavaDoc componentIdPath = encoding.getParameterValue(ServiceConstants.COMPONENT);
52
53         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("/");
54         buffer.append(pageName);
55
56         buffer.append(",");
57         buffer.append(componentIdPath);
58
59         buffer.append(".");
60         buffer.append(stateful != null ? _statefulExtension : _statelessExtension);
61
62         encoding.setServletPath(buffer.toString());
63
64         encoding.setParameterValue(ServiceConstants.SERVICE, null);
65         encoding.setParameterValue(ServiceConstants.PAGE, null);
66         encoding.setParameterValue(ServiceConstants.SESSION, null);
67         encoding.setParameterValue(ServiceConstants.COMPONENT, null);
68     }
69
70     public void decode(ServiceEncoding encoding)
71     {
72         String JavaDoc servletPath = encoding.getServletPath();
73
74         int dotx = servletPath.lastIndexOf('.');
75         if (dotx < 0)
76             return;
77
78         String JavaDoc extension = servletPath.substring(dotx + 1);
79
80         if (!(extension.equals(_statefulExtension) || extension.equals(_statelessExtension)))
81             return;
82
83         int commax = servletPath.lastIndexOf(',');
84
85         String JavaDoc pageName = servletPath.substring(1, commax);
86         String JavaDoc componentIdPath = servletPath.substring(commax + 1, dotx);
87
88         encoding.setParameterValue(ServiceConstants.SERVICE, Tapestry.DIRECT_SERVICE);
89         encoding.setParameterValue(ServiceConstants.PAGE, pageName);
90         encoding.setParameterValue(
91                 ServiceConstants.SESSION,
92                 extension.equals(_statefulExtension) ? "T" : null);
93         encoding.setParameterValue(ServiceConstants.COMPONENT, componentIdPath);
94     }
95
96     public void setStatefulExtension(String JavaDoc statefulExtension)
97     {
98         _statefulExtension = statefulExtension;
99     }
100
101     public void setStatelessExtension(String JavaDoc statelessExtension)
102     {
103         _statelessExtension = statelessExtension;
104     }
105 }
Popular Tags