1 package com.opensymphony.webwork.components; 2 3 import com.opensymphony.webwork.views.util.UrlHelper; 4 import com.opensymphony.xwork.util.OgnlValueStack; 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 import javax.servlet.http.HttpUtils ; 11 import java.io.IOException ; 12 import java.io.Writer ; 13 14 19 public class URL extends Component { 20 private static final Log LOG = LogFactory.getLog(URL.class); 21 22 31 public static final String NONE = "none"; 32 public static final String GET = "get"; 33 public static final String ALL = "all"; 34 35 private HttpServletRequest req; 36 private HttpServletResponse res; 37 38 protected String includeParams; 39 protected String scheme; 40 protected String value; 41 protected boolean encode = true; 42 protected boolean includeContext = true; 43 44 public URL(OgnlValueStack stack, HttpServletRequest req, HttpServletResponse res) { 45 super(stack); 46 this.req = req; 47 this.res = res; 48 } 49 50 public void start(Writer writer) { 51 if (value != null) { 52 value = findString(value); 53 } 54 55 try { 58 String includeParams = null; 59 60 if (this.includeParams != null) { 61 includeParams = findString(this.includeParams); 62 } 63 64 if ((includeParams == null && value == null) || GET.equalsIgnoreCase(includeParams)) { 65 String query = req.getQueryString(); 67 68 if (query != null) { 69 int idx = query.lastIndexOf('#'); 71 72 if (idx != -1) { 73 query = query.substring(0, idx - 1); 74 } 75 76 parameters.putAll(HttpUtils.parseQueryString(query)); 77 } 78 } else if (ALL.equalsIgnoreCase(includeParams)) { 79 parameters.putAll(req.getParameterMap()); 80 } else if (value == null && !NONE.equalsIgnoreCase(includeParams)) { 81 LOG.warn("Unknown value for includeParams parameter to URL tag: " + includeParams); 82 } 83 } catch (Exception e) { 84 LOG.warn("Unable to put request parameters (" + req.getQueryString() + ") into parameter map.", e); 85 } 86 87 } 88 89 public void end(Writer writer) { 90 String scheme = req.getScheme(); 91 92 if (this.scheme != null) { 93 scheme = this.scheme; 94 } 95 96 String result = UrlHelper.buildUrl(value, req, res, parameters, scheme, includeContext, encode); 97 98 String id = getId(); 99 100 if (id != null) { 101 getStack().getContext().put(id, result); 102 103 req.setAttribute(id, result); 105 } else { 106 try { 107 writer.write(result); 108 writer.flush(); 109 } catch (IOException e) { 110 e.printStackTrace(); 111 throw new RuntimeException ("IOError: " + e.getMessage(), e); 112 } 113 } 114 115 super.end(writer); 116 } 117 118 public void setIncludeParams(String includeParams) { 119 this.includeParams = includeParams; 120 } 121 122 public void setScheme(String scheme) { 123 this.scheme = scheme; 124 } 125 126 public void setValue(String value) { 127 this.value = value; 128 } 129 130 public void setEncode(boolean encode) { 131 this.encode = encode; 132 } 133 134 public void setIncludeContext(boolean includeContext) { 135 this.includeContext = includeContext; 136 } 137 } 138 | Popular Tags |