blob: d2196c6c642075d9087b203677ebb230bb0fa648 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2019 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.control;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.eclipse.mdm.api.base.file.FileService;
import org.eclipse.mdm.api.base.model.Entity;
import org.eclipse.mdm.api.base.model.FileLink;
import org.eclipse.mdm.api.base.query.DataAccessException;
import org.eclipse.mdm.api.dflt.ApplicationContext;
import org.eclipse.mdm.connector.boundary.ConnectorService;
import com.google.common.io.ByteStreams;
@Stateless
public class FileLinkActivity {
@Inject
private ConnectorService connectorService;
public void streamFileLink(String sourceName, Entity entity, FileLink fileLink, OutputStream outputStream) {
try {
ApplicationContext context = this.connectorService.getContextByName(sourceName);
FileService fileService = context.getFileService().orElseThrow(
() -> new MDMEntityAccessException("FileService not present in '" + sourceName + "'."));
try (InputStream in = fileService.openStream(entity, fileLink)) {
ByteStreams.copy(in, outputStream);
}
} catch (DataAccessException e) {
throw new MDMEntityAccessException(e.getMessage(), e);
} catch (IOException e) {
throw new MDMEntityAccessException(e.getMessage(), e);
}
}
}