1   /**
2    * Copyright (c) 2000-2007 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.util.ContentTypes;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.model.impl.GroupImpl;
36  import com.liferay.portal.service.GroupLocalServiceUtil;
37  import com.liferay.portal.util.PortalInstances;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
40  import com.liferay.util.Http;
41  import com.liferay.util.servlet.ServletResponseUtil;
42  
43  import java.io.IOException;
44  
45  import java.text.SimpleDateFormat;
46  
47  import java.util.Calendar;
48  import java.util.Date;
49  import java.util.Enumeration;
50  import java.util.Properties;
51  
52  import javax.servlet.ServletException;
53  import javax.servlet.http.HttpServlet;
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpServletResponse;
56  
57  import org.apache.commons.logging.Log;
58  import org.apache.commons.logging.LogFactory;
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(HttpServletRequest req, HttpServletResponse res)
69          throws IOException, ServletException {
70  
71          try {
72              long groupId = getGroupId(req);
73              String baseImageURL = getBaseImageURL(req);
74              Date oldestDate = getOldestDate(req);
75              int maxNumOfVersions = ParamUtil.getInteger(
76                  req, "maxNumOfVersions");
77              Properties repoSettings = getRepoSettings(req);
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, baseImageURL, oldestDate, maxNumOfVersions,
89                      repoSettings);
90  
91              String fileName = null;
92              byte[] byteArray = repositoryXML.getBytes("UTF-8");
93  
94              ServletResponseUtil.sendFile(
95                  res, fileName, byteArray, ContentTypes.TEXT_XML_UTF8);
96          }
97          catch (NoSuchGroupException nsge) {
98              res.sendError(HttpServletResponse.SC_NOT_FOUND);
99          }
100         catch (Exception e) {
101             if (_log.isWarnEnabled()) {
102                 _log.warn(e, e);
103             }
104 
105             res.sendError(
106                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
107         }
108     }
109 
110     protected String getBaseImageURL(HttpServletRequest req) {
111         String host = PortalUtil.getHost(req);
112 
113         String portalURL = PortalUtil.getPortalURL(
114             host, req.getServerPort(), req.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 req)
129         throws SystemException, PortalException {
130 
131         long groupId = ParamUtil.getLong(req, "groupId");
132 
133         if (groupId <= 0) {
134             String path = GetterUtil.getString(req.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(req);
152 
153             Group guestGroup = GroupLocalServiceUtil.getGroup(
154                 companyId, GroupImpl.GUEST);
155 
156             groupId = guestGroup.getGroupId();
157         }
158 
159         return groupId;
160     }
161 
162     protected Date getOldestDate(HttpServletRequest req) {
163         Date oldestDate = null;
164 
165         oldestDate = ParamUtil.getDate(
166             req, "oldestDate", new SimpleDateFormat("yyyy.MM.dd"), null);
167 
168         if (oldestDate == null) {
169             int daysOld = ParamUtil.getInteger(req, "maxAge", -1);
170 
171             if (daysOld != -1) {
172                 Calendar cal = Calendar.getInstance();
173 
174                 cal.add(Calendar.DATE, (0 - daysOld));
175 
176                 oldestDate = cal.getTime();
177             }
178         }
179 
180         return oldestDate;
181     }
182 
183     protected Properties getRepoSettings(HttpServletRequest req) {
184         Properties repoSettings = new Properties();
185 
186         String prefix = "setting_";
187 
188         Enumeration enu = req.getParameterNames();
189 
190         while (enu.hasMoreElements()) {
191             String name = (String)enu.nextElement();
192 
193             if (name.startsWith(prefix)) {
194                 String settingName = name.substring(
195                     prefix.length(), name.length());
196 
197                 String value = ParamUtil.getString(req, name);
198 
199                 if (Validator.isNotNull(value)) {
200                     repoSettings.setProperty(settingName , value);
201                 }
202             }
203         }
204 
205         return repoSettings;
206     }
207 
208     private static Log _log = LogFactory.getLog(SoftwareCatalogServlet.class);
209 
210 }