1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.taglib.security;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.HttpUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Company;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.util.Encryptor;
32  import com.liferay.util.EncryptorException;
33  
34  import java.security.Key;
35  
36  import java.util.Set;
37  import java.util.StringTokenizer;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.jsp.JspException;
41  import javax.servlet.jsp.PageContext;
42  
43  /**
44   * <a href="EncryptTagUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class EncryptTagUtil {
50  
51      public static void doStartTag(
52              String className, String style, String protocol,
53              Set<String> unencryptedParamsSet, String url, String target,
54              PageContext pageContext)
55          throws JspException {
56  
57          try {
58              StringBuilder sb = new StringBuilder();
59  
60              // Open anchor
61  
62              sb.append("<a ");
63  
64              // Class
65  
66              if (Validator.isNotNull(className)) {
67                  sb.append("class=\"");
68                  sb.append(className);
69                  sb.append("\" ");
70              }
71  
72              // HREF
73  
74              sb.append("href=\"").append(protocol).append("://");
75  
76              int pos = url.indexOf("?");
77  
78              if (pos == -1) {
79                  sb.append(url);
80              }
81              else {
82                  sb.append(url.substring(0, pos)).append("?");
83  
84                  Company company = PortalUtil.getCompany(
85                      (HttpServletRequest)pageContext.getRequest());
86  
87                  Key key = company.getKeyObj();
88  
89                  StringTokenizer st = new StringTokenizer(
90                      url.substring(pos + 1, url.length()), "&");
91  
92                  while (st.hasMoreTokens()) {
93                      String paramAndValue = st.nextToken();
94  
95                      int x = paramAndValue.indexOf("=");
96  
97                      String param = paramAndValue.substring(0, x);
98                      String value = paramAndValue.substring(
99                          x + 1, paramAndValue.length());
100 
101                     sb.append(param).append("=");
102 
103                     if (unencryptedParamsSet.contains(param)) {
104                         sb.append(HttpUtil.encodeURL(value));
105                     }
106                     else {
107                         try {
108                             sb.append(HttpUtil.encodeURL(
109                                 Encryptor.encrypt(key, value)));
110                         }
111                         catch (EncryptorException ee) {
112                             _log.error(ee.getMessage());
113                         }
114 
115                         if (st.hasMoreTokens()) {
116                             sb.append("&");
117                         }
118                     }
119                 }
120 
121                 sb.append("&shuo=1");
122             }
123 
124             sb.append("\" ");
125 
126             // Style
127 
128             if (Validator.isNotNull(style)) {
129                 sb.append("style=\"");
130                 sb.append(style);
131                 sb.append("\" ");
132             }
133 
134             // Target
135 
136             if (Validator.isNotNull(target)) {
137                 sb.append("target=\"" + target + "\"");
138             }
139 
140             // Close anchor
141 
142             sb.append(">");
143 
144             pageContext.getOut().print(sb.toString());
145         }
146         catch (Exception e) {
147             throw new JspException(e);
148         }
149     }
150 
151     public static void doEndTag(PageContext pageContext) throws JspException {
152         try {
153             pageContext.getOut().print("</a>");
154         }
155         catch (Exception e) {
156             throw new JspException(e);
157         }
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(EncryptTagUtil.class);
161 
162 }