summaryrefslogtreecommitdiffstats
path: root/vespa-maven-plugin
diff options
context:
space:
mode:
authorJon Marius Venstad <jvenstad@yahoo-inc.com>2019-04-29 09:39:23 +0200
committerJon Marius Venstad <jvenstad@yahoo-inc.com>2019-04-29 09:39:23 +0200
commit9b2cccc9a3c3c919ed56c0bd65ca652a797bd4a1 (patch)
tree8dcedb06b6f9dd3f6c3dd12009ff7c2592577abc /vespa-maven-plugin
parent1749fc5eab95388e1944e42fd3110546d8c46ba3 (diff)
Move API things to new module: hosted-api
Diffstat (limited to 'vespa-maven-plugin')
-rw-r--r--vespa-maven-plugin/pom.xml16
-rw-r--r--vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/Method.java16
-rw-r--r--vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java144
-rw-r--r--vespa-maven-plugin/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java69
4 files changed, 2 insertions, 243 deletions
diff --git a/vespa-maven-plugin/pom.xml b/vespa-maven-plugin/pom.xml
index b9bff8ee342..4516c57f32a 100644
--- a/vespa-maven-plugin/pom.xml
+++ b/vespa-maven-plugin/pom.xml
@@ -4,17 +4,15 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
- <groupId>ai.vespa.hosted</groupId>
- <artifactId>vespa-maven-plugin</artifactId>
<parent>
<groupId>com.yahoo.vespa</groupId>
<artifactId>parent</artifactId>
<version>7-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
- <version>7-SNAPSHOT</version>
- <packaging>maven-plugin</packaging>
+ <artifactId>vespa-maven-plugin</artifactId>
<description>Maven Plugin for deploying a Vespa application package</description>
+ <packaging>maven-plugin</packaging>
<prerequisites>
<maven>3.5.0</maven>
@@ -22,16 +20,6 @@
<dependencies>
<dependency>
- <groupId>com.yahoo.vespa</groupId>
- <artifactId>config-provisioning</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>com.yahoo.vespa</groupId>
- <artifactId>vespajlib</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/Method.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/Method.java
deleted file mode 100644
index ff7c1e4270b..00000000000
--- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/Method.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package ai.vespa.hosted.api;
-
-/**
- * HTTP methods.
- *
- * @author jonmv
- */
-public enum Method {
-
- GET,
- PUT,
- POST,
- PATCH,
- DELETE;
-
-}
diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java
deleted file mode 100644
index e42df52ca3f..00000000000
--- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/api/MultiPartStreamer.java
+++ /dev/null
@@ -1,144 +0,0 @@
-package ai.vespa.hosted.api;
-
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.SequenceInputStream;
-import java.io.UncheckedIOException;
-import java.net.http.HttpRequest;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
-import java.util.function.Supplier;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-/**
- * Used to create builders for multi part http body entities, which stream their data.
- *
- * @author jonmv
- */
-public class MultiPartStreamer {
-
- private final String boundary;
- private final List<Supplier<InputStream>> streams;
-
- MultiPartStreamer(String boundary) {
- this.boundary = boundary;
- this.streams = new ArrayList<>();
- }
-
- /** Creates a new MultiPartBodyStreamer in which parts can be aggregated, and then streamed. */
- public MultiPartStreamer() {
- this(UUID.randomUUID().toString());
- }
-
- /** Adds the given data as a named part in this, using the {@code "text/plain"} content type. */
- public MultiPartStreamer addText(String name, String json) {
- return addData(name, "text/plain", json);
- }
-
- /** Adds the given data as a named part in this, using the {@code "application/json"} content type. */
- public MultiPartStreamer addJson(String name, String json) {
- return addData(name, "application/json", json);
- }
-
- /** Adds the given data as a named part in this, using the given content type. */
- public MultiPartStreamer addData(String name, String type, String data) {
- streams.add(() -> separator(name, type));
- streams.add(() -> asStream(data));
-
- return this;
- }
-
- /** Adds the contents of the file at the given path as a named part in this. */
- public MultiPartStreamer addFile(String name, Path path) {
- streams.add(() -> separator(name, path));
- streams.add(() -> asStream(path));
-
- return this;
- }
-
- /**
- * Returns a builder whose data is an aggregate stream of the current parts of this.
- * Modifications to this streamer after a request builder has been obtained is not reflected in that builder.
- * This method can be used multiple times, to create new requests.
- * The request builder's method and content should not be set after it has been obtained.
- */
- public HttpRequest.Builder requestBuilder(Method method) {
- InputStream aggregate = data(); // Get the streams now, not when the aggregate is used.
- return HttpRequest.newBuilder()
- .setHeader("Content-Type", "multipart/form-data; boundary=" + boundary)
- .method(method.name(), HttpRequest.BodyPublishers.ofInputStream(() -> aggregate));
- }
-
- /** Returns an input stream which is an aggregate of all current parts in this, plus an end marker. */
- public InputStream data() {
- InputStream aggregate = new SequenceInputStream(Collections.enumeration(Stream.concat(streams.stream().map(Supplier::get),
- Stream.of(end()))
- .collect(Collectors.toList())));
-
- try {
- if (aggregate.skip(2) != 2)// This should never happen, as the first stream is a ByteArrayInputStream.
- throw new IllegalStateException("Failed skipping extraneous bytes.");
- }
- catch (IOException e) { // This should never happen, as the first stream is a ByteArrayInputStream;
- throw new IllegalStateException("Failed skipping extraneous bytes.", e);
- }
- return new BufferedInputStream(aggregate);
- }
-
- /** Returns the separator to put between one part and the next, when this is a string. */
- private InputStream separator(String name, String contentType) {
- return asStream(disposition(name) + type(contentType));
- }
-
- /** Returns the separator to put between one part and the next, when this is a file. */
- private InputStream separator(String name, Path path) {
- try {
- String contentType = Files.probeContentType(path);
- return asStream(disposition(name) + "; filename=\"" + path.getFileName() + "\"" +
- type(contentType != null ? contentType : "application/octet-stream"));
- }
- catch (IOException e) {
- throw new UncheckedIOException(e);
- }
- }
-
- /** Returns the end delimiter of the request, with line breaks prepended. */
- private InputStream end() {
- return asStream("\r\n--" + boundary + "--");
- }
-
- /** Returns the boundary and disposition header for a part, with line breaks prepended. */
- private String disposition(String name) {
- return "\r\n--" + boundary + "\r\n" +
- "Content-Disposition: form-data; name=\"" + name + "\"";
- }
-
- /** Returns the content type header for a part, with line breaks pre- and appended. */
- private String type(String contentType) {
- return "\r\nContent-Type: " + contentType + "\r\n\r\n";
- }
-
- /** Returns the a ByteArrayInputStream over the given string, UTF-8 encoded. */
- private static InputStream asStream(String string) {
- return new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
- }
-
- /** Returns an InputStream over the file at the given path — rethrows any IOException as UncheckedIOException. */
- private InputStream asStream(Path path) {
- try {
- return Files.newInputStream(path);
- }
- catch (IOException e) {
- throw new UncheckedIOException(e);
- }
- }
-
-}
diff --git a/vespa-maven-plugin/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java
deleted file mode 100644
index 3abb04976c1..00000000000
--- a/vespa-maven-plugin/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java
+++ /dev/null
@@ -1,69 +0,0 @@
-package ai.vespa.hosted.api;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-
-import java.io.IOException;
-import java.net.URI;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-
-public class MultiPartStreamerTest {
-
- @Rule
- public TemporaryFolder tmp = new TemporaryFolder();
-
- @Test
- public void test() throws IOException {
- Path file = tmp.newFile().toPath();
- Files.write(file, new byte[]{0x48, 0x69});
- MultiPartStreamer streamer = new MultiPartStreamer("My boundary");
-
- assertEquals("--My boundary--",
- new String(streamer.data().readAllBytes()));
-
- streamer.addData("data", "uss/enterprise", "lore")
- .addJson("json", "{\"xml\":false}")
- .addText("text", "Hello!")
- .addFile("file", file);
-
- String expected = "--My boundary\r\n" +
- "Content-Disposition: form-data; name=\"data\"\r\n" +
- "Content-Type: uss/enterprise\r\n" +
- "\r\n" +
- "lore\r\n" +
- "--My boundary\r\n" +
- "Content-Disposition: form-data; name=\"json\"\r\n" +
- "Content-Type: application/json\r\n" +
- "\r\n" +
- "{\"xml\":false}\r\n" +
- "--My boundary\r\n" +
- "Content-Disposition: form-data; name=\"text\"\r\n" +
- "Content-Type: text/plain\r\n" +
- "\r\n" +
- "Hello!\r\n" +
- "--My boundary\r\n" +
- "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getFileName() + "\"\r\n" +
- "Content-Type: application/octet-stream\r\n" +
- "\r\n" +
- "Hi\r\n" +
- "--My boundary--";
-
- assertEquals(expected,
- new String(streamer.data().readAllBytes()));
-
- // Verify that all data is read again for a new builder.
- assertEquals(expected,
- new String(streamer.data().readAllBytes()));
-
- assertEquals(List.of("multipart/form-data; boundary=My boundary"),
- streamer.requestBuilder(Method.POST)
- .uri(URI.create("https://uri/path"))
- .build().headers().allValues("Content-Type"));
- }
-
-}