KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > consumer > impl > URLGeneratorImpl


1 /*
2 * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3 * Please look at license.txt in info directory for more license detail.
4 */

5
6 package org.exoplatform.services.wsrp.consumer.impl;
7
8
9 import java.net.URLEncoder JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
13 import org.exoplatform.Constants;
14 import org.exoplatform.services.wsrp.WSRPConstants;
15 import org.exoplatform.services.wsrp.consumer.URLGenerator;
16
17 /*
18  * @author Mestrallet Benjamin
19  * benjmestrallet@users.sourceforge.net
20  * Date: 6 févr. 2004
21  * Time: 13:19:37
22  */

23
24 public class URLGeneratorImpl implements URLGenerator{
25
26   public String JavaDoc getBlockingActionURL(String JavaDoc baseURL, Map JavaDoc params) {
27     return getURL(baseURL, params);
28   }
29
30   public String JavaDoc getRenderURL(String JavaDoc baseURL, Map JavaDoc params) {
31     return getURL(baseURL, params);
32   }
33
34   public String JavaDoc getResourceURL(String JavaDoc baseURL, Map JavaDoc params) {
35     return getURL(baseURL, params);
36   }
37
38   private String JavaDoc getURL(String JavaDoc baseURL, Map JavaDoc params){
39     StringBuffer JavaDoc sB = new StringBuffer JavaDoc();
40     sB.append(baseURL);
41     return computeParameters(sB, params);
42   }
43
44   public String JavaDoc getNamespacedToken(String JavaDoc token) {
45     return token;
46   }
47
48   private String JavaDoc computeParameters(StringBuffer JavaDoc sB, Map JavaDoc parameters){
49     Set JavaDoc names = parameters.keySet();
50     for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
51       String JavaDoc name = (String JavaDoc) iterator.next() ;
52       Object JavaDoc obj = parameters.get(name) ;
53       if (obj instanceof String JavaDoc) {
54         String JavaDoc value = (String JavaDoc) obj ;
55         sB.append("&");
56         sB.append(URLEncoder.encode(replaceName(name)));
57         sB.append("=");
58         sB.append(URLEncoder.encode(replaceValue(value)));
59       } else {
60         String JavaDoc[] values = (String JavaDoc[]) obj ;
61         for (int i=0; i < values.length ; i++) {
62           name = replaceName(name);
63           sB.append("&");
64           sB.append(URLEncoder.encode(name));
65           sB.append("=");
66           sB.append(URLEncoder.encode(values[i]));
67         }
68       }
69     }
70     return sB.toString();
71   }
72
73   private String JavaDoc replaceName(String JavaDoc name){
74     if(WSRPConstants.WSRP_MODE.equals(name))
75       return Constants.PORTLET_MODE_PARAMETER;
76     else if(WSRPConstants.WSRP_WINDOW_STATE.equals(name))
77       return Constants.WINDOW_STATE_PARAMETER;
78     else if(WSRPConstants.WSRP_PORTLET_HANDLE.equals(name))
79       return Constants.COMPONENT_PARAMETER;
80     else if(WSRPConstants.WSRP_SECURE_URL.equals(name))
81       return Constants.SECURE_PARAMETER;
82     else if(WSRPConstants.WSRP_URL_TYPE.equals(name))
83       return Constants.TYPE_PARAMETER;
84     return name;
85   }
86
87   private String JavaDoc replaceValue(String JavaDoc value){
88     if(value.startsWith(WSRPConstants.WSRP_PREFIX))
89       value = value.substring(WSRPConstants.WSRP_PREFIX.length());
90     if(WSRPConstants.URL_TYPE_BLOCKINGACTION.equals(value))
91       value = "action";
92     return value;
93   }
94
95 }
Popular Tags