001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     vpasquier <vpasquier@nuxeo.com>
016 *     dmetzler <dmetzler@nuxeo.com>
017 */
018package org.nuxeo.box.api.search;
019
020import com.google.common.base.Objects;
021import org.nuxeo.box.api.BoxConstants;
022import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException;
023import org.nuxeo.box.api.service.BoxService;
024import org.nuxeo.ecm.webengine.model.WebObject;
025import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
026import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
027import org.nuxeo.runtime.api.Framework;
028
029import javax.ws.rs.GET;
030import javax.ws.rs.Produces;
031import javax.ws.rs.QueryParam;
032import javax.ws.rs.core.MediaType;
033
034/**
035 * WebObject for a Box Search
036 *
037 * @since 5.9.3
038 */
039@WebObject(type = "search")
040@Produces({ MediaType.APPLICATION_JSON })
041public class BoxSearchObject extends AbstractResource<ResourceTypeImpl> {
042
043    BoxService boxService;
044
045    @Override
046    public void initialize(Object... args) {
047        boxService = Framework.getLocalService(BoxService.class);
048    }
049
050    /**
051     * The string in query to search for; can be matched against item names, descriptions, text content of a file, and
052     * other fields of the different item types.
053     */
054    @GET
055    public String doSearch(@QueryParam("query") String query, @QueryParam("offset") String offset,
056            @QueryParam("limit") String limit) throws BoxJSONException {
057        return boxService.toJSONString(boxService.searchBox(query, ctx.getCoreSession(),
058                Objects.firstNonNull(limit, BoxConstants.BOX_LIMIT),
059                Objects.firstNonNull(offset, BoxConstants.BOX_OFFSET)));
060    }
061
062}