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 | |
import org.slf4j.Logger; |
21 | |
import org.slf4j.LoggerFactory; |
22 | |
import org.apache.commons.httpclient.methods.RequestEntity; |
23 | |
import org.apache.commons.httpclient.methods.StringRequestEntity; |
24 | |
import org.w3c.dom.Document; |
25 | |
|
26 | |
import java.io.OutputStream; |
27 | |
import java.io.IOException; |
28 | |
import java.io.ByteArrayOutputStream; |
29 | |
|
30 | |
import javax.xml.transform.OutputKeys; |
31 | |
import javax.xml.transform.Transformer; |
32 | |
import javax.xml.transform.TransformerException; |
33 | |
import javax.xml.transform.TransformerFactory; |
34 | |
import javax.xml.transform.dom.DOMSource; |
35 | |
import javax.xml.transform.stream.StreamResult; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class XmlRequestEntity |
41 | |
implements RequestEntity |
42 | |
{ |
43 | |
|
44 | 1 | private static Logger log = LoggerFactory.getLogger( XmlRequestEntity.class ); |
45 | |
|
46 | |
private final RequestEntity delegatee; |
47 | |
|
48 | |
public XmlRequestEntity( Document xmlDocument ) |
49 | |
throws IOException |
50 | |
{ |
51 | 24 | super(); |
52 | 24 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
53 | |
|
54 | |
try |
55 | |
{ |
56 | 24 | TransformerFactory factory = TransformerFactory.newInstance(); |
57 | 24 | Transformer transformer = factory.newTransformer(); |
58 | 24 | transformer.setOutputProperty( OutputKeys.METHOD, "xml" ); |
59 | 24 | transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); |
60 | 24 | transformer.setOutputProperty( OutputKeys.INDENT, "no" ); |
61 | 24 | transformer.transform( new DOMSource( xmlDocument ), new StreamResult( out ) ); |
62 | |
} |
63 | 0 | catch ( TransformerException e ) |
64 | |
{ |
65 | 0 | log.error( "XML serialization failed", e ); |
66 | 0 | IOException exception = new IOException( "XML serialization failed" ); |
67 | 0 | exception.initCause( e ); |
68 | 0 | throw exception; |
69 | 24 | } |
70 | |
|
71 | 24 | delegatee = new StringRequestEntity( out.toString(), "text/xml", "UTF-8" ); |
72 | 24 | } |
73 | |
|
74 | |
public boolean isRepeatable() |
75 | |
{ |
76 | 0 | return delegatee.isRepeatable(); |
77 | |
} |
78 | |
|
79 | |
public String getContentType() |
80 | |
{ |
81 | 48 | return delegatee.getContentType(); |
82 | |
} |
83 | |
|
84 | |
public void writeRequest( OutputStream out ) throws IOException |
85 | |
{ |
86 | 24 | delegatee.writeRequest( out ); |
87 | 24 | } |
88 | |
|
89 | |
public long getContentLength() |
90 | |
{ |
91 | 48 | return delegatee.getContentLength(); |
92 | |
} |
93 | |
} |