KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > EmailLink


1 package org.tigris.scarab.util;
2
3 /* ================================================================
4  * Copyright (c) 2000-2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by CollabNet <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of CollabNet.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of CollabNet.
47  */

48
49 import java.util.ArrayList JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.apache.commons.lang.StringUtils;
53 import org.apache.fulcrum.pool.InitableRecyclable;
54 import org.tigris.scarab.om.Issue;
55 import org.tigris.scarab.om.Module;
56
57 /**
58  *
59  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
60  * @version $Id: EmailLink.java 9476 2005-03-16 22:56:11Z dabbous $
61  */

62 public class EmailLink
63     implements InitableRecyclable, SkipFiltering
64 {
65     private String JavaDoc label;
66     private String JavaDoc attributeText;
67     private String JavaDoc alternateText;
68     private Integer JavaDoc currentModuleId;
69     private Module currentModule;
70     private boolean isOmitModule;
71     private boolean isOmitIssueType;
72     private boolean overrideSecurity;
73
74     private boolean disposed = false;
75
76     /** An ArrayList that contains all the path info if any. */
77     private List JavaDoc pathInfo = new ArrayList JavaDoc();
78
79     /** HTTP protocol. */
80     public static final String JavaDoc HTTP = "http";
81
82     /** HTTPS protocol. */
83     public static final String JavaDoc HTTPS = "https";
84
85     /**
86      * Constructor to allow factory instantiation of
87      * EmailLinks. setCurrentModule must be called before
88      * first use.
89      */

90     public EmailLink()
91     {
92     }
93
94     /**
95      * Constructor.
96      */

97     public EmailLink(Module currentModule)
98     {
99         setCurrentModule(currentModule);
100     }
101
102     public void init(Object JavaDoc obj)
103     {
104         if (obj instanceof Module)
105         {
106             setCurrentModule((Module)obj);
107         }
108     }
109
110     public Module getCurrentModule()
111     {
112         return this.currentModule;
113     }
114
115     public void setCurrentModule(Module cM)
116     {
117         currentModuleId = cM.getModuleId();
118         this.currentModule = cM;
119     }
120
121     public void refresh()
122     {
123         label = null;
124         attributeText = null;
125         alternateText = null;
126         currentModuleId = null;
127         currentModule = null;
128         isOmitModule = false;
129         isOmitIssueType = false;
130         this.pathInfo.clear();
131     }
132
133     private String JavaDoc convertAndTrim(String JavaDoc value)
134     {
135         String JavaDoc tmp = null;
136         if (value != null)
137         {
138             tmp = value.trim();
139             tmp = tmp.toLowerCase();
140         }
141         return tmp;
142     }
143
144     /**
145      * Add a key value pair (in the form of a 2 object array) to the provided
146      * list
147      *
148      * @param list List to add to.
149      * @param name A String with the name to add.
150      * @param value A String with the value to add.
151      */

152     protected void addPair(List JavaDoc list,
153                            String JavaDoc name,
154                            String JavaDoc value)
155     {
156         Object JavaDoc[] tmp = new Object JavaDoc[2];
157
158         tmp[0] = convertAndTrim(name);
159         tmp[1] = value;
160
161         list.add(tmp);
162     }
163
164     /**
165      * Adds a name=value pair to the path_info string.
166      *
167      * @param name A String with the name to add.
168      * @param value A String with the value to add.
169      */

170     public EmailLink addPathInfo(String JavaDoc name, String JavaDoc value)
171     {
172         addPair(pathInfo, name, value);
173         return this;
174     }
175
176     /**
177      * Adds a name=value pair to the path_info string.
178      *
179      * @param name A String with the name to add.
180      * @param value An Object with the value to add.
181      */

182     public EmailLink addPathInfo(String JavaDoc name, Object JavaDoc value)
183     {
184         addPathInfo(name, value.toString());
185         return this;
186     }
187
188     /**
189      * Adds a name=value pair to the path_info string.
190      *
191      * @param name A String with the name to add.
192      * @param value A double with the value to add.
193      */

194     public EmailLink addPathInfo(String JavaDoc name, double value)
195     {
196         addPathInfo(name, Double.toString(value));
197         return this;
198     }
199
200     /**
201      * Adds a name=value pair to the path_info string.
202      *
203      * @param name A String with the name to add.
204      * @param value An int with the value to add.
205      */

206     public EmailLink addPathInfo(String JavaDoc name, int value)
207     {
208         addPathInfo(name, Integer.toString(value));
209         return this;
210     }
211
212     /**
213      * Adds a name=value pair to the path_info string.
214      *
215      * @param name A String with the name to add.
216      * @param value A long with the value to add.
217      */

218     public EmailLink addPathInfo(String JavaDoc name, long value)
219     {
220         addPathInfo(name, Long.toString(value));
221         return this;
222     }
223
224     /**
225      * Adds a name=value pair to the path_info string.
226      *
227      * @param name A String with the name to add.
228      * @param value A double with the value to add.
229      */

230     public EmailLink addPathInfo(String JavaDoc name, boolean value)
231     {
232         addPathInfo(name, (value ? "true" : "false"));
233         return this;
234     }
235
236     public EmailLink setPathInfo(String JavaDoc key, String JavaDoc value)
237     {
238         removePathInfo(key);
239         addPathInfo(key, value);
240         return this;
241     }
242
243     /**
244      * Helper method to remove one or more pairs by its name (ie key).
245      * It is intended to be used with <tt>queryData</tt> and <tt>pathInfo</tt>.
246      * @param pairs the list of pairs to look over for removal.
247      * @param name the name of the pair(s) to remove.
248      */

249     protected void removePairByName(List JavaDoc pairs, String JavaDoc name)
250     {
251         name = convertAndTrim(name);
252         // CAUTION: the dynamic evaluation of the size is on purpose because
253
// elements may be removed on the fly.
254
for (int i = 0; i < pairs.size(); i++)
255         {
256             Object JavaDoc[] pair = (Object JavaDoc[])pairs.get(i);
257             if ( name.equals( pair[0] ) )
258             {
259                 pairs.remove(i);
260             }
261         }
262     }
263
264     /**
265      * Removes all the path info elements.
266      */

267     public void removePathInfo()
268     {
269         this.pathInfo.clear();
270     }
271
272     /**
273      * Removes a name=value pair from the path info.
274      *
275      * @param name A String with the name to be removed.
276      */

277     public void removePathInfo(String JavaDoc name)
278     {
279         removePairByName( pathInfo, name );
280     }
281
282     /**
283      * Gets the server name.
284      *
285      * @return A String with the server name.
286      */

287     public String JavaDoc getServerName()
288     {
289         String JavaDoc domain = null;
290         if (currentModule != null)
291         {
292             domain = currentModule.getHttpDomain();
293             if (domain == null || domain.length() == 0)
294             {
295                 domain = "check.Scarab.properties";
296             }
297         }
298         
299         return domain;
300     }
301
302     /**
303      * Gets the server port.
304      *
305      * @return A the server port, or <code>-1</code> if unknown.
306      */

307     public int getServerPort()
308     {
309         int result = -1;
310         if (currentModule != null)
311         {
312             try
313             {
314                 String JavaDoc port = currentModule.getPort();
315                 if (StringUtils.isNotEmpty(port))
316                 {
317                     result = Integer.parseInt(port);
318                 }
319             }
320             catch (Exception JavaDoc e)
321             {
322                 Log.get().debug(e);
323             }
324         }
325         return result;
326     }
327
328     /**
329      * Gets the server scheme (HTTP or HTTPS).
330      *
331      * @return A String with the server scheme.
332      */

333     public String JavaDoc getServerScheme()
334     {
335         String JavaDoc result = null;
336         try
337         {
338             if (currentModule != null)
339             {
340                 result = currentModule.getScheme();
341             }
342         }
343         catch (Exception JavaDoc e)
344         {
345             Log.get().debug(e);
346         }
347         return result;
348     }
349
350     /**
351      * Gets the server scriptName (/scarab/issues).
352      *
353      * @return A String with the server scriptName.
354      */

355     public String JavaDoc getScriptName()
356     {
357         String JavaDoc result = null;
358         try
359         {
360             if (currentModule != null)
361             {
362                 result = currentModule.getScriptName();
363             }
364         }
365         catch (Exception JavaDoc e)
366         {
367             Log.get().debug(e);
368         }
369         return result;
370     }
371
372     /**
373      * Does this URI have path info.
374      */

375     public boolean hasPathInfo()
376     {
377         return ! pathInfo.isEmpty();
378     }
379
380     /**
381      * This method takes a Vector of key/value arrays and writes it to the
382      * supplied StringBuffer as encoded path info.
383      *
384      * @param pairs A Vector of key/value arrays.
385      * @return a String to which encoded path info is written
386      */

387     protected String JavaDoc renderPathInfo(List JavaDoc pairs)
388     {
389         return renderPairs( pairs, '/', '/' );
390     }
391
392     /**
393      * This method takes a List of key/value arrays and converts it
394      * into a URL encoded key/value pair format with the appropriate
395      * separator.
396      *
397      * @return a String to which the pairs are written to.
398      * @param pairs A List of key/value arrays.
399      * @param pairSep the character to use as a separator between pairs.
400      * For example for a query-like rendering it would be '&'.
401      * @param keyValSep the character to use as a separator between
402      * key and value. For example for a query-like rendering, it would be '='.
403      */

404     protected String JavaDoc renderPairs(List JavaDoc pairs, char pairSep, char keyValSep)
405     {
406         boolean first = true;
407         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
408         final int count = pairs.size();
409         for (int i = 0; i < count; i++)
410         {
411             Object JavaDoc[] pair = (Object JavaDoc[]) pairs.get(i);
412
413             if ( first )
414             {
415                 first = false;
416             }
417             else
418             {
419                 out.append(pairSep);
420             }
421
422             out.append(ScarabUtil.urlEncode((String JavaDoc) pair[0]));
423             out.append(keyValSep);
424             out.append(ScarabUtil.urlEncode((String JavaDoc) pair[1]));
425         }
426         return out.toString();
427     }
428
429     /**
430      * Builds the URL with all of the data URL-encoded as well as
431      * encoded using HttpServletResponse.encodeUrl().
432      *
433      * <p>
434      * <code><pre>
435      * DynamicURI dui = new DynamicURI (data, "UserScreen" );
436      * dui.addPathInfo("user","jon");
437      * dui.toString();
438      * </pre></code>
439      *
440      * The above call to toString() would return the String:
441      *
442      * <p>
443      * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
444      *
445      * @return A String with the built URL.
446      */

447     public String JavaDoc toString()
448     {
449         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
450         output.append(getServerScheme());
451         output.append("://");
452         output.append(getServerName());
453         int port = getServerPort();
454         if (port >= 0
455             && ((HTTP.equals(getServerScheme()) && port != 80)
456                 || (HTTPS.equals(getServerScheme()) && port != 443)))
457         {
458             output.append(':');
459             output.append(port);
460         }
461
462         output.append(getScriptName());
463
464         if (this.hasPathInfo())
465         {
466             output.append('/');
467             output.append(renderPathInfo(this.pathInfo));
468         }
469         return output.toString();
470     }
471
472     /**
473      * Causes the link to not include the module id. Useful for templates
474      * where a module is not required or desired.
475      *
476      * @return a <code>EmailLink</code> value
477      */

478     public EmailLink omitModule()
479     {
480         isOmitModule = true;
481         return this;
482     }
483
484     /**
485      * Sets the template variable used by the Template Service. The
486      * module id of the new selected module is given.
487      *
488      * @param t A String with the template name.
489      * @return A EmailLink.
490      */

491     public EmailLink setPage(String JavaDoc t)
492     {
493         addPathInfo(ScarabConstants.TEMPLATE, t);
494
495         if (!isOmitModule)
496         {
497             addPathInfo(ScarabConstants.CURRENT_MODULE, currentModuleId);
498         }
499         return this;
500     }
501
502     /**
503      * Sets the action= value for this URL.
504      *
505      * <p>By default it adds the information to the path_info instead
506      * of the query data.
507      *
508      * @param action A String with the action value.
509      * @return A EmailLink (self).
510      */

511     public EmailLink setAction(String JavaDoc action)
512     {
513         addPathInfo(ScarabConstants.ACTION, action);
514         return this;
515     }
516
517     /**
518      * Returns a short link for viewing a single issue
519      *
520      * @param issue an <code>Issue</code> value
521      * @return a <code>String</code> value
522      * @exception Exception if an error occurs
523      */

524     public EmailLink getIssueIdLink(Issue issue)
525         throws Exception JavaDoc
526     {
527         this.addPathInfo(ScarabConstants.ID, issue.getUniqueId());
528         return this;
529     }
530
531     // ****************************************************************
532
// ****************************************************************
533
// Implementation of Recyclable
534
// ****************************************************************
535
// ****************************************************************
536

537     /**
538      * Recycles the object by removing its disposed flag.
539      */

540     public void recycle()
541     {
542         disposed = false;
543     }
544
545     /**
546      * Disposes the object by setting its disposed flag.
547      */

548     public void dispose()
549     {
550         refresh();
551         disposed = true;
552     }
553
554     /**
555      * Checks whether the object is disposed.
556      *
557      * @return true, if the object is disposed.
558      */

559     public boolean isDisposed()
560     {
561         return disposed;
562     }
563 }
564
Popular Tags