001/*
002 * (C) Copyright 2013 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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.nuxeo.common.utils.URIUtils;
023import org.nuxeo.ecm.core.api.DocumentLocation;
024import org.nuxeo.ecm.core.api.IdRef;
025import org.nuxeo.ecm.platform.url.api.DocumentView;
026import org.nuxeo.ecm.platform.url.service.AbstractDocumentViewCodec;
027
028import com.google.common.base.Joiner;
029
030/**
031 * Codec that resolve the Rest url for a document
032 *
033 * @since 5.7.2
034 */
035public class RestDocumentViewCodec extends AbstractDocumentViewCodec {
036
037    public static final String PREFIX = "site/api/v1/id";
038
039    public RestDocumentViewCodec() {
040    }
041
042    public RestDocumentViewCodec(String prefix) {
043    }
044
045    @Override
046    public String getPrefix() {
047        if (prefix != null) {
048            return prefix;
049        }
050        return PREFIX;
051    }
052
053    @Override
054    public String getUrlFromDocumentView(DocumentView docView) {
055        DocumentLocation docLoc = docView.getDocumentLocation();
056        if (docLoc != null) {
057
058            List<String> items = new ArrayList<String>();
059            items.add(getPrefix());
060            IdRef docRef = docLoc.getIdRef();
061            if (docRef == null) {
062                return null;
063            }
064            items.add(docRef.toString());
065            String uri = Joiner.on("/").join(items);
066            return URIUtils.addParametersToURIQuery(uri, docView.getParameters());
067        }
068        return null;
069    }
070
071    /**
072     * There is no document veiw for Rest codec
073     */
074    @Override
075    public DocumentView getDocumentViewFromUrl(String url) {
076        return null;
077    }
078
079}