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