KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > util > Utils


1 /**
2  * Copyright 2004-2005 jManage.org
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 package org.jmanage.webui.util;
17
18 import org.jmanage.core.services.ServiceContext;
19 import org.jmanage.core.services.ServiceContextImpl;
20 import org.jmanage.core.util.Expression;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.Cookie JavaDoc;
24 import java.net.URLEncoder JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26
27 /**
28  *
29  * date: Jun 20, 2004
30  * @author Rakesh Kalra
31  */

32 public class Utils {
33
34     /**
35      * Appends param and value to the given url.
36      *
37      * @param url
38      * @param param
39      * @param value
40      * @return
41      */

42     public static String JavaDoc appendURLParam(String JavaDoc url, String JavaDoc param, String JavaDoc value){
43
44         StringBuffer JavaDoc urlString = new StringBuffer JavaDoc(url);
45         if (url.indexOf("?") == -1) {
46             urlString.append("?");
47         }else if(url.endsWith("&") == false) {
48             urlString.append("&");
49         }
50         urlString.append(param);
51         urlString.append("=");
52         try {
53             urlString.append(URLEncoder.encode(value, "UTF-8"));
54         } catch (UnsupportedEncodingException JavaDoc e) {
55             throw new RuntimeException JavaDoc(e);
56         }
57
58         return urlString.toString();
59     }
60
61     public static ServiceContext getServiceContext(WebContext webContext){
62         return webContext.getServiceContext();
63     }
64
65     public static ServiceContext getServiceContext(WebContext context,
66                                              Expression expression){
67         ServiceContextImpl srvcContext = new ServiceContextImpl();
68         srvcContext.setUser(context.getUser());
69         if(expression.getAppName().equals(Expression.WILDCARD)){
70             srvcContext.setApplicationName((
71                     context.getApplicationConfig().getName()));
72         }else{
73             srvcContext.setApplicationName(expression.getAppName());
74         }
75         srvcContext.setMBeanName(expression.getMBeanName());
76         return srvcContext;
77     }
78
79     public static String JavaDoc getCookieValue(HttpServletRequest JavaDoc request,
80                                         String JavaDoc cookieName){
81         Cookie JavaDoc[] cookies = request.getCookies();
82         if(cookies != null){
83             for(int i=0; i<cookies.length; i++){
84                 if(cookies[i].getName().equals(cookieName)){
85                     return cookies[i].getValue();
86                 }
87             }
88         }
89         return null;
90     }
91
92     public static String JavaDoc urlEncode(String JavaDoc url){
93         String JavaDoc urlString;
94         try{
95             urlString = URLEncoder.encode(url,"UTF-8");
96         }catch(UnsupportedEncodingException JavaDoc e){
97             throw new RuntimeException JavaDoc(e);
98         }
99         return urlString;
100     }
101 }
102
Popular Tags