001/* 002 * (C) Copyright 2006-2009 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 */ 021 022package org.nuxeo.project.sample.restAPI; 023 024import java.io.Serializable; 025import java.util.HashMap; 026import java.util.List; 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.NuxeoException; 033import org.nuxeo.ecm.core.api.PropertyException; 034import org.nuxeo.ecm.platform.query.api.PageProvider; 035import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 036import org.nuxeo.ecm.platform.query.api.PageProviderService; 037import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 038import org.nuxeo.ecm.platform.ui.web.restAPI.BaseNuxeoRestlet; 039import org.nuxeo.runtime.api.Framework; 040import org.restlet.data.MediaType; 041import org.restlet.data.Request; 042import org.restlet.data.Response; 043 044/** 045 * This Restlet get the latest created Book document and send back a custom JSon object to the client. 046 * 047 * @author ldoguin 048 */ 049public class LastBookRestlet extends BaseNuxeoRestlet { 050 051 public static final String LAST_BOOK_PROVIDER = "LAST_BOOK"; 052 053 /** 054 * override the handle method to do custom json serialization. 055 */ 056 @Override 057 public void handle(Request req, Response res) { 058 DOMDocumentFactory domfactory = new DOMDocumentFactory(); 059 DOMDocument result = (DOMDocument) domfactory.createDocument(); 060 061 try (CoreSession session = CoreInstance.openCoreSession(null, getUserPrincipal(req))) { 062 063 PageProviderService pps = Framework.getService(PageProviderService.class); 064 065 PageProviderDefinition ppd = pps.getPageProviderDefinition(LAST_BOOK_PROVIDER); 066 HashMap<String, Serializable> props = new HashMap<String, Serializable>(); 067 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 068 069 PageProvider<?> provider = pps.getPageProvider(LAST_BOOK_PROVIDER, ppd, null, null, Long.valueOf(1), 070 Long.valueOf(0), props); 071 072 // fetch result 073 List<?> dms = provider.getCurrentPage(); 074 if (dms.size() == 0) { 075 handleError(result, res, "No result available"); 076 return; 077 } 078 DocumentModel lastBookDm = (DocumentModel) dms.get(0); 079 // serialize and write in response 080 String json = serialize(lastBookDm); 081 res.setEntity(json, MediaType.TEXT_PLAIN); 082 } catch (NuxeoException e) { 083 handleError(res, e); 084 } 085 } 086 087 private String serialize(DocumentModel lastBookDm) throws PropertyException { 088 String jSonString = "{\"book\": {" + "\"title\": \"" + lastBookDm.getPropertyValue("dc:title") + "\"," 089 + "\"isbn\": \"" + lastBookDm.getPropertyValue("bk:isbn") + "\"," + "\"rating\": \"" 090 + lastBookDm.getPropertyValue("bk:rating") + "\"," + "\"publicationDate\": \"" 091 + lastBookDm.getPropertyValue("bk:publicationDate") + "\"," + "\"keywords\": \"" 092 + lastBookDm.getPropertyValue("bk:keywords") + "\"}}"; 093 return jSonString; 094 } 095 096}