1
22
23 package com.liferay.taglib.util;
24
25 import com.liferay.portal.kernel.servlet.StringServletResponse;
26 import com.liferay.portal.util.WebKeys;
27 import com.liferay.util.servlet.DynamicServletRequest;
28
29 import java.io.IOException;
30
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33
34 import javax.servlet.RequestDispatcher;
35 import javax.servlet.ServletContext;
36 import javax.servlet.ServletException;
37 import javax.servlet.ServletRequest;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.jsp.tagext.BodyTagSupport;
41
42
49 public class ParamAndPropertyAncestorTagImpl
50 extends BodyTagSupport implements ParamAncestorTag, PropertyAncestorTag {
51
52 public void addParam(String name, String value) {
53 if (_params == null) {
54 _params = new LinkedHashMap<String, String[]>();
55 }
56
57 String[] values = _params.get(name);
58
59 if (values == null) {
60 values = new String[] {value};
61 }
62 else {
63 String[] newValues = new String[values.length + 1];
64
65 System.arraycopy(values, 0, newValues, 0, values.length);
66
67 newValues[newValues.length - 1] = value;
68
69 values = newValues;
70 }
71
72 _params.put(name, values);
73 }
74
75 public void addProperty(String name, String value) {
76 if (_properties == null) {
77 _properties = new LinkedHashMap<String, String[]>();
78 }
79
80 String[] values = _properties.get(name);
81
82 if (values == null) {
83 values = new String[] {value};
84 }
85 else {
86 String[] newValues = new String[values.length + 1];
87
88 System.arraycopy(values, 0, newValues, 0, values.length);
89
90 newValues[newValues.length - 1] = value;
91
92 values = newValues;
93 }
94
95 _properties.put(name, values);
96 }
97
98 public void clearParams() {
99 if (_params != null) {
100 _params.clear();
101 }
102 }
103
104 public void clearProperties() {
105 if (_properties != null) {
106 _properties.clear();
107 }
108 }
109
110 public Map<String, String[]> getParams() {
111 return _params;
112 }
113
114 public Map<String, String[]> getProperties() {
115 return _properties;
116 }
117
118 public ServletContext getServletContext() {
119 ServletRequest req = pageContext.getRequest();
120
121 return (ServletContext)req.getAttribute(WebKeys.CTX);
122 }
123
124 public HttpServletRequest getServletRequest() {
125 HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
126
127 if (_params != null) {
128 req = new DynamicServletRequest(req, _params);
129 }
130
131 return req;
132 }
133
134 public StringServletResponse getServletResponse() {
135 return new StringServletResponse(
136 (HttpServletResponse)pageContext.getResponse());
137 }
138
139 public void include(String path) throws IOException, ServletException {
140 ServletContext ctx = getServletContext();
141 HttpServletRequest req = getServletRequest();
142 StringServletResponse res = getServletResponse();
143
144 RequestDispatcher rd = ctx.getRequestDispatcher(path);
145
146 rd.include(req, res);
147
148 pageContext.getOut().print(res.getString());
149 }
150
151 private Map<String, String[]> _params;
152 private Map<String, String[]> _properties;
153
154 }