@Test public void testCopy() throws Exception { MultipartBuilder builder1 = MultipartBuilder.create("stuff"); builder1.setPreamble("Hello"); builder1.setEpilogue("Bye"); builder1.addTextPart("stuff1", Charsets.UTF_8); builder1.addBinaryPart(new byte[]{1, 2, 3}, "some/stuff"); Multipart multipart = builder1.build(); MultipartBuilder builder2 = MultipartBuilder.createCopy(multipart); Assert.assertEquals("stuff", builder2.getSubType()); Assert.assertEquals("Hello", builder2.getPreamble()); Assert.assertEquals("Bye", builder2.getEpilogue()); Assert.assertEquals(2, builder2.getCount()); List<Entity> bodyParts = builder2.getBodyParts(); Assert.assertNotNull(bodyParts); Assert.assertEquals(2, bodyParts.size()); Entity entity1 = bodyParts.get(0); Assert.assertNotNull(entity1); Header header1 = entity1.getHeader(); Assert.assertNotNull(header1); List<Field> fields1 = header1.getFields(); Assert.assertNotNull(fields1); Assert.assertEquals(2, fields1.size()); ContentTypeField ct1 = header1.getField("Content-Type", ContentTypeField.class); Assert.assertEquals("text/plain", ct1.getMimeType()); Assert.assertEquals("UTF-8", ct1.getCharset()); ContentTransferEncodingField te1 = header1.getField("Content-Transfer-Encoding", ContentTransferEncodingField.class); Assert.assertEquals("quoted-printable", te1.getEncoding()); Entity entity2 = bodyParts.get(1); Assert.assertNotNull(entity2); Header header2 = entity2.getHeader(); Assert.assertNotNull(header2); List<Field> fields2 = header2.getFields(); Assert.assertNotNull(fields2); Assert.assertEquals(2, fields2.size()); ContentTypeField ct2 = header2.getField("Content-Type", ContentTypeField.class); Assert.assertEquals("some/stuff", ct2.getMimeType()); Assert.assertEquals(null, ct2.getCharset()); ContentTransferEncodingField te2 = entity2.getHeader().getField("Content-Transfer-Encoding", ContentTransferEncodingField.class); Assert.assertEquals("base64", te2.getEncoding()); }
public MultipartBuilder copy(Multipart other) { if (other == null) { return this; } subType = other.getSubType(); bodyParts.clear(); final List<Entity> otherParts = other.getBodyParts(); for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { HeaderImpl header = new HeaderImpl(); for (Field otherField : otherHeader.getFields()) { header.addField(otherField); } bodyPart.setHeader(header); } final Body otherBody = otherPart.getBody(); if (otherBody != null) { Body body = null; if (otherBody instanceof Message) { body = MessageBuilder.createCopy((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy(); } bodyPart.setBody(body); } bodyParts.add(bodyPart); } preamble = other.getPreamble(); epilogue = other.getEpilogue(); return this; }
MultipartBuilder { public MultipartBuilder copy(Multipart other) { if (other == null) { return this; } subType = other.getSubType(); bodyParts.clear(); final List<Entity> otherParts = other.getBodyParts(); for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { HeaderImpl header = new HeaderImpl(); for (Field otherField : otherHeader.getFields()) { header.addField(otherField); } bodyPart.setHeader(header); } final Body otherBody = otherPart.getBody(); if (otherBody != null) { Body body = null; if (otherBody instanceof Message) { body = MessageBuilder.createCopy((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy(); } bodyPart.setBody(body); } bodyParts.add(bodyPart); } preamble = other.getPreamble(); epilogue = other.getEpilogue(); return this; } }
MultipartBuilder { public MultipartBuilder copy(Multipart other) { if (other == null) { return this; } subType = other.getSubType(); bodyParts.clear(); final List<Entity> otherParts = other.getBodyParts(); for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { HeaderImpl header = new HeaderImpl(); for (Field otherField : otherHeader.getFields()) { header.addField(otherField); } bodyPart.setHeader(header); } final Body otherBody = otherPart.getBody(); if (otherBody != null) { Body body = null; if (otherBody instanceof Message) { body = MessageBuilder.createCopy((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy(); } bodyPart.setBody(body); } bodyParts.add(bodyPart); } preamble = other.getPreamble(); epilogue = other.getEpilogue(); return this; } private  MultipartBuilder(); }
MultipartBuilder { public MultipartBuilder copy(Multipart other) { if (other == null) { return this; } subType = other.getSubType(); bodyParts.clear(); final List<Entity> otherParts = other.getBodyParts(); for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { HeaderImpl header = new HeaderImpl(); for (Field otherField : otherHeader.getFields()) { header.addField(otherField); } bodyPart.setHeader(header); } final Body otherBody = otherPart.getBody(); if (otherBody != null) { Body body = null; if (otherBody instanceof Message) { body = MessageBuilder.createCopy((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy(); } bodyPart.setBody(body); } bodyParts.add(bodyPart); } preamble = other.getPreamble(); epilogue = other.getEpilogue(); return this; } private  MultipartBuilder(); static MultipartBuilder create(String subType); static MultipartBuilder createCopy(Multipart other); static MultipartBuilder create(); MultipartBuilder use(final BodyFactory bodyFactory); String getSubType(); MultipartBuilder setSubType(String subType); int getCount(); List<Entity> getBodyParts(); MultipartBuilder addBodyPart(Entity bodyPart); MultipartBuilder addBodyPart(BodyPartBuilder bodyPart); MultipartBuilder addBodyPart(Entity bodyPart, int index); MultipartBuilder removeBodyPart(int index); MultipartBuilder replaceBodyPart(Entity bodyPart, int index); String getPreamble(); MultipartBuilder setPreamble(String preamble); String getEpilogue(); MultipartBuilder setEpilogue(String epilogue); MultipartBuilder addContentTypeParameter(NameValuePair parameter); MultipartBuilder addTextPart(String text, Charset charset); MultipartBuilder addBinaryPart(byte[] bin, String mimeType); MultipartBuilder copy(Multipart other); Multipart build(); }
MultipartBuilder { public MultipartBuilder copy(Multipart other) { if (other == null) { return this; } subType = other.getSubType(); bodyParts.clear(); final List<Entity> otherParts = other.getBodyParts(); for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { HeaderImpl header = new HeaderImpl(); for (Field otherField : otherHeader.getFields()) { header.addField(otherField); } bodyPart.setHeader(header); } final Body otherBody = otherPart.getBody(); if (otherBody != null) { Body body = null; if (otherBody instanceof Message) { body = MessageBuilder.createCopy((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy(); } bodyPart.setBody(body); } bodyParts.add(bodyPart); } preamble = other.getPreamble(); epilogue = other.getEpilogue(); return this; } private  MultipartBuilder(); static MultipartBuilder create(String subType); static MultipartBuilder createCopy(Multipart other); static MultipartBuilder create(); MultipartBuilder use(final BodyFactory bodyFactory); String getSubType(); MultipartBuilder setSubType(String subType); int getCount(); List<Entity> getBodyParts(); MultipartBuilder addBodyPart(Entity bodyPart); MultipartBuilder addBodyPart(BodyPartBuilder bodyPart); MultipartBuilder addBodyPart(Entity bodyPart, int index); MultipartBuilder removeBodyPart(int index); MultipartBuilder replaceBodyPart(Entity bodyPart, int index); String getPreamble(); MultipartBuilder setPreamble(String preamble); String getEpilogue(); MultipartBuilder setEpilogue(String epilogue); MultipartBuilder addContentTypeParameter(NameValuePair parameter); MultipartBuilder addTextPart(String text, Charset charset); MultipartBuilder addBinaryPart(byte[] bin, String mimeType); MultipartBuilder copy(Multipart other); Multipart build();  }