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.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   */
43  public abstract class JBIRequestURL {
44  
45      public JBIRequestURL() {
46          this(null);
47      }
48  
49      public JBIRequestURL(User user) {
50          _params = new LinkedHashMap<String, String>();
51  
52          if (user != null) {
53              _user = user;
54  
55              _params.put("userId", String.valueOf(_user.getUserId()));
56              _params.put("timeZoneId", _user.getTimeZone().getID());
57          }
58          else {
59              _params.put("userId", "0");
60              _params.put("timeZoneId", TimeZoneUtil.getDefault().getID());
61          }
62      }
63  
64      public void addParameterMap(Map<String, String[]> parameterMap) {
65          Iterator<Map.Entry<String, String[]>> itr =
66              parameterMap.entrySet().iterator();
67  
68          while (itr.hasNext()) {
69              Map.Entry<String, String[]> entry = itr.next();
70  
71              String key = entry.getKey();
72              String[] value = entry.getValue();
73  
74              if ((Validator.isNotNull(key)) && (value != null) &&
75                  (value.length > 0) && (Validator.isNotNull(value[0]))) {
76  
77                  _params.put(key, value[0]);
78              }
79          }
80      }
81  
82      public void setParameter(String name, boolean value) {
83          setParameter(name, String.valueOf(value));
84      }
85  
86      public void setParameter(String name, double value) {
87          setParameter(name, String.valueOf(value));
88      }
89  
90      public void setParameter(String name, float value) {
91          setParameter(name, String.valueOf(value));
92      }
93  
94      public void setParameter(String name, int value) {
95          setParameter(name, String.valueOf(value));
96      }
97  
98      public void setParameter(String name, long value) {
99          setParameter(name, String.valueOf(value));
100     }
101 
102     public void setParameter(String name, short value) {
103         setParameter(name, String.valueOf(value));
104     }
105 
106     public void setParameter(String name, String value) {
107         _params.put(name, value);
108     }
109 
110     public String getContent() throws IOException {
111         Http.Options options = new Http.Options();
112 
113         options.setLocation(getURL());
114         options.setParts(_params);
115         options.setPost(true);
116 
117         return HttpUtil.URLtoString(options);
118     }
119 
120     protected abstract String getURL();
121 
122     private User _user;
123     private Map<String, String> _params;
124 
125 }