View Javadoc
1   package org.apache.jackrabbit.webdav.client.methods;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.apache.commons.httpclient.methods.RequestEntity;
25  import org.apache.commons.httpclient.methods.StringRequestEntity;
26  import org.w3c.dom.Document;
27  
28  import java.io.OutputStream;
29  import java.io.IOException;
30  import java.io.ByteArrayOutputStream;
31  
32  import javax.xml.transform.OutputKeys;
33  import javax.xml.transform.Transformer;
34  import javax.xml.transform.TransformerException;
35  import javax.xml.transform.TransformerFactory;
36  import javax.xml.transform.dom.DOMSource;
37  import javax.xml.transform.stream.StreamResult;
38  
39  /**
40   * <code>XmlRequestEntity</code>...
41   */
42  public class XmlRequestEntity
43      implements RequestEntity
44  {
45  
46      private static Logger log = LoggerFactory.getLogger( XmlRequestEntity.class );
47  
48      private final RequestEntity delegatee;
49  
50      public XmlRequestEntity( Document xmlDocument )
51          throws IOException
52      {
53          super();
54          ByteArrayOutputStream out = new ByteArrayOutputStream();
55  
56          try
57          {
58              TransformerFactory factory = TransformerFactory.newInstance();
59              Transformer transformer = factory.newTransformer();
60              transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
61              transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
62              transformer.setOutputProperty( OutputKeys.INDENT, "no" );
63              transformer.transform( new DOMSource( xmlDocument ), new StreamResult( out ) );
64          }
65          catch ( TransformerException e )
66          {
67              log.error( "XML serialization failed", e );
68              IOException exception = new IOException( "XML serialization failed" );
69              exception.initCause( e );
70              throw exception;
71          }
72  
73          delegatee = new StringRequestEntity( out.toString(), "text/xml", "UTF-8" );
74      }
75  
76      public boolean isRepeatable()
77      {
78          return delegatee.isRepeatable();
79      }
80  
81      public String getContentType()
82      {
83          return delegatee.getContentType();
84      }
85  
86      public void writeRequest( OutputStream out ) throws IOException
87      {
88          delegatee.writeRequest( out );
89      }
90  
91      public long getContentLength()
92      {
93          return delegatee.getContentLength();
94      }
95  }