KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > PortletURLImpl


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.core.impl;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.portlet.PortletMode;
27 import javax.portlet.PortletModeException;
28 import javax.portlet.PortletSecurityException;
29 import javax.portlet.PortletURL;
30 import javax.portlet.WindowState;
31 import javax.portlet.WindowStateException;
32
33 import org.apache.pluto.om.portlet.ContentTypeSet;
34 import org.apache.pluto.om.portlet.PortletDefinition;
35 import org.apache.pluto.om.window.PortletWindow;
36 import org.apache.pluto.services.information.DynamicInformationProvider;
37 import org.apache.pluto.services.information.InformationProviderAccess;
38 import org.apache.pluto.services.information.PortletURLProvider;
39 import org.apache.pluto.util.StringUtils;
40
41 public class PortletURLImpl implements PortletURL {
42     protected DynamicInformationProvider provider; // <ibm>
43
protected PortletMode mode = null;
44
45     protected Map JavaDoc parameters = new HashMap JavaDoc();
46
47     protected PortletWindow portletWindow;
48
49     protected boolean secure;
50     protected javax.servlet.http.HttpServletRequest JavaDoc servletRequest;
51     protected javax.servlet.http.HttpServletResponse JavaDoc servletResponse;
52     protected WindowState state;
53     protected boolean isAction;
54
55     public PortletURLImpl(PortletWindow portletWindow,
56                           javax.servlet.http.HttpServletRequest JavaDoc servletRequest,
57                           javax.servlet.http.HttpServletResponse JavaDoc servletResponse,
58                           boolean isAction)
59     {
60         this.portletWindow = portletWindow;
61         this.servletRequest = servletRequest;
62         this.servletResponse = servletResponse;
63         secure = servletRequest.isSecure();
64         this.isAction = isAction;
65     }
66
67     // javax.portlet.PortletURL -------------------------------------------------------------------
68
public void setWindowState(WindowState windowState) throws WindowStateException
69     {
70         if (provider == null)
71             provider = InformationProviderAccess.getDynamicProvider(servletRequest);
72         if (windowState != null && provider.isWindowStateAllowed(windowState))
73         {
74             state = windowState;
75             return;
76         }
77  
78         throw new WindowStateException("unsupported Window State used: " + windowState,windowState);
79     }
80
81     public void setPortletMode (PortletMode portletMode) throws PortletModeException
82     {
83   
84         // check if portal supports portlet mode
85
// DynamicInformationProvider provider = InformationProviderAccess.getDynamicProvider(servletRequest);
86
if (provider == null)
87             provider = InformationProviderAccess.getDynamicProvider(servletRequest);
88         if (portletMode != null && provider.isPortletModeAllowed(portletMode))
89         {
90             if (isPortletModeSupported(portletMode, portletWindow))
91             {
92                 mode = portletMode;
93                 return;
94             }
95         }
96         throw new PortletModeException("unsupported Portlet Mode used: " + portletMode,portletMode);
97     }
98
99     public void setParameter(String JavaDoc name, String JavaDoc value)
100     {
101         if (name == null || value == null) {
102             throw new IllegalArgumentException JavaDoc("name and value must not be null");
103         }
104
105         parameters.put( name, new String JavaDoc[]{value});
106     }
107
108     public void setParameter (String JavaDoc name, String JavaDoc[] values)
109     {
110         if (name == null || values == null || values.length == 0) {
111             throw new IllegalArgumentException JavaDoc("name and values must not be null or values be an empty array");
112         }
113
114         parameters.put(name, StringUtils.copy(values));
115     }
116     
117     /* (non-Javadoc)
118      * @see javax.portlet.PortletURL#setParameters(Map)
119      */

120     public void setParameters(Map JavaDoc parameters) {
121         if (parameters == null) {
122             throw new IllegalArgumentException JavaDoc("Parameters must not be null.");
123         }
124         for (Iterator JavaDoc iter = parameters.entrySet().iterator(); iter.hasNext();) {
125             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
126             if (!(entry.getKey() instanceof String JavaDoc)) {
127                 throw new IllegalArgumentException JavaDoc("Key must not be null and of type java.lang.String.");
128             }
129             if (!(entry.getValue() instanceof String JavaDoc[])) {
130                 throw new IllegalArgumentException JavaDoc("Value must not be null and of type java.lang.String[].");
131             }
132         }
133
134         this.parameters = StringUtils.copyParameters(parameters);
135     }
136
137     public void setSecure (boolean secure) throws PortletSecurityException
138     {
139         this.secure = secure;
140     }
141     
142     public String JavaDoc toString()
143     {
144         StringBuffer JavaDoc url = new StringBuffer JavaDoc(200);
145
146         PortletURLProvider urlProvider = InformationProviderAccess.getDynamicProvider(servletRequest).getPortletURLProvider(portletWindow);
147
148         if (mode != null) {
149             urlProvider.setPortletMode(mode);
150         }
151         if (state != null) {
152             urlProvider.setWindowState(state);
153         }
154         if (isAction) {
155             urlProvider.setAction();
156         }
157         if (secure) {
158             urlProvider.setSecure();
159         }
160         urlProvider.clearParameters();
161         urlProvider.setParameters(parameters);
162
163         url.append(urlProvider.toString());
164
165         return url.toString();
166     }
167     // --------------------------------------------------------------------------------------------
168

169     // internal methods ---------------------------------------------------------------------------
170
private boolean isPortletModeSupported(PortletMode requestedPortletMode,PortletWindow referencedPortletWindow)
171     {
172         // PLT 8.1: View Portlet Mode should always be supported by a portlet, even if not defined in the descriptor
173
if (requestedPortletMode.equals(PortletMode.VIEW) || requestedPortletMode.toString().equalsIgnoreCase("view"))
174         {
175             return true;
176         }
177         PortletDefinition portletDefinition = referencedPortletWindow.getPortletEntity().getPortletDefinition();
178         ContentTypeSet contentTypes = portletDefinition.getContentTypeSet();
179         return contentTypes.supportsPortletMode(requestedPortletMode);
180     }
181     // --------------------------------------------------------------------------------------------
182

183     // additional methods -------------------------------------------------------------------------
184
public String JavaDoc getParameter(String JavaDoc name)
185     {
186         return(String JavaDoc)parameters.get(name);
187     }
188
189     public String JavaDoc[] getParameters(String JavaDoc name)
190     {
191         return(String JavaDoc[])parameters.get(name);
192     }
193
194     public PortletMode getPortletMode ()
195     {
196         return mode;
197     }
198
199     public WindowState getWindowState ()
200     {
201         return state;
202     }
203     // --------------------------------------------------------------------------------------------
204
}
205
Popular Tags