-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(document): document deserialization & serialization (#3066)
* wip (cherry picked from commit a57e940) * wip (cherry picked from commit 5634aea) * prepare end-to-end draft (cherry picked from commit 92ec79f) * prepare end-to-end draft (cherry picked from commit 5a808bd) * rework deserializers (cherry picked from commit 0b0a558) * incorporate feedback, move jackson code into a separate module * clean up accidental change * naming adjustments * lint * remove extra dollar sign * remove extra dollar sign
- Loading branch information
1 parent
694c204
commit 1ed03da
Showing
46 changed files
with
1,649 additions
and
211 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
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
70 changes: 70 additions & 0 deletions
70
...untime-core/src/main/java/io/camunda/connector/runtime/core/document/CamundaDocument.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,70 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; 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 io.camunda.connector.runtime.core.document; | ||
|
||
import io.camunda.connector.api.document.Document; | ||
import io.camunda.connector.api.document.DocumentMetadata; | ||
import io.camunda.connector.api.document.DocumentReference; | ||
import io.camunda.connector.api.document.DocumentReference.CamundaDocumentReference; | ||
import io.camunda.connector.api.document.store.CamundaDocumentStore; | ||
import java.io.InputStream; | ||
import java.util.Base64; | ||
|
||
public class CamundaDocument implements Document { | ||
|
||
private final DocumentMetadata metadata; | ||
private final CamundaDocumentReference reference; | ||
private final CamundaDocumentStore documentStore; | ||
|
||
public CamundaDocument( | ||
DocumentMetadata metadata, | ||
CamundaDocumentReference reference, | ||
CamundaDocumentStore documentStore) { | ||
this.metadata = metadata; | ||
this.reference = reference; | ||
this.documentStore = documentStore; | ||
} | ||
|
||
@Override | ||
public DocumentMetadata metadata() { | ||
return metadata; | ||
} | ||
|
||
@Override | ||
public String asBase64() { | ||
return Base64.getEncoder().encodeToString(asByteArray()); | ||
} | ||
|
||
@Override | ||
public InputStream asInputStream() { | ||
return documentStore.getDocumentContent(reference); | ||
} | ||
|
||
@Override | ||
public byte[] asByteArray() { | ||
try { | ||
return documentStore.getDocumentContent(reference).readAllBytes(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to read document content", e); | ||
} | ||
} | ||
|
||
@Override | ||
public DocumentReference reference() { | ||
return reference; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/io/camunda/connector/runtime/core/document/CamundaDocumentReferenceImpl.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,24 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; 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 io.camunda.connector.runtime.core.document; | ||
|
||
import io.camunda.connector.api.document.DocumentMetadata; | ||
import io.camunda.connector.api.document.DocumentReference; | ||
|
||
public record CamundaDocumentReferenceImpl( | ||
String storeId, String documentId, DocumentMetadata metadata) | ||
implements DocumentReference.CamundaDocumentReference {} |
51 changes: 51 additions & 0 deletions
51
...me-core/src/main/java/io/camunda/connector/runtime/core/document/DocumentFactoryImpl.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,51 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; 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 io.camunda.connector.runtime.core.document; | ||
|
||
import io.camunda.connector.api.document.Document; | ||
import io.camunda.connector.api.document.DocumentFactory; | ||
import io.camunda.connector.api.document.DocumentReference; | ||
import io.camunda.connector.api.document.DocumentReference.CamundaDocumentReference; | ||
import io.camunda.connector.api.document.DocumentReference.ExternalDocumentReference; | ||
import io.camunda.connector.api.document.store.CamundaDocumentStore; | ||
import io.camunda.connector.api.document.store.DocumentCreationRequest; | ||
|
||
public class DocumentFactoryImpl implements DocumentFactory { | ||
|
||
private final CamundaDocumentStore documentStore; | ||
|
||
public DocumentFactoryImpl(CamundaDocumentStore documentStore) { | ||
this.documentStore = documentStore; | ||
} | ||
|
||
@Override | ||
public Document resolve(DocumentReference reference) { | ||
return switch (reference) { | ||
case CamundaDocumentReference camundaDocumentReference -> new CamundaDocument( | ||
camundaDocumentReference.metadata(), camundaDocumentReference, documentStore); | ||
case ExternalDocumentReference ignored -> throw new IllegalArgumentException( | ||
"External document references are not yet supported: " + reference.getClass()); | ||
default -> throw new IllegalArgumentException("Unsupported document reference: " + reference); | ||
}; | ||
} | ||
|
||
@Override | ||
public Document create(DocumentCreationRequest request) { | ||
var reference = documentStore.createDocument(request); | ||
return resolve(reference); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...-core/src/main/java/io/camunda/connector/runtime/core/document/InMemoryDocumentStore.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,63 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; 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 io.camunda.connector.runtime.core.document; | ||
|
||
import io.camunda.connector.api.document.DocumentMetadata; | ||
import io.camunda.connector.api.document.DocumentReference.CamundaDocumentReference; | ||
import io.camunda.connector.api.document.store.CamundaDocumentStore; | ||
import io.camunda.connector.api.document.store.DocumentCreationRequest; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class InMemoryDocumentStore implements CamundaDocumentStore { | ||
|
||
public static final String STORE_ID = "in-memory"; | ||
|
||
private final Map<String, byte[]> documents = new HashMap<>(); | ||
|
||
@Override | ||
public CamundaDocumentReference createDocument(DocumentCreationRequest request) { | ||
final String id = | ||
request.documentId() != null ? request.documentId() : UUID.randomUUID().toString(); | ||
final DocumentMetadata metadata = request.metadata(); | ||
final byte[] content; | ||
try (InputStream contentStream = request.content()) { | ||
content = contentStream.readAllBytes(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to read document content", e); | ||
} | ||
documents.put(id, content); | ||
return new CamundaDocumentReferenceImpl(STORE_ID, id, metadata); | ||
} | ||
|
||
@Override | ||
public InputStream getDocumentContent(CamundaDocumentReference reference) { | ||
var content = documents.get(reference.documentId()); | ||
if (content == null) { | ||
throw new RuntimeException("Document not found: " + reference.documentId()); | ||
} | ||
return new ByteArrayInputStream(content); | ||
} | ||
|
||
@Override | ||
public void deleteDocument(CamundaDocumentReference reference) { | ||
documents.remove(reference.documentId()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...e/src/main/java/io/camunda/connector/runtime/core/document/operation/Base64Operation.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,34 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; 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 io.camunda.connector.runtime.core.document.operation; | ||
|
||
import io.camunda.connector.api.document.Document; | ||
import io.camunda.connector.api.document.operation.DocumentOperation; | ||
import io.camunda.connector.api.document.operation.DocumentOperationExecutor; | ||
|
||
public class Base64Operation implements DocumentOperationExecutor { | ||
|
||
@Override | ||
public boolean matches(DocumentOperation operationReference) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object execute(DocumentOperation operationReference, Document document) { | ||
return null; | ||
} | ||
} |
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.