users@jersey.java.net

Re: [Jersey] Hello World! and Welcome to jersey-multipart

From: Gili <cowwoc_at_bbs.darktech.org>
Date: Mon, 3 Nov 2008 22:24:36 -0800 (PST)

Craig,

Here is what I am proposing:

Let "example header" refer to "Content-Type: application/xml; charset=UTF-8"

1a) BodyPart.getHeaders should return MultivaluedMap<String,
MatrixParameters> instead of MultivaluedMap<String, String>. Where
MatrixParameters extends Map<String, String>.

The example header would return MatrixParameters =
{[Content-Type,application/xml], [charset, UTF-8]}

*or*

1b) BodyPart.getHeaders should return MultivaluedMap<String, HeaderValue>
instead of MultivaluedMap<String, String>. Where HeaderValue defines two
methods:

String HeaderValue.getValue() and
Map<String, String> HeaderValue.getAttributes()

The example header would return:

HeaderValue.getValue() = "application/xml"
HeaderValue.getAttributes() = "[charset, UTF-8]"


Let me know if you can think of a cleaner way to represent this...

2) Here is the algorithm I used to parse the matrix parameters:

        /**
         * Converts matrix parameters to key-value pairs.
         *
         * @param key the header name
         * @param value the header value
         * @return the matrix parameters
         */
        private Map<String, String> getMatrixParameters(String key, String value)
        {
                Map<String, String> result = new HashMap<String, String>();
                String[] pairs = value.split(";");
                if (pairs.length > 0)
                        result.put(key, unquoted(pairs[0]));
                for (int i = 1, size = pairs.length; i < size; ++i)
                {
                        String[] tokens = pairs[i].split("=");
                        assert (tokens.length == 2): Arrays.toString(tokens);
                        result.put(tokens[0], unquoted(tokens[1]));
                }
                return result;
        }

        /**
         * Removes any quotes surrounding the input text.
         *
         * @param text input text
         * @return unquoted text
         */
        private String unquoted(String text)
        {
                text = text.trim();
                if (text.charAt(0) == '\"' && text.charAt(text.length() - 1) == '\"')
                        return text.substring(1, text.length() - 1);
                return text;
        }

3) Modify the package name so it's obvious that this implementation isn't
specific to Jersey (I think it's okay that the tests are Jersey-specific)
and publish it on JAX-RS's mailing list in the hopes of getting other people
to use and support it.

It looks to me like pretty much all major JAX-Rs implementations have
built-in MimeMultipart (from javax.mail) providers but this API is not as
easy to use as it should be. The value proposition of your provider over
those implementations would be a cleaner, more modern API.

What do you think?

Gili
-- 
View this message in context: http://n2.nabble.com/Hello-World%21-and-Welcome-to-jersey-multipart-tp1343189p1453213.html
Sent from the Jersey mailing list archive at Nabble.com.