1 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 ; 23 import javax.servlet.http.Cookie ; 24 import java.net.URLEncoder ; 25 import java.io.UnsupportedEncodingException ; 26 27 32 public class Utils { 33 34 42 public static String appendURLParam(String url, String param, String value){ 43 44 StringBuffer urlString = new StringBuffer (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 e) { 55 throw new RuntimeException (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 getCookieValue(HttpServletRequest request, 80 String cookieName){ 81 Cookie [] 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 urlEncode(String url){ 93 String urlString; 94 try{ 95 urlString = URLEncoder.encode(url,"UTF-8"); 96 }catch(UnsupportedEncodingException e){ 97 throw new RuntimeException (e); 98 } 99 return urlString; 100 } 101 } 102 | Popular Tags |