001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.CharPool;
023    import com.liferay.portal.kernel.util.ContentTypes;
024    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.Http;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.GroupConstants;
033    import com.liferay.portal.plugin.PluginPackageUtil;
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    import com.liferay.util.servlet.ServletResponseUtil;
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            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 host = PortalUtil.getHost(request);
104    
105                    String portalURL = PortalUtil.getPortalURL(
106                            host, request.getServerPort(), request.isSecure());
107    
108                    String pathImage = PortalUtil.getPathImage();
109    
110                    if (pathImage.startsWith(Http.HTTP_WITH_SLASH) ||
111                            pathImage.startsWith(Http.HTTPS_WITH_SLASH)) {
112    
113                            return pathImage + "/software_catalog";
114                    }
115                    else {
116                            return portalURL + pathImage + "/software_catalog";
117                    }
118            }
119    
120            protected long getGroupId(HttpServletRequest request)
121                    throws SystemException, PortalException {
122    
123                    long groupId = ParamUtil.getLong(request, "groupId");
124    
125                    if (groupId <= 0) {
126                            String path = GetterUtil.getString(request.getPathInfo());
127    
128                            path = StringUtil.replace(
129                                    path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
130    
131                            if (Validator.isNotNull(path)) {
132                                    int pos = path.indexOf(CharPool.SLASH, 1);
133    
134                                    if (pos == -1) {
135                                            pos = path.length();
136                                    }
137    
138                                    groupId = GetterUtil.getLong(path.substring(1, pos));
139                            }
140                    }
141    
142                    if (groupId <= 0) {
143                            long companyId = PortalInstances.getCompanyId(request);
144    
145                            Group guestGroup = GroupLocalServiceUtil.getGroup(
146                                    companyId, GroupConstants.GUEST);
147    
148                            groupId = guestGroup.getGroupId();
149                    }
150    
151                    return groupId;
152            }
153    
154            protected Date getOldestDate(HttpServletRequest request) {
155                    Date oldestDate = null;
156    
157                    oldestDate = ParamUtil.getDate(
158                            request, "oldestDate",
159                            DateFormatFactoryUtil.getSimpleDateFormat("yyyy.MM.dd"), null);
160    
161                    if (oldestDate == null) {
162                            int daysOld = ParamUtil.getInteger(request, "maxAge", -1);
163    
164                            if (daysOld != -1) {
165                                    Calendar cal = Calendar.getInstance();
166    
167                                    cal.add(Calendar.DATE, (0 - daysOld));
168    
169                                    oldestDate = cal.getTime();
170                            }
171                    }
172    
173                    return oldestDate;
174            }
175    
176            protected Properties getRepoSettings(HttpServletRequest request) {
177                    Properties repoSettings = new Properties();
178    
179                    String prefix = "setting_";
180    
181                    Enumeration<String> enu = request.getParameterNames();
182    
183                    while (enu.hasMoreElements()) {
184                            String name = enu.nextElement();
185    
186                            if (name.startsWith(prefix)) {
187                                    String settingName = name.substring(
188                                            prefix.length(), name.length());
189    
190                                    String value = ParamUtil.getString(request, name);
191    
192                                    if (Validator.isNotNull(value)) {
193                                            repoSettings.setProperty(settingName , value);
194                                    }
195                            }
196                    }
197    
198                    return repoSettings;
199            }
200    
201            protected String getVersion(HttpServletRequest request) {
202                    String version = ParamUtil.getString(request, "version");
203    
204                    String prefix =
205                            PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
206                    String extension =
207                            StringPool.PERIOD +
208                                    PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
209    
210                    if (Validator.isNull(version)) {
211                            String path = GetterUtil.getString(request.getPathInfo());
212    
213                            if (Validator.isNotNull(path)) {
214                                    int x = path.indexOf(prefix);
215    
216                                    if (x != -1) {
217                                            version = path.substring(
218                                                    x + prefix.length(), path.indexOf(extension, x));
219                                    }
220                            }
221                    }
222    
223                    if (_log.isDebugEnabled()) {
224                            if (Validator.isNull(version)) {
225                                    _log.debug("Serving repository for all versions");
226                            }
227                            else {
228                                    _log.debug("Serving repository for version " + version);
229                            }
230                    }
231    
232                    return version;
233            }
234    
235            private static Log _log = LogFactoryUtil.getLog(
236                    SoftwareCatalogServlet.class);
237    
238    }