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