1
14
15 package com.liferay.portal.servlet;
16
17 import com.liferay.portal.NoSuchGroupException;
18 import com.liferay.portal.kernel.exception.PortalException;
19 import com.liferay.portal.kernel.exception.SystemException;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.util.ContentTypes;
23 import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
24 import com.liferay.portal.kernel.util.GetterUtil;
25 import com.liferay.portal.kernel.util.Http;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.model.Group;
31 import com.liferay.portal.model.GroupConstants;
32 import com.liferay.portal.plugin.PluginPackageUtil;
33 import com.liferay.portal.service.GroupLocalServiceUtil;
34 import com.liferay.portal.util.PortalInstances;
35 import com.liferay.portal.util.PortalUtil;
36 import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
37 import com.liferay.util.servlet.ServletResponseUtil;
38
39 import java.io.IOException;
40
41 import java.util.Calendar;
42 import java.util.Date;
43 import java.util.Enumeration;
44 import java.util.Properties;
45
46 import javax.servlet.ServletException;
47 import javax.servlet.http.HttpServlet;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51
56 public class SoftwareCatalogServlet extends HttpServlet {
57
58 public void service(
59 HttpServletRequest request, HttpServletResponse response)
60 throws IOException, ServletException {
61
62 try {
63 long groupId = getGroupId(request);
64 String version = getVersion(request);
65 String baseImageURL = getBaseImageURL(request);
66 Date oldestDate = getOldestDate(request);
67 int maxNumOfVersions = ParamUtil.getInteger(
68 request, "maxNumOfVersions");
69 Properties repoSettings = getRepoSettings(request);
70
71 if (_log.isDebugEnabled()) {
72 _log.debug("Group ID " + groupId);
73 _log.debug("Base image URL " + baseImageURL);
74 _log.debug("Oldtest date " + oldestDate);
75 _log.debug("Maximum number of versions " + maxNumOfVersions);
76 }
77
78 String repositoryXML =
79 SCProductEntryLocalServiceUtil.getRepositoryXML(
80 groupId, version, baseImageURL, oldestDate,
81 maxNumOfVersions, repoSettings);
82
83 ServletResponseUtil.sendFile(
84 request, response, null,
85 repositoryXML.getBytes(StringPool.UTF8),
86 ContentTypes.TEXT_XML_UTF8);
87 }
88 catch (NoSuchGroupException nsge) {
89 PortalUtil.sendError(
90 HttpServletResponse.SC_NOT_FOUND, nsge, request, response);
91 }
92 catch (Exception e) {
93 if (_log.isWarnEnabled()) {
94 _log.warn(e, e);
95 }
96
97 PortalUtil.sendError(
98 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
99 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 SystemException, PortalException {
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(StringPool.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 }