1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.jbi;
21  
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.TimeZoneUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.User;
26  
27  import java.io.IOException;
28  
29  import java.util.Iterator;
30  import java.util.LinkedHashMap;
31  import java.util.Map;
32  
33  /**
34   * <a href="JBIRequestURL.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public abstract class JBIRequestURL {
40  
41      public JBIRequestURL() {
42          this(null);
43      }
44  
45      public JBIRequestURL(User user) {
46          _params = new LinkedHashMap<String, String>();
47  
48          if (user != null) {
49              _user = user;
50  
51              _params.put("userId", String.valueOf(_user.getUserId()));
52              _params.put("timeZoneId", _user.getTimeZone().getID());
53          }
54          else {
55              _params.put("userId", "0");
56              _params.put("timeZoneId", TimeZoneUtil.getDefault().getID());
57          }
58      }
59  
60      public void addParameterMap(Map<String, String[]> parameterMap) {
61          Iterator<Map.Entry<String, String[]>> itr =
62              parameterMap.entrySet().iterator();
63  
64          while (itr.hasNext()) {
65              Map.Entry<String, String[]> entry = itr.next();
66  
67              String key = entry.getKey();
68              String[] value = entry.getValue();
69  
70              if ((Validator.isNotNull(key)) && (value != null) &&
71                  (value.length > 0) && (Validator.isNotNull(value[0]))) {
72  
73                  _params.put(key, value[0]);
74              }
75          }
76      }
77  
78      public void setParameter(String name, boolean value) {
79          setParameter(name, String.valueOf(value));
80      }
81  
82      public void setParameter(String name, double value) {
83          setParameter(name, String.valueOf(value));
84      }
85  
86      public void setParameter(String name, float value) {
87          setParameter(name, String.valueOf(value));
88      }
89  
90      public void setParameter(String name, int value) {
91          setParameter(name, String.valueOf(value));
92      }
93  
94      public void setParameter(String name, long value) {
95          setParameter(name, String.valueOf(value));
96      }
97  
98      public void setParameter(String name, short value) {
99          setParameter(name, String.valueOf(value));
100     }
101 
102     public void setParameter(String name, String value) {
103         _params.put(name, value);
104     }
105 
106     public String getContent() throws IOException {
107         return HttpUtil.URLtoString(getURL(), null, null, _params, true);
108     }
109 
110     protected abstract String getURL();
111 
112     private User _user;
113     private Map<String, String> _params;
114 
115 }