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