Various small fixes for beacon exchanges

This commit is contained in:
Christopher Schnick 2022-12-25 10:19:18 +01:00
parent 5ce39cf11e
commit 560efbd345
9 changed files with 57 additions and 21 deletions

View file

@ -1,5 +1,6 @@
package io.xpipe.beacon;
import io.xpipe.beacon.exchange.WriteStreamExchange;
import io.xpipe.beacon.exchange.cli.StoreAddExchange;
import io.xpipe.beacon.util.QuietDialogHandler;
import io.xpipe.core.impl.InternalStreamStore;
@ -174,13 +175,26 @@ public abstract class BeaconConnection implements AutoCloseable {
}
public InternalStreamStore createInternalStreamStore() {
return createInternalStreamStore(null);
}
public InternalStreamStore createInternalStreamStore(String name) {
var store = new InternalStreamStore();
var addReq = StoreAddExchange.Request.builder().storeInput(store).name(store.getUuid().toString()).build();
var addReq = StoreAddExchange.Request.builder().storeInput(store).name(name != null ? name : store.getUuid().toString()).build();
StoreAddExchange.Response addRes = performSimpleExchange(addReq);
QuietDialogHandler.handle(addRes.getConfig(), this);
return store;
}
public void writeStream(InternalStreamStore s, InputStream in) {
writeStream(s.getUuid().toString(), in);
}
public void writeStream(String name, InputStream in) {
performOutputExchange(
WriteStreamExchange.Request.builder().name(name).build(), in::transferTo);
}
private BeaconException unwrapException(Exception exception) {
if (exception instanceof ServerException s) {
return new BeaconException("An internal server error occurred", s);

View file

@ -21,8 +21,6 @@ public class RemoveStoreExchange implements MessageExchange {
public static class Request implements RequestMessage {
@NonNull
String storeName;
boolean removeUnderlying;
}
@Jacksonized

View file

@ -10,6 +10,7 @@ import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.util.List;
import java.util.Map;
public class StoreProviderListExchange implements MessageExchange {
@ -28,6 +29,6 @@ public class StoreProviderListExchange implements MessageExchange {
@Value
public static class Response implements ResponseMessage {
@NonNull
List<ProviderEntry> entries;
Map<String, List<ProviderEntry>> entries;
}
}

View file

@ -30,10 +30,14 @@ public class WriteExecuteExchange implements MessageExchange {
@NonNull
UUID id;
String mode;
}
@Jacksonized
@Builder
@Value
public static class Response implements ResponseMessage {}
public static class Response implements ResponseMessage {
boolean hasBody;
}
}

View file

@ -27,8 +27,9 @@ public class WritePreparationExchange implements MessageExchange {
public static class Request implements RequestMessage {
String type;
@NonNull
DataStore output;
DataStore outputStore;
DataSourceReference outputSource;
@NonNull
DataSourceReference source;
@ -38,8 +39,6 @@ public class WritePreparationExchange implements MessageExchange {
@Builder
@Value
public static class Response implements ResponseMessage {
boolean hasBody;
@NonNull
DialogReference config;
}

View file

@ -18,6 +18,18 @@ public class FileNames {
return normalize(joined);
}
public static boolean isAbsolute(String file) {
if (!file.contains("/") && !file.contains("\\")) {
return false;
}
if (!file.startsWith("/") && !file.startsWith("~") && !file.matches("^\\w:.*")) {
return false;
}
return true;
}
public static String getParent(String file) {
return file.substring(0, file.length() - getFileName(file).length() - 1);
}

View file

@ -64,6 +64,9 @@ public class FileStore extends JacksonizedValue implements FilenameStore, Stream
if (file == null) {
throw new IllegalStateException("File is missing");
}
if (!FileNames.isAbsolute(file)) {
throw new IllegalStateException("File path is not absolute");
}
}
@Override

View file

@ -14,28 +14,32 @@ public class WriteMode extends JacksonizedValue {
public static void init(ModuleLayer layer) {
if (ALL.size() == 0) {
ALL.addAll(ServiceLoader.load(layer, WriteMode.class).stream()
.map(p -> p.get())
.toList());
.map(p -> p.get())
.toList());
}
}
@JsonTypeName("replace")
public static final class Replace extends WriteMode {
}
public static final class Replace extends WriteMode {}
@JsonTypeName("append")
public static final class Append extends WriteMode {
}
public static final class Append extends WriteMode {}
@JsonTypeName("prepend")
public static final class Prepend extends WriteMode {
}
public static final class Prepend extends WriteMode {}
public static final Replace REPLACE = new Replace();
public static final Append APPEND = new Append();
public static final Prepend PREPEND = new Prepend();
public final String getId() {
public final String getId() {
return getClass().getAnnotation(JsonTypeName.class).value();
}
public static WriteMode byId(String id) {
return ALL.stream()
.filter(writeMode -> writeMode.getId().equalsIgnoreCase(id))
.findFirst()
.orElseThrow();
}
}

View file

@ -112,13 +112,14 @@ public class TrackEvent {
if (tags.size() > 0) {
s.append(" {\n");
for (var e : tags.entrySet()) {
var value = e.toString().contains("\n")
var valueString = e.getValue() != null ? e.getValue().toString() : "null";
var value = valueString.contains("\n")
? "\n"
+ (e.toString()
+ (valueString.toString()
.lines()
.map(line -> " | " + line)
.collect(Collectors.joining("\n")))
: e.toString();
: valueString;
s.append(" ")
.append(e.getKey())
.append("=")