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