View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.pluto.driver.services.container;
18  
19  import java.io.IOException;
20  import java.io.Writer;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.portlet.PortletMode;
26  import javax.portlet.PortletSecurityException;
27  import javax.portlet.ResourceURL;
28  import javax.portlet.WindowState;
29  
30  import org.apache.pluto.container.PortletURLProvider;
31  import org.apache.pluto.container.PortletWindow;
32  import org.apache.pluto.driver.core.PortletWindowImpl;
33  import org.apache.pluto.driver.url.PortalURL;
34  import org.apache.pluto.driver.url.PortalURLParameter;
35  import org.apache.pluto.driver.url.impl.RelativePortalURLImpl;
36  
37  
38  /**
39   *
40   */
41  public class PortletURLProviderImpl implements PortletURLProvider
42  {
43      private final PortalURL url;
44      private final TYPE type;
45      private final String window;
46      private PortletMode portletMode;
47      private WindowState windowState;
48      private String cacheLevel;
49      private String resourceID;
50      private Map<String, String[]> renderParameters;
51      private Map<String, String[]> publicRenderParameters;
52      private Map<String, List<String>> properties;
53      private int _nLutecePortletId;
54      public PortletURLProviderImpl(PortalURL url, TYPE type, PortletWindow portletWindow)
55      {
56          this.url = url;
57          this.type = type;
58          this.window = portletWindow.getId().getStringId();
59        
60          //if portletwindowImpl is not overloaded
61          /*if(portletWindow instanceof fr.paris.lutece.plugins.jsr286.pluto.PortletWindowImpl)
62          {
63          	 _nLutecePortletId = ((PortletWindowImpl) portletWindow).getLutecePortletID();
64          }       */
65          _nLutecePortletId = ((PortletWindowImpl) portletWindow).getLutecePortletID();
66      }
67      
68      public PortalURL apply()
69      {
70      	RelativePortalURLImpl url = (RelativePortalURLImpl)this.url.clone();
71          if (PortletURLProvider.TYPE.ACTION == type)
72          {
73              url.setActionWindow(window);
74              url.setResourceWindow(null);
75              url.clearParameters(window);
76          }
77          else if (PortletURLProvider.TYPE.RESOURCE == type)
78          {
79              url.setActionWindow(null);
80              url.setResourceWindow(window);
81              if (!ResourceURL.FULL.equals(cacheLevel))
82              {
83                  for (PortalURLParameter parm : url.getParameters())
84                  {
85                      if (window.equals(parm.getWindowId()))
86                      {
87                          url.getPrivateRenderParameters().put(parm.getName(), parm.getValues());
88                      }                            
89                  }
90              }
91              url.clearParameters(window);
92          }
93          else
94          {
95              url.setResourceWindow(null);
96              url.setActionWindow(null);
97              url.clearParameters(window);
98          }
99          if (portletMode != null)
100         {
101             url.setPortletMode(window, portletMode);
102         }
103         if (windowState != null)
104         {
105             url.setWindowState(window, windowState);
106         }
107         if (renderParameters != null)
108         {
109             for (Map.Entry<String,String[]> entry : renderParameters.entrySet())
110             {
111                 if (publicRenderParameters == null || !publicRenderParameters.containsKey(entry.getKey()))
112                 {
113                     url.addParameter(new PortalURLParameter(window, entry.getKey(), entry.getValue()));
114                 }
115             }
116         }
117         if (publicRenderParameters != null)
118         {
119             for (Map.Entry<String,String[]> entry : publicRenderParameters.entrySet())
120             {
121                 url.getNewPublicParameters().put(entry.getKey(),entry.getValue() != null ? entry.getValue() : new String[]{null});
122             }
123         }
124         url.setResourceID(resourceID);
125         url.setCacheability(cacheLevel);
126         
127         url.setLutecePortletId( Integer.toString(this._nLutecePortletId));
128      
129         return url;
130     }
131     
132     public TYPE getType()
133     {
134         return type;
135     }
136     
137     public void setPortletMode(PortletMode mode)
138     {
139         this.portletMode = mode;
140     }
141     
142     public PortletMode getPortletMode()
143     {
144         return portletMode;
145     }
146 
147     public void setWindowState(WindowState state)
148     {
149         this.windowState = state;
150     }
151 
152     public WindowState getWindowState()
153     {
154         return windowState;
155     }
156 
157     public void setSecure(boolean secure) throws PortletSecurityException {
158         // ignore: not supported
159     }
160     
161     public boolean isSecure()
162     {
163         return false;
164     }
165     
166     public Map<String,String[]> getRenderParameters()
167     {
168         if (renderParameters == null)
169         {
170             renderParameters = new HashMap<String,String[]>();
171         }
172         return renderParameters;
173     }
174     
175     public Map<String,String[]> getPublicRenderParameters()
176     {
177         if (publicRenderParameters == null)
178         {
179             publicRenderParameters = new HashMap<String,String[]>();
180         }
181         return publicRenderParameters;
182     }
183     
184     public String getCacheability()
185     {
186         return cacheLevel;
187     }
188 
189     public void setCacheability(String cacheLevel)
190     {
191         this.cacheLevel = cacheLevel;
192     }
193 
194     public String getResourceID()
195     {
196         return resourceID;
197     }
198 
199     public void setResourceID(String resourceID)
200     {
201         this.resourceID = resourceID;
202     }
203     
204     public String toURL()
205     {
206         return toURL(false);
207     }
208 
209     public String toURL(boolean absolute)
210     {
211         return apply().toURL(absolute);
212     }
213 
214     public void write(Writer out, boolean escapeXML) throws IOException
215     {
216         String result = apply().toURL(false);
217         if (escapeXML)
218         {
219             result = result.replaceAll("&", "&amp;");
220             result = result.replaceAll("<", "&lt;");
221             result = result.replaceAll(">", "&gt;");
222             result = result.replaceAll("\'", "&#039;");
223             result = result.replaceAll("\"", "&#034;");
224         }
225         out.write(result);
226     }
227 
228     public Map<String, List<String>> getProperties()
229     {
230         if (properties == null)
231         {
232             properties = new HashMap<String, List<String>>();
233         }
234         return properties;
235     }
236 }