cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Prototyping the code to do the local WadlGenerator transforms
Date Wed, 04 Feb 2015 13:12:37 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 429afd6bb -> 4e85d25dc


Prototyping the code to do the local WadlGenerator transforms


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4e85d25d
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4e85d25d
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4e85d25d

Branch: refs/heads/3.0.x-fixes
Commit: 4e85d25dcc21ac18500bad7eceeae6bd04969332
Parents: 429afd6
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Feb 4 13:10:37 2015 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed Feb 4 13:12:06 2015 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/model/wadl/WadlGenerator.java     | 60 +++++++++++++++-----
 1 file changed, 45 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/4e85d25d/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
index 3422555..dd86d37 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
@@ -72,6 +72,7 @@ import javax.xml.namespace.QName;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMResult;
@@ -176,6 +177,7 @@ public class WadlGenerator implements ContainerRequestFilter {
     private MediaType defaultWadlResponseMediaType = MediaType.APPLICATION_XML_TYPE;
     private MediaType defaultRepMediaType = MediaType.WILDCARD_TYPE;
     private String stylesheetReference;
+    private boolean applyStylesheetLocally;
     private Bus bus;
     private DocumentationProvider docProvider;
     private ResourceIdGenerator idGenerator;     
@@ -230,15 +232,17 @@ public class WadlGenerator implements ContainerRequestFilter {
             return;
         }
 
-        boolean isJson = type == MediaType.APPLICATION_JSON_TYPE;
+        boolean isJson = isJson(type);
 
         StringBuilder sbMain = generateWADL(getBaseURI(m, ui), getResourcesList(m, ui), isJson,
m, ui);
 
         m.getExchange().put(JAXRSUtils.IGNORE_MESSAGE_WRITERS, !isJson && ignoreMessageWriters);
-        Response r = Response.ok().type(type).entity(createResponseEntity(sbMain.toString(),
isJson)).build();
+        Response r = Response.ok().type(type).entity(createResponseEntity(m, ui, sbMain.toString(),
isJson)).build();
         context.abortWith(r);
     }
-
+    private boolean isJson(MediaType mt) {
+        return mt == MediaType.APPLICATION_JSON_TYPE;
+    }
     private String getStylesheetInstructionData(Message m, UriInfo ui) {
         String theStylesheetReference = stylesheetReference;
         if (!keepRelativeDocLinks) {
@@ -253,7 +257,7 @@ public class WadlGenerator implements ContainerRequestFilter {
                                        Message m,
                                        UriInfo ui) {
         StringBuilder sbMain = new StringBuilder();
-        if (!isJson && stylesheetReference != null) {
+        if (!isJson && stylesheetReference != null && !applyStylesheetLocally)
{
             sbMain.append("<?xml-stylesheet " + getStylesheetInstructionData(m, ui) +
"?>");
         }
         sbMain.append("<application");
@@ -314,11 +318,15 @@ public class WadlGenerator implements ContainerRequestFilter {
         return sbMain;
     }
 
-    private Object createResponseEntity(String entity, boolean isJson) {
-        if (!isJson) {
-            return entity;
-        }
+    private Object createResponseEntity(Message m, UriInfo ui, String entity, boolean isJson)
{
         try {
+            if (!isJson) {
+                if (stylesheetReference != null && applyStylesheetLocally) {
+                    return transformLocally(m, ui, new StreamSource(new StringReader(entity)));
+                } else {
+                    return entity;
+                }
+            }
             return StaxUtils.read(new StringReader(entity));
         } catch (Exception ex) {
             throw ExceptionUtils.toInternalServerErrorException(ex, null);
@@ -1104,6 +1112,9 @@ public class WadlGenerator implements ContainerRequestFilter {
                 try {
                     InputStream is = ResourceUtils.getResourceStream(loc, (Bus)ep.get(Bus.class.getName()));
                     if (is != null) {
+                        if (isJson(mt)) {
+                            return Response.ok(is, mt).build();
+                        }
                         Document wadlDoc = StaxUtils.read(is);
                         Element appEl = wadlDoc.getDocumentElement();
 
@@ -1139,17 +1150,23 @@ public class WadlGenerator implements ContainerRequestFilter {
     }
     private Response finalizeExistingWadlResponse(Document wadlDoc, Message m, UriInfo ui,
MediaType mt) 
         throws Exception {
+        Object entity = null;
         if (stylesheetReference != null) {
-            ProcessingInstruction pi = wadlDoc.createProcessingInstruction("xml-stylesheet",

-                                          getStylesheetInstructionData(m, ui));
-            wadlDoc.insertBefore(pi, wadlDoc.getDocumentElement());
-            String wadlDocString = copyDOMToString(wadlDoc);
-            return Response.ok().type(mt).entity(wadlDocString).build();
+            if (!applyStylesheetLocally) {
+                ProcessingInstruction pi = wadlDoc.createProcessingInstruction("xml-stylesheet",

+                                              getStylesheetInstructionData(m, ui));
+                wadlDoc.insertBefore(pi, wadlDoc.getDocumentElement());
+                entity = copyDOMToString(wadlDoc);
+            } else {
+                entity = transformLocally(m, ui, new DOMSource(wadlDoc));
+            }
         } else {
-            return Response.ok().type(mt).entity(new DOMSource(wadlDoc)).build();
+            entity = new DOMSource(wadlDoc);
         }
+        return Response.ok(entity, mt).build();
+        
     }
-    private static String copyDOMToString(Document wadlDoc) throws Exception {
+    private String copyDOMToString(Document wadlDoc) throws Exception {
         DOMSource domSource = new DOMSource(wadlDoc);
         // temporary workaround
         StringWriter stringWriter = new StringWriter();
@@ -1158,6 +1175,15 @@ public class WadlGenerator implements ContainerRequestFilter {
         transformer.transform(domSource, new StreamResult(stringWriter));
         return stringWriter.toString();
     }
+    private String transformLocally(Message m, UriInfo ui, Source source) throws Exception
{
+        InputStream is = ResourceUtils.getResourceStream(stylesheetReference, m.getExchange().getBus());
+        Transformer t = TransformerFactory.newInstance().newTemplates(new StreamSource(is)).newTransformer();
+        t.setParameter("base.path", (String)m.get("http.base.path"));
+        StringWriter stringWriter = new StringWriter();
+        t.transform(source, new StreamResult(stringWriter));
+        return stringWriter.toString();
+    }
+    
 
     // TODO: deal with caching later on
     public Response getExistingResource(Message m, UriInfo ui, String href) {
@@ -2091,6 +2117,10 @@ public class WadlGenerator implements ContainerRequestFilter {
         this.keepRelativeDocLinks = keepRelativeDocLinks;
     }
 
+    public void setApplyStylesheetLocally(boolean applyStylesheetLocally) {
+        this.applyStylesheetLocally = applyStylesheetLocally;
+    }
+
     private static class SchemaConverter extends DelegatingXMLStreamWriter {
         private static final String SCHEMA_LOCATION = "schemaLocation";
         private Map<String, String> locsMap;


Mime
View raw message