1 package com.opensymphony.webwork.views.util; 2 3 import com.opensymphony.webwork.ServletActionContext; 4 import com.opensymphony.webwork.config.Configuration; 5 import com.opensymphony.xwork.util.OgnlValueStack; 6 import com.opensymphony.xwork.util.TextParseUtil; 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 import java.io.UnsupportedEncodingException ; 13 import java.net.URLEncoder ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 18 24 public class UrlHelper { 25 private static final Log LOG = LogFactory.getLog(UrlHelper.class); 26 27 30 private static final int DEFAULT_HTTP_PORT = 80; 31 32 35 private static final int DEFAULT_HTTPS_PORT = 443; 36 37 private static final String AMP = "&"; 38 39 public static String buildUrl(String action, HttpServletRequest request, HttpServletResponse response, Map params) { 40 return buildUrl(action, request, response, params, null, true, true); 41 } 42 43 public static String buildUrl(String action, HttpServletRequest request, HttpServletResponse response, Map params, String scheme, boolean includeContext, boolean encodeResult) { 44 StringBuffer link = new StringBuffer (); 45 46 boolean changedScheme = false; 47 48 int httpPort = DEFAULT_HTTP_PORT; 49 50 try { 51 httpPort = Integer.parseInt((String ) Configuration.get("webwork.url.http.port")); 52 } catch (Exception ex) { 53 } 54 55 int httpsPort = DEFAULT_HTTPS_PORT; 56 57 try { 58 httpsPort = Integer.parseInt((String ) Configuration.get("webwork.url.https.port")); 59 } catch (Exception ex) { 60 } 61 62 if ((scheme != null) && !scheme.equals(request.getScheme())) { 64 changedScheme = true; 65 link.append(scheme); 66 link.append("://"); 67 link.append(request.getServerName()); 68 69 if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT)) || (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT)) { 70 link.append(":"); 71 link.append(scheme.equals("http") ? httpPort : httpsPort); 72 } 73 } 74 75 if (action != null) { 76 if (action.startsWith("/") && includeContext) { 79 link.append(request.getContextPath()); 80 } else if (changedScheme) { 81 String uri = request.getRequestURI(); 82 link.append(uri.substring(0, uri.lastIndexOf('/'))); 83 } 84 85 link.append(action); 87 } else { 88 String requestURI = (String ) request.getAttribute("webwork.request_uri"); 90 91 if (requestURI == null) { 92 requestURI = request.getRequestURI(); 93 } 94 95 link.append(requestURI); 96 } 97 98 if ((params != null) && (params.size() > 0)) { 100 if (link.toString().indexOf("?") == -1) { 101 link.append("?"); 102 } else { 103 link.append(AMP); 104 } 105 106 Iterator iter = params.entrySet().iterator(); 108 109 String [] valueHolder = new String [1]; 110 111 while (iter.hasNext()) { 112 Map.Entry entry = (Map.Entry ) iter.next(); 113 String name = (String ) entry.getKey(); 114 Object value = entry.getValue(); 115 116 String [] values; 117 118 if (value instanceof String []) { 119 values = (String []) value; 120 } else { 121 valueHolder[0] = value.toString(); 122 values = valueHolder; 123 } 124 125 for (int i = 0; i < values.length; i++) { 126 if (values[i] != null) { 127 link.append(name); 128 link.append('='); 129 link.append(translateAndEncode(values[i])); 130 } 131 132 if (i < (values.length - 1)) { 133 link.append("&"); 134 } 135 } 136 137 if (iter.hasNext()) { 138 link.append("&"); 139 } 140 } 141 } 142 143 String result; 144 145 try { 146 result = encodeResult ? response.encodeURL(link.toString()) : link.toString(); 147 } catch (Exception ex) { 148 result = link.toString(); 151 } 152 153 return result; 154 } 155 156 164 public static String translateAndEncode(String input) { 165 OgnlValueStack valueStack = ServletActionContext.getContext().getValueStack(); 166 String output = TextParseUtil.translateVariables(input, valueStack); 167 168 try { 169 return URLEncoder.encode(output, "UTF-8"); 170 } catch (UnsupportedEncodingException e) { 171 LOG.warn("Could not encode URL parameter '" + input + "', returning value un-encoded"); 172 return output; 173 } 174 } 175 } 176 | Popular Tags |