View Javadoc

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