You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2008/11/20 18:48:34 UTC
svn commit: r719305 - in /cxf/trunk:
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/
rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
systests/src/test/java/org/apa...
Author: sergeyb
Date: Thu Nov 20 09:48:33 2008
New Revision: 719305
URL: http://svn.apache.org/viewvc?rev=719305&view=rev
Log:
JAXRS : adding system schema validation test, improving handling of exception mappers
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties (with props)
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties (with props)
cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java (with props)
cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd (with props)
cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt (with props)
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
cxf/trunk/systests/src/test/resources/jaxrs/WEB-INF/beans.xml
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties?rev=719305&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties Thu Nov 20 09:48:33 2008
@@ -0,0 +1,23 @@
+#
+#
+# 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.
+#
+#
+WEB_APP_EXCEPTION=WebApplicationExceptionMapper has caught an exception {0}
+DEFAULT_EXCEPTION_MESSAGE=Internal server exception occurred. Please consult logs for more information.
+
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/Messages.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WebApplicationExceptionMapper.java Thu Nov 20 09:48:33 2008
@@ -19,15 +19,47 @@
package org.apache.cxf.jaxrs.impl;
+import java.util.ResourceBundle;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.common.logging.LogUtils;
+
public class WebApplicationExceptionMapper
implements ExceptionMapper<WebApplicationException> {
+ private static final Logger LOG = LogUtils.getL7dLogger(WebApplicationExceptionMapper.class);
+ private static final ResourceBundle BUNDLE = BundleUtils.getBundle(WebApplicationExceptionMapper.class);
+
public Response toResponse(WebApplicationException ex) {
- return ex.getResponse();
+ if (LOG.isLoggable(Level.FINE)) {
+ org.apache.cxf.common.i18n.Message errorMsg =
+ new org.apache.cxf.common.i18n.Message("WEB_APP_EXCEPTION",
+ BUNDLE, ex.getCause() == null ? ex.getMessage() : ex.getCause().getMessage());
+ LOG.fine(errorMsg.toString());
+ }
+
+ Response r = ex.getResponse();
+ if (r == null) {
+ String message = null;
+ if (ex.getCause() == null) {
+ message = new org.apache.cxf.common.i18n.Message("DEFAULT_EXCEPTION_MESSAGE",
+ BUNDLE).toString();
+ } else {
+ message = ex.getCause().getMessage();
+ if (message == null) {
+ message = ex.getCause().getClass().getName();
+ }
+ }
+ r = Response.status(500).type(MediaType.TEXT_PLAIN).entity(message).build();
+ }
+ return r;
}
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java Thu Nov 20 09:48:33 2008
@@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.ResourceBundle;
import java.util.WeakHashMap;
import java.util.logging.Logger;
@@ -52,6 +53,7 @@
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
+import org.apache.cxf.common.i18n.BundleUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.PackageUtils;
import org.apache.cxf.jaxrs.utils.AnnotationUtils;
@@ -60,6 +62,8 @@
public abstract class AbstractJAXBProvider
implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
+ protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
+
private static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
private static final String CHARSET_PARAMETER = "charset";
@@ -67,10 +71,11 @@
private static Map<String, JAXBContext> packageContexts = new WeakHashMap<String, JAXBContext>();
private static Map<Class<?>, JAXBContext> classContexts = new WeakHashMap<Class<?>, JAXBContext>();
-
+
@Context protected ContextResolver<JAXBContext> resolver;
private Schema schema;
+
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns, MediaType mt) {
return isSupported(type, genericType, anns)
|| AnnotationUtils.getAnnotation(anns, XmlJavaTypeAdapter.class) != null;
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Thu Nov 20 09:48:33 2008
@@ -19,7 +19,6 @@
package org.apache.cxf.jaxrs.provider;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -29,9 +28,12 @@
import java.util.List;
import java.util.Map;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
@@ -39,6 +41,8 @@
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
+@Produces({"application/xml", "text/xml" })
+@Consumes({"application/xml", "text/xml" })
@Provider
public class JAXBElementProvider extends AbstractJAXBProvider {
@@ -70,6 +74,15 @@
}
} catch (JAXBException e) {
+ // TODO : refactor it such that thsi caode can be used by across the board
+ String message = new org.apache.cxf.common.i18n.Message("JAXB_EXCEPTION",
+ BUNDLE,
+ e.getLinkedException() != null
+ ? e.getLinkedException().getMessage() : e.getMessage()).toString();
+ Response r = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .type(MediaType.TEXT_PLAIN).entity(message).build();
+ throw new WebApplicationException(r);
+ } catch (Exception e) {
throw new WebApplicationException(e);
}
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java Thu Nov 20 09:48:33 2008
@@ -86,6 +86,8 @@
throw new WebApplicationException(e);
} catch (XMLStreamException e) {
throw new WebApplicationException(e);
+ } catch (Exception e) {
+ throw new WebApplicationException(e);
}
}
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties?rev=719305&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties Thu Nov 20 09:48:33 2008
@@ -0,0 +1,22 @@
+#
+#
+# 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.
+#
+#
+JAXB_EXCEPTION=JAXBException occurred : {0}
+
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/Messages.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java Thu Nov 20 09:48:33 2008
@@ -60,6 +60,8 @@
new ArrayList<ProviderInfo<MessageBodyWriter>>();
private List<ProviderInfo<ContextResolver>> userContextResolvers =
new ArrayList<ProviderInfo<ContextResolver>>();
+ private List<ProviderInfo<ExceptionMapper>> defaultExceptionMappers =
+ new ArrayList<ProviderInfo<ExceptionMapper>>();
private List<ProviderInfo<ExceptionMapper>> userExceptionMappers =
new ArrayList<ProviderInfo<ExceptionMapper>>();
private List<ProviderInfo<RequestHandler>> requestHandlers =
@@ -76,7 +78,7 @@
userContextResolvers,
requestHandlers,
responseHandlers,
- userExceptionMappers,
+ defaultExceptionMappers,
new JAXBElementProvider(),
new JSONProvider(),
new BinaryDataProvider(),
@@ -123,9 +125,25 @@
return null;
}
- @SuppressWarnings("unchecked")
+
public <T extends Throwable> ExceptionMapper<T> createExceptionMapper(Class<?> exceptionType, Message m) {
- for (ProviderInfo<ExceptionMapper> em : userExceptionMappers) {
+
+ ExceptionMapper<T> mapper = doCreateExceptionMapper(userExceptionMappers,
+ exceptionType,
+ m);
+ if (mapper != null) {
+ return mapper;
+ }
+
+ return doCreateExceptionMapper(defaultExceptionMappers,
+ exceptionType,
+ m);
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T extends Throwable> ExceptionMapper<T> doCreateExceptionMapper(
+ List<ProviderInfo<ExceptionMapper>> mappers, Class<?> exceptionType, Message m) {
+ for (ProviderInfo<ExceptionMapper> em : mappers) {
Type[] types = em.getProvider().getClass().getGenericInterfaces();
for (Type t : types) {
if (t instanceof ParameterizedType) {
Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java Thu Nov 20 09:48:33 2008
@@ -31,9 +31,11 @@
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.xml.bind.JAXBContext;
@@ -44,8 +46,10 @@
import org.apache.abdera.model.Feed;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.JAXBContextProvider;
+import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
import org.apache.cxf.jaxrs.model.ProviderInfo;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.message.MessageImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -59,6 +63,21 @@
}
@Test
+ public void testExceptionMappers() throws Exception {
+ ProviderFactory pf = ProviderFactory.getInstance();
+ ExceptionMapper<?> mapper =
+ pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
+ assertNotNull(mapper);
+ WebApplicationExceptionMapper m = new WebApplicationExceptionMapper();
+ pf.registerUserProvider(m);
+ ExceptionMapper<?> mapper2 =
+ pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
+ assertNotSame(mapper, mapper2);
+ assertSame(m, mapper2);
+
+ }
+
+ @Test
public void testSortEntityProviders() throws Exception {
ProviderFactory pf = ProviderFactory.getInstance();
pf.registerUserProvider(new TestStringProvider());
Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java?rev=719305&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java (added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java Thu Nov 20 09:48:33 2008
@@ -0,0 +1,62 @@
+/**
+ * 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 javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+
+@XmlRootElement(name = "Book", namespace = "http://www.example.org/books")
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "Book", propOrder = {"name", "id" })
+public class Book2 {
+ @XmlElement(name = "name", namespace = "http://www.example.org/books")
+ private String name;
+ @XmlElement(name = "id", namespace = "http://www.example.org/books")
+ private long id;
+
+ public Book2() {
+ }
+
+ public Book2(String name, long id) {
+ this.name = name;
+ this.id = id;
+ }
+
+ public void setName(String n) {
+ name = n;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setId(long i) {
+ id = i;
+ }
+ public long getId() {
+ return id;
+ }
+
+
+}
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/Book2.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java Thu Nov 20 09:48:33 2008
@@ -24,10 +24,14 @@
import java.util.Map;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriInfo;
@Path("/bookstore")
@Produces("application/json")
@@ -35,7 +39,9 @@
private Map<Long, Book> books = new HashMap<Long, Book>();
private Long mainId = 123L;
-
+ @Context
+ private UriInfo ui;
+
public BookStoreSpring() {
init();
System.out.println("----books: " + books.size());
@@ -48,6 +54,14 @@
}
@GET
+ @Path("/bookinfo")
+ public Book getBookByUriInfo() throws Exception {
+ MultivaluedMap<String, String> params = ui.getQueryParameters();
+ String id = params.getFirst("param1") + params.getFirst("param2");
+ return books.get(Long.valueOf(id));
+ }
+
+ @GET
@Path("/booksquery")
public Book getBookByQuery(@QueryParam("id") String id) {
@@ -72,6 +86,16 @@
return books.get(mainId);
}
+ @POST
+ @Path("books/convert")
+ @Produces("application/xml")
+ public Book convertBook(Book2 book) {
+ // how to have Book2 populated ?
+ Book b = new Book();
+ b.setId(book.getId());
+ b.setName(book.getName());
+ return b;
+ }
final void init() {
Book book = new Book();
Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java Thu Nov 20 09:48:33 2008
@@ -19,10 +19,15 @@
package org.apache.cxf.systest.jaxrs;
+import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.FileRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -33,7 +38,16 @@
@BeforeClass
public static void startServers() throws Exception {
- assertTrue("server did not launch correctly", launchServer(BookServerSpring.class));
+ assertTrue("server did not launch correctly",
+ launchServer(BookServerSpring.class));
+ }
+
+ @Test
+ public void testGetBookByUriInfo() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/bookstore/bookinfo?"
+ + "param1=12¶m2=3";
+ getBook(endpointAddress);
}
@Test
@@ -76,6 +90,57 @@
assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in));
}
+ @Test
+ public void testAddInvalidBook() throws Exception {
+
+ String endpointAddress =
+ "http://localhost:9080/bookstore/books/convert";
+
+ File input = new File(getClass().getResource("resources/add_book.txt").toURI());
+ PostMethod post = new PostMethod(endpointAddress);
+ post.setRequestHeader("Content-Type", "application/xml");
+ RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
+ post.setRequestEntity(entity);
+ HttpClient httpclient = new HttpClient();
+
+ try {
+ int result = httpclient.executeMethod(post);
+ assertEquals(500, result);
+ assertTrue(post.getResponseBodyAsString().contains("JAXBException"));
+ } finally {
+ // Release current connection to the connection pool once you are done
+ post.releaseConnection();
+ }
+
+ }
+
+ @Test
+ public void testAddValidBook() throws Exception {
+
+ String endpointAddress =
+ "http://localhost:9080/bookstore/books/convert";
+
+ File input = new File(getClass().getResource("resources/add_book2.txt").toURI());
+ PostMethod post = new PostMethod(endpointAddress);
+ post.setRequestHeader("Content-Type", "application/xml");
+ RequestEntity entity = new FileRequestEntity(input, "text/xml");
+ post.setRequestEntity(entity);
+ HttpClient httpclient = new HttpClient();
+
+ try {
+ int result = httpclient.executeMethod(post);
+ assertEquals(200, result);
+
+ InputStream expected = getClass().getResourceAsStream("resources/expected_get_book123.txt");
+
+ assertEquals(getStringFromInputStream(expected), post.getResponseBodyAsString());
+ } finally {
+ // Release current connection to the connection pool once you are done
+ post.releaseConnection();
+ }
+
+ }
+
private String getStringFromInputStream(InputStream in) throws Exception {
CachedOutputStream bos = new CachedOutputStream();
IOUtils.copy(in, bos);
Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd?rev=719305&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd (added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd Thu Nov 20 09:48:33 2008
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ 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.
+-->
+
+<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/books" xmlns:tns="http://www.example.org/books" elementFormDefault="qualified">
+ <element name="Book">
+ <complexType>
+ <sequence>
+ <element name="name" type="xs:string"/>
+ <element name="id" type="xs:long"/>
+ </sequence>
+ </complexType>
+ </element>
+</schema>
\ No newline at end of file
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/book.xsd
------------------------------------------------------------------------------
svn:mime-type = text/xml
Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt?rev=719305&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt (added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt Thu Nov 20 09:48:33 2008
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?><b:Book xmlns:b="http://www.example.org/books"><b:name>CXF in Action</b:name><b:id>123</b:id></b:Book>
\ No newline at end of file
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/add_book2.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: cxf/trunk/systests/src/test/resources/jaxrs/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/resources/jaxrs/WEB-INF/beans.xml?rev=719305&r1=719304&r2=719305&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/resources/jaxrs/WEB-INF/beans.xml (original)
+++ cxf/trunk/systests/src/test/resources/jaxrs/WEB-INF/beans.xml Thu Nov 20 09:48:33 2008
@@ -42,7 +42,19 @@
<jaxrs:serviceBeans>
<bean class="org.apache.cxf.systest.jaxrs.BookStoreSpring" />
</jaxrs:serviceBeans>
+ <jaxrs:providers>
+ <ref bean="jaxbProvider"/>
+
+ </jaxrs:providers>
+
</jaxrs:server>
+ <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
+ <property name="schemas">
+ <list>
+ <value>classpath:/org/apache/cxf/systest/jaxrs/book.xsd</value>
+ </list>
+ </property>
+ </bean>
</beans>
<!-- END SNIPPET: beans -->