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.CloseableCoreSession; 032import org.nuxeo.ecm.core.api.CoreInstance; 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.core.query.sql.NXQL; 037import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions; 038import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 039import org.restlet.Request; 040import org.restlet.Response; 041import org.restlet.data.CharacterSet; 042import org.restlet.data.MediaType; 043import org.restlet.representation.Representation; 044import org.restlet.representation.StringRepresentation; 045 046/** 047 * Basic OpenSearch REST fulltext search implementation using the RSS 2.0 results format. 048 * <p> 049 * TODO: make it possible to change the page size and navigate to next results pages using additional query parameters. 050 * See http://opensearch.org for official specifications. 051 * <p> 052 * TODO: use a OPENSEARCH stateless query model to be able to override the currently hardcoded request pattern. 053 * <p> 054 * TODO: add OpenSearch XML description snippet in the default theme so that Firefox can autodetect the service URL. 055 * 056 * @author Olivier Grisel 057 */ 058public class OpenSearchRestlet extends BaseNuxeoRestlet { 059 060 public static final String RSS_TAG = "rss"; 061 062 public static final String CHANNEL_TAG = "channel"; 063 064 public static final String TITLE_TAG = "title"; 065 066 public static final String DESCRIPTION_TAG = "description"; 067 068 public static final String LINK_TAG = "link"; 069 070 public static final String ITEM_TAG = "item"; 071 072 public static final String QUERY = "SELECT * FROM Document WHERE ecm:fulltext LIKE '%s' ORDER BY dc:modified DESC"; 073 074 public static final int MAX = 10; 075 076 public static final Namespace OPENSEARCH_NS = new Namespace("opensearch", "http://a9.com/-/spec/opensearch/1.1/"); 077 078 public static final Namespace ATOM_NS = new Namespace("atom", "http://www.w3.org/2005/Atom"); 079 080 @Override 081 public void handle(Request req, Response res) { 082 logDeprecation(); 083 try (CloseableCoreSession session = CoreInstance.openCoreSession(null)) { 084 // read the search term passed as the 'q' request parameter 085 String keywords = getQueryParamValue(req, "q", " "); 086 087 // perform the search on the fulltext index and wrap the results as 088 // a DocumentModelList with the 10 first matching results ordered by 089 // modification time 090 String query = String.format(QUERY, NXQL.escapeStringInner(keywords)); 091 DocumentModelList documents = session.query(query, null, MAX, 0, true); 092 093 // build the RSS 2.0 response document holding the results 094 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 095 DOMDocument resultDocument = (DOMDocument) domFactory.createDocument(); 096 097 // rss root tag 098 Element rssElement = resultDocument.addElement(RSS_TAG); 099 rssElement.addAttribute("version", "2.0"); 100 rssElement.addNamespace(OPENSEARCH_NS.getPrefix(), OPENSEARCH_NS.getURI()); 101 rssElement.addNamespace(ATOM_NS.getPrefix(), ATOM_NS.getURI()); 102 103 // channel with OpenSearch metadata 104 Element channelElement = rssElement.addElement(CHANNEL_TAG); 105 106 channelElement.addElement(TITLE_TAG).setText("Nuxeo EP OpenSearch channel for " + keywords); 107 channelElement.addElement("link").setText( 108 BaseURL.getBaseURL(getHttpRequest(req)) + "restAPI/opensearch?q=" 109 + URLEncoder.encode(keywords, "UTF-8")); 110 channelElement.addElement(new QName("totalResults", OPENSEARCH_NS)).setText( 111 Long.toString(documents.totalSize())); 112 channelElement.addElement(new QName("startIndex", OPENSEARCH_NS)).setText("0"); 113 channelElement.addElement(new QName("itemsPerPage", OPENSEARCH_NS)).setText( 114 Integer.toString(documents.size())); 115 116 Element queryElement = channelElement.addElement(new QName("Query", OPENSEARCH_NS)); 117 queryElement.addAttribute("role", "request"); 118 queryElement.addAttribute("searchTerms", keywords); 119 queryElement.addAttribute("startPage", Integer.toString(1)); 120 121 // document items 122 String baseUrl = BaseURL.getBaseURL(getHttpRequest(req)); 123 124 for (DocumentModel doc : documents) { 125 Element itemElement = channelElement.addElement(ITEM_TAG); 126 Element titleElement = itemElement.addElement(TITLE_TAG); 127 String title = doc.getTitle(); 128 if (title != null) { 129 titleElement.setText(title); 130 } 131 Element descriptionElement = itemElement.addElement(DESCRIPTION_TAG); 132 String description = doc.getProperty("dublincore:description").getValue(String.class); 133 if (description != null) { 134 descriptionElement.setText(description); 135 } 136 Element linkElement = itemElement.addElement("link"); 137 linkElement.setText(baseUrl + DocumentModelFunctions.documentUrl(doc)); 138 } 139 140 Representation rep = new StringRepresentation(resultDocument.asXML(), MediaType.APPLICATION_XML); 141 rep.setCharacterSet(CharacterSet.UTF_8); 142 res.setEntity(rep); 143 144 } catch (NuxeoException | IOException e) { 145 handleError(res, e); 146 } 147 } 148 149}