cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r772137 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/ systests/src/test/java/or...
Date Wed, 06 May 2009 10:34:43 GMT
Author: sergeyb
Date: Wed May  6 10:34:42 2009
New Revision: 772137

URL: http://svn.apache.org/viewvc?rev=772137&view=rev
Log:
JAX-RS : adding JAXB-based XSLT provider

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl 
 (with props)
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java   (with
props)
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xhtml
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl 
 (with props)
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
  (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Book.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

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=772137&r1=772136&r2=772137&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
Wed May  6 10:34:42 2009
@@ -106,7 +106,7 @@
             if (JAXBElement.class.isAssignableFrom(type)) {
                 response = unmarshaller.unmarshal(new StreamSource(is), theType);
             } else {
-                response = unmarshaller.unmarshal(is);
+                response = doUnmarshal(unmarshaller, is, mt);
             }
             response = checkAdapter(response, anns, false);
             return response;
@@ -122,6 +122,10 @@
         return null;
     }
 
+    protected Object doUnmarshal(Unmarshaller unmarshaller, InputStream is, MediaType mt)

+        throws JAXBException {
+        return unmarshaller.unmarshal(is);
+    }
     
     public void writeTo(Object obj, Class<?> cls, Type genericType, Annotation[] anns,
 
         MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) 
@@ -133,7 +137,7 @@
                 genericType = actualClass;
             }
             String encoding = getEncoding(m, headers);
-            marshal(actualObject, actualClass, genericType, encoding, os);
+            marshal(actualObject, actualClass, genericType, encoding, os, m);
         } catch (JAXBException e) {
             handleJAXBException(e);
         }  catch (WebApplicationException e) {
@@ -143,7 +147,8 @@
         }
     }
 
-    protected void marshal(Object obj, Class<?> cls, Type genericType, String enc,
OutputStream os)
+    protected void marshal(Object obj, Class<?> cls, Type genericType, 
+                           String enc, OutputStream os, MediaType mt)
         throws Exception {
         
         Marshaller ms = createMarshaller(obj, cls, genericType, enc);
@@ -158,8 +163,12 @@
             }
             ms.marshal(obj, writer);
         } else {
-            ms.marshal(obj, os);
+            doMarshal(ms, obj, os, mt);
         }
     }
     
+    protected void doMarshal(Marshaller ms, Object obj, OutputStream os, MediaType mt) 
+        throws Exception {
+        ms.marshal(obj, os);
+    }
 }

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java?rev=772137&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
Wed May  6 10:34:42 2009
@@ -0,0 +1,342 @@
+/**
+ * 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.jaxrs.provider;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+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.PathSegment;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLFilter;
+
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxrs.ext.MessageContext;
+
+@Produces({"application/xml", "application/*+xml", "text/xml", "text/html" })
+@Consumes({"application/xml", "application/*+xml", "text/xml", "text/html" })
+@Provider
+public class XSLTJaxbProvider extends JAXBElementProvider {
+    
+    private static final Logger LOG = LogUtils.getL7dLogger(XSLTJaxbProvider.class);
+    
+    private static final String ABSOLUTE_PATH_PARAMETER = "absolute.path";
+    private static final String BASE_PATH_PARAMETER = "base.path";
+    private static final String RELATIVE_PATH_PARAMETER = "relative.path";
+    
+    private SAXTransformerFactory factory;
+    private Templates inTemplates;
+    private Templates outTemplates;
+    private Map<String, Templates> inMediaTemplates;
+    private Map<String, Templates> outMediaTemplates;
+
+    private List<String> inClassesToHandle;
+    private List<String> outClassesToHandle;
+    private Map<String, Object> inParamsMap;
+    private Map<String, Object> outParamsMap;
+    private Map<String, String> inProperties;
+    private Map<String, String> outProperties;
+    private URIResolver uriResolver;
+    private String systemId;
+    
+    @Override
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] anns, MediaType
mt) {
+        return inTemplatesAvailable(mt) && inClassCanBeHandled(type.getName())
+                   && super.isReadable(type, genericType, anns, mt);
+    }
+    
+    @Override
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns,
MediaType mt) {
+        return outTemplatesAvailable(mt) && outClassCanBeHandled(type.getName())

+                   && super.isWriteable(type, genericType, anns, mt);
+    }
+    
+    protected boolean inTemplatesAvailable(MediaType mt) {
+        return inTemplates != null || inMediaTemplates.containsKey(mt.getType() + "/" + mt.getSubtype());
+    }
+    
+    protected boolean outTemplatesAvailable(MediaType mt) {
+        return outTemplates != null || outMediaTemplates.containsKey(mt.getType() + "/" +
mt.getSubtype());
+    }
+    
+    protected Templates getInTemplates(MediaType mt) {
+        return inTemplates != null ? inTemplates 
+            : inMediaTemplates.get(mt.getType() + "/" + mt.getSubtype());
+    }
+    
+    protected Templates getOutTemplates(MediaType mt) {
+        return outTemplates != null ? outTemplates 
+            : outMediaTemplates.get(mt.getType() + "/" + mt.getSubtype());
+    }
+    
+    @Override
+    protected Object doUnmarshal(Unmarshaller unmarshaller, InputStream is, MediaType mt)

+        throws JAXBException {
+        try {
+            XMLFilter filter = factory.newXMLFilter(
+                createTemplates(getInTemplates(mt), inParamsMap, inProperties));
+            SAXSource source = new SAXSource(filter, new InputSource(is));
+            if (systemId != null) {
+                source.setSystemId(systemId);
+            }
+            return unmarshaller.unmarshal(source);
+        } catch (TransformerConfigurationException ex) {
+            LOG.warning("Transformation exception : " + ex.getMessage());
+            throw new WebApplicationException(ex);
+        }
+    }
+    
+    @Override
+    protected void doMarshal(Marshaller ms, Object obj, OutputStream os, MediaType mt)
+        throws Exception {
+        TransformerHandler th = factory.newTransformerHandler(
+            createTemplates(getOutTemplates(mt), outParamsMap, outProperties));
+        Result result = new StreamResult(os);
+        if (systemId != null) {
+            result.setSystemId(systemId);
+        }
+        th.setResult(result);
+        ms.marshal(obj, th);
+    }
+    
+    public void setOutTemplate(String loc) {
+        outTemplates = createTemplates(loc);
+    }
+    
+    public void setInTemplate(String loc) {
+        inTemplates = createTemplates(loc);
+    }
+    
+    public void setInMediaTemplates(Map<String, String> map) {
+        inMediaTemplates = new HashMap<String, Templates>();
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            inMediaTemplates.put(entry.getKey(), createTemplates(entry.getValue()));
+        }
+    }
+    
+    public void setOutMediaTemplates(Map<String, String> map) {
+        outMediaTemplates = new HashMap<String, Templates>();
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            outMediaTemplates.put(entry.getKey(), createTemplates(entry.getValue()));
+        }
+    }
+    
+    public void setResolver(URIResolver resolver) {
+        uriResolver = resolver;
+        if (factory != null) {
+            factory.setURIResolver(uriResolver);
+        }
+    }
+    
+    public void setSystemId(String system) {
+        systemId = system;
+    }
+    
+    public void setInParameters(Map<String, Object> inParams) {
+        this.inParamsMap = inParams;
+    }
+    
+    public void setOutParameters(Map<String, Object> outParams) {
+        this.outParamsMap = outParams;
+    }
+    
+    public void setInProperties(Map<String, String> inProps) {
+        this.inProperties = inProps;
+    }
+    
+    public void setOutProperties(Map<String, String> outProps) {
+        this.outProperties = outProps;
+    }
+    
+    public void setInClassname(String className) {
+        if (inClassesToHandle == null) {
+            inClassesToHandle = new ArrayList<String>();
+        }
+        inClassesToHandle.add(className);
+    }
+    
+    public boolean inClassCanBeHandled(String className) {
+        return inClassesToHandle == null || inClassesToHandle.contains(className); 
+    }
+    
+    public void setOutClassname(String className) {
+        if (outClassesToHandle == null) {
+            outClassesToHandle = new ArrayList<String>();
+        }
+        outClassesToHandle.add(className);
+    }
+    
+    public boolean outClassCanBeHandled(String className) {
+        return outClassesToHandle == null || outClassesToHandle.contains(className); 
+    }
+    
+    @SuppressWarnings("unchecked")
+    private Templates createTemplates(Templates templates, 
+                                      Map<String, Object> configuredParams,
+                                      Map<String, String> outProps) {
+        if (templates == null) {
+            LOG.severe("No template is available");
+            throw new WebApplicationException(500);
+        }
+        
+        TemplatesImpl templ =  new TemplatesImpl(templates);
+        MessageContext mc = getContext();
+        if (mc != null) {
+            UriInfo ui = mc.getUriInfo();
+            MultivaluedMap<String, String> params = ui.getPathParameters();
+            for (Map.Entry<String, List<String>> entry : params.entrySet()) {
+                String value = entry.getValue().get(0);
+                int ind = value.indexOf(";");
+                if (ind > 0) {
+                    value = value.substring(0, ind);
+                }
+                templ.setTransformerParameter(entry.getKey(), value);
+            }
+            
+            List<PathSegment> segments = ui.getPathSegments();
+            if (segments.size() > 0) {
+                setTransformParameters(templ, segments.get(segments.size() - 1).getMatrixParameters());
+            }
+            setTransformParameters(templ, ui.getQueryParameters());
+            templ.setTransformerParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
+            templ.setTransformerParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
+            templ.setTransformerParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
+            if (configuredParams != null) {
+                for (Map.Entry<String, Object> entry : configuredParams.entrySet())
{
+                    templ.setTransformerParameter(entry.getKey(), entry.getValue());
+                }
+            }
+        }
+        if (outProps != null) {
+            templ.setOutProperties(outProps);
+        }
+        
+        return templ;
+    }
+
+    private void setTransformParameters(TemplatesImpl templ, MultivaluedMap<String, String>
params) {
+        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
+            templ.setTransformerParameter(entry.getKey(), entry.getValue().get(0));
+        }    
+    }
+    
+    protected Templates createTemplates(String loc) {
+        try {
+            InputStream is = null;
+            if (loc.startsWith("classpath:")) {
+                String path = loc.substring("classpath:".length() + 1);
+                is = this.getClass().getClassLoader().getResourceAsStream(path);
+            } else {
+                File f = new File(loc);
+                if (f.exists()) {
+                    is = new FileInputStream(f);
+                }
+            }
+            if (is == null) {
+                LOG.warning("No template is available at : " + loc);
+                return null;
+            }
+            
+            Reader r = new BufferedReader(
+                           new InputStreamReader(is, "UTF-8"));
+            Source source = new StreamSource(r);
+            if (factory == null) {
+                factory = (SAXTransformerFactory)TransformerFactory.newInstance();
+                if (uriResolver != null) {
+                    factory.setURIResolver(uriResolver);
+                }
+            }
+            return factory.newTemplates(source);
+            
+        } catch (Exception ex) {
+            LOG.warning("No template can be created : " + ex.getMessage());
+        }
+        return null;
+    }
+    
+    private static class TemplatesImpl implements Templates {
+
+        private Templates templates;
+        private Map<String, Object> transformParameters = new HashMap<String, Object>();
+        private Map<String, String> outProps = new HashMap<String, String>();
+        
+        public TemplatesImpl(Templates templates) {
+            this.templates = templates;
+        }
+        
+        public void setTransformerParameter(String name, Object value) {
+            transformParameters.put(name, value);
+        }
+        
+        public void setOutProperties(Map<String, String> props) {
+            this.outProps = props;
+        }
+        
+        public Properties getOutputProperties() {
+            return templates.getOutputProperties();
+        }
+
+        public Transformer newTransformer() throws TransformerConfigurationException {
+            Transformer tr = templates.newTransformer();
+            for (Map.Entry<String, Object> entry : transformParameters.entrySet())
{
+                tr.setParameter(entry.getKey(), entry.getValue());
+            }
+            for (Map.Entry<String, String> entry : outProps.entrySet()) {
+                tr.setOutputProperty(entry.getKey(), entry.getValue());
+            }
+            return tr;
+        }
+        
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java?rev=772137&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java
Wed May  6 10:34:42 2009
@@ -0,0 +1,74 @@
+/**
+ * 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.jaxrs.provider;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.StringReader;
+
+import javax.ws.rs.core.MediaType;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.resources.Book;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class XSLTJaxbProviderTest extends Assert {
+    
+    private static final String TEMPLATE_LOCATION = "classpath:/org/apache/cxf/jaxrs/provider/template.xsl";
+    private static final String BOOK_XML = "<Book><id>123</id><name>TheBook</name></Book>";
+    
+    @Test
+    public void testWrite() throws Exception {
+        XSLTJaxbProvider provider = new XSLTJaxbProvider();
+        provider.setOutTemplate(TEMPLATE_LOCATION);
+        
+        Book b = new Book();
+        b.setId(123L);
+        b.setName("TheBook");
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(),
+                         MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(),
bos);
+        Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
+        Book b2 = (Book)um.unmarshal(new StringReader(bos.toString()));
+        b.setName("TheBook2");
+        assertEquals("Transformation is bad", b, b2);
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testRead() throws Exception {
+        XSLTJaxbProvider provider = new XSLTJaxbProvider();
+        provider.setInTemplate(TEMPLATE_LOCATION);
+        
+        Book b = new Book();
+        b.setId(123L);
+        b.setName("TheBook");
+        Book b2 = (Book)provider.readFrom((Class)Book.class, Book.class, b.getClass().getAnnotations(),
+                          MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(),
+                          new ByteArrayInputStream(BOOK_XML.getBytes()));
+        b.setName("TheBook2");
+        assertEquals("Transformation is bad", b, b2);
+    }
+    
+    
+    
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl?rev=772137&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl Wed
May  6 10:34:42 2009
@@ -0,0 +1,13 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+ <xsl:template match="@*|node()">
+   <xsl:copy>
+      <xsl:apply-templates select="@*|node()"/>
+   </xsl:copy>
+ </xsl:template>
+ 
+ <xsl:template match="name">
+	<xsl:copy>TheBook2</xsl:copy>
+ </xsl:template>
+ 
+ 
+</xsl:stylesheet> 

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/template.xsl
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Book.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Book.java?rev=772137&r1=772136&r2=772137&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Book.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/resources/Book.java Wed
May  6 10:34:42 2009
@@ -37,6 +37,11 @@
     public Book() {
     }
     
+    public Book(String name, long id) {
+        this.name = name;
+        this.id = id;
+    }
+    
     public void setName(String n) {
         name = n;
     }
@@ -62,4 +67,18 @@
     public String getState() {
         return "";
     }
+    
+    public int hashCode() { 
+        return name.hashCode() * 37 + new Long(id).hashCode();
+    }
+    
+    public boolean equals(Object o) {
+        if (!(o instanceof Book)) {
+            return false;
+        }
+        Book other = (Book)o;
+        
+        return other.name.equals(name) && other.id == id;
+        
+    }
 }

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=772137&r1=772136&r2=772137&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 Wed
May  6 10:34:42 2009
@@ -25,6 +25,7 @@
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
+import javax.ws.rs.MatrixParam;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -117,7 +118,18 @@
         return b;
     }
     
-    
+    @GET
+    @Path("books/xslt/{id}")
+    @Produces({"text/html", "application/xhtml+xml", "application/xml" })
+    public Book getBookXSLT(@PathParam("id") long id, 
+                            @QueryParam("name") String name,
+                            @MatrixParam("name2") String name2) {
+        // how to have Book2 populated ?
+        Book b = new Book();
+        b.setId(999);
+        b.setName("CXF in ");
+        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=772137&r1=772136&r2=772137&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
Wed May  6 10:34:42 2009
@@ -23,6 +23,11 @@
 import java.io.InputStream;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.FileRequestEntity;
@@ -30,8 +35,12 @@
 import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class JAXRSClientServerSpringBookTest extends AbstractBusClientServerTestBase {
@@ -39,7 +48,7 @@
     @BeforeClass
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly", 
-                   launchServer(BookServerSpring.class, true));
+                   launchServer(BookServerSpring.class));
     }
     
     @Test
@@ -173,13 +182,41 @@
     }
     
     @Test
-    public void testAddGetBookAegis() throws Exception {
+    public void testGetBookAegis() throws Exception {
         
         String endpointAddress =
             "http://localhost:9080/the/thebooks4/bookstore/books/aegis"; 
         getBook(endpointAddress, "resources/expected_add_book_aegis.txt", "application/xml");

     }
     
+    
+    @Test
+    public void testGetBookXSLTXml() throws Exception {
+        String endpointAddress =
+            "http://localhost:9080/the/thebooks5/bookstore/books/xslt";
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xml").path(666).matrix("name2", 2).query("name", "Action -
");
+        Book b = wc.get(Book.class);
+        assertEquals(666, b.getId());
+        assertEquals("CXF in Action - 2", b.getName());
+    }
+    
+    
+    @Test
+    public void testGetBookXSLTHtml() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:9080/the/thebooks5/bookstore/books/xslt";
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xhtml+xml").path(666).matrix("name2", 2).query("name", "Action
- ");
+        XMLSource source = wc.get(XMLSource.class);
+        Map<String, String> namespaces = new HashMap<String, String>();
+        namespaces.put("xhtml", "http://www.w3.org/1999/xhtml");
+        Book2 b = source.getNode("xhtml:html/xhtml:body/xhtml:ul/xhtml:Book", namespaces,
Book2.class);
+        assertEquals(666, b.getId());
+        assertEquals("CXF in Action - 2", b.getName());
+    }
+    
     @Test
     public void testAddValidBookJson() throws Exception {
         doPost("http://localhost:9080/the/bookstore/books/convert",
@@ -236,4 +273,31 @@
         return bos.getOut().toString();        
     }
 
+    @Ignore
+    @XmlRootElement(name = "Book", namespace = "http://www.w3.org/1999/xhtml")
+    public static class Book2 {
+        @XmlElement(name = "id", namespace = "http://www.w3.org/1999/xhtml")
+        private long id1;
+        @XmlElement(name = "name", namespace = "http://www.w3.org/1999/xhtml")
+        private String name1;
+        public Book2() {
+            
+        }
+        public long getId() {
+            return id1;
+        }
+        
+        public void setId(Long theId) {
+            id1 = theId;
+        }
+        
+        public String getName() {
+            return name1;
+        }
+        
+        public void setName(String n) {
+            name1 = n;
+        }
+        
+    }
 }

Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java?rev=772137&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java (added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java Wed
May  6 10:34:42 2009
@@ -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.io.InputStream;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamSource;
+
+public class URIResolverImpl implements URIResolver {
+
+    public Source resolve(String href, String base) throws TransformerException {
+        InputStream is = getClass().getClassLoader()
+            .getResourceAsStream("org/apache/cxf/systest/jaxrs/resources/" + href);
+        return new StreamSource(is);
+    }
+
+}

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/URIResolverImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xhtml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xhtml?rev=772137&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xhtml (added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xhtml Wed
May  6 10:34:42 2009
@@ -0,0 +1,10 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:books="http://www.w3.org/books">
+<head> <title>Testing XML Example</title> </head>
+<body>
+	<h1>Book</h1>
+	<ul>
+	   <books:bookTag/>
+	</ul>
+</body>
+</html>

Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl?rev=772137&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl (added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl Wed
May  6 10:34:42 2009
@@ -0,0 +1,16 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:books="http://www.w3.org/books">
+ 
+ <xsl:import href="template2.xsl"/>
+
+ <xsl:variable name="root" select="/"/>
+ <xsl:variable name="htmlDoc" select="document('book.xhtml')"/>
+
+ <xsl:template match="/">
+    <xsl:apply-templates select="$htmlDoc/*"/>
+ </xsl:template>
+ 
+ <xsl:template match="books:bookTag">
+    <xsl:apply-templates select="$root/*"/>
+ </xsl:template>
+ 
+</xsl:stylesheet> 

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template.xsl
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl?rev=772137&view=auto
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
(added)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
Wed May  6 10:34:42 2009
@@ -0,0 +1,28 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:books="http://www.w3.org/books">
+
+ <xsl:param name="id" select="''"/>
+ <xsl:param name="name" select="''"/>
+ <xsl:param name="name2" select="''"/>
+
+ <xsl:template match="@*|node()">
+   <xsl:copy>
+      <xsl:apply-templates select="@*|node()"/>
+   </xsl:copy>
+ </xsl:template>
+ 
+ <xsl:template match="id">
+     <xsl:copy>
+	   <xsl:value-of select="$id"/>
+	</xsl:copy>
+ </xsl:template>
+ 
+ <xsl:template match="name">
+	<xsl:copy>
+	   <xsl:value-of select="."/>
+	   <xsl:value-of select="$name"/>
+	   <xsl:value-of select="$name2"/>
+	</xsl:copy>
+ </xsl:template>
+ 
+ 
+</xsl:stylesheet> 

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/template2.xsl
------------------------------------------------------------------------------
    svn:mime-type = text/xml

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=772137&r1=772136&r2=772137&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 Wed May  6 10:34:42 2009
@@ -90,6 +90,32 @@
     
   </jaxrs:server> 
 
+  <jaxrs:server id="bookservice5"
+		        address="/thebooks5/bookstore">
+    <jaxrs:serviceBeans>
+      <ref bean="serviceBean" />
+    </jaxrs:serviceBeans>		  
+
+    <jaxrs:providers>
+       <ref bean="xsltProvider"/>
+    </jaxrs:providers> 
+  </jaxrs:server> 
+  
+  <util:map id="outTemplates">
+      <entry key="application/xhtml+xml" value="classpath:/org/apache/cxf/systest/jaxrs/resources/template.xsl"/>
+      <entry key="application/xml" value="classpath:/org/apache/cxf/systest/jaxrs/resources/template2.xsl"/>
+  </util:map> 
+  
+  <bean id="xsltProvider" class="org.apache.cxf.jaxrs.provider.XSLTJaxbProvider"> 
  
+      <property name="resolver" ref="uriResolver"/>
+      <!-- 
+      <property name="outTemplate" value="classpath:/org/apache/cxf/systest/jaxrs/resources/template.xsl"/>
+      -->
+      <property name="outMediaTemplates" ref="outTemplates"/>
+  </bean>
+
+  <bean id="uriResolver" class="org.apache.cxf.systest.jaxrs.URIResolverImpl"/>   
 
+
   <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
       <property name="schemaHandler" ref="schemaHolder"/>
   </bean>



Mime
View raw message