1 48 49 50 package com.caucho.portal.generic.taglib; 51 52 import com.caucho.portal.generic.InvocationURL; 53 54 import javax.portlet.*; 55 import javax.servlet.ServletRequest ; 56 import javax.servlet.jsp.JspException ; 57 import javax.servlet.jsp.tagext.TagSupport ; 58 import java.io.IOException ; 59 import java.util.ArrayList ; 60 import java.util.Iterator ; 61 import java.util.LinkedHashMap ; 62 import java.util.Map ; 63 64 65 abstract public class AbstractUrlTag extends TagSupport { 66 private String _var; 68 private WindowState _windowState; 69 private PortletMode _portletMode; 70 private Boolean _secure; 71 72 74 private LinkedHashMap <String ,Object > _paramMap 75 = new LinkedHashMap <String ,Object >(); 76 private LinkedHashMap <String ,LinkedHashMap <String ,Object >> _namespaceParamMap 77 = new LinkedHashMap <String ,LinkedHashMap <String ,Object >>(); 78 79 abstract protected PortletURL createPortletURL(RenderResponse response); 80 81 85 public void setVar(String var) 86 { 87 _var = var; 88 } 89 90 93 public void setWindowState(String windowState) 94 { 95 _windowState = new WindowState(windowState); 96 } 97 98 101 public void setPortletMode(String portletMode) 102 { 103 _portletMode = new PortletMode(portletMode); 104 } 105 106 111 public void setSecure(String secure) 112 throws JspException 113 { 114 if (secure == null) 115 _secure = null; 116 else if (secure.equalsIgnoreCase("true")) 117 _secure = Boolean.TRUE; 118 else if (secure.equalsIgnoreCase("false")) 119 _secure = Boolean.FALSE; 120 else 121 throw new JspException ("secure must be `true' or `false' or unspecified"); 122 } 123 124 public int doStartTag() 125 throws JspException 126 { 127 _paramMap.clear(); 128 _namespaceParamMap.clear(); 129 130 return EVAL_BODY_INCLUDE; 131 } 132 133 136 public void addParam(String name, String value) 137 { 138 addParam(_paramMap, name, value); 139 } 140 141 144 public void addParam(String namespace, String name, String value) 145 { 146 LinkedHashMap <String ,Object > map = _namespaceParamMap.get(namespace); 147 148 if (map == null) { 149 map = new LinkedHashMap <String ,Object >(); 150 _namespaceParamMap.put(namespace,map); 151 } 152 153 addParam(map, name, value); 154 } 155 156 private void addParam( LinkedHashMap <String , Object > map, 157 String name, 158 String value ) 159 { 160 Object current = map.put(name, value); 162 163 if (current != null) { 164 if (current instanceof ArrayList ) { 165 ArrayList <String > list = (ArrayList <String >) current; 166 list.add(value); 167 map.put(name, list); 168 } 169 else { 170 ArrayList <String > list = new ArrayList <String >(); 171 list.add((String ) current); 172 list.add(value); 173 map.put(name, list); 174 } 175 } 176 } 177 178 public int doEndTag() 179 throws JspException 180 { 181 PortletURL url; 182 183 ServletRequest request = pageContext.getRequest(); 184 RenderResponse response = (RenderResponse) 185 request.getAttribute("javax.portlet.renderResponse"); 186 187 if (response == null) 188 throw new JspException ("No RenderResponse available"); 189 190 url = createPortletURL(response); 191 192 if (_windowState != null) { 193 try { 194 url.setWindowState(_windowState); 195 } 196 catch (WindowStateException ex) { 197 throw new JspException (ex); 198 } 199 } 200 201 if (_portletMode != null) { 202 try { 203 url.setPortletMode(_portletMode); 204 } 205 catch (PortletModeException ex) { 206 throw new JspException (ex); 207 } 208 } 209 210 if (_secure != null) { 211 try { 212 if (_secure == Boolean.FALSE) 213 url.setSecure(false); 214 else 215 url.setSecure(true); 216 } 217 catch (PortletSecurityException ex) { 218 throw new JspException (ex); 219 } 220 } 221 222 if (_paramMap.size() > 0) { 223 Iterator <Map.Entry <String ,Object >> iter = _paramMap.entrySet().iterator(); 224 225 while (iter.hasNext()) { 226 Map.Entry <String ,Object > entry = iter.next(); 227 228 String name = entry.getKey(); 229 Object value = entry.getValue(); 230 231 if (value instanceof String ) 232 url.setParameter(name,(String ) value); 233 else 234 url.setParameter(name, makeStringArray( (ArrayList <String >) value)); 235 } 236 } 237 238 if (_namespaceParamMap.size() > 0) { 239 InvocationURL invocationUrl = (InvocationURL) url; 240 241 Iterator <Map.Entry <String ,LinkedHashMap <String ,Object >>> iter 242 = _namespaceParamMap.entrySet().iterator(); 243 244 while (iter.hasNext()) { 245 Map.Entry <String ,LinkedHashMap <String ,Object >> entry = iter.next(); 246 247 String namespace = entry.getKey(); 248 LinkedHashMap <String ,Object > map = entry.getValue(); 249 250 Iterator <Map.Entry <String ,Object >> mapIter = map.entrySet().iterator(); 251 252 while (mapIter.hasNext()) { 253 Map.Entry <String ,Object > mapEntry = mapIter.next(); 254 255 String name = mapEntry.getKey(); 256 Object value = mapEntry.getValue(); 257 258 if (value instanceof String ) { 259 invocationUrl.setParameter(namespace, name, (String ) value); 260 } 261 else { 262 String [] values = makeStringArray( (ArrayList <String >) value); 263 invocationUrl.setParameter( namespace, name, values ); 264 } 265 } 266 } 267 } 268 269 String urlString = url.toString(); 270 271 if (_var != null) { 272 pageContext.setAttribute(_var, urlString); 273 } 274 else { 275 try { 276 pageContext.getOut().print(urlString); 277 } 278 catch (IOException ex) { 279 throw new JspException (ex); 280 } 281 } 282 283 return EVAL_PAGE; 284 } 285 286 private String [] makeStringArray(ArrayList <String > list) 287 { 288 int sz = list.size(); 289 290 String [] array = new String [sz]; 291 292 for (int i = 0; i < sz; i++) { 293 array[i] = list.get(i); 294 } 295 296 return array; 297 } 298 } 299 | Popular Tags |