Return-Path: Delivered-To: apmail-incubator-cxf-commits-archive@locus.apache.org Received: (qmail 67938 invoked from network); 13 Sep 2007 13:16:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Sep 2007 13:16:51 -0000 Received: (qmail 27457 invoked by uid 500); 13 Sep 2007 13:16:44 -0000 Delivered-To: apmail-incubator-cxf-commits-archive@incubator.apache.org Received: (qmail 27398 invoked by uid 500); 13 Sep 2007 13:16:44 -0000 Mailing-List: contact cxf-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cxf-dev@incubator.apache.org Delivered-To: mailing list cxf-commits@incubator.apache.org Received: (qmail 27389 invoked by uid 99); 13 Sep 2007 13:16:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Sep 2007 06:16:44 -0700 X-ASF-Spam-Status: No, hits=-98.5 required=10.0 tests=ALL_TRUSTED,WEIRD_PORT X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Sep 2007 13:18:29 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E61A61A9832; Thu, 13 Sep 2007 06:16:26 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r575303 - in /incubator/cxf/branches/jliu: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/src/test/java/org/apache/cxf/systest/jaxrs/ systests/src/te... Date: Thu, 13 Sep 2007 13:16:26 -0000 To: cxf-commits@incubator.apache.org From: jliu@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20070913131626.E61A61A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jliu Date: Thu Sep 13 06:16:25 2007 New Revision: 575303 URL: http://svn.apache.org/viewvc?rev=575303&view=rev Log: Looks like the JAXB databinding in JAX-RS can be very simple. Different from WSDL-based services in CXF, the JAXB databinding in JAX-RS does not need to handle Array, List etc as a pure plain-old-xml binding does not allow multiple root elements anyway, you always have to use a wrapper class, like Books. Added: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java (with props) incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt (with props) Modified: incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Modified: incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=575303&r1=575302&r2=575303&view=diff ============================================================================== --- incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java (original) +++ incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java Thu Sep 13 06:16:25 2007 @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.OutputStream; -import java.util.List; import javax.ws.rs.core.Response; import javax.ws.rs.ext.EntityProvider; @@ -61,9 +60,7 @@ if (objs.get(0) instanceof Response) { Response response = (Response)responseObj; responseObj = response.getEntity(); - - //HttpServletResponse hsr = (HttpServletResponse)message.get("HTTP.RESPONSE"); - //hsr.setStatus(response.getStatus()); + message.put(Message.RESPONSE_CODE, response.getStatus()); if (responseObj == null) { @@ -72,12 +69,15 @@ } Class targetType = responseObj.getClass(); - if (responseObj.getClass().isArray()) { +/* if (responseObj.getClass().isArray()) { targetType = responseObj.getClass().getComponentType(); } else if (responseObj instanceof List && ((List)responseObj).get(0) != null) { + //NOTE: if its a List, the provider should try to determine if it can support + //every object inside the List instead of the first one only. targetType = ((List)responseObj).get(0).getClass(); - } + }*/ + EntityProvider provider = ProviderFactory.getInstance().createEntityProvider(targetType); try { Modified: incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=575303&r1=575302&r2=575303&view=diff ============================================================================== --- incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original) +++ incubator/cxf/branches/jliu/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Thu Sep 13 06:16:25 2007 @@ -22,19 +22,15 @@ import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Array; -import java.util.List; import java.util.Map; import java.util.WeakHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.EntityProvider; import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.namespace.QName; public final class JAXBElementProvider implements EntityProvider { @@ -59,7 +55,9 @@ public void writeTo(Object obj, MultivaluedMap headers, OutputStream os) { try { - if (obj.getClass().isArray() || obj instanceof List) { + //Looks like we do not need to deal with Array and List as multiple root elements + //is not allowed in a plain-old-xml binding anyway. +/* if (obj.getClass().isArray() || obj instanceof List) { Class cls = null; Object objArray; if (obj instanceof List) { @@ -79,12 +77,14 @@ marshaller.marshal(new JAXBElement(new QName(null, o.getClass().getSimpleName()), cls == null ? o.getClass() : cls, o), os); } - } else { - JAXBContext context = getJAXBContext(obj.getClass()); - Marshaller marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); - marshaller.marshal(obj, os); - } + } else {*/ + JAXBContext context = getJAXBContext(obj.getClass()); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); + marshaller.marshal(obj, os); + + //TODO: support Calendar type + //} } catch (JAXBException e) { e.printStackTrace(); } Modified: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=575303&r1=575302&r2=575303&view=diff ============================================================================== --- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original) +++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Thu Sep 13 06:16:25 2007 @@ -36,7 +36,9 @@ public class BookStore { private static List books = new ArrayList(); + private static List cds = new ArrayList(); private static long bookId = 123; + private static long cdId = 123; @HttpContext UriInfo uriInfo; @@ -45,17 +47,26 @@ book.setId(bookId); book.setName("CXF in Action"); books.add(book); + + CD cd = new CD(); + cd.setId(cdId); + cd.setName("BOHEMIAN RHAYSODY"); + cds.add(cd); + CD cd1 = new CD(); + cd1.setId(++cdId); + cd1.setName("BICYCLE RACE"); + cds.add(cd1); } public BookStore() { } - +/* @HttpMethod("GET") public List getAllItems() { System.out.println("----invoking getBooks"); return books; - } + } */ @HttpMethod("GET") @UriTemplate("/books/{bookId}/") @@ -79,8 +90,7 @@ books.add(book); - Response r = Response.Builder.ok(book).build(); - return r; + return Response.Builder.ok(book).build(); } @HttpMethod("PUT") @@ -132,16 +142,27 @@ return r; } - - @UriTemplate("/cd/{CDId}/") - public CD getCD(@UriParam("CDId") String cdId) { - System.out.println("----invoking getCD with cdId: " + cdId); - CD cd = new CD(); - cd.setId(223); - cd.setName("BOHEMIAN RHAYSODY"); + @UriTemplate("/cds/{CDId}/") + public CD getCD(@UriParam("CDId") String id) { + System.out.println("----invoking getCD with cdId: " + id); + long idNumber = Long.parseLong(id); + for (CD b : cds) { + if (idNumber == b.getId()) { + return b; + } + } - return cd; + return null; + } + + @HttpMethod("GET") + @UriTemplate("/cds/") + public CDs getCDs() { + System.out.println("----invoking getCDs"); + CDs c = new CDs(); + c.setCD(cds); + return c; } } Added: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java?rev=575303&view=auto ============================================================================== --- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java (added) +++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java Thu Sep 13 06:16:25 2007 @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.cxf.systest.jaxrs; + +import java.util.Collection; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "CDs") +public class CDs { + private Collection cds; + + public Collection getCD() { + return cds; + } + + public void setCD(Collection c) { + this.cds = c; + } +} Propchange: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/CDs.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=575303&r1=575302&r2=575303&view=diff ============================================================================== --- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original) +++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Thu Sep 13 06:16:25 2007 @@ -42,21 +42,6 @@ public static void startServers() throws Exception { assertTrue("server did not launch correctly", launchServer(BookServer.class)); } - - @Test - public void testGetBooks() throws Exception { - String endpointAddress = - "http://localhost:9080/xml/bookstore"; - URL url = new URL(endpointAddress); - InputStream in = url.openStream(); - assertNotNull(in); - - InputStream expected = getClass() - .getResourceAsStream("resources/expected_get_books.txt"); - - //System.out.println("---" + getStringFromInputStream(in)); - assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); - } @Test public void testGetBook123() throws Exception { @@ -173,6 +158,22 @@ post.releaseConnection(); } } + + + @Test + public void testGetCDs() throws Exception { + String endpointAddress = + "http://localhost:9080/xml/bookstore/cds"; + URL url = new URL(endpointAddress); + InputStream in = url.openStream(); + assertNotNull(in); + + InputStream expected = getClass() + .getResourceAsStream("resources/expected_get_cds.txt"); + + //System.out.println("---" + getStringFromInputStream(in)); + assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); + } private String getStringFromInputStream(InputStream in) throws Exception { CachedOutputStream bos = new CachedOutputStream(); Added: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt URL: http://svn.apache.org/viewvc/incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt?rev=575303&view=auto ============================================================================== --- incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt (added) +++ incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt Thu Sep 13 06:16:25 2007 @@ -0,0 +1 @@ +123BOHEMIAN RHAYSODY124BICYCLE RACE \ No newline at end of file Propchange: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/branches/jliu/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_cds.txt ------------------------------------------------------------------------------ svn:mime-type = text/plain