0 Replies Latest reply on May 19, 2010 11:42 PM by kevinmoodley

    Using MutlipartInput via ClientRequest

    kevinmoodley

      Does anyone have an example of how to set up a request with MultipartInput using the ClientRequest class ?

       

      I tried something like this - without success :

       

       

      Client:

       

              String url = http://localhost:8080/resteasy/test
              ClientRequest request = new ClientRequest(url);
              Customer customer = new Customer("kmoodley");
              StringBuilder resource = new StringBuilder(JAXBMarshall.marshall(customer));
              User user = new User("kmoodley");
              resource.append("SECURE_USER");
              resource.append(JAXBMarshall.marshall(user));
              Map<String, String> parameters = new HashMap<String, String>();
              parameters.put("boundary", "SECURE_USER");
              MediaType contentType = new MediaType("multipart",null,parameters);
              MultipartInputImpl input = new MultipartInputImpl(contentType, ResteasyProviderFactory.getInstance());
              ByteArrayInputStream bais = new ByteArrayInputStream(resource.toString().getBytes());
              input.parse(bais);
              request.body(contentType, input );
              response = request.post();

       

       

      The JAXBMarshall class is a utility class that marshalls classes annotated with @XmlRootElement into XML

       

       

      Server:

       

          @POST
          @Path("/resteasy/test")
          @Produces("application/xml")
          @Consumes("multipart/mixed")
          public String testMultipart(MultipartInput input) {
              Customer customer = null;
              User user = null;
              for (InputPart part : input.getParts()){
                  try {
                      customer = part.getBody(Customer.class, null);
                      user = part.getBody(User.class, null);
                  } catch (IOException e) {
                      logger.error("Failed to get objects from multipart input");
                      return null;
                  }
              }
              ....
             
              return "SUCCESS";
          }

       

       

      Thanks

       

      Kevin