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 * {@code 052 * { 053 * "name": "...", 054 * "mime-type": "...", 055 * ... 056 * "appLinks": [ 057 * { 058 * "appName": "...", 059 * "icon": "...", 060 * "link": "..." 061 * }, 062 * ... 063 * ] 064 * } 065 * } 066 * </pre> 067 * 068 * @since 10.3 069 */ 070@Setup(mode = SINGLETON, priority = REFERENCE) 071public class BlobAppLinksJsonEnricher extends AbstractJsonEnricher<BlobProperty> { 072 073 public static final String NAME = "appLinks"; 074 075 public BlobAppLinksJsonEnricher() { 076 super(NAME); 077 } 078 079 @Override 080 public void write(JsonGenerator jg, BlobProperty blobProperty) throws IOException { 081 Blob blob = (Blob) blobProperty.getValue(); 082 if (!(blob instanceof ManagedBlob)) { 083 return; 084 } 085 086 ManagedBlob managedBlob = (ManagedBlob) blob; 087 088 BlobManager blobManager = Framework.getService(BlobManager.class); 089 BlobProvider blobProvider = blobManager.getBlobProvider(managedBlob.getProviderId()); 090 if (blobProvider == null) { 091 return; 092 } 093 094 DocumentModel doc = ctx.getParameter(DocumentModelJsonWriter.ENTITY_TYPE); 095 if (doc == null) { 096 return; 097 } 098 099 jg.writeFieldName(NAME); 100 jg.writeStartArray(); 101 try (RenderingContext.SessionWrapper wrapper = ctx.getSession(doc)) { 102 NuxeoPrincipal principal = wrapper.getSession().getPrincipal(); 103 if (principal != null) { 104 List<AppLink> apps = blobProvider.getAppLinks(principal.getName(), managedBlob); 105 for (AppLink app : apps) { 106 jg.writeObject(app); 107 } 108 } 109 } 110 jg.writeEndArray(); 111 } 112 113}