title: RESTful Web Service - resource context and response questions
body: <p>I have developed a simple RESTful web service. </p>
  
  <p>Root Resource Class: </p>
  
  <pre><code>@Path("/order")
  @RequestScoped
  public class CustOrderContainerResource {
  
    //&lt;editor-fold defaultstate="collapsed" desc="Instance Variable"&gt;
    @Context
    private UriInfo myUriInfo;
  
    @Context
    private ResourceContext myResourceContext;
  
    @Context
    private SecurityContext mySecurityContext;
  
    @Inject
    private CustOrderDAO myCustOrderDAO;
  
    public CustOrderContainerResource() {
    }
  
  
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML})
    public List&lt;Custorder&gt; ReadCustomerOrder(@QueryParam("min")int min, 
        @QueryParam("max")int max, @Context Request myRequest, 
        @Context HttpHeaders myHeader) {
  
      int totalOrder = 0;
      List&lt;Custorder&gt; resultList = null;
  
      totalOrder = myCustOrderDAO.count();
      if(min == 0 &amp;&amp; max == 0) {
        throw new QueryParamException("Order ID is empty");
      }
      else if(max &gt; totalOrder) {
        throw new QueryParamException("Order ID Range is invalid");
      }
      resultList = myCustOrderDAO.findRange(min, max, "findOrderIDRange");
  
      return resultList;
    }
  
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List&lt;Custorder&gt; ReadCustomerOrder() {
  
      // Check conditional get here
      return myCustOrderDAO.findAll(); 
    }
  
    @POST
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
    public Response createOrder(Custorder myCustOrder) {
  
      String orderID = null;
  
      myCustOrder.setStatus("pending");
      myCustOrder.setOrderdate(new Date());
      myCustOrder.setTotal("");
  
      // Persist
      myCustOrderDAO.create(myCustOrder);
  
      // Get Order ID
  
      // Embedded created URL for new customer order in response
      return Response.created(myUriInfo.getAbsolutePath().resolve(myCustOrder.getOrderid() + "/")).build();
    }
  
    @Path("{orderID}")
  //  @Produces(MediaType.TEXT_HTML)
    public CustOrderResource ReadSingleCustomerOrder(@PathParam("orderID") String orderID) {
  
      int userOrderID = Integer.parseInt(orderID);
      int myOrderID = myCustOrderDAO.count();
  
      CustOrderResource myCustorder = null;
  
      if(userOrderID &gt; myOrderID 
              || myCustOrderDAO.find(orderID) == null) {
        throw new OrderNotFoundException("Order ID Not Found");
      }
  
      if(!mySecurityContext.isUserInRole("admin")) {  
        // Propogates to specific resource class
        myCustorder = myResourceContext.getResource(CustOrderResource.class);
        myCustorder.setOrderID(orderID);
      }
  
      return myCustorder;
      //    return CustOrderResource.getInstance(myCustOrderDAO, orderID);
    }
  }
  </code></pre>
  
  <p>Sub Resource Locator Class :     </p>
  
  <pre><code>@RequestScoped
  public class CustOrderResource {
  
    //&lt;editor-fold defaultstate="collapsed" desc="Instance Variable"&gt;
    @Inject
    private CustOrderDAO myCustOrderDAO;
  
    private String orderID;
  
    private static final Logger myLogger = Logger.getLogger(CustOrderResource.class.getName()); 
  
    //&lt;/editor-fold&gt;
  
    // ========================================================
    public CustOrderResource() {
    }
  
    private CustOrderResource(String orderID) {
      this.orderID = orderID;
    }
  
  
    public static Custorder getInstance(CustOrderDAO myCustOrderDAO, String orderID) {
      return myCustOrderDAO.find(orderID);
    }
  
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML})
    public Custorder getCustomerOrder() {
      return myCustOrderDAO.find(orderID);
    }
  
    @POST
    @Consumes(MediaType.APPLICATION_XML)
    public String updateCustomerOrder() {
  
      return "so";
  
      /*try {
        myCustOrderDAO.update(myCustOrder);
      }
      catch(Exception e) {
  
        myLogger.log(Level.ALL, e.toString());
  
        throw new WebApplicationException(
                Response.status(Status.INTERNAL_SERVER_ERROR)
                .entity("Cust Order Update Failed").build());
      }*/
    }
  
    @DELETE
    // 415 Unsupported media type
    public String deleteCustomerOrder() {
  
      return "Deleted";
      //    myCustOrderDAO.delete(myCustOrder);
    }
  
    public String getOrderID() {
      return orderID;
    }
  
    public void setOrderID(String orderID) {
      this.orderID = orderID;
    }
  }
  </code></pre>
  
  <p>My question is </p>
  
  <ul>
  <li>AFAIK, the resource context will propagate to specific resource class
  when we specify it as an argument according to the HTTP method like
  POST or DELETE. How do I pass the parameter from sub resource locator
  method into sub resource class method?</li>
  </ul>
  
  <p>I tried to update customer order using post method with XML data but unfortunately the JAX-RS runtime returns 415 Unsupported media type. </p>
  
  <p>I am using the REST client from <a href="http://code.google.com/p/rest-client/" rel="nofollow">http://code.google.com/p/rest-client/</a> to test my application, by pasting an XML file into the body tab content. What is wrong with it? </p>
  
  <ul>
  <li>Does the JAXB automatically convert to XML when I return a list of
  objects? I have tested and it return xml format but just want
  confirmation. Is it more flexible to return response object?</li>
  </ul>
  
  <p>I wonder how to build a response object with list of object and list of URI or Atom XML with list of object (Apache Abdera).</p>
  
  <ul>
  <li>How to find out a id of a newly persisted object into database in my
  <code>createCustomerOrder</code> method ?</li>
  </ul>
  
  <p>Thanks. </p>
  
  <p>Please help.</p>