1 package br.com.ibnetwork.guara.pull.tools;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.apache.avalon.framework.configuration.Configurable;
9 import org.apache.avalon.framework.configuration.Configuration;
10 import org.apache.avalon.framework.configuration.ConfigurationException;
11
12 import br.com.ibnetwork.guara.pipeline.valve.modules.ActionExecutor;
13 import br.com.ibnetwork.guara.pull.impl.ApplicationToolSupport;
14 import br.com.ibnetwork.guara.rundata.RunData;
15 import br.com.ibnetwork.guara.util.ServletUtils;
16
17 /***
18 * @author leandro
19 */
20 public class LinkTool
21 extends ApplicationToolSupport
22 implements Configurable
23 {
24 private String baseAddress;
25
26 private String pipeline;
27
28 private String template;
29
30 private String action;
31
32 private String layout;
33
34 private String screen;
35
36 private String encoding;
37
38 private String queryString;
39
40 private String pathString;
41
42 public LinkTool()
43 {
44
45 }
46
47 public void configure(Configuration conf)
48 throws ConfigurationException
49 {
50 encoding = conf.getChild("encoding").getAttribute("name","ISO-8859-1");
51 }
52
53 public void refresh(RunData data)
54 {
55 this.data = data;
56 clear();
57 if(baseAddress != null)
58 {
59 return;
60 }
61 HttpServletRequest request = data.getRequest();
62 baseAddress = ServletUtils.getBaseAddressFromRequest(request);
63 }
64
65 public LinkTool page(String templateName)
66 {
67 return setPage(templateName);
68 }
69
70 public LinkTool setPage(String templateName)
71 {
72 return setTemplate(templateName);
73 }
74
75 public LinkTool template(String templateName)
76 {
77 return setTemplate(templateName);
78 }
79
80 public LinkTool setTemplate(String templateName)
81 {
82 this.template = templateName;
83 return this;
84 }
85
86 public LinkTool pipeline(String pipelineName)
87 {
88 return setPipeline(pipelineName);
89 }
90
91 public LinkTool setPipeline(String pipelineName)
92 {
93 this.pipeline = pipelineName;
94 return this;
95 }
96
97 public LinkTool action(String actionName)
98 {
99 return setAction(actionName);
100 }
101
102 public LinkTool setAction(String actionName)
103 {
104 this.action = actionName;
105 return this;
106 }
107
108 public LinkTool layout(String layoutName)
109 {
110 return setLayout(layoutName);
111 }
112
113 public LinkTool setLayout(String layoutName)
114 {
115 this.layout = layoutName;
116 return this;
117 }
118
119 public LinkTool screen(String screenName)
120 {
121 return setScreen(screenName);
122 }
123
124 public LinkTool setScreen(String screenName)
125 {
126 this.screen = screenName;
127 return this;
128 }
129
130 public LinkTool exec(String name)
131 throws UnsupportedEncodingException
132 {
133 return addQueryData(ActionExecutor.METHOD_KEY+name,"1");
134 }
135
136 public LinkTool add(String name)
137 throws UnsupportedEncodingException
138 {
139 return addQueryData(name);
140 }
141
142 public LinkTool add(String name, Object value)
143 throws UnsupportedEncodingException
144 {
145 return addQueryData(name,value);
146 }
147
148 public LinkTool addQueryData(String name)
149 throws UnsupportedEncodingException
150 {
151 return addQueryData(name,null);
152 }
153
154 public LinkTool addQueryData(String name, Object value)
155 throws UnsupportedEncodingException
156 {
157 if(name == null)
158 {
159 return this;
160 }
161 name = URLEncoder.encode(name,encoding);
162 if(queryString == null)
163 {
164 queryString = "?"+name;
165 }
166 else
167 {
168 queryString += "&"+name;
169 }
170 if(value != null)
171 {
172 value = URLEncoder.encode(value.toString(),encoding);
173 queryString += "="+value;
174 }
175 return this;
176 }
177
178 public LinkTool addPathInfo(String name, Object value)
179 throws UnsupportedEncodingException
180 {
181 if(name == null)
182 {
183 return this;
184 }
185 name = URLEncoder.encode(name,encoding);
186 value = value != null ? URLEncoder.encode(value.toString(),encoding) : "" ;
187 if(pathString == null)
188 {
189 pathString = "";
190 }
191 pathString += "/"+name+"/"+value;
192
193 return this;
194 }
195
196 public String toString()
197 {
198 String result = baseAddress
199 + (pipeline != null ? "/pipeline/" + pipeline : "")
200 + (template != null ? "/template/" + template : "")
201 + (layout != null ? "/layout/" + layout : "")
202 + (screen != null ? "/screen/" + screen : "")
203 + (action != null ? "/action/" + action : "")
204 + (pathString != null ? pathString : "")
205 + (queryString != null ? queryString : "")
206 ;
207
208 clear();
209 return result;
210 }
211
212 private void clear()
213 {
214 pipeline = null;
215 template = null;
216 screen = null;
217 layout = null;
218 action = null;
219 queryString = null;
220 pathString = null;
221 }
222
223 }