001/*
002 * (C) Copyright 2015 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.salesforce;
018
019import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
020import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
021
022import java.io.IOException;
023
024import org.codehaus.jackson.JsonGenerator;
025import org.nuxeo.common.utils.URIUtils;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.io.download.DownloadService;
032import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
033import org.nuxeo.ecm.core.io.registry.reflect.Setup;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Enricher used to retrieve drive edit url.
038 *
039 * @since 7.10
040 */
041@Setup(mode = SINGLETON, priority = REFERENCE)
042public class DriveEditEnricher extends AbstractJsonEnricher<DocumentModel> {
043
044    public static final String NXDRIVE_PROTOCOL = "nxdrive";
045
046    public static final String PROTOCOL_COMMAND_EDIT = "edit";
047
048    public static final String NEW_DRIVE_EDIT_URL_PROP_KEY = "org.nuxeo.drive.new.edit.url";
049
050    public static final String NAME = "driveEditURL";
051
052    public DriveEditEnricher() {
053        super(NAME);
054    }
055
056    @Override
057    public void write(JsonGenerator jg, DocumentModel doc) throws IOException {
058        jg.writeStringField(NAME, doGetDriveEditURL(doc));
059    }
060
061    protected String doGetDriveEditURL(DocumentModel currentDocument) throws IOException {
062        CoreSession session = currentDocument.getCoreSession();
063        BlobHolder bh = currentDocument.getAdapter(BlobHolder.class);
064        if (bh == null) {
065            return null;
066        }
067        Blob blob = bh.getBlob();
068        if (blob == null) {
069            return null;
070        }
071        String fileName = blob.getFilename();
072        String baseURL = ctx.getBaseUrl();
073        StringBuffer sb = new StringBuffer();
074        sb.append(NXDRIVE_PROTOCOL).append("://");
075        sb.append(PROTOCOL_COMMAND_EDIT).append("/");
076        sb.append(baseURL.replaceFirst("://", "/"));
077        if (Boolean.valueOf(Framework.getProperty(NEW_DRIVE_EDIT_URL_PROP_KEY))) {
078            sb.append("user/");
079            sb.append(session.getPrincipal().getName());
080            sb.append("/");
081        }
082        sb.append("repo/");
083        sb.append(session.getRepositoryName());
084        sb.append("/nxdocid/");
085        sb.append(currentDocument.getId());
086        sb.append("/filename/");
087        String escapedFilename = fileName.replaceAll("(/|\\\\|\\*|<|>|\\?|\"|:|\\|)", "-");
088        sb.append(URIUtils.quoteURIPathComponent(escapedFilename, true));
089        if (Boolean.valueOf(Framework.getProperty(NEW_DRIVE_EDIT_URL_PROP_KEY))) {
090            sb.append("/downloadUrl/");
091            DownloadService downloadService = Framework.getService(DownloadService.class);
092            String downloadUrl = downloadService.getDownloadUrl(currentDocument, DownloadService.BLOBHOLDER_0, "");
093            sb.append(downloadUrl);
094        }
095        return sb.toString();
096    }
097
098}