001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.servlet;
022:
023: import com.liferay.portal.NoSuchGroupException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.ContentTypes;
027: import com.liferay.portal.kernel.util.GetterUtil;
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.impl.GroupImpl;
034: import com.liferay.portal.plugin.PluginPackageUtil;
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: import com.liferay.util.Http;
040: import com.liferay.util.servlet.ServletResponseUtil;
041:
042: import java.io.IOException;
043:
044: import java.text.SimpleDateFormat;
045:
046: import java.util.Calendar;
047: import java.util.Date;
048: import java.util.Enumeration;
049: import java.util.Properties;
050:
051: import javax.servlet.ServletException;
052: import javax.servlet.http.HttpServlet;
053: import javax.servlet.http.HttpServletRequest;
054: import javax.servlet.http.HttpServletResponse;
055:
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058:
059: /**
060: * <a href="SoftwareCatalogServlet.java.html"><b><i>View Source</i></b></a>
061: *
062: * @author Jorge Ferrer
063: *
064: */
065: public class SoftwareCatalogServlet extends HttpServlet {
066:
067: public void service(HttpServletRequest req, HttpServletResponse res)
068: throws IOException, ServletException {
069:
070: try {
071: long groupId = getGroupId(req);
072: String version = getVersion(req);
073: String baseImageURL = getBaseImageURL(req);
074: Date oldestDate = getOldestDate(req);
075: int maxNumOfVersions = ParamUtil.getInteger(req,
076: "maxNumOfVersions");
077: Properties repoSettings = getRepoSettings(req);
078:
079: if (_log.isDebugEnabled()) {
080: _log.debug("Group ID " + groupId);
081: _log.debug("Base image URL " + baseImageURL);
082: _log.debug("Oldtest date " + oldestDate);
083: _log.debug("Maximum number of versions "
084: + maxNumOfVersions);
085: }
086:
087: String repositoryXML = SCProductEntryLocalServiceUtil
088: .getRepositoryXML(groupId, version, baseImageURL,
089: oldestDate, maxNumOfVersions, repoSettings);
090:
091: String fileName = null;
092: byte[] byteArray = repositoryXML.getBytes("UTF-8");
093:
094: ServletResponseUtil.sendFile(res, fileName, byteArray,
095: ContentTypes.TEXT_XML_UTF8);
096: } catch (NoSuchGroupException nsge) {
097: PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND,
098: nsge, req, res);
099: } catch (Exception e) {
100: if (_log.isWarnEnabled()) {
101: _log.warn(e, e);
102: }
103:
104: PortalUtil.sendError(
105: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e,
106: req, res);
107: }
108: }
109:
110: protected String getBaseImageURL(HttpServletRequest req) {
111: String host = PortalUtil.getHost(req);
112:
113: String portalURL = PortalUtil.getPortalURL(host, req
114: .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: } else {
123: return portalURL + pathImage + "/software_catalog";
124: }
125: }
126:
127: protected long getGroupId(HttpServletRequest req)
128: throws SystemException, PortalException {
129:
130: long groupId = ParamUtil.getLong(req, "groupId");
131:
132: if (groupId <= 0) {
133: String path = GetterUtil.getString(req.getPathInfo());
134:
135: path = StringUtil.replace(path, StringPool.DOUBLE_SLASH,
136: StringPool.SLASH);
137:
138: if (Validator.isNotNull(path)) {
139: int pos = path.indexOf(StringPool.SLASH, 1);
140:
141: if (pos == -1) {
142: pos = path.length();
143: }
144:
145: groupId = GetterUtil.getLong(path.substring(1, pos));
146: }
147: }
148:
149: if (groupId <= 0) {
150: long companyId = PortalInstances.getCompanyId(req);
151:
152: Group guestGroup = GroupLocalServiceUtil.getGroup(
153: companyId, GroupImpl.GUEST);
154:
155: groupId = guestGroup.getGroupId();
156: }
157:
158: return groupId;
159: }
160:
161: protected Date getOldestDate(HttpServletRequest req) {
162: Date oldestDate = null;
163:
164: oldestDate = ParamUtil.getDate(req, "oldestDate",
165: new SimpleDateFormat("yyyy.MM.dd"), null);
166:
167: if (oldestDate == null) {
168: int daysOld = ParamUtil.getInteger(req, "maxAge", -1);
169:
170: if (daysOld != -1) {
171: Calendar cal = Calendar.getInstance();
172:
173: cal.add(Calendar.DATE, (0 - daysOld));
174:
175: oldestDate = cal.getTime();
176: }
177: }
178:
179: return oldestDate;
180: }
181:
182: protected Properties getRepoSettings(HttpServletRequest req) {
183: Properties repoSettings = new Properties();
184:
185: String prefix = "setting_";
186:
187: Enumeration enu = req.getParameterNames();
188:
189: while (enu.hasMoreElements()) {
190: String name = (String) enu.nextElement();
191:
192: if (name.startsWith(prefix)) {
193: String settingName = name.substring(prefix.length(),
194: name.length());
195:
196: String value = ParamUtil.getString(req, name);
197:
198: if (Validator.isNotNull(value)) {
199: repoSettings.setProperty(settingName, value);
200: }
201: }
202: }
203:
204: return repoSettings;
205: }
206:
207: protected String getVersion(HttpServletRequest req)
208: throws PortalException, SystemException {
209:
210: String version = ParamUtil.getString(req, "version");
211:
212: String prefix = PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX
213: + StringPool.DASH;
214: String extension = StringPool.PERIOD
215: + PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
216:
217: if (Validator.isNull(version)) {
218: String path = GetterUtil.getString(req.getPathInfo());
219:
220: if (Validator.isNotNull(path)) {
221: int x = path.indexOf(prefix);
222:
223: if (x != -1) {
224: version = path.substring(x + prefix.length(), path
225: .indexOf(extension, x));
226: }
227: }
228: }
229:
230: if (_log.isDebugEnabled()) {
231: if (Validator.isNull(version)) {
232: _log.debug("Serving repository for all versions");
233: } else {
234: _log.debug("Serving repository for version " + version);
235: }
236: }
237:
238: return version;
239: }
240:
241: private static Log _log = LogFactory
242: .getLog(SoftwareCatalogServlet.class);
243:
244: }
|