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.apache.http.client.methods.HttpEntityEnclosingRequestBase;
023import org.apache.http.client.methods.HttpPost;
024import org.apache.http.entity.StringEntity;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.w3c.dom.Document;
028
029import java.io.OutputStream;
030import java.io.IOException;
031import java.io.ByteArrayOutputStream;
032
033import javax.xml.transform.OutputKeys;
034import javax.xml.transform.Transformer;
035import javax.xml.transform.TransformerException;
036import javax.xml.transform.TransformerFactory;
037import javax.xml.transform.dom.DOMSource;
038import javax.xml.transform.stream.StreamResult;
039
040/**
041 * <code>XmlRequestEntity</code>...
042 * @deprecated is it really use???
043 */
044public class XmlRequestEntity
045    extends HttpEntityEnclosingRequestBase
046{
047
048    private static Logger log = LoggerFactory.getLogger( XmlRequestEntity.class );
049
050    private final StringEntity delegatee;
051
052    public XmlRequestEntity( Document xmlDocument )
053        throws IOException
054    {
055        super();
056        ByteArrayOutputStream out = new ByteArrayOutputStream();
057
058        try
059        {
060            TransformerFactory factory = TransformerFactory.newInstance();
061            Transformer transformer = factory.newTransformer();
062            transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
063            transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
064            transformer.setOutputProperty( OutputKeys.INDENT, "no" );
065            transformer.transform( new DOMSource( xmlDocument ), new StreamResult( out ) );
066        }
067        catch ( TransformerException e )
068        {
069            log.error( "XML serialization failed", e );
070            IOException exception = new IOException( "XML serialization failed" );
071            exception.initCause( e );
072            throw exception;
073        }
074
075        delegatee = new StringEntity( out.toString(), "text/xml", "UTF-8" );
076    }
077
078    public boolean isRepeatable()
079    {
080        return delegatee.isRepeatable();
081    }
082
083    public String getContentType()
084    {
085        return delegatee.getContentType().getValue();
086    }
087
088    public void writeRequest( OutputStream out ) throws IOException
089    {
090        delegatee.writeTo( out );
091    }
092
093    public long getContentLength()
094    {
095        return delegatee.getContentLength();
096    }
097
098    @Override
099    public String getMethod()
100    {
101        return HttpPost.METHOD_NAME;
102    }
103}