| /******************************************************************************** |
| * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ********************************************************************************/ |
| |
| package org.eclipse.mdm.businessobjects.service; |
| |
| import java.io.IOException; |
| import java.io.OutputStream; |
| |
| import javax.ws.rs.WebApplicationException; |
| import javax.ws.rs.core.StreamingOutput; |
| |
| import org.eclipse.mdm.api.base.model.FileLink; |
| import org.eclipse.mdm.api.base.model.FilesAttachable; |
| import org.eclipse.mdm.businessobjects.control.FileLinkActivity; |
| import org.eclipse.mdm.businessobjects.control.MDMEntityAccessException; |
| |
| public class EntityFileLink { |
| private FilesAttachable entity; |
| private FileLink fileLink; |
| |
| public EntityFileLink(FilesAttachable entity, String remotePath) { |
| this.entity = entity; |
| this.fileLink = findFileLink(remotePath); |
| } |
| |
| public FilesAttachable getEntity() { |
| return entity; |
| } |
| |
| public FileLink getFileLink() { |
| return fileLink; |
| } |
| |
| public StreamingOutput toStreamingOutput(FileLinkActivity fileLinkActivity, String sourceName) { |
| return new StreamingOutput() { |
| @Override |
| public void write(OutputStream output) throws IOException, WebApplicationException { |
| fileLinkActivity.streamFileLink(sourceName, entity, fileLink, output); |
| } |
| }; |
| } |
| |
| private FileLink findFileLink(String remotePath) { |
| for (FileLink l : entity.getFileLinks()) { |
| if (l.isRemote() && l.getRemotePath().equals(remotePath)) { |
| return l; |
| } |
| } |
| throw new MDMEntityAccessException("FileLink with remotePath " + remotePath + " not found!"); |
| } |
| } |