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