1 package org.apache.jackrabbit.webdav.client.methods;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
23 import org.apache.http.client.methods.HttpPost;
24 import org.apache.http.entity.StringEntity;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.w3c.dom.Document;
28
29 import java.io.OutputStream;
30 import java.io.IOException;
31 import java.io.ByteArrayOutputStream;
32
33 import javax.xml.transform.OutputKeys;
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.stream.StreamResult;
39
40
41
42
43
44 public class XmlRequestEntity
45 extends HttpEntityEnclosingRequestBase
46 {
47
48 private static Logger log = LoggerFactory.getLogger( XmlRequestEntity.class );
49
50 private final StringEntity delegatee;
51
52 public XmlRequestEntity( Document xmlDocument )
53 throws IOException
54 {
55 super();
56 ByteArrayOutputStream out = new ByteArrayOutputStream();
57
58 try
59 {
60 TransformerFactory factory = TransformerFactory.newInstance();
61 Transformer transformer = factory.newTransformer();
62 transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
63 transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
64 transformer.setOutputProperty( OutputKeys.INDENT, "no" );
65 transformer.transform( new DOMSource( xmlDocument ), new StreamResult( out ) );
66 }
67 catch ( TransformerException e )
68 {
69 log.error( "XML serialization failed", e );
70 IOException exception = new IOException( "XML serialization failed" );
71 exception.initCause( e );
72 throw exception;
73 }
74
75 delegatee = new StringEntity( out.toString(), "text/xml", "UTF-8" );
76 }
77
78 public boolean isRepeatable()
79 {
80 return delegatee.isRepeatable();
81 }
82
83 public String getContentType()
84 {
85 return delegatee.getContentType().getValue();
86 }
87
88 public void writeRequest( OutputStream out ) throws IOException
89 {
90 delegatee.writeTo( out );
91 }
92
93 public long getContentLength()
94 {
95 return delegatee.getContentLength();
96 }
97
98 @Override
99 public String getMethod()
100 {
101 return HttpPost.METHOD_NAME;
102 }
103 }