merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / oooimprovement / soapsender.cxx
blobbb248cdabb6e54dd6b991d6618b33bbe1725405c
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: soapsender.cxx,v $
10 * $Revision: 1.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_extensions.hxx"
33 #include "soapsender.hxx"
34 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
35 #include <com/sun/star/uno/RuntimeException.hpp>
36 #include <com/sun/star/util/XURLTransformer.hpp>
37 #include <com/sun/star/io/XTempFile.hpp>
38 #include <com/sun/star/io/XOutputStream.hpp>
39 #include <com/sun/star/io/XInputStream.hpp>
40 #include <osl/socket.hxx>
41 #include <rtl/strbuf.hxx>
42 #include <boost/shared_ptr.hpp>
45 using namespace com::sun::star::uno;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::io;
48 using boost::shared_ptr;
49 using com::sun::star::io::XTempFile;
50 using com::sun::star::ucb::XSimpleFileAccess;
51 using com::sun::star::util::URL;
52 using com::sun::star::util::XURLTransformer;
53 using osl::ConnectorSocket;
54 using rtl::OString;
55 using rtl::OUString;
56 using rtl::OStringBuffer;
59 namespace
61 static OString getHttpPostHeader(OString path, sal_Int32 length)
63 OStringBuffer result =
64 "POST " + path + " HTTP/1.0\r\n"
65 "Content-Type: text/xml; charset=\"utf-8\"\r\n"
66 "Content-Length: ";
67 result.append(length);
68 result.append("\r\nSOAPAction: \"\"\r\n\r\n");
69 return result.makeStringAndClear();
73 namespace oooimprovement
75 SoapSender::SoapSender(const Reference<XMultiServiceFactory> sf, const OUString& url)
76 : m_ServiceFactory(sf)
77 , m_Url(url)
78 { }
80 void SoapSender::send(const SoapRequest& request) const
82 Reference<XTempFile> temp_file(
83 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.io.TempFile")),
84 UNO_QUERY_THROW);
85 Reference<XSimpleFileAccess> file_access(
86 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
87 UNO_QUERY_THROW);
88 Reference<XURLTransformer> url_trans(
89 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.util.URLTransformer")),
90 UNO_QUERY_THROW);
92 // writing request to tempfile
94 Reference<XOutputStream> temp_stream = temp_file->getOutputStream();
95 request.writeTo(temp_stream);
96 temp_stream->flush();
97 temp_stream->closeOutput();
100 // parse Url
101 URL url;
103 url.Complete = m_Url;
104 url_trans->parseStrict(url);
107 // connect socket
108 shared_ptr<ConnectorSocket> socket(new ConnectorSocket(osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream));
110 ::osl::SocketAddr addr(url.Server, url.Port);
111 oslSocketResult result = socket->connect(addr);
112 if(result != osl_Socket_Ok)
113 throw RuntimeException(
114 OUString::createFromAscii("unable to connect to SOAP server"),
115 Reference<XInterface>());
118 // send header
120 OStringBuffer path_on_server =
121 OUStringToOString(url.Path, RTL_TEXTENCODING_ASCII_US) +
122 OUStringToOString(url.Name, RTL_TEXTENCODING_ASCII_US);
123 const OString header = getHttpPostHeader(path_on_server.makeStringAndClear(), file_access->getSize(temp_file->getUri()));
124 if(socket->write(header.getStr(), header.getLength()) != static_cast<sal_Int32>(header.getLength()))
125 throw RuntimeException(
126 OUString::createFromAscii("error while sending HTTP header"),
127 Reference<XInterface>());
130 // send soap request
132 Reference<XInputStream> temp_stream = file_access->openFileRead(temp_file->getUri());
133 const sal_Int32 bufsize = 1024;
134 sal_Int32 bytes_read;
135 Sequence<sal_Int8> buf(bufsize);
136 char buf2[bufsize];
139 bytes_read = temp_stream->readBytes(buf, bufsize);
140 buf.realloc(bytes_read);
141 for(sal_Int32 idx = 0; idx < bytes_read; idx++)
142 buf2[idx] = buf[idx];
143 if(socket->write(buf2, bytes_read) != bytes_read)
144 throw RuntimeException(
145 OUString::createFromAscii("error while sending SOAP request"),
146 Reference<XInterface>());
147 } while(bytes_read == bufsize);
150 // receive answer
152 const sal_Int32 bufsize = 1024;
153 char buf[bufsize];
154 sal_Int32 bytes_read = socket->read(buf, bufsize);
155 OString answer(buf, bytes_read);
156 const sal_Int32 returncode_start = answer.indexOf(' ');
157 if(returncode_start==-1 || !answer.copy(returncode_start, 4).equals(OString(" 200")))
158 throw RuntimeException(
159 OUString::createFromAscii("SOAP server returns a error"),
160 Reference<XInterface>());