001/* 002 * (C) Copyright 2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019package org.nuxeo.ecm.platform.ui.web.restAPI; 020 021import java.io.IOException; 022import java.net.URLEncoder; 023 024import org.dom4j.Element; 025import org.dom4j.Namespace; 026import org.dom4j.QName; 027import org.dom4j.dom.DOMDocument; 028import org.dom4j.dom.DOMDocumentFactory; 029import org.nuxeo.ecm.core.api.CoreInstance; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.DocumentModelList; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions; 035import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 036import org.restlet.data.CharacterSet; 037import org.restlet.data.MediaType; 038import org.restlet.data.Request; 039import org.restlet.data.Response; 040import org.restlet.resource.Representation; 041import org.restlet.resource.StringRepresentation; 042 043/** 044 * Basic OpenSearch REST fulltext search implementation using the RSS 2.0 results format. 045 * <p> 046 * TODO: make it possible to change the page size and navigate to next results pages using additional query parameters. 047 * See http://opensearch.org for official specifications. 048 * <p> 049 * TODO: use a OPENSEARCH stateless query model to be able to override the currently hardcoded request pattern. 050 * <p> 051 * TODO: add OpenSearch XML description snippet in the default theme so that Firefox can autodetect the service URL. 052 * 053 * @author Olivier Grisel 054 */ 055public class OpenSearchRestlet extends BaseNuxeoRestlet { 056 057 public static final String RSS_TAG = "rss"; 058 059 public static final String CHANNEL_TAG = "channel"; 060 061 public static final String TITLE_TAG = "title"; 062 063 public static final String DESCRIPTION_TAG = "description"; 064 065 public static final String LINK_TAG = "link"; 066 067 public static final String ITEM_TAG = "item"; 068 069 public static final String QUERY = "SELECT * FROM Document WHERE ecm:fulltext LIKE '%s' ORDER BY dc:modified DESC"; 070 071 public static final int MAX = 10; 072 073 public static final Namespace OPENSEARCH_NS = new Namespace("opensearch", "http://a9.com/-/spec/opensearch/1.1/"); 074 075 public static final Namespace ATOM_NS = new Namespace("atom", "http://www.w3.org/2005/Atom"); 076 077 @Override 078 public void handle(Request req, Response res) { 079 try (CoreSession session = CoreInstance.openCoreSession(null, getUserPrincipal(req))) { 080 // read the search term passed as the 'q' request parameter 081 String keywords = getQueryParamValue(req, "q", " "); 082 083 // perform the search on the fulltext index and wrap the results as 084 // a DocumentModelList with the 10 first matching results ordered by 085 // modification time 086 String query = String.format(QUERY, keywords); 087 DocumentModelList documents = session.query(query, null, MAX, 0, true); 088 089 // build the RSS 2.0 response document holding the results 090 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 091 DOMDocument resultDocument = (DOMDocument) domFactory.createDocument(); 092 093 // rss root tag 094 Element rssElement = resultDocument.addElement(RSS_TAG); 095 rssElement.addAttribute("version", "2.0"); 096 rssElement.addNamespace(OPENSEARCH_NS.getPrefix(), OPENSEARCH_NS.getURI()); 097 rssElement.addNamespace(ATOM_NS.getPrefix(), ATOM_NS.getURI()); 098 099 // channel with OpenSearch metadata 100 Element channelElement = rssElement.addElement(CHANNEL_TAG); 101 102 channelElement.addElement(TITLE_TAG).setText("Nuxeo EP OpenSearch channel for " + keywords); 103 channelElement.addElement("link").setText( 104 BaseURL.getBaseURL(getHttpRequest(req)) + "restAPI/opensearch?q=" 105 + URLEncoder.encode(keywords, "UTF-8")); 106 channelElement.addElement(new QName("totalResults", OPENSEARCH_NS)).setText( 107 Long.toString(documents.totalSize())); 108 channelElement.addElement(new QName("startIndex", OPENSEARCH_NS)).setText("0"); 109 channelElement.addElement(new QName("itemsPerPage", OPENSEARCH_NS)).setText( 110 Integer.toString(documents.size())); 111 112 Element queryElement = channelElement.addElement(new QName("Query", OPENSEARCH_NS)); 113 queryElement.addAttribute("role", "request"); 114 queryElement.addAttribute("searchTerms", keywords); 115 queryElement.addAttribute("startPage", Integer.toString(1)); 116 117 // document items 118 String baseUrl = BaseURL.getBaseURL(getHttpRequest(req)); 119 120 for (DocumentModel doc : documents) { 121 Element itemElement = channelElement.addElement(ITEM_TAG); 122 Element titleElement = itemElement.addElement(TITLE_TAG); 123 String title = doc.getTitle(); 124 if (title != null) { 125 titleElement.setText(title); 126 } 127 Element descriptionElement = itemElement.addElement(DESCRIPTION_TAG); 128 String description = doc.getProperty("dublincore:description").getValue(String.class); 129 if (description != null) { 130 descriptionElement.setText(description); 131 } 132 Element linkElement = itemElement.addElement("link"); 133 linkElement.setText(baseUrl + DocumentModelFunctions.documentUrl(doc)); 134 } 135 136 Representation rep = new StringRepresentation(resultDocument.asXML(), MediaType.APPLICATION_XML); 137 rep.setCharacterSet(CharacterSet.UTF_8); 138 res.setEntity(rep); 139 140 } catch (NuxeoException | IOException e) { 141 handleError(res, e); 142 } 143 } 144 145}