This is an automated email from the ASF dual-hosted git repository.
riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git
The following commit(s) were added to refs/heads/dev by this push:
new f3fd3802e Add creation of attachments for CouchDB (#1716)
f3fd3802e is described below
commit f3fd3802ec39aa9cc2576666811a1d69beec8755
Author: Sven Oehler <[email protected]>
AuthorDate: Tue Jun 27 14:46:16 2023 +0200
Add creation of attachments for CouchDB (#1716)
---
.../model/file/GenericStorageAttachment.java | 34 +++++++++++++---------
.../streampipes/storage/api/IGenericStorage.java | 7 +++++
.../storage/couchdb/impl/GenericStorageImpl.java | 20 +++++++++++++
.../streampipes/storage/couchdb/utils/Utils.java | 6 ++++
4 files changed, 53 insertions(+), 14 deletions(-)
diff --git
a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
b/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java
similarity index 58%
copy from
streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
copy to
streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java
index b74bbc5b3..54c7f44fa 100644
---
a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
+++
b/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java
@@ -15,24 +15,30 @@
* limitations under the License.
*
*/
+package org.apache.streampipes.model.file;
-package org.apache.streampipes.storage.api;
+public class GenericStorageAttachment {
+ String contentType;
+ byte[] attachment;
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
+ public GenericStorageAttachment(String contentType, byte[] attachment) {
+ this.contentType = contentType;
+ this.attachment = attachment;
+ }
-public interface IGenericStorage {
+ public String getContentType() {
+ return contentType;
+ }
- List<Map<String, Object>> findAll(String type) throws IOException;
+ public void setContentType(String contentType) {
+ this.contentType = contentType;
+ }
- Map<String, Object> findOne(String id) throws IOException;
+ public byte[] getAttachment() {
+ return attachment;
+ }
- Map<String, Object> create(String payload) throws IOException;
-
- <T> T create(T payload, Class<T> targetClass) throws IOException;
-
- Map<String, Object> update(String id, String payload) throws IOException;
-
- void delete(String id, String rev) throws IOException;
+ 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 b74bbc5b3..528e8c93b 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<String, Object> 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 c0f814b58..6d5884ee4 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;
@@ -102,6 +103,25 @@ public class GenericStorageImpl implements IGenericStorage
{
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<String, Object> 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 4ec1b20a2..125270494 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
@@ -221,6 +221,12 @@ public class Utils {
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();
}