This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): new endpoints to upload and download attachments
- Introduce AttachmentFrontendUtils - Add attachment download endpoints for rest - Add attachment upload endpoints for rest Signed-off-by: Maximilian Huber <external.maximilian.huber2@bosch-si.com>
- Loading branch information
Showing
14 changed files
with
628 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
libraries/commonIO/src/main/java/org/eclipse/sw360/commonIO/AttachmentFrontendUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright Bosch Software Innovations GmbH, 2018. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* SPDX-License-Identifier: EPL-1.0 | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.sw360.commonIO; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.datahandler.common.CommonUtils; | ||
import org.eclipse.sw360.datahandler.common.Duration; | ||
import org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector; | ||
import org.eclipse.sw360.datahandler.thrift.SW360Exception; | ||
import org.eclipse.sw360.datahandler.thrift.ThriftClients; | ||
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment; | ||
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent; | ||
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentService; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class AttachmentFrontendUtils { | ||
|
||
private static final Logger log = Logger.getLogger(AttachmentFrontendUtils.class); | ||
protected final AttachmentService.Iface client; | ||
|
||
private AttachmentStreamConnector connector; | ||
// TODO add Config class and DI | ||
private final Duration downloadTimeout = Duration.durationOf(30, TimeUnit.SECONDS); | ||
|
||
public AttachmentFrontendUtils() { | ||
this(new ThriftClients()); | ||
} | ||
|
||
public AttachmentFrontendUtils(ThriftClients thriftClients) { | ||
client = thriftClients.makeAttachmentClient(); | ||
} | ||
|
||
public InputStream getStreamToServeAFile(Collection<AttachmentContent> attachments, User user, Object context) | ||
throws TException, IOException { | ||
if(attachments == null || attachments.size() == 0){ | ||
throw new SW360Exception("Tried to download empty set of Attachments"); | ||
}else if(attachments.size() == 1){ | ||
return getConnector().getAttachmentStream(attachments.stream().findAny().get(), user, context); | ||
} else { | ||
return getConnector().getAttachmentBundleStream(new HashSet<>(attachments), user, context); | ||
} | ||
} | ||
|
||
private synchronized void makeConnector() throws TException { | ||
if (connector == null) { | ||
try { | ||
connector = new AttachmentStreamConnector(downloadTimeout); | ||
} catch (MalformedURLException e) { | ||
log.error("Invalid database address received...", e); | ||
throw new TException(e); | ||
} | ||
} | ||
} | ||
|
||
public AttachmentStreamConnector getConnector() throws TException { | ||
if (connector == null) makeConnector(); | ||
return connector; | ||
} | ||
|
||
public AttachmentContent getAttachmentContent(String id) throws TException { | ||
return client.getAttachmentContent(id); | ||
} | ||
|
||
public AttachmentContent makeAttachmentContent(AttachmentContent attachmentContent) throws TException { | ||
return client.makeAttachmentContent(attachmentContent); | ||
} | ||
|
||
public Attachment getAttachmentForDisplay(User user, String attachmentContentId) { | ||
try { | ||
String filename = getAttachmentContent(attachmentContentId).getFilename(); | ||
return CommonUtils.getNewAttachment(user, attachmentContentId, filename); | ||
} catch (TException e) { | ||
log.error("Could not get attachment content", e); | ||
} | ||
return null; | ||
} | ||
|
||
public void deleteAttachments(Set<String> attachmentContentIds){ | ||
try { | ||
for(String id: attachmentContentIds) { | ||
client.deleteAttachmentContent(id); | ||
} | ||
} catch (TException e){ | ||
log.error("Could not delete attachments from database.",e); | ||
} | ||
} | ||
|
||
|
||
public Attachment uploadAttachmentContent(AttachmentContent attachmentContent, InputStream fileStream, User sw360User) | ||
throws TException { | ||
final AttachmentStreamConnector attachmentStreamConnector = getConnector(); | ||
if (attachmentContent != null) { | ||
try { | ||
attachmentStreamConnector.uploadAttachment(attachmentContent, fileStream); | ||
return CommonUtils.getNewAttachment(sw360User, attachmentContent.getId(), attachmentContent.getFilename()); | ||
} catch (TException e) { | ||
log.error("Error saving attachment part", e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
protected AttachmentContent updateAttachmentContent(AttachmentContent attachment) throws TException { | ||
try { | ||
client.updateAttachmentContent(attachment); | ||
} catch (SW360Exception e) { | ||
log.error("Error updating attachment", e); | ||
return null; | ||
} | ||
return attachment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.