001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.Http;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.GroupConstants;
032    import com.liferay.portal.plugin.PluginPackageUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.util.PortalInstances;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
037    import com.liferay.util.servlet.ServletResponseUtil;
038    
039    import java.io.IOException;
040    
041    import java.util.Calendar;
042    import java.util.Date;
043    import java.util.Enumeration;
044    import java.util.Properties;
045    
046    import javax.servlet.ServletException;
047    import javax.servlet.http.HttpServlet;
048    import javax.servlet.http.HttpServletRequest;
049    import javax.servlet.http.HttpServletResponse;
050    
051    /**
052     * @author Jorge Ferrer
053     */
054    public class SoftwareCatalogServlet extends HttpServlet {
055    
056            public void service(
057                            HttpServletRequest request, HttpServletResponse response)
058                    throws IOException, ServletException {
059    
060                    try {
061                            long groupId = getGroupId(request);
062                            String version = getVersion(request);
063                            String baseImageURL = getBaseImageURL(request);
064                            Date oldestDate = getOldestDate(request);
065                            int maxNumOfVersions = ParamUtil.getInteger(
066                                    request, "maxNumOfVersions");
067                            Properties repoSettings = getRepoSettings(request);
068    
069                            if (_log.isDebugEnabled()) {
070                                    _log.debug("Group ID " + groupId);
071                                    _log.debug("Base image URL " + baseImageURL);
072                                    _log.debug("Oldtest date " + oldestDate);
073                                    _log.debug("Maximum number of versions " + maxNumOfVersions);
074                            }
075    
076                            String repositoryXML =
077                                    SCProductEntryLocalServiceUtil.getRepositoryXML(
078                                            groupId, version, baseImageURL, oldestDate,
079                                            maxNumOfVersions, repoSettings);
080    
081                            ServletResponseUtil.sendFile(
082                                    request, response, null,
083                                    repositoryXML.getBytes(StringPool.UTF8),
084                                    ContentTypes.TEXT_XML_UTF8);
085                    }
086                    catch (NoSuchGroupException nsge) {
087                            PortalUtil.sendError(
088                                    HttpServletResponse.SC_NOT_FOUND, nsge, request, response);
089                    }
090                    catch (Exception e) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn(e, e);
093                            }
094    
095                            PortalUtil.sendError(
096                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
097                                    response);
098                    }
099            }
100    
101            protected String getBaseImageURL(HttpServletRequest request) {
102                    String host = PortalUtil.getHost(request);
103    
104                    String portalURL = PortalUtil.getPortalURL(
105                            host, request.getServerPort(), request.isSecure());
106    
107                    String pathImage = PortalUtil.getPathImage();
108    
109                    if (pathImage.startsWith(Http.HTTP_WITH_SLASH) ||
110                            pathImage.startsWith(Http.HTTPS_WITH_SLASH)) {
111    
112                            return pathImage + "/software_catalog";
113                    }
114                    else {
115                            return portalURL + pathImage + "/software_catalog";
116                    }
117            }
118    
119            protected long getGroupId(HttpServletRequest request)
120                    throws SystemException, PortalException {
121    
122                    long groupId = ParamUtil.getLong(request, "groupId");
123    
124                    if (groupId <= 0) {
125                            String path = GetterUtil.getString(request.getPathInfo());
126    
127                            path = StringUtil.replace(
128                                    path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
129    
130                            if (Validator.isNotNull(path)) {
131                                    int pos = path.indexOf(StringPool.SLASH, 1);
132    
133                                    if (pos == -1) {
134                                            pos = path.length();
135                                    }
136    
137                                    groupId = GetterUtil.getLong(path.substring(1, pos));
138                            }
139                    }
140    
141                    if (groupId <= 0) {
142                            long companyId = PortalInstances.getCompanyId(request);
143    
144                            Group guestGroup = GroupLocalServiceUtil.getGroup(
145                                    companyId, GroupConstants.GUEST);
146    
147                            groupId = guestGroup.getGroupId();
148                    }
149    
150                    return groupId;
151            }
152    
153            protected Date getOldestDate(HttpServletRequest request) {
154                    Date oldestDate = null;
155    
156                    oldestDate = ParamUtil.getDate(
157                            request, "oldestDate",
158                            DateFormatFactoryUtil.getSimpleDateFormat("yyyy.MM.dd"), null);
159    
160                    if (oldestDate == null) {
161                            int daysOld = ParamUtil.getInteger(request, "maxAge", -1);
162    
163                            if (daysOld != -1) {
164                                    Calendar cal = Calendar.getInstance();
165    
166                                    cal.add(Calendar.DATE, (0 - daysOld));
167    
168                                    oldestDate = cal.getTime();
169                            }
170                    }
171    
172                    return oldestDate;
173            }
174    
175            protected Properties getRepoSettings(HttpServletRequest request) {
176                    Properties repoSettings = new Properties();
177    
178                    String prefix = "setting_";
179    
180                    Enumeration<String> enu = request.getParameterNames();
181    
182                    while (enu.hasMoreElements()) {
183                            String name = enu.nextElement();
184    
185                            if (name.startsWith(prefix)) {
186                                    String settingName = name.substring(
187                                            prefix.length(), name.length());
188    
189                                    String value = ParamUtil.getString(request, name);
190    
191                                    if (Validator.isNotNull(value)) {
192                                            repoSettings.setProperty(settingName , value);
193                                    }
194                            }
195                    }
196    
197                    return repoSettings;
198            }
199    
200            protected String getVersion(HttpServletRequest request) {
201                    String version = ParamUtil.getString(request, "version");
202    
203                    String prefix =
204                            PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
205                    String extension =
206                            StringPool.PERIOD +
207                                    PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
208    
209                    if (Validator.isNull(version)) {
210                            String path = GetterUtil.getString(request.getPathInfo());
211    
212                            if (Validator.isNotNull(path)) {
213                                    int x = path.indexOf(prefix);
214    
215                                    if (x != -1) {
216                                            version = path.substring(
217                                                    x + prefix.length(), path.indexOf(extension, x));
218                                    }
219                            }
220                    }
221    
222                    if (_log.isDebugEnabled()) {
223                            if (Validator.isNull(version)) {
224                                    _log.debug("Serving repository for all versions");
225                            }
226                            else {
227                                    _log.debug("Serving repository for version " + version);
228                            }
229                    }
230    
231                    return version;
232            }
233    
234            private static Log _log = LogFactoryUtil.getLog(
235                    SoftwareCatalogServlet.class);
236    
237    }