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(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(StringPool.UTF8);
95
96 ServletResponseUtil.sendFile(
97 res, fileName, byteArray, ContentTypes.TEXT_XML_UTF8);
98 }
99 catch (NoSuchGroupException nsge) {
100 PortalUtil.sendError(
101 HttpServletResponse.SC_NOT_FOUND, nsge, req, res);
102 }
103 catch (Exception e) {
104 if (_log.isWarnEnabled()) {
105 _log.warn(e, e);
106 }
107
108 PortalUtil.sendError(
109 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, req, res);
110 }
111 }
112
113 protected String getBaseImageURL(HttpServletRequest req) {
114 String host = PortalUtil.getHost(req);
115
116 String portalURL = PortalUtil.getPortalURL(
117 host, req.getServerPort(), req.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 req)
132 throws SystemException, PortalException {
133
134 long groupId = ParamUtil.getLong(req, "groupId");
135
136 if (groupId <= 0) {
137 String path = GetterUtil.getString(req.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(req);
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 req) {
166 Date oldestDate = null;
167
168 oldestDate = ParamUtil.getDate(
169 req, "oldestDate", new SimpleDateFormat("yyyy.MM.dd"), null);
170
171 if (oldestDate == null) {
172 int daysOld = ParamUtil.getInteger(req, "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 req) {
187 Properties repoSettings = new Properties();
188
189 String prefix = "setting_";
190
191 Enumeration<String> enu = req.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(req, 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 req)
212 throws PortalException, SystemException {
213
214 String version = ParamUtil.getString(req, "version");
215
216 String prefix =
217 PluginPackageUtil.REPOSITORY_XML_FILENAME_PREFIX + StringPool.DASH;
218 String extension =
219 StringPool.PERIOD +
220 PluginPackageUtil.REPOSITORY_XML_FILENAME_EXTENSION;
221
222 if (Validator.isNull(version)) {
223 String path = GetterUtil.getString(req.getPathInfo());
224
225 if (Validator.isNotNull(path)) {
226 int x = path.indexOf(prefix);
227
228 if (x != -1) {
229 version = path.substring(
230 x + prefix.length(), path.indexOf(extension, x));
231 }
232 }
233 }
234
235 if (_log.isDebugEnabled()) {
236 if (Validator.isNull(version)) {
237 _log.debug("Serving repository for all versions");
238 }
239 else {
240 _log.debug("Serving repository for version " + version);
241 }
242 }
243
244 return version;
245 }
246
247 private static Log _log = LogFactory.getLog(SoftwareCatalogServlet.class);
248
249 }