KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > PortletLink


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.portlet;
16
17 import javax.portlet.PortletURL;
18
19 import org.apache.hivemind.util.Defense;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.engine.ILink;
22 import org.apache.tapestry.util.QueryParameterMap;
23
24 /**
25  * Wrapper around {@link javax.portlet.PortletURL}.
26  *
27  * @author Howard M. Lewis Ship
28  * @since 4.0
29  */

30 public class PortletLink implements ILink
31 {
32     private final IRequestCycle _cycle;
33
34     private final PortletURL _portletURL;
35
36     private final boolean _stateful;
37
38     private final QueryParameterMap _parameters;
39
40     public PortletLink(IRequestCycle cycle, PortletURL portletURL, QueryParameterMap parameters,
41             boolean stateful)
42     {
43         Defense.notNull(cycle, "cycle");
44         Defense.notNull(portletURL, "portletURL");
45         Defense.notNull(parameters, "parameters");
46
47         _cycle = cycle;
48         _portletURL = portletURL;
49         _parameters = parameters;
50         _stateful = stateful;
51     }
52
53     public String JavaDoc getURL()
54     {
55         return getURL(null, true);
56     }
57
58     public String JavaDoc getURL(String JavaDoc anchor, boolean includeParameters)
59     {
60         if (includeParameters)
61             loadParameters();
62
63         String JavaDoc url = _portletURL.toString();
64
65         url = unencode(url);
66
67         if (_stateful)
68             url = _cycle.encodeURL(url);
69
70         if (anchor != null)
71             url = url + "#" + anchor;
72
73         return url;
74     }
75
76     /**
77      * The PortletURL class returns a url that's already XML-escaped, ready for inclusion directly
78      * into the response stream. However, the IMarkupWriter expects to do that encoding too ... and
79      * double encoding is bad. So we back out the most likely encoding (convert '&' to just
80      * '&').
81      */

82
83     private String JavaDoc unencode(String JavaDoc url)
84     {
85         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(url.length());
86         String JavaDoc text = url;
87
88         while (true)
89         {
90             int ampx = text.indexOf("&");
91
92             if (ampx < 0)
93                 break;
94
95             // Take up to and including the '&'
96

97             buffer.append(text.substring(0, ampx + 1));
98
99             text = text.substring(ampx + 5);
100         }
101
102         buffer.append(text);
103
104         return buffer.toString();
105     }
106
107     private void loadParameters()
108     {
109         String JavaDoc[] names = _parameters.getParameterNames();
110
111         for (int i = 0; i < names.length; i++)
112         {
113             String JavaDoc name = names[i];
114             String JavaDoc[] values = _parameters.getParameterValues(name);
115
116             if (values != null)
117                 _portletURL.setParameter(name, values);
118         }
119     }
120
121     public String JavaDoc getAbsoluteURL()
122     {
123         throw new UnsupportedOperationException JavaDoc(PortletMessages.unsupportedMethod("getAbsoluteURL"));
124     }
125
126     public String JavaDoc getAbsoluteURL(String JavaDoc scheme, String JavaDoc server, int port, String JavaDoc anchor,
127             boolean includeParameters)
128     {
129         throw new UnsupportedOperationException JavaDoc(PortletMessages.unsupportedMethod("getAbsoluteURL"));
130     }
131
132     public String JavaDoc[] getParameterNames()
133     {
134         return _parameters.getParameterNames();
135     }
136
137     public String JavaDoc[] getParameterValues(String JavaDoc name)
138     {
139         return _parameters.getParameterValues(name);
140     }
141
142 }
Popular Tags