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