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