001
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.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.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
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
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 host = PortalUtil.getHost(request);
105
106 String portalURL = PortalUtil.getPortalURL(
107 host, request.getServerPort(), request.isSecure());
108
109 String pathImage = PortalUtil.getPathImage();
110
111 if (pathImage.startsWith(Http.HTTP_WITH_SLASH) ||
112 pathImage.startsWith(Http.HTTPS_WITH_SLASH)) {
113
114 return pathImage + "/software_catalog";
115 }
116 else {
117 return portalURL + pathImage + "/software_catalog";
118 }
119 }
120
121 protected long getGroupId(HttpServletRequest request)
122 throws PortalException, SystemException {
123
124 long groupId = ParamUtil.getLong(request, "groupId");
125
126 if (groupId <= 0) {
127 String path = GetterUtil.getString(request.getPathInfo());
128
129 path = StringUtil.replace(
130 path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
131
132 if (Validator.isNotNull(path)) {
133 int pos = path.indexOf(CharPool.SLASH, 1);
134
135 if (pos == -1) {
136 pos = path.length();
137 }
138
139 groupId = GetterUtil.getLong(path.substring(1, pos));
140 }
141 }
142
143 if (groupId <= 0) {
144 long companyId = PortalInstances.getCompanyId(request);
145
146 Group guestGroup = GroupLocalServiceUtil.getGroup(
147 companyId, GroupConstants.GUEST);
148
149 groupId = guestGroup.getGroupId();
150 }
151
152 return groupId;
153 }
154
155 protected Date getOldestDate(HttpServletRequest request) {
156 Date oldestDate = null;
157
158 oldestDate = ParamUtil.getDate(
159 request, "oldestDate",
160 DateFormatFactoryUtil.getSimpleDateFormat("yyyy.MM.dd"), null);
161
162 if (oldestDate == null) {
163 int daysOld = ParamUtil.getInteger(request, "maxAge", -1);
164
165 if (daysOld != -1) {
166 Calendar cal = Calendar.getInstance();
167
168 cal.add(Calendar.DATE, (0 - daysOld));
169
170 oldestDate = cal.getTime();
171 }
172 }
173
174 return oldestDate;
175 }
176
177 protected Properties getRepoSettings(HttpServletRequest request) {
178 Properties repoSettings = new Properties();
179
180 String prefix = "setting_";
181
182 Enumeration<String> enu = request.getParameterNames();
183
184 while (enu.hasMoreElements()) {
185 String name = enu.nextElement();
186
187 if (name.startsWith(prefix)) {
188 String settingName = name.substring(
189 prefix.length(), name.length());
190
191 String value = ParamUtil.getString(request, name);
192
193 if (Validator.isNotNull(value)) {
194 repoSettings.setProperty(settingName, value);
195 }
196 }
197 }
198
199 return repoSettings;
200 }
201
202 protected String getVersion(HttpServletRequest request) {
203 String version = ParamUtil.getString(request, "version");
204
205 String prefix =
206 PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
207 String extension =
208 StringPool.PERIOD +
209 PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
210
211 if (Validator.isNull(version)) {
212 String path = GetterUtil.getString(request.getPathInfo());
213
214 if (Validator.isNotNull(path)) {
215 int x = path.indexOf(prefix);
216
217 if (x != -1) {
218 version = path.substring(
219 x + prefix.length(), path.indexOf(extension, x));
220 }
221 }
222 }
223
224 if (_log.isDebugEnabled()) {
225 if (Validator.isNull(version)) {
226 _log.debug("Serving repository for all versions");
227 }
228 else {
229 _log.debug("Serving repository for version " + version);
230 }
231 }
232
233 return version;
234 }
235
236 private static Log _log = LogFactoryUtil.getLog(
237 SoftwareCatalogServlet.class);
238
239 }