From 9a5c847c98384323bc36bea00e04c72154bed85a Mon Sep 17 00:00:00 2001 From: Sven Oehler Date: Tue, 20 Jun 2023 17:32:05 +0200 Subject: [PATCH] Add creation of attachments for CouchDB --- .../model/file/GenericStorageAttachment.java | 44 +++++++++++++++++++ .../storage/api/IGenericStorage.java | 7 +++ .../couchdb/impl/GenericStorageImpl.java | 20 +++++++++ .../storage/couchdb/utils/Utils.java | 6 +++ 4 files changed, 77 insertions(+) create mode 100644 streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java b/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java new file mode 100644 index 0000000000..54c7f44fa3 --- /dev/null +++ b/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.streampipes.model.file; + +public class GenericStorageAttachment { + String contentType; + byte[] attachment; + + public GenericStorageAttachment(String contentType, byte[] attachment) { + this.contentType = contentType; + this.attachment = attachment; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public byte[] getAttachment() { + return attachment; + } + + public void setAttachment(byte[] attachment) { + this.attachment = attachment; + } +} diff --git a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java b/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java index b74bbc5b34..528e8c93b8 100644 --- a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java +++ b/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java @@ -18,6 +18,8 @@ package org.apache.streampipes.storage.api; +import org.apache.streampipes.model.file.GenericStorageAttachment; + import java.io.IOException; import java.util.List; import java.util.Map; @@ -35,4 +37,9 @@ public interface IGenericStorage { Map update(String id, String payload) throws IOException; void delete(String id, String rev) throws IOException; + + void createAttachment(String docId, String attachmentName, String contentType, byte[] payload, String rev) + throws IOException; + + GenericStorageAttachment findAttachment(String docId, String attachmentName) throws IOException; } diff --git a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java index 0817e52299..bf2936d0b3 100644 --- a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java +++ b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java @@ -18,6 +18,7 @@ package org.apache.streampipes.storage.couchdb.impl; +import org.apache.streampipes.model.file.GenericStorageAttachment; import org.apache.streampipes.storage.api.IGenericStorage; import org.apache.streampipes.storage.couchdb.constants.GenericCouchDbConstants; import org.apache.streampipes.storage.couchdb.utils.Utils; @@ -103,6 +104,25 @@ public void delete(String id, String rev) throws IOException { Content content = executeAndReturnContent(req); } + @Override + public void createAttachment(String docId, + String attachmentName, + String contentType, + byte[] payload, + String rev) throws IOException { + Request req = Utils.putRequest( + getDatabaseRoute() + SLASH + docId + SLASH + attachmentName + "?rev=" + rev, payload, contentType + ); + executeAndReturnContent(req); + } + + @Override + public GenericStorageAttachment findAttachment(String docId, String attachmentName) throws IOException { + Request req = Utils.getRequest(getDatabaseRoute() + SLASH + docId + SLASH + attachmentName); + Content content = executeAndReturnContent(req); + return new GenericStorageAttachment(content.getType().getMimeType(), content.asBytes()); + } + private Map queryDocuments(String route) throws IOException { Request req = Utils.getRequest(route); Content content = executeAndReturnContent(req); diff --git a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java index ba4356f0f0..36dbe20296 100644 --- a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java +++ b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java @@ -216,6 +216,12 @@ public static Request putRequest(String route, return append(Request.Put(route).bodyString(payload, ContentType.APPLICATION_JSON)); } + public static Request putRequest(String route, + byte[] payload, + String contentType) { + return append(Request.Put(route).bodyByteArray(payload, ContentType.getByMimeType(contentType))); + } + private static Environment getEnvironment() { return Environments.getEnvironment(); }