001package org.apache.jackrabbit.webdav.client.methods;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022import org.apache.commons.httpclient.methods.RequestEntity;
023import org.apache.commons.httpclient.methods.StringRequestEntity;
024import org.w3c.dom.Document;
025
026import java.io.OutputStream;
027import java.io.IOException;
028import java.io.ByteArrayOutputStream;
029
030import javax.xml.transform.OutputKeys;
031import javax.xml.transform.Transformer;
032import javax.xml.transform.TransformerException;
033import javax.xml.transform.TransformerFactory;
034import javax.xml.transform.dom.DOMSource;
035import javax.xml.transform.stream.StreamResult;
036
037/**
038 * <code>XmlRequestEntity</code>...
039 */
040public class XmlRequestEntity
041    implements RequestEntity
042{
043
044    private static Logger log = LoggerFactory.getLogger( XmlRequestEntity.class );
045
046    private final RequestEntity delegatee;
047
048    public XmlRequestEntity( Document xmlDocument )
049        throws IOException
050    {
051        super();
052        ByteArrayOutputStream out = new ByteArrayOutputStream();
053
054        try
055        {
056            TransformerFactory factory = TransformerFactory.newInstance();
057            Transformer transformer = factory.newTransformer();
058            transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
059            transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
060            transformer.setOutputProperty( OutputKeys.INDENT, "no" );
061            transformer.transform( new DOMSource( xmlDocument ), new StreamResult( out ) );
062        }
063        catch ( TransformerException e )
064        {
065            log.error( "XML serialization failed", e );
066            IOException exception = new IOException( "XML serialization failed" );
067            exception.initCause( e );
068            throw exception;
069        }
070
071        delegatee = new StringRequestEntity( out.toString(), "text/xml", "UTF-8" );
072    }
073
074    public boolean isRepeatable()
075    {
076        return delegatee.isRepeatable();
077    }
078
079    public String getContentType()
080    {
081        return delegatee.getContentType();
082    }
083
084    public void writeRequest( OutputStream out ) throws IOException
085    {
086        delegatee.writeRequest( out );
087    }
088
089    public long getContentLength()
090    {
091        return delegatee.getContentLength();
092    }
093}