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 *     Nelson Silva <nsilva@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.core.io.marshallers.json.enrichers;
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;
026import java.util.List;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
032import org.nuxeo.ecm.core.blob.BlobManager;
033import org.nuxeo.ecm.core.blob.BlobProvider;
034import org.nuxeo.ecm.core.blob.ManagedBlob;
035import org.nuxeo.ecm.core.blob.apps.AppLink;
036import org.nuxeo.ecm.core.io.marshallers.json.document.DocumentModelJsonWriter;
037import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
038import org.nuxeo.ecm.core.io.registry.reflect.Setup;
039import org.nuxeo.runtime.api.Framework;
040
041import com.fasterxml.jackson.core.JsonGenerator;
042
043/**
044 * Enrich {@link Blob} json with list of {@link AppLink}.
045 * <p>
046 * Enabled when parameter enrichers-blob=appLinks is present.
047 * <p>
048 * Blob format is:
049 *
050 * <pre>
051 * {
052 *  "name": "...",
053 *  "mime-type": "...",
054 *  ...
055 *  "appLinks": [
056 *    {
057 *      "appName": "...",
058 *      "icon": "...",
059 *      "link": "..."
060 *    },
061 *    ...
062 *  ]
063 * }
064 * </pre>
065 *
066 * @since 10.3
067 */
068@Setup(mode = SINGLETON, priority = REFERENCE)
069public class BlobAppLinksJsonEnricher extends AbstractJsonEnricher<BlobProperty> {
070
071    public static final String NAME = "appLinks";
072
073    public BlobAppLinksJsonEnricher() {
074        super(NAME);
075    }
076
077    @Override
078    public void write(JsonGenerator jg, BlobProperty blobProperty) throws IOException {
079        Blob blob = (Blob) blobProperty.getValue();
080        if (!(blob instanceof ManagedBlob)) {
081            return;
082        }
083
084        ManagedBlob managedBlob = (ManagedBlob) blob;
085
086        BlobManager blobManager = Framework.getService(BlobManager.class);
087        BlobProvider blobProvider = blobManager.getBlobProvider(managedBlob.getProviderId());
088        if (blobProvider == null) {
089            return;
090        }
091
092        DocumentModel doc = ctx.getParameter(DocumentModelJsonWriter.ENTITY_TYPE);
093        if (doc == null) {
094            return;
095        }
096
097        jg.writeFieldName(NAME);
098        jg.writeStartArray();
099        try (RenderingContext.SessionWrapper wrapper = ctx.getSession(doc)) {
100            NuxeoPrincipal principal = wrapper.getSession().getPrincipal();
101            if (principal != null) {
102                List<AppLink> apps = blobProvider.getAppLinks(principal.getName(), managedBlob);
103                for (AppLink app : apps) {
104                    jg.writeObject(app);
105                }
106            }
107        }
108        jg.writeEndArray();
109    }
110
111}