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