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.servlet;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.ContentTypes;
31  import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.Http;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.model.Group;
39  import com.liferay.portal.model.GroupConstants;
40  import com.liferay.portal.plugin.PluginPackageUtil;
41  import com.liferay.portal.service.GroupLocalServiceUtil;
42  import com.liferay.portal.util.PortalInstances;
43  import com.liferay.portal.util.PortalUtil;
44  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
45  import com.liferay.util.servlet.ServletResponseUtil;
46  
47  import java.io.IOException;
48  
49  import java.util.Calendar;
50  import java.util.Date;
51  import java.util.Enumeration;
52  import java.util.Properties;
53  
54  import javax.servlet.ServletException;
55  import javax.servlet.http.HttpServlet;
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  /**
60   * <a href="SoftwareCatalogServlet.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Jorge Ferrer
63   */
64  public class SoftwareCatalogServlet extends HttpServlet {
65  
66      public void service(
67              HttpServletRequest request, HttpServletResponse response)
68          throws IOException, ServletException {
69  
70          try {
71              long groupId = getGroupId(request);
72              String version = getVersion(request);
73              String baseImageURL = getBaseImageURL(request);
74              Date oldestDate = getOldestDate(request);
75              int maxNumOfVersions = ParamUtil.getInteger(
76                  request, "maxNumOfVersions");
77              Properties repoSettings = getRepoSettings(request);
78  
79              if (_log.isDebugEnabled()) {
80                  _log.debug("Group ID " + groupId);
81                  _log.debug("Base image URL " + baseImageURL);
82                  _log.debug("Oldtest date " + oldestDate);
83                  _log.debug("Maximum number of versions " + maxNumOfVersions);
84              }
85  
86              String repositoryXML =
87                  SCProductEntryLocalServiceUtil.getRepositoryXML(
88                      groupId, version, baseImageURL, oldestDate,
89                      maxNumOfVersions, repoSettings);
90  
91              ServletResponseUtil.sendFile(
92                  response, null, repositoryXML.getBytes(StringPool.UTF8),
93                  ContentTypes.TEXT_XML_UTF8);
94          }
95          catch (NoSuchGroupException nsge) {
96              PortalUtil.sendError(
97                  HttpServletResponse.SC_NOT_FOUND, nsge, request, response);
98          }
99          catch (Exception e) {
100             if (_log.isWarnEnabled()) {
101                 _log.warn(e, e);
102             }
103 
104             PortalUtil.sendError(
105                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
106                 response);
107         }
108     }
109 
110     protected String getBaseImageURL(HttpServletRequest request) {
111         String host = PortalUtil.getHost(request);
112 
113         String portalURL = PortalUtil.getPortalURL(
114             host, request.getServerPort(), request.isSecure());
115 
116         String pathImage = PortalUtil.getPathImage();
117 
118         if (pathImage.startsWith(Http.HTTP_WITH_SLASH) ||
119             pathImage.startsWith(Http.HTTPS_WITH_SLASH)) {
120 
121             return pathImage + "/software_catalog";
122         }
123         else {
124             return portalURL + pathImage + "/software_catalog";
125         }
126     }
127 
128     protected long getGroupId(HttpServletRequest request)
129         throws SystemException, PortalException {
130 
131         long groupId = ParamUtil.getLong(request, "groupId");
132 
133         if (groupId <= 0) {
134             String path = GetterUtil.getString(request.getPathInfo());
135 
136             path = StringUtil.replace(
137                 path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
138 
139             if (Validator.isNotNull(path)) {
140                 int pos = path.indexOf(StringPool.SLASH, 1);
141 
142                 if (pos == -1) {
143                     pos = path.length();
144                 }
145 
146                 groupId = GetterUtil.getLong(path.substring(1, pos));
147             }
148         }
149 
150         if (groupId <= 0) {
151             long companyId = PortalInstances.getCompanyId(request);
152 
153             Group guestGroup = GroupLocalServiceUtil.getGroup(
154                 companyId, GroupConstants.GUEST);
155 
156             groupId = guestGroup.getGroupId();
157         }
158 
159         return groupId;
160     }
161 
162     protected Date getOldestDate(HttpServletRequest request) {
163         Date oldestDate = null;
164 
165         oldestDate = ParamUtil.getDate(
166             request, "oldestDate",
167             DateFormatFactoryUtil.getSimpleDateFormat("yyyy.MM.dd"), null);
168 
169         if (oldestDate == null) {
170             int daysOld = ParamUtil.getInteger(request, "maxAge", -1);
171 
172             if (daysOld != -1) {
173                 Calendar cal = Calendar.getInstance();
174 
175                 cal.add(Calendar.DATE, (0 - daysOld));
176 
177                 oldestDate = cal.getTime();
178             }
179         }
180 
181         return oldestDate;
182     }
183 
184     protected Properties getRepoSettings(HttpServletRequest request) {
185         Properties repoSettings = new Properties();
186 
187         String prefix = "setting_";
188 
189         Enumeration<String> enu = request.getParameterNames();
190 
191         while (enu.hasMoreElements()) {
192             String name = enu.nextElement();
193 
194             if (name.startsWith(prefix)) {
195                 String settingName = name.substring(
196                     prefix.length(), name.length());
197 
198                 String value = ParamUtil.getString(request, name);
199 
200                 if (Validator.isNotNull(value)) {
201                     repoSettings.setProperty(settingName , value);
202                 }
203             }
204         }
205 
206         return repoSettings;
207     }
208 
209     protected String getVersion(HttpServletRequest request) {
210         String version = ParamUtil.getString(request, "version");
211 
212         String prefix =
213             PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
214         String extension =
215             StringPool.PERIOD +
216                 PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
217 
218         if (Validator.isNull(version)) {
219             String path = GetterUtil.getString(request.getPathInfo());
220 
221             if (Validator.isNotNull(path)) {
222                 int x = path.indexOf(prefix);
223 
224                 if (x != -1) {
225                     version = path.substring(
226                         x + prefix.length(), path.indexOf(extension, x));
227                 }
228             }
229         }
230 
231         if (_log.isDebugEnabled()) {
232             if (Validator.isNull(version)) {
233                 _log.debug("Serving repository for all versions");
234             }
235             else {
236                 _log.debug("Serving repository for version " + version);
237             }
238         }
239 
240         return version;
241     }
242 
243     private static Log _log =
244         LogFactoryUtil.getLog(SoftwareCatalogServlet.class);
245 
246 }