001    /**
002     * Copyright (c) 2000-present 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.plugin.PluginPackage;
022    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023    import com.liferay.portal.kernel.util.CharPool;
024    import com.liferay.portal.kernel.util.ContentTypes;
025    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.Http;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.model.GroupConstants;
034    import com.liferay.portal.service.GroupLocalServiceUtil;
035    import com.liferay.portal.util.PortalInstances;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
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            @Override
057            public void service(
058                            HttpServletRequest request, HttpServletResponse response)
059                    throws IOException, ServletException {
060    
061                    try {
062                            long groupId = getGroupId(request);
063                            String version = getVersion(request);
064                            String baseImageURL = getBaseImageURL(request);
065                            Date oldestDate = getOldestDate(request);
066                            int maxNumOfVersions = ParamUtil.getInteger(
067                                    request, "maxNumOfVersions");
068                            Properties repoSettings = getRepoSettings(request);
069    
070                            if (_log.isDebugEnabled()) {
071                                    _log.debug("Group ID " + groupId);
072                                    _log.debug("Base image URL " + baseImageURL);
073                                    _log.debug("Oldtest date " + oldestDate);
074                                    _log.debug("Maximum number of versions " + maxNumOfVersions);
075                            }
076    
077                            String repositoryXML =
078                                    SCProductEntryLocalServiceUtil.getRepositoryXML(
079                                            groupId, version, baseImageURL, oldestDate,
080                                            maxNumOfVersions, repoSettings);
081    
082                            ServletResponseUtil.sendFile(
083                                    request, response, null,
084                                    repositoryXML.getBytes(StringPool.UTF8),
085                                    ContentTypes.TEXT_XML_UTF8);
086                    }
087                    catch (NoSuchGroupException nsge) {
088                            PortalUtil.sendError(
089                                    HttpServletResponse.SC_NOT_FOUND, nsge, request, response);
090                    }
091                    catch (Exception e) {
092                            if (_log.isWarnEnabled()) {
093                                    _log.warn(e, e);
094                            }
095    
096                            PortalUtil.sendError(
097                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
098                                    response);
099                    }
100            }
101    
102            protected String getBaseImageURL(HttpServletRequest request) {
103                    String portalURL = PortalUtil.getPortalURL(request);
104    
105                    String pathImage = PortalUtil.getPathImage();
106    
107                    if (pathImage.startsWith(Http.HTTP_WITH_SLASH) ||
108                            pathImage.startsWith(Http.HTTPS_WITH_SLASH)) {
109    
110                            return pathImage + "/software_catalog";
111                    }
112                    else {
113                            return portalURL + pathImage + "/software_catalog";
114                    }
115            }
116    
117            protected long getGroupId(HttpServletRequest request)
118                    throws PortalException {
119    
120                    long groupId = ParamUtil.getLong(request, "groupId");
121    
122                    if (groupId <= 0) {
123                            String path = GetterUtil.getString(request.getPathInfo());
124    
125                            path = StringUtil.replace(
126                                    path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
127    
128                            if (Validator.isNotNull(path)) {
129                                    int pos = path.indexOf(CharPool.SLASH, 1);
130    
131                                    if (pos == -1) {
132                                            pos = path.length();
133                                    }
134    
135                                    groupId = GetterUtil.getLong(path.substring(1, pos));
136                            }
137                    }
138    
139                    if (groupId <= 0) {
140                            long companyId = PortalInstances.getCompanyId(request);
141    
142                            Group guestGroup = GroupLocalServiceUtil.getGroup(
143                                    companyId, GroupConstants.GUEST);
144    
145                            groupId = guestGroup.getGroupId();
146                    }
147    
148                    return groupId;
149            }
150    
151            protected Date getOldestDate(HttpServletRequest request) {
152                    Date oldestDate = null;
153    
154                    oldestDate = ParamUtil.getDate(
155                            request, "oldestDate",
156                            DateFormatFactoryUtil.getSimpleDateFormat("yyyy.MM.dd"), null);
157    
158                    if (oldestDate == null) {
159                            int daysOld = ParamUtil.getInteger(request, "maxAge", -1);
160    
161                            if (daysOld != -1) {
162                                    Calendar cal = Calendar.getInstance();
163    
164                                    cal.add(Calendar.DATE, (0 - daysOld));
165    
166                                    oldestDate = cal.getTime();
167                            }
168                    }
169    
170                    return oldestDate;
171            }
172    
173            protected Properties getRepoSettings(HttpServletRequest request) {
174                    Properties repoSettings = new Properties();
175    
176                    String prefix = "setting_";
177    
178                    Enumeration<String> enu = request.getParameterNames();
179    
180                    while (enu.hasMoreElements()) {
181                            String name = enu.nextElement();
182    
183                            if (name.startsWith(prefix)) {
184                                    String settingName = name.substring(prefix.length());
185    
186                                    String value = ParamUtil.getString(request, name);
187    
188                                    if (Validator.isNotNull(value)) {
189                                            repoSettings.setProperty(settingName, value);
190                                    }
191                            }
192                    }
193    
194                    return repoSettings;
195            }
196    
197            protected String getVersion(HttpServletRequest request) {
198                    String version = ParamUtil.getString(request, "version");
199    
200                    String prefix =
201                            PluginPackage.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
202                    String extension =
203                            StringPool.PERIOD + PluginPackage.REPOSITORY_XML_FILENAME_EXTENSION;
204    
205                    if (Validator.isNull(version)) {
206                            String path = GetterUtil.getString(request.getPathInfo());
207    
208                            if (Validator.isNotNull(path)) {
209                                    int x = path.indexOf(prefix);
210    
211                                    if (x != -1) {
212                                            version = path.substring(
213                                                    x + prefix.length(), path.indexOf(extension, x));
214                                    }
215                            }
216                    }
217    
218                    if (_log.isDebugEnabled()) {
219                            if (Validator.isNull(version)) {
220                                    _log.debug("Serving repository for all versions");
221                            }
222                            else {
223                                    _log.debug("Serving repository for version " + version);
224                            }
225                    }
226    
227                    return version;
228            }
229    
230            private static final Log _log = LogFactoryUtil.getLog(
231                    SoftwareCatalogServlet.class);
232    
233    }