Hi all,

I've taken the time to write a provider that decodes post parameters into a
hashmap (code below).

There is a similar implementation in Jersey. My implementation is a bit
rough and ready but works. I thought it might be useful for some people.

A example use would be a form post to a url with parameter username and
password. Once the provider is registered it could be used like so:

    @POST
    @Path("/authorisation")
    @ConsumeMime("application/x-www-form-urlencoded")
    @ProduceMime("text/xml")
    public Response createAuthorisation(Map<String, String> params) throws
Exception {
        System.out.println("----invoking createAuthorisation, params: " +
params);
        String username = params.get("username");
     ....create & return auth Response

    }

This isn't required for the JAX-RS standard but if there is a desire to get
this into the project I can tidy it up a bit, let me know!

Barry

*Code Below:*

package org.barry;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.ConsumeMime;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;

import org.apache.cxf.common.util.StringUtils;

@ConsumeMime("application/x-www-form-urlencoded")
public final class FormEncodingReaderProvider implements
MessageBodyReader<Object> {

    public boolean isReadable(Class<?> type) {
        return type.isAssignableFrom(HashMap.class);
    }

    public Object readFrom(Class<Object> type, MediaType m,
MultivaluedMap<String, String> headers,
                           InputStream is) {
        try {

            String charset = "UTF-8";

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            copy(is, bos, 1024);
            String postBody = new String(bos.toByteArray(), charset);

            Map<String, String> params = getParams(postBody);

            return params;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    protected static void copy(final InputStream input,
                            final OutputStream output,
                            final int bufferSize)
        throws IOException {
        final byte[] buffer = new byte[bufferSize];
        int n = 0;
        n = input.read(buffer);
        while (-1 != n) {
            output.write(buffer, 0, n);
            n = input.read(buffer);
        }
    }

    /**
     * Retrieve map of parameters from the passed in message
     * @param message
     * @return a Map of parameters.
     */
    protected static Map<String, String> getParams(String body) {
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (!StringUtils.isEmpty(body)) {
            List<String> parts = Arrays.asList(body.split("&"));
            for (String part : parts) {
                String[] keyValue = part.split("=");
                params.put(keyValue[0], keyValue[1]);
            }
        }
        return params;
    }
}

Reply via email to