Skip to content

Commit

Permalink
Add creation of attachments for CouchDB (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenO3 authored Jun 27, 2023
1 parent 117af96 commit f3fd380
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,6 +103,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<String, Object> queryDocuments(String route) throws IOException {
Request req = Utils.getRequest(route);
Content content = executeAndReturnContent(req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,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();
}
Expand Down

0 comments on commit f3fd380

Please sign in to comment.