View Javadoc

1   /* Copyright (2005-2007) Schibsted Søk AS
2    * This file is part of SESAT.
3    *
4    *   SESAT is free software: you can redistribute it and/or modify
5    *   it under the terms of the GNU Affero General Public License as published by
6    *   the Free Software Foundation, either version 3 of the License, or
7    *   (at your option) any later version.
8    *
9    *   SESAT is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU Affero General Public License for more details.
13   *
14   *   You should have received a copy of the GNU Affero General Public License
15   *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
16  */
17  package no.sesat.search.result;
18  
19  import java.util.StringTokenizer;
20  import no.sesat.search.site.Site;
21  import no.sesat.search.site.config.SiteConfiguration;
22  import org.apache.log4j.Logger;
23  
24  /**
25   * Boomerang manipulates URL links, for example to ensure outbound links are logged.
26   *
27   * <b> Immutable. </b>
28   *
29   *
30   *
31   * @version $Id: Boomerang.java 6596 2008-05-10 10:05:48Z ssmiweve $
32   */
33  public final class Boomerang {
34  
35      // Constants -----------------------------------------------------
36  
37      private static final Logger LOG = Logger.getLogger(Boomerang.class);
38  
39      private static final String BASE_URL = "boomerang/";
40  
41      // Attributes ----------------------------------------------------
42  
43      // Static --------------------------------------------------------
44  
45      public static String getUrl(
46              final Site site,
47              final String orgUrl,
48              final String paramString) {
49  
50          final StringBuilder toUrl = new StringBuilder("http://" + site.getName() + BASE_URL);
51  
52          // click attributes comes as a string seperated by ';'
53          final StringTokenizer tokeniser = new StringTokenizer(paramString, ";");
54          while(tokeniser.hasMoreTokens()){
55              toUrl.append(tokeniser.nextToken().replace(':', '='));
56              toUrl.append(';');
57          }
58  
59          // remove last ';'
60          toUrl.setLength(toUrl.length() - 1);
61  
62          toUrl.append('/');
63  
64          // add the destination url
65          if(!orgUrl.startsWith("http")){
66  
67              // any relative url must be made absolute against the current skin
68              toUrl.append("http://" + site.getName());
69  
70              // avoid double /
71              if(orgUrl.startsWith("/")){
72                  toUrl.setLength(toUrl.length() - 1);
73              }
74          }
75  
76          // append the original destination url
77          toUrl.append(orgUrl);
78  
79          return toUrl.toString();
80      }
81  
82      // Constructors -------------------------------------------------
83  
84      // Public --------------------------------------------------------
85  
86      // Package protected ---------------------------------------------
87  
88      // Protected -----------------------------------------------------
89  
90      // Private -------------------------------------------------------
91  
92      // Inner classes -------------------------------------------------
93  }