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