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