1
22
23 package com.liferay.portal.servlet;
24
25 import com.liferay.portal.NoSuchGroupException;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.ContentTypes;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.Http;
31 import com.liferay.portal.kernel.util.ParamUtil;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.model.Group;
36 import com.liferay.portal.model.impl.GroupImpl;
37 import com.liferay.portal.plugin.PluginPackageUtil;
38 import com.liferay.portal.service.GroupLocalServiceUtil;
39 import com.liferay.portal.util.PortalInstances;
40 import com.liferay.portal.util.PortalUtil;
41 import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
42 import com.liferay.util.servlet.ServletResponseUtil;
43
44 import java.io.IOException;
45
46 import java.text.SimpleDateFormat;
47
48 import java.util.Calendar;
49 import java.util.Date;
50 import java.util.Enumeration;
51 import java.util.Properties;
52
53 import javax.servlet.ServletException;
54 import javax.servlet.http.HttpServlet;
55 import javax.servlet.http.HttpServletRequest;
56 import javax.servlet.http.HttpServletResponse;
57
58 import org.apache.commons.logging.Log;
59 import org.apache.commons.logging.LogFactory;
60
61
67 public class SoftwareCatalogServlet extends HttpServlet {
68
69 public void service(
70 HttpServletRequest request, HttpServletResponse response)
71 throws IOException, ServletException {
72
73 try {
74 long groupId = getGroupId(request);
75 String version = getVersion(request);
76 String baseImageURL = getBaseImageURL(request);
77 Date oldestDate = getOldestDate(request);
78 int maxNumOfVersions = ParamUtil.getInteger(
79 request, "maxNumOfVersions");
80 Properties repoSettings = getRepoSettings(request);
81
82 if (_log.isDebugEnabled()) {
83 _log.debug("Group ID " + groupId);
84 _log.debug("Base image URL " + baseImageURL);
85 _log.debug("Oldtest date " + oldestDate);
86 _log.debug("Maximum number of versions " + maxNumOfVersions);
87 }
88
89 String repositoryXML =
90 SCProductEntryLocalServiceUtil.getRepositoryXML(
91 groupId, version, baseImageURL, oldestDate,
92 maxNumOfVersions, repoSettings);
93
94 ServletResponseUtil.sendFile(
95 response, null, repositoryXML.getBytes(StringPool.UTF8),
96 ContentTypes.TEXT_XML_UTF8);
97 }
98 catch (NoSuchGroupException nsge) {
99 PortalUtil.sendError(
100 HttpServletResponse.SC_NOT_FOUND, nsge, request, response);
101 }
102 catch (Exception e) {
103 if (_log.isWarnEnabled()) {
104 _log.warn(e, e);
105 }
106
107 PortalUtil.sendError(
108 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
109 response);
110 }
111 }
112
113 protected String getBaseImageURL(HttpServletRequest request) {
114 String host = PortalUtil.getHost(request);
115
116 String portalURL = PortalUtil.getPortalURL(
117 host, request.getServerPort(), request.isSecure());
118
119 String pathImage = PortalUtil.getPathImage();
120
121 if (pathImage.startsWith(Http.HTTP_WITH_SLASH) ||
122 pathImage.startsWith(Http.HTTPS_WITH_SLASH)) {
123
124 return pathImage + "/software_catalog";
125 }
126 else {
127 return portalURL + pathImage + "/software_catalog";
128 }
129 }
130
131 protected long getGroupId(HttpServletRequest request)
132 throws SystemException, PortalException {
133
134 long groupId = ParamUtil.getLong(request, "groupId");
135
136 if (groupId <= 0) {
137 String path = GetterUtil.getString(request.getPathInfo());
138
139 path = StringUtil.replace(
140 path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
141
142 if (Validator.isNotNull(path)) {
143 int pos = path.indexOf(StringPool.SLASH, 1);
144
145 if (pos == -1) {
146 pos = path.length();
147 }
148
149 groupId = GetterUtil.getLong(path.substring(1, pos));
150 }
151 }
152
153 if (groupId <= 0) {
154 long companyId = PortalInstances.getCompanyId(request);
155
156 Group guestGroup = GroupLocalServiceUtil.getGroup(
157 companyId, GroupImpl.GUEST);
158
159 groupId = guestGroup.getGroupId();
160 }
161
162 return groupId;
163 }
164
165 protected Date getOldestDate(HttpServletRequest request) {
166 Date oldestDate = null;
167
168 oldestDate = ParamUtil.getDate(
169 request, "oldestDate", new SimpleDateFormat("yyyy.MM.dd"), null);
170
171 if (oldestDate == null) {
172 int daysOld = ParamUtil.getInteger(request, "maxAge", -1);
173
174 if (daysOld != -1) {
175 Calendar cal = Calendar.getInstance();
176
177 cal.add(Calendar.DATE, (0 - daysOld));
178
179 oldestDate = cal.getTime();
180 }
181 }
182
183 return oldestDate;
184 }
185
186 protected Properties getRepoSettings(HttpServletRequest request) {
187 Properties repoSettings = new Properties();
188
189 String prefix = "setting_";
190
191 Enumeration<String> enu = request.getParameterNames();
192
193 while (enu.hasMoreElements()) {
194 String name = enu.nextElement();
195
196 if (name.startsWith(prefix)) {
197 String settingName = name.substring(
198 prefix.length(), name.length());
199
200 String value = ParamUtil.getString(request, name);
201
202 if (Validator.isNotNull(value)) {
203 repoSettings.setProperty(settingName , value);
204 }
205 }
206 }
207
208 return repoSettings;
209 }
210
211 protected String getVersion(HttpServletRequest request) {
212 String version = ParamUtil.getString(request, "version");
213
214 String prefix =
215 PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
216 String extension =
217 StringPool.PERIOD +
218 PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
219
220 if (Validator.isNull(version)) {
221 String path = GetterUtil.getString(request.getPathInfo());
222
223 if (Validator.isNotNull(path)) {
224 int x = path.indexOf(prefix);
225
226 if (x != -1) {
227 version = path.substring(
228 x + prefix.length(), path.indexOf(extension, x));
229 }
230 }
231 }
232
233 if (_log.isDebugEnabled()) {
234 if (Validator.isNull(version)) {
235 _log.debug("Serving repository for all versions");
236 }
237 else {
238 _log.debug("Serving repository for version " + version);
239 }
240 }
241
242 return version;
243 }
244
245 private static Log _log = LogFactory.getLog(SoftwareCatalogServlet.class);
246
247 }