001/*
002 * (C) Copyright 2006-2016 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 *     troger
018 */
019package org.nuxeo.ecm.platform.preview.codec;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028import org.nuxeo.common.utils.URIUtils;
029import org.nuxeo.ecm.core.api.DocumentLocation;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
033import org.nuxeo.ecm.platform.url.DocumentViewImpl;
034import org.nuxeo.ecm.platform.url.api.DocumentView;
035import org.nuxeo.ecm.platform.url.service.AbstractDocumentViewCodec;
036
037/**
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 */
040public class DocumentPreviewCodec extends AbstractDocumentViewCodec {
041
042    public static final String PROPERTY_PATH_KEY = "PROPERTY_PATH_KEY";
043
044    public static final String PREFIX = "restAPI/preview";
045
046    // /server/docId/property_path/
047    public static final String URLPattern = "/(\\w+)/([a-zA-Z_0-9\\-]+)/([a-zA-Z_0-9:\\-\\.\\]\\[]*)/";
048
049    @Override
050    public String getPrefix() {
051        if (prefix != null) {
052            return prefix;
053        }
054        return PREFIX;
055    }
056
057    @Override
058    public DocumentView getDocumentViewFromUrl(String url) {
059        final Pattern pattern = Pattern.compile(getPrefix() + URLPattern);
060        Matcher m = pattern.matcher(url);
061        if (m.matches()) {
062            final String server = m.group(1);
063            String uuid = m.group(2);
064            final DocumentRef docRef = new IdRef(uuid);
065
066            Map<String, String> params = new HashMap<>();
067            final String property = m.group(3);
068            params.put(PROPERTY_PATH_KEY, property);
069
070            final DocumentLocation docLoc = new DocumentLocationImpl(server, docRef);
071            final DocumentView docView = new DocumentViewImpl(docLoc, null, params);
072
073            return docView;
074        }
075        return null;
076    }
077
078    @Override
079    public String getUrlFromDocumentView(DocumentView docView) {
080        DocumentLocation docLoc = docView.getDocumentLocation();
081        String property = docView.getParameter(PROPERTY_PATH_KEY);
082
083        // NXP-11215 Avoid NPE with not persisted documentModel
084        if (docLoc != null && docLoc.getDocRef() != null) {
085            List<String> items = new ArrayList<>();
086            items.add(getPrefix());
087            items.add(docLoc.getServerName());
088            items.add(docLoc.getDocRef().toString());
089            items.add(property);
090            String uri = String.join("/", items) + '/';
091
092            Map<String, String> requestParams = new HashMap<>(docView.getParameters());
093            requestParams.remove(PROPERTY_PATH_KEY);
094            return URIUtils.addParametersToURIQuery(uri, requestParams);
095        }
096        return null;
097    }
098
099}