1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.Http;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.util.TimeZoneUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.User;
30  
31  import java.io.IOException;
32  
33  import java.util.Iterator;
34  import java.util.LinkedHashMap;
35  import java.util.Map;
36  
37  /**
38   * <a href="JBIRequestURL.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
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         Http.Options options = new Http.Options();
111 
112         options.setLocation(getURL());
113         options.setParts(_params);
114         options.setPost(true);
115 
116         return HttpUtil.URLtoString(options);
117     }
118 
119     protected abstract String getURL();
120 
121     private User _user;
122     private Map<String, String> _params;
123 
124 }