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