From 36c303c22d2732437956c6f5d3aaf501aa265db0 Mon Sep 17 00:00:00 2001 From: Imran M Yousuf Date: Wed, 25 Mar 2009 13:50:00 +0600 Subject: [PATCH] Fix content disposition construction with null name argument Despite 2 constructors ensuring that 'name' is a mandatory parameter the all arguments constructor does not respect it and the builder also does not respect it. This change fixes this behavior and ensures conherence. This builder was internally being used from FormDataBodyPart where if null name was being set it used to create a content disposition which was not supposed to be there. This change also fixes that and encapsulates the build statements from exceptions in the builder. Signed-off-by: Imran M Yousuf --- .../com/sun/jersey/multipart/FormDataBodyPart.java | 8 +++++++- .../sun/jersey/multipart/FormDataBodyPartTest.java | 8 ++++++++ .../core/header/FormDataContentDisposition.java | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletions(-) diff --git a/contribs/jersey-multipart/src/main/java/com/sun/jersey/multipart/FormDataBodyPart.java b/contribs/jersey-multipart/src/main/java/com/sun/jersey/multipart/FormDataBodyPart.java index 3b9317b..5d6e966 100644 --- a/contribs/jersey-multipart/src/main/java/com/sun/jersey/multipart/FormDataBodyPart.java +++ b/contribs/jersey-multipart/src/main/java/com/sun/jersey/multipart/FormDataBodyPart.java @@ -240,8 +240,14 @@ public class FormDataBodyPart extends BodyPart { * @param name the control name. */ public void setName(String name) { + if(name == null) { + throw new IllegalArgumentException("Name can not be null."); + } if (getFormDataContentDisposition() == null) { - super.setContentDisposition(FormDataContentDisposition.name(name).build()); + FormDataContentDisposition contentDisposition; + contentDisposition = FormDataContentDisposition.name(name) + .build(); + super.setContentDisposition(contentDisposition); } else { FormDataContentDisposition cd = getFormDataContentDisposition(); cd = FormDataContentDisposition.name(name). diff --git a/contribs/jersey-multipart/src/test/java/com/sun/jersey/multipart/FormDataBodyPartTest.java b/contribs/jersey-multipart/src/test/java/com/sun/jersey/multipart/FormDataBodyPartTest.java index a29d59c..0191b85 100644 --- a/contribs/jersey-multipart/src/test/java/com/sun/jersey/multipart/FormDataBodyPartTest.java +++ b/contribs/jersey-multipart/src/test/java/com/sun/jersey/multipart/FormDataBodyPartTest.java @@ -114,6 +114,14 @@ public class FormDataBodyPartTest extends BodyPartTest { } assertEquals("bar", fdbp.getEntity()); assertTrue(!fdbp.isSimple()); + try { + fdbp = new FormDataBodyPart(); + fdbp.setName(null); + fail("Name should be null settable!"); + } + catch(IllegalArgumentException argumentException) { + //expected + } } diff --git a/jersey-core/src/main/java/com/sun/jersey/core/header/FormDataContentDisposition.java b/jersey-core/src/main/java/com/sun/jersey/core/header/FormDataContentDisposition.java index 0f3c73c..f1d524d 100644 --- a/jersey-core/src/main/java/com/sun/jersey/core/header/FormDataContentDisposition.java +++ b/jersey-core/src/main/java/com/sun/jersey/core/header/FormDataContentDisposition.java @@ -65,6 +65,12 @@ public class FormDataContentDisposition extends ContentDisposition { long size) { super(type, fileName, creationDate, modificationDate, readDate, size); this.name = name; + if (!getType().equals("form-data")) { + throw new IllegalArgumentException("The content dispostion type is not equal to form-data"); + } + if (name == null) { + throw new IllegalArgumentException("The name parameter is not present"); + } } public FormDataContentDisposition(String header) throws ParseException { @@ -108,6 +114,9 @@ public class FormDataContentDisposition extends ContentDisposition { * @return the form data content disposition builder. */ public static FormDataContentDispositionBuilder name(String name) { + if(name == null) { + throw new IllegalArgumentException("The name parameter is not present"); + } return new FormDataContentDispositionBuilder(name); } -- 1.5.6