001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019
020package org.nuxeo.wopi;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024
025import java.io.IOException;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.model.Property;
030import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
031import org.nuxeo.ecm.core.io.marshallers.json.document.DocumentModelJsonWriter;
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
036import com.fasterxml.jackson.core.JsonGenerator;
037
038/**
039 * Gets the WOPI action URLs available for each blob of the document.
040 * <p>
041 * Enabled when parameter {@code enrichers.blob:wopi} is present.
042 * <p>
043 * Blob format is:
044 *
045 * <pre>
046 * {@code
047 * {
048 *   "entity-type":"document",
049 *   "properties": {
050 *     "file:content": {
051 *       "name": "...",
052 *       "mime-type": "...",
053 *       ...,
054 *       "wopi": {
055 *         "appName": "Word",
056 *         "view": "http://localhost:8080/nuxeo/wopi/view/REPOSITORY/DOC_ID/file:content",
057 *         "edit": "http://localhost:8080/nuxeo/wopi/edit/REPOSITORY/DOC_ID/file:content"
058 *       }
059 *     },
060 *     "other:xpath": {
061 *       "name": "...",
062 *       "mime-type": "...",
063 *       ...,
064 *       "wopi": {
065 *         "appName": "Excel",
066 *         "view": "http://localhost:8080/nuxeo/wopi/view/REPOSITORY/DOC_ID/other:xpath",
067 *         "edit": "http://localhost:8080/nuxeo/wopi/edit/REPOSITORY/DOC_ID/other:xpath"
068 *       }
069 *     }
070 *   }
071 * }
072 * }
073 * </pre>
074 *
075 * @since 10.3
076 */
077@Setup(mode = SINGLETON, priority = REFERENCE)
078public class WOPIJsonEnricher extends AbstractJsonEnricher<BlobProperty> {
079
080    public static final String NAME = "wopi";
081
082    public static final String APP_NAME_FIELD = "appName";
083
084    public WOPIJsonEnricher() {
085        super(NAME);
086    }
087
088    @Override
089    public void write(JsonGenerator jg, BlobProperty blobProperty) throws IOException {
090        DocumentModel doc = ctx.getParameter(DocumentModelJsonWriter.ENTITY_TYPE);
091        if (doc == null) {
092            return;
093        }
094
095        Blob blob = (Blob) blobProperty.getValue();
096        WOPIBlobInfo info = Framework.getService(WOPIService.class).getWOPIBlobInfo(blob);
097        if (info == null) {
098            return;
099        }
100
101        jg.writeFieldName(NAME);
102        jg.writeStartObject();
103        writeWOPIBlobInfo(jg, info, doc, getXPath(blobProperty));
104        jg.writeEndObject();
105    }
106
107    protected void writeWOPIBlobInfo(JsonGenerator jg, WOPIBlobInfo info, DocumentModel doc, String xpath)
108            throws IOException {
109        jg.writeStringField(APP_NAME_FIELD, info.appName);
110        for (String action : info.actions) {
111            jg.writeStringField(action, Helpers.getWOPIURL(ctx.getBaseUrl(), action, doc, xpath));
112        }
113    }
114
115    protected String getXPath(Property property) {
116        String xpath = property.getXPath();
117        // if no prefix, use schema name as prefix
118        if (!xpath.contains(":")) {
119            xpath = property.getSchema().getName() + ":" + xpath;
120        }
121        return xpath;
122    }
123
124}