View Javadoc

1   /* Copyright (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.util;
18  
19  import java.security.MessageDigest;
20  import java.security.NoSuchAlgorithmException;
21  import java.util.UUID;
22  import javax.servlet.http.Cookie;
23  import javax.servlet.http.HttpServletRequest;
24  import org.apache.commons.codec.binary.Hex;
25  
26  
27  
28  /**
29   * <b> Must be threadsafe </b>
30   *
31   * @version <tt>$Id$</tt>
32   */
33  public final class TradeDoubler {
34  
35      private final static String secretCode = "997";
36      private final static String organization = "1064392";
37      private final static String event = "46757";
38      private final HttpServletRequest request;
39  
40      public TradeDoubler(HttpServletRequest request) {
41          this.request = request;
42      }
43  
44      public String getChecksum(
45              final String orderNumber,
46              final String orderValue) throws RuntimeException {
47  
48          MessageDigest digest = null;
49          try {
50              digest = MessageDigest.getInstance("MD5");
51          } catch(NoSuchAlgorithmException e) {
52              throw new RuntimeException(e.getMessage());
53          }
54          final String s = new String(TradeDoubler.getSecretCode() + orderNumber + orderValue);
55          digest.update(s.getBytes());
56          return new String(Hex.encodeHex(digest.digest()));
57      }
58  
59      public String getUUID() {
60          return UUID.randomUUID().toString();
61      }
62  
63      public static String getEvent() {
64          return event;
65      }
66  
67      public static String getOrganization() {
68          return organization;
69      }
70  
71      public static String getSecretCode() {
72          return secretCode;
73      }
74  
75      public String getCookieTDUID() {
76          return getCookie("TRADEDOUBLER");
77      }
78  
79      public String getCookieOrderNumber(){
80          return getCookie("TRADEDOUBLER-onr");
81      }
82  
83      public String getCookieChecksum(){
84          return getCookie("TRADEDOUBLER-cs");
85      }
86  
87      public String getCookieReportInfo(){
88          return getCookie("TRADEDOUBLER-ri");
89      }
90  
91      private String getCookie(String name){
92          if (this.request == null){
93              return "";
94          }
95          String value = "";
96          final Cookie[] cookies = this.request.getCookies();
97          if (cookies != null){
98              for (int i = 0; i < cookies.length; i++) {
99                  if (cookies[i].getName().equals(name)) {
100                     if (cookies[i].getValue() != null) {
101                         value = cookies[i].getValue();
102                     }
103                 }
104             }
105         }
106         return value;
107     }
108 }