View Javadoc

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