View Javadoc

1   /**
2    * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda.
3    * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999
4    * http://www.digitalis.pt
5    */
6   package pt.digitalis.dif.controller.objects;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  import java.util.Map.Entry;
11  
12  import pt.digitalis.dif.controller.interfaces.IDIFRequest;
13  import pt.digitalis.dif.controller.interfaces.IDIFSession;
14  import pt.digitalis.dif.dem.interfaces.IStage;
15  import pt.digitalis.dif.dem.managers.IDEMManager;
16  import pt.digitalis.dif.ioc.DIFIoCRegistry;
17  import pt.digitalis.dif.utils.ObjectFormatter;
18  
19  /**
20   * Implementation for {@link IDIFRequest}.
21   * 
22   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
23   * @author Luis Pinto <a href="mailto:lpinto@digitalis.pt">lpinto@digitalis.pt</a>
24   * @created 2007/03/16
25   */
26  public class DIFRequest implements IDIFRequest {
27  
28      /** The key to the original request stored as an attribute. */
29      final static public String ORIGINAL_REQUEST = "originalRequest";
30  
31      /** Ajax mode, false by default */
32      private boolean ajaxMode = false;
33  
34      /**
35       * The attribute collection with the values from the original request.
36       */
37      private Map<String, Object> attributes = new HashMap<String, Object>();
38  
39      /** The descriptor of the client */
40      private ClientDescriptor client;
41  
42      /** Component mode, false by default */
43      private boolean componentMode = false;
44  
45      /** The requested response format. */
46      private String format;
47  
48      /** Help mode, false by default */
49      private boolean helpMode = false;
50  
51      /**
52       * The parameter collection with the values from the original request
53       */
54      private Map<String, Object> parameters = new HashMap<String, Object>();
55  
56      /** Popup mode, false by default */
57      private boolean popupMode = false;
58  
59      /** REST action, if set in request */
60      RESTAction restAction = null;
61  
62      /** REST call, false by default */
63      private boolean restCall = false;
64  
65      /** The requested stage. */
66      private String stage;
67  
68      /** Template mode, false by default */
69      private boolean templateMode = false;
70  
71      /** The session. */
72      private IDIFSession theSession;
73  
74      /**
75       * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#addAttribute(String, Object)
76       */
77      public void addAttribute(String attributeName, Object attributeValue)
78      {
79          attributes.put(attributeName, attributeValue);
80      }
81  
82      /**
83       * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#addParameter(String, Object)
84       */
85      public void addParameter(String parameterName, Object parameterValue)
86      {
87          parameters.put(parameterName.toLowerCase(), parameterValue);
88      }
89  
90      /**
91       * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getAttribute(java.lang.String)
92       */
93      public Object getAttribute(String name)
94      {
95          return this.attributes.get(name);
96      }
97  
98      /**
99       * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getAttributes()
100      */
101     public Map<String, Object> getAttributes()
102     {
103         return this.attributes;
104     }
105 
106     /**
107      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getClient()
108      */
109     public ClientDescriptor getClient()
110     {
111         return client;
112     }
113 
114     /**
115      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getFormat()
116      */
117     public String getFormat()
118     {
119         return format;
120     }
121 
122     /**
123      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getParameter(java.lang.String)
124      */
125     public Object getParameter(String name)
126     {
127         return this.parameters.get(name.toLowerCase());
128     }
129 
130     /**
131      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getParameters()
132      */
133     public Map<String, Object> getParameters()
134     {
135         return this.parameters;
136     }
137 
138     /**
139      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getRestAction()
140      */
141     public RESTAction getRestAction()
142     {
143         return restAction;
144     }
145 
146     /**
147      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getSession()
148      */
149     public IDIFSession getSession()
150     {
151         return this.theSession;
152     }
153 
154     /**
155      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getStage()
156      */
157     public String getStage()
158     {
159         return stage;
160     }
161 
162     /**
163      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#getStageProxy()
164      */
165     public IStage getStageProxy()
166     {
167         return DIFIoCRegistry.getRegistry().getImplementation(IDEMManager.class).getStage(getStage());
168     }
169 
170     /**
171      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#isAjaxMode()
172      */
173     public boolean isAjaxMode()
174     {
175         return ajaxMode;
176     }
177 
178     /**
179      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#isComponentMode()
180      */
181     public boolean isComponentMode()
182     {
183         return componentMode;
184     }
185 
186     /**
187      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#isHelpMode()
188      */
189     public boolean isHelpMode()
190     {
191         return helpMode;
192     }
193 
194     /**
195      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#isPopupMode()
196      */
197     public boolean isPopupMode()
198     {
199         return popupMode;
200     }
201 
202     /**
203      * Inspector for the 'restCall' attribute.
204      * 
205      * @return the restCall value
206      */
207     public boolean isRestCall()
208     {
209         return restCall;
210     }
211 
212     /**
213      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#isTemplateMode()
214      */
215     public boolean isTemplateMode()
216     {
217         return templateMode;
218     }
219 
220     /**
221      * Modifier for the 'ajaxMode' attribute.
222      * 
223      * @param ajaxMode
224      *            the new ajaxMode value to set
225      */
226     public void setAjaxMode(boolean ajaxMode)
227     {
228         this.ajaxMode = ajaxMode;
229     }
230 
231     /**
232      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setAttributes(Map)
233      */
234     public void setAttributes(Map<String, Object> attributes)
235     {
236         this.attributes = attributes;
237     }
238 
239     /**
240      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setClient(ClientDescriptor)
241      */
242     public void setClient(ClientDescriptor client)
243     {
244         this.client = client;
245     }
246 
247     /**
248      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setComponentMode(boolean)
249      */
250     public void setComponentMode(boolean componentMode)
251     {
252         this.componentMode = componentMode;
253     }
254 
255     /**
256      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setFormat(java.lang.String)
257      */
258     public void setFormat(String format)
259     {
260         this.format = format;
261     }
262 
263     /**
264      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setHelpMode(boolean)
265      */
266     public void setHelpMode(boolean helpMode)
267     {
268         this.helpMode = helpMode;
269     }
270 
271     /**
272      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setParameters(Map)
273      */
274     public void setParameters(Map<String, Object> parameters)
275     {
276         for (Entry<String, Object> entry: parameters.entrySet())
277             addParameter(entry.getKey().toLowerCase(), entry.getValue());
278     }
279 
280     /**
281      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setPopupMode(boolean)
282      */
283     public void setPopupMode(boolean popupMode)
284     {
285         this.popupMode = popupMode;
286     }
287 
288     /**
289      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setRestAction(pt.digitalis.dif.controller.objects.RESTAction)
290      */
291     public void setRestAction(RESTAction restAction)
292     {
293         this.restAction = restAction;
294     }
295 
296     /**
297      * Modifier for the 'restCall' attribute.
298      * 
299      * @param restCall
300      *            the new restCall value to set
301      */
302     public void setRestCall(boolean restCall)
303     {
304         this.restCall = restCall;
305     }
306 
307     /**
308      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setSession(pt.digitalis.dif.controller.interfaces.IDIFSession)
309      */
310     public void setSession(IDIFSession session)
311     {
312         this.theSession = session;
313 
314     }
315 
316     /**
317      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setStage(String)
318      */
319     public void setStage(String newStage)
320     {
321         this.stage = newStage;
322     }
323 
324     /**
325      * @see pt.digitalis.dif.controller.interfaces.IDIFRequest#setTemplateMode(boolean)
326      */
327     public void setTemplateMode(boolean templateMode)
328     {
329         this.templateMode = templateMode;
330     }
331 
332     /**
333      * Returns the Request object in a human-readable form.
334      * 
335      * @see java.lang.Object#toString()
336      */
337     @Override
338     public String toString()
339     {
340 
341         ObjectFormatter formatter = new ObjectFormatter();
342         formatter.addItem("Client", client);
343         formatter.addItem("stage", stage);
344         formatter.addItem("Template Mode", templateMode);
345         formatter.addItem("Component Mode", componentMode);
346         formatter.addItem("Popup Mode", isPopupMode());
347         formatter.addItem("REST URL Mode", isRestCall());
348         formatter.addItem("Help Mode", helpMode);
349         formatter.addItem("Session", theSession);
350 
351         Map<String, Object> allowedAttributes = new HashMap<String, Object>();
352         allowedAttributes.putAll(attributes);
353         allowedAttributes.remove("PageContentAttribute");
354         formatter.addItem("Attributes", allowedAttributes);
355 
356         Map<String, Object> allowedParams = new HashMap<String, Object>();
357         allowedParams.putAll(parameters);
358         if (allowedParams.containsKey(PASSWORD_PARAMETER_ID))
359             allowedParams.put(PASSWORD_PARAMETER_ID, "*****");
360         if (allowedParams.containsKey("password"))
361             allowedParams.put("password", "*****");
362         formatter.addItem("Parameters", allowedParams);
363 
364         return formatter.getFormatedObject();
365     }
366 }