Browse code

FEATWEBML-509 Fix: need fetch request to be a GET for front-end work

kladier authored on 29/11/2019 10:12:47
Showing 7 changed files
1 1
deleted file mode 100644
... ...
@@ -1,50 +0,0 @@
1
-package net.bluemind.backend.mail.api;
2
-
3
-import net.bluemind.core.api.BMApi;
4
-
5
-/**
6
- * Optional parameters when fetching a {@link MailboxItem}
7
- */
8
-@BMApi(version = "3")
9
-public class FetchOptions {
10
-
11
-	/**
12
-	 * set null to fetch pristine part
13
-	 */
14
-	public String encoding;
15
-
16
-	/**
17
-	 * override the mime type of the response
18
-	 */
19
-	public String mime;
20
-
21
-	/**
22
-	 * override the charset of the response
23
-	 */
24
-	public String charset;
25
-
26
-	/**
27
-	 * set a part name (useful for download purpose)
28
-	 */
29
-	public String filename;
30
-
31
-	public static FetchOptions of(String encoding, String mime, String charset, String filename) {
32
-		FetchOptions options = new FetchOptions();
33
-		options.encoding = encoding;
34
-		options.mime = mime;
35
-		options.charset = charset;
36
-		options.filename = filename;
37
-		return options;
38
-	}
39
-
40
-	public static FetchOptions decoded(String encoding) {
41
-		FetchOptions options = new FetchOptions();
42
-		options.encoding = encoding;
43
-		return options;
44
-	}
45
-
46
-	public static FetchOptions pristine() {
47
-		return new FetchOptions();
48
-	}
49
-
50
-}
... ...
@@ -27,6 +27,7 @@ import javax.ws.rs.PUT;
27 27
 import javax.ws.rs.Path;
28 28
 import javax.ws.rs.PathParam;
29 29
 import javax.ws.rs.Produces;
30
+import javax.ws.rs.QueryParam;
30 31
 
31 32
 import net.bluemind.backend.mail.api.MessageBody.Part;
32 33
 import net.bluemind.core.api.BMApi;
... ...
@@ -141,13 +142,18 @@ public interface IMailboxItems
141 142
 	 * 
142 143
 	 * @param imapUid
143 144
 	 * @param address
144
-	 * @param fetchOptions optional parameters
145
+	 * @param encoding set null to fetch pristine part
146
+	 * @param mime     override the mime type of the response
147
+	 * @param charset  override the charset of the response
148
+	 * @param filename set a part name (useful for download purpose)
145 149
 	 * @return a stream of the (optionally) decoded part
146 150
 	 */
147
-	@POST
151
+	@GET
148 152
 	@Path("part/{imapUid}/{address}")
149 153
 	@Produces("application/octet-stream")
150
-	Stream fetch(@PathParam("imapUid") long imapUid, @PathParam("address") String address, FetchOptions fetchOptions);
154
+	Stream fetch(@PathParam("imapUid") long imapUid, @PathParam("address") String address,
155
+			@QueryParam("encoding") String encoding, @QueryParam("mime") String mime,
156
+			@QueryParam("charset") String charset, @QueryParam("filename") String filename);
151 157
 
152 158
 	/**
153 159
 	 * @param imapUid
... ...
@@ -120,7 +120,7 @@ public class HttpFetchPartWithFilenameTests extends AbstractRollingReplicationTe
120 120
 		AsyncHttpClient httpClient = new AsyncHttpClient();
121 121
 
122 122
 		RequestBuilder requestBuilder = new RequestBuilder();
123
-		requestBuilder.setMethod("POST");
123
+		requestBuilder.setMethod("GET");
124 124
 		requestBuilder.setHeader("X-BM-ApiKey", apiKey);
125 125
 		requestBuilder.setHeader("Content-Type", "application/json");
126 126
 
... ...
@@ -130,8 +130,7 @@ public class HttpFetchPartWithFilenameTests extends AbstractRollingReplicationTe
130 130
 
131 131
 		// Don't specify any encoding
132 132
 		requestBuilder.setUrl("http://localhost:8090/api/mail_items/" + inbox.uid + "/part/" + item.value.imapUid + "/"
133
-				+ pdfAttachmentAddress);
134
-		requestBuilder.setBody("{ \"filename\": \"blabla.pdf\" }");
133
+				+ pdfAttachmentAddress + "?filename=blabla.pdf");
135 134
 		Response resp = httpClient.executeRequest(requestBuilder.build()).get(10, TimeUnit.SECONDS);
136 135
 
137 136
 		String expectedContentDisposition = "attachment; filename=\"blabla.pdf\";";
... ...
@@ -141,8 +140,7 @@ public class HttpFetchPartWithFilenameTests extends AbstractRollingReplicationTe
141 140
 
142 141
 		// Ask for an encoding
143 142
 		requestBuilder.setUrl("http://localhost:8090/api/mail_items/" + inbox.uid + "/part/" + item.value.imapUid + "/"
144
-				+ pdfAttachmentAddress);
145
-		requestBuilder.setBody("{ \"encoding\": \"base64\" , \"filename\": \"blabla.pdf\" }");
143
+				+ pdfAttachmentAddress + "?encoding=base64&filename=blabla.pdf");
146 144
 		resp = httpClient.executeRequest(requestBuilder.build()).get(10, TimeUnit.SECONDS);
147 145
 
148 146
 		assertEquals(200, resp.getStatusCode());
... ...
@@ -58,7 +58,6 @@ import com.google.common.collect.ImmutableMap;
58 58
 
59 59
 import net.bluemind.backend.cyrus.partitions.CyrusPartition;
60 60
 import net.bluemind.backend.mail.api.DispositionType;
61
-import net.bluemind.backend.mail.api.FetchOptions;
62 61
 import net.bluemind.backend.mail.api.IMailboxFolders;
63 62
 import net.bluemind.backend.mail.api.IMailboxItems;
64 63
 import net.bluemind.backend.mail.api.ImportMailboxItemSet;
... ...
@@ -526,7 +525,7 @@ public class ReplicationStackTests extends AbstractRollingReplicationTests {
526 525
 		MessageBody bodyVal = item.value.body;
527 526
 		JsonObject js = new JsonObject(JsonUtils.asString(bodyVal.structure));
528 527
 		System.out.println("Structure is " + js.encodePrettily());
529
-		Stream partStream = recordsApi.fetch(item.value.imapUid, "3", FetchOptions.pristine());
528
+		Stream partStream = recordsApi.fetch(item.value.imapUid, "3", null, null, null, null);
530 529
 		fetchPart(partStream);
531 530
 	}
532 531
 
... ...
@@ -1087,7 +1086,7 @@ public class ReplicationStackTests extends AbstractRollingReplicationTests {
1087 1086
 		PartsWalker<Void> walk = new PartsWalker<>(null);
1088 1087
 		AtomicInteger partBytes = new AtomicInteger();
1089 1088
 		walk.visit((ctx, part) -> {
1090
-			Stream fetched = itemsApi.fetch(added.value.imapUid, part.address, FetchOptions.pristine());
1089
+			Stream fetched = itemsApi.fetch(added.value.imapUid, part.address, null, null, null, null);
1091 1090
 			try {
1092 1091
 				Buffer asBuffer = fetchPart(fetched);
1093 1092
 				partBytes.addAndGet(asBuffer.length());
... ...
@@ -59,7 +59,6 @@ import com.google.common.io.ByteStreams;
59 59
 import io.netty.buffer.ByteBuf;
60 60
 import io.netty.buffer.ByteBufInputStream;
61 61
 import io.netty.buffer.Unpooled;
62
-import net.bluemind.backend.mail.api.FetchOptions;
63 62
 import net.bluemind.backend.mail.api.IMailboxItems;
64 63
 import net.bluemind.backend.mail.api.MailboxItem;
65 64
 import net.bluemind.backend.mail.api.MailboxItem.SystemFlag;
... ...
@@ -610,17 +609,17 @@ public class ImapMailboxRecordsService extends BaseMailboxRecordsService impleme
610 609
 	@Override
611 610
 	public Stream fetchComplete(long imapUid) {
612 611
 		rbac.check(Verb.Read.name());
613
-		return fetch(imapUid, "", FetchOptions.pristine());
612
+		return fetch(imapUid, "", null, null, null, null);
614 613
 	}
615 614
 
616 615
 	@Override
617
-	public Stream fetch(long imapUid, String address, FetchOptions options) {
616
+	public Stream fetch(long imapUid, String address, String encoding, String mime, String charset, String filename) {
618 617
 		rbac.check(Verb.Read.name());
619 618
 		ByteBuf downloaded = fetch(imapUid, address);
620 619
 
621 620
 		Buffer buffer = null;
622
-		if (options.encoding != null) {
623
-			try (InputStream in = dec(downloaded, options.encoding)) {
621
+		if (encoding != null) {
622
+			try (InputStream in = dec(downloaded, encoding)) {
624 623
 				buffer = new Buffer(Unpooled.wrappedBuffer(ByteStreams.toByteArray(in)));
625 624
 			} catch (Exception e) {
626 625
 				logger.error(e.getMessage(), e);
... ...
@@ -630,7 +629,7 @@ public class ImapMailboxRecordsService extends BaseMailboxRecordsService impleme
630 629
 			buffer = new Buffer(downloaded);
631 630
 		}
632 631
 
633
-		return VertxStream.stream(buffer, options.mime, options.charset, options.filename);
632
+		return VertxStream.stream(buffer, mime, charset, filename);
634 633
 	}
635 634
 
636 635
 	private InputStream dec(ByteBuf downloaded, String encoding) {
... ...
@@ -25,7 +25,6 @@ import java.util.concurrent.TimeoutException;
25 25
 
26 26
 import org.vertx.java.core.buffer.Buffer;
27 27
 
28
-import net.bluemind.backend.mail.api.FetchOptions;
29 28
 import net.bluemind.backend.mail.api.IMailboxItems;
30 29
 import net.bluemind.backend.mail.api.MailboxItem;
31 30
 import net.bluemind.core.api.Stream;
... ...
@@ -55,7 +54,7 @@ public class AttachmentLoader extends CoreConnect {
55 54
 			return null;
56 55
 		}
57 56
 
58
-		Stream partStream = service.fetch(item.value.imapUid, partAddr, FetchOptions.decoded(contentTransferEncoding));
57
+		Stream partStream = service.fetch(item.value.imapUid, partAddr, contentTransferEncoding, null, null, null);
59 58
 		CompletableFuture<Buffer> partContent = SyncStreamDownload.read(partStream);
60 59
 		Buffer part = partContent.get(10, TimeUnit.SECONDS);
61 60
 		return new FastByteInputStream(part.getBytes());
... ...
@@ -29,7 +29,6 @@ import org.apache.james.mime4j.dom.field.FieldName;
29 29
 import org.slf4j.Logger;
30 30
 import org.slf4j.LoggerFactory;
31 31
 
32
-import net.bluemind.backend.mail.api.FetchOptions;
33 32
 import net.bluemind.backend.mail.api.IMailboxItems;
34 33
 import net.bluemind.backend.mail.api.MailboxItem;
35 34
 import net.bluemind.backend.mail.api.MessageBody.Header;
... ...
@@ -110,8 +109,8 @@ public class BodyMailLoader extends CoreConnect {
110 109
 			bodyAccumulator.consumeMime(content);
111 110
 		} else {
112 111
 			Part bodyPart = bodyPart(item.value.body.structure);
113
-			Stream content = service.fetch(item.value.imapUid, bodyPart.address,
114
-					FetchOptions.of(bodyPart.encoding, bodyPart.mime, bodyPart.charset, null));
112
+			Stream content = service.fetch(item.value.imapUid, bodyPart.address, bodyPart.encoding, bodyPart.mime,
113
+					bodyPart.charset, null);
115 114
 			bodyAccumulator.consumeBodyPart(bodyPart, content);
116 115
 		}
117 116