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.jbi;
24  
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.TimeZoneUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.User;
29  
30  import java.io.IOException;
31  
32  import java.util.Iterator;
33  import java.util.LinkedHashMap;
34  import java.util.Map;
35  
36  /**
37   * <a href="JBIRequestURL.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public abstract class JBIRequestURL {
43  
44      public JBIRequestURL() {
45          this(null);
46      }
47  
48      public JBIRequestURL(User user) {
49          _params = new LinkedHashMap<String, String>();
50  
51          if (user != null) {
52              _user = user;
53  
54              _params.put("userId", String.valueOf(_user.getUserId()));
55              _params.put("timeZoneId", _user.getTimeZone().getID());
56          }
57          else {
58              _params.put("userId", "0");
59              _params.put("timeZoneId", TimeZoneUtil.getDefault().getID());
60          }
61      }
62  
63      public void addParameterMap(Map<String, String[]> parameterMap) {
64          Iterator<Map.Entry<String, String[]>> itr =
65              parameterMap.entrySet().iterator();
66  
67          while (itr.hasNext()) {
68              Map.Entry<String, String[]> entry = itr.next();
69  
70              String key = entry.getKey();
71              String[] value = entry.getValue();
72  
73              if ((Validator.isNotNull(key)) && (value != null) &&
74                  (value.length > 0) && (Validator.isNotNull(value[0]))) {
75  
76                  _params.put(key, value[0]);
77              }
78          }
79      }
80  
81      public void setParameter(String name, boolean value) {
82          setParameter(name, String.valueOf(value));
83      }
84  
85      public void setParameter(String name, double value) {
86          setParameter(name, String.valueOf(value));
87      }
88  
89      public void setParameter(String name, float value) {
90          setParameter(name, String.valueOf(value));
91      }
92  
93      public void setParameter(String name, int value) {
94          setParameter(name, String.valueOf(value));
95      }
96  
97      public void setParameter(String name, long value) {
98          setParameter(name, String.valueOf(value));
99      }
100 
101     public void setParameter(String name, short value) {
102         setParameter(name, String.valueOf(value));
103     }
104 
105     public void setParameter(String name, String value) {
106         _params.put(name, value);
107     }
108 
109     public String getContent() throws IOException {
110         return HttpUtil.URLtoString(getURL(), null, _params, true);
111     }
112 
113     protected abstract String getURL();
114 
115     private User _user;
116     private Map<String, String> _params;
117 
118 }