1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.search;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.search.SearchContainer;
27  import com.liferay.portal.kernel.search.OpenSearch;
28  import com.liferay.portal.kernel.search.SearchException;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.HttpUtil;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
33  import com.liferay.portal.kernel.xml.Document;
34  import com.liferay.portal.kernel.xml.Element;
35  import com.liferay.portal.kernel.xml.SAXReaderUtil;
36  import com.liferay.portal.model.Layout;
37  import com.liferay.portal.service.LayoutLocalServiceUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.PortletURLImpl;
41  
42  import java.util.Date;
43  
44  import javax.portlet.PortletMode;
45  import javax.portlet.PortletModeException;
46  import javax.portlet.PortletRequest;
47  import javax.portlet.PortletURL;
48  import javax.portlet.WindowState;
49  import javax.portlet.WindowStateException;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * <a href="BaseOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Charles May
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public abstract class BaseOpenSearchImpl implements OpenSearch {
61  
62      public boolean isEnabled() {
63          return true;
64      }
65  
66      public String search(HttpServletRequest request, String url)
67          throws SearchException {
68  
69          String keywords = GetterUtil.getString(
70              HttpUtil.getParameter(url, "keywords", false));
71          int startPage = GetterUtil.getInteger(
72              HttpUtil.getParameter(url, "p", false), 1);
73          int itemsPerPage = GetterUtil.getInteger(
74              HttpUtil.getParameter(url, "c", false),
75              SearchContainer.DEFAULT_DELTA);
76          String format = GetterUtil.getString(
77              HttpUtil.getParameter(url, "format", false));
78  
79          return search(request, keywords, startPage, itemsPerPage, format);
80      }
81  
82      public abstract String search(
83              HttpServletRequest request, String keywords, int startPage,
84              int itemsPerPage, String format)
85          throws SearchException;
86  
87      protected void addSearchResult(
88          Element root, String title, String link, Date updated,
89          String summary, double score, String format) {
90  
91          addSearchResult(
92              root, title, link, updated, summary, new String[0], 0, score,
93              format);
94      }
95  
96      protected void addSearchResult(
97          Element root, String title, String link, Date updated, String summary,
98          String[] tags, double ratings, double score, String format) {
99  
100         if (format.equals("rss")) {
101             addSearchResultRSS(
102                 root, title, link, updated, summary, tags, ratings, score);
103         }
104         else {
105             addSearchResultAtom(
106                 root, title, link, updated, summary, tags, ratings, score);
107         }
108     }
109 
110     protected void addSearchResultAtom(
111         Element root, String title, String link, Date updated, String summary,
112         String[] tags, double ratings, double score) {
113 
114         // entry
115 
116         Element entry = OpenSearchUtil.addElement(
117             root, "entry", OpenSearchUtil.DEFAULT_NAMESPACE);
118 
119         // title
120 
121         OpenSearchUtil.addElement(
122             entry, "title", OpenSearchUtil.DEFAULT_NAMESPACE, title);
123 
124         // link
125 
126         Element entryLink = OpenSearchUtil.addElement(
127             entry, "link", OpenSearchUtil.DEFAULT_NAMESPACE);
128 
129         entryLink.addAttribute("href", link);
130 
131         // id
132 
133         OpenSearchUtil.addElement(
134             entry, "id", OpenSearchUtil.DEFAULT_NAMESPACE,
135             "urn:uuid:" + PortalUUIDUtil.generate());
136 
137         // updated
138 
139         OpenSearchUtil.addElement(
140             entry, "updated", OpenSearchUtil.DEFAULT_NAMESPACE, updated);
141 
142         // summary
143 
144         OpenSearchUtil.addElement(
145             entry, "summary", OpenSearchUtil.DEFAULT_NAMESPACE, summary);
146 
147         // tags
148 
149         OpenSearchUtil.addElement(
150             entry, "tags", OpenSearchUtil.DEFAULT_NAMESPACE,
151             StringUtil.merge(tags));
152 
153         // ratings
154 
155         OpenSearchUtil.addElement(
156             entry, "ratings", OpenSearchUtil.DEFAULT_NAMESPACE, ratings);
157 
158         // relevance:score
159 
160         OpenSearchUtil.addElement(
161             entry, "score", OpenSearchUtil.RELEVANCE_NAMESPACE, score);
162     }
163 
164     protected void addSearchResultRSS(
165         Element root, String title, String link, Date updated, String summary,
166         String[] tags, double ratings, double score) {
167 
168         // item
169 
170         Element item = root.addElement("item");
171 
172         // title
173 
174         OpenSearchUtil.addElement(
175             item, "title", OpenSearchUtil.NO_NAMESPACE, title);
176 
177         // link
178 
179         OpenSearchUtil.addElement(
180             item, "link", OpenSearchUtil.NO_NAMESPACE, link);
181 
182         // summary
183 
184         OpenSearchUtil.addElement(
185             item, "description", OpenSearchUtil.NO_NAMESPACE, summary);
186 
187         // tags
188 
189         OpenSearchUtil.addElement(
190             item, "tags", OpenSearchUtil.NO_NAMESPACE, StringUtil.merge(tags));
191 
192         // ratings
193 
194         OpenSearchUtil.addElement(
195             item, "ratings", OpenSearchUtil.NO_NAMESPACE, ratings);
196 
197         // relevance:score
198 
199         OpenSearchUtil.addElement(
200             item, "score", OpenSearchUtil.RELEVANCE_NAMESPACE, score);
201     }
202 
203     protected Object[] addSearchResults(
204         String keywords, int startPage, int itemsPerPage, int total, int start,
205         String title, String searchPath, String format,
206         ThemeDisplay themeDisplay) {
207 
208         int totalPages = 0;
209 
210         if (total % itemsPerPage == 0) {
211             totalPages = total / itemsPerPage;
212         }
213         else {
214             totalPages = (total / itemsPerPage) + 1;
215         }
216 
217         int previousPage = startPage - 1;
218         int nextPage = startPage + 1;
219 
220         Document doc = SAXReaderUtil.createDocument();
221 
222         if (format.equals("rss")) {
223             return addSearchResultsRSS(
224                 doc, keywords, startPage, itemsPerPage, total, start,
225                 totalPages, previousPage, nextPage, title, searchPath,
226                 themeDisplay);
227         }
228         else {
229             return addSearchResultsAtom(
230                 doc, keywords, startPage, itemsPerPage, total, start,
231                 totalPages, previousPage, nextPage, title, searchPath,
232                 themeDisplay);
233         }
234     }
235 
236     protected Object[] addSearchResultsAtom(
237         Document doc, String keywords, int startPage, int itemsPerPage,
238         int total, int start, int totalPages, int previousPage, int nextPage,
239         String title, String searchPath, ThemeDisplay themeDisplay) {
240 
241         // feed
242 
243         Element root = doc.addElement("feed");
244 
245         root.add(OpenSearchUtil.getNamespace(OpenSearchUtil.DEFAULT_NAMESPACE));
246         root.add(OpenSearchUtil.getNamespace(OpenSearchUtil.OS_NAMESPACE));
247         root.add(
248             OpenSearchUtil.getNamespace(OpenSearchUtil.RELEVANCE_NAMESPACE));
249 
250         // title
251 
252         OpenSearchUtil.addElement(
253             root, "title", OpenSearchUtil.DEFAULT_NAMESPACE, title);
254 
255         // updated
256 
257         OpenSearchUtil.addElement(
258             root, "updated", OpenSearchUtil.DEFAULT_NAMESPACE, new Date());
259 
260         // author
261 
262         Element author = OpenSearchUtil.addElement(
263             root, "author", OpenSearchUtil.DEFAULT_NAMESPACE);
264 
265         // name
266 
267         OpenSearchUtil.addElement(
268             author, "name", OpenSearchUtil.DEFAULT_NAMESPACE,
269             themeDisplay.getUserId());
270 
271         // id
272 
273         OpenSearchUtil.addElement(
274             root, "id", OpenSearchUtil.DEFAULT_NAMESPACE,
275             "urn:uuid:" + PortalUUIDUtil.generate());
276 
277         // opensearch:totalResults
278 
279         OpenSearchUtil.addElement(
280             root, "totalResults", OpenSearchUtil.OS_NAMESPACE, total);
281 
282         // opensearch:startIndex
283 
284         OpenSearchUtil.addElement(
285             root, "startIndex", OpenSearchUtil.OS_NAMESPACE, start + 1);
286 
287         // opensearch:itemsPerPage
288 
289         OpenSearchUtil.addElement(
290             root, "itemsPerPage", OpenSearchUtil.OS_NAMESPACE, itemsPerPage);
291 
292         // opensearch:Query
293 
294         Element query = OpenSearchUtil.addElement(
295             root, "Query", OpenSearchUtil.OS_NAMESPACE);
296 
297         query.addAttribute("role", "request");
298         query.addAttribute("searchTerms", keywords);
299         query.addAttribute("startPage", String.valueOf(startPage));
300 
301         // links
302 
303         String searchURL = themeDisplay.getURLPortal() + searchPath;
304 
305         OpenSearchUtil.addLink(
306             root, searchURL, "self", keywords, startPage, itemsPerPage);
307         OpenSearchUtil.addLink(
308             root, searchURL, "first", keywords, 1, itemsPerPage);
309 
310         if (previousPage > 0) {
311             OpenSearchUtil.addLink(
312                 root, searchURL, "previous", keywords, previousPage,
313                 itemsPerPage);
314         }
315 
316         if (nextPage <= totalPages) {
317             OpenSearchUtil.addLink(
318                 root, searchURL, "next", keywords, nextPage, itemsPerPage);
319         }
320 
321         OpenSearchUtil.addLink(
322             root, searchURL, "last", keywords, totalPages, itemsPerPage);
323 
324         Element link = OpenSearchUtil.addElement(
325             root, "link", OpenSearchUtil.DEFAULT_NAMESPACE);
326 
327         link.addAttribute("rel", "search");
328         link.addAttribute("href", searchPath + "_description.xml");
329         link.addAttribute("type", "application/opensearchdescription+xml");
330 
331         return new Object[] {doc, root};
332     }
333 
334     protected Object[] addSearchResultsRSS(
335         Document doc, String keywords, int startPage, int itemsPerPage,
336         int total, int start, int totalPages, int previousPage, int nextPage,
337         String title, String searchPath, ThemeDisplay themeDisplay) {
338 
339         // rss
340 
341         Element root = doc.addElement("rss");
342 
343         root.addAttribute("version", "2.0");
344         root.add(
345             SAXReaderUtil.createNamespace(
346                 "atom", "http://www.w3.org/2005/Atom"));
347         root.add(OpenSearchUtil.getNamespace(OpenSearchUtil.OS_NAMESPACE));
348         root.add(
349             OpenSearchUtil.getNamespace(OpenSearchUtil.RELEVANCE_NAMESPACE));
350 
351         // channel
352 
353         Element channel = root.addElement("channel");
354 
355         // title
356 
357         OpenSearchUtil.addElement(
358             channel, "title", OpenSearchUtil.NO_NAMESPACE, title);
359 
360         // link
361 
362         OpenSearchUtil.addElement(
363             channel, "link", OpenSearchUtil.NO_NAMESPACE,
364             themeDisplay.getURLPortal() + searchPath);
365 
366         // description
367 
368         OpenSearchUtil.addElement(
369             channel, "description", OpenSearchUtil.NO_NAMESPACE, title);
370 
371         // opensearch:totalResults
372 
373         OpenSearchUtil.addElement(
374             channel, "totalResults", OpenSearchUtil.OS_NAMESPACE, total);
375 
376         // opensearch:startIndex
377 
378         OpenSearchUtil.addElement(
379             channel, "startIndex", OpenSearchUtil.OS_NAMESPACE, start + 1);
380 
381         // opensearch:itemsPerPage
382 
383         OpenSearchUtil.addElement(
384             channel, "itemsPerPage", OpenSearchUtil.OS_NAMESPACE, itemsPerPage);
385 
386         // opensearch:Query
387 
388         Element query = OpenSearchUtil.addElement(
389             channel, "Query", OpenSearchUtil.OS_NAMESPACE);
390 
391         query.addAttribute("role", "request");
392         query.addAttribute("searchTerms", keywords);
393         query.addAttribute("startPage", String.valueOf(startPage));
394 
395         return new Object[] {doc, channel};
396     }
397 
398     protected PortletURL getPortletURL(
399             HttpServletRequest request, String portletId)
400         throws PortletModeException, SystemException, WindowStateException {
401 
402         return getPortletURL(request, portletId, 0);
403     }
404 
405     protected PortletURL getPortletURL(
406             HttpServletRequest request, String portletId, long groupId)
407         throws PortletModeException, SystemException, WindowStateException {
408 
409         long plid = LayoutLocalServiceUtil.getDefaultPlid(
410             groupId, false, portletId);
411 
412         if (plid == 0) {
413             plid = LayoutLocalServiceUtil.getDefaultPlid(
414                 groupId, true, portletId);
415         }
416 
417         if (plid == 0) {
418             Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
419 
420             if (layout != null) {
421                 plid = layout.getPlid();
422             }
423         }
424 
425         PortletURL portletURL = new PortletURLImpl(
426             request, portletId, plid, PortletRequest.RENDER_PHASE);
427 
428         portletURL.setWindowState(WindowState.MAXIMIZED);
429         portletURL.setPortletMode(PortletMode.VIEW);
430 
431         return portletURL;
432     }
433 
434 }