Hi Barry

I think it's actually required, but it should be


@ConsumeMime("application/x-www-form-urlencoded")
public Response createAuthorisation(MutivaluedMap<String, String> params) 
throws Exception {
}

Please check a 3.1.4 section

We already have a MetadataMap<K, V> in the trunk which implements 
MutivaluedMap, so if you could update your provider to support MutivaluedMaps 
then it would be super...I'm not sure about Map<String, String>. Does Jersey 
supports it ? If so then it'd likely be spec-uncompliant...


Thanks, Sergey

  ----- Original Message ----- 
  From: Barry Fitzgerald 
  To: [email protected] 
  Sent: Wednesday, February 27, 2008 6:02 PM
  Subject: JAX-RS Form Encoding Provider


  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;
      }
  }


----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Reply via email to