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