1   /**
2    * Copyright (c) 2000-2007 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.TimeZoneUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.User;
28  import com.liferay.util.Http;
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();
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 parameterMap) {
64          Iterator itr = parameterMap.entrySet().iterator();
65  
66          while (itr.hasNext()) {
67              Map.Entry entry = (Map.Entry)itr.next();
68  
69              String key = (String)entry.getKey();
70              String[] value = (String[])entry.getValue();
71  
72              if ((Validator.isNotNull(key)) && (value != null) &&
73                  (value.length > 0) && (Validator.isNotNull(value[0]))) {
74  
75                  _params.put(key, value[0]);
76              }
77          }
78      }
79  
80      public void setParameter(String name, boolean value) {
81          setParameter(name, String.valueOf(value));
82      }
83  
84      public void setParameter(String name, double value) {
85          setParameter(name, String.valueOf(value));
86      }
87  
88      public void setParameter(String name, float value) {
89          setParameter(name, String.valueOf(value));
90      }
91  
92      public void setParameter(String name, int value) {
93          setParameter(name, String.valueOf(value));
94      }
95  
96      public void setParameter(String name, long value) {
97          setParameter(name, String.valueOf(value));
98      }
99  
100     public void setParameter(String name, short value) {
101         setParameter(name, String.valueOf(value));
102     }
103 
104     public void setParameter(String name, String value) {
105         _params.put(name, value);
106     }
107 
108     public String getContent() throws IOException {
109         return Http.URLtoString(getURL(), null, _params, true);
110     }
111 
112     protected abstract String getURL();
113 
114     private User _user;
115     private Map _params;
116 
117 }