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