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