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