1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_extensions.hxx" 26 27 #include "soapsender.hxx" 28 #include <com/sun/star/ucb/XSimpleFileAccess.hpp> 29 #include <com/sun/star/uno/RuntimeException.hpp> 30 #include <com/sun/star/util/XURLTransformer.hpp> 31 #include <com/sun/star/io/XTempFile.hpp> 32 #include <com/sun/star/io/XOutputStream.hpp> 33 #include <com/sun/star/io/XInputStream.hpp> 34 #include <osl/socket.hxx> 35 #include <rtl/strbuf.hxx> 36 #include <boost/shared_ptr.hpp> 37 38 39 using namespace com::sun::star::uno; 40 using namespace com::sun::star::lang; 41 using namespace com::sun::star::io; 42 using boost::shared_ptr; 43 using com::sun::star::io::XTempFile; 44 using com::sun::star::ucb::XSimpleFileAccess; 45 using com::sun::star::util::URL; 46 using com::sun::star::util::XURLTransformer; 47 using osl::ConnectorSocket; 48 using rtl::OString; 49 using rtl::OUString; 50 using rtl::OStringBuffer; 51 52 53 namespace 54 { getHttpPostHeader(OString path,sal_Int32 length)55 static OString getHttpPostHeader(OString path, sal_Int32 length) 56 { 57 OStringBuffer result = 58 "POST " + path + " HTTP/1.0\r\n" 59 "Content-Type: text/xml; charset=\"utf-8\"\r\n" 60 "Content-Length: "; 61 result.append(length); 62 result.append("\r\nSOAPAction: \"\"\r\n\r\n"); 63 return result.makeStringAndClear(); 64 }; 65 } 66 67 namespace oooimprovement 68 { SoapSender(const Reference<XMultiServiceFactory> sf,const OUString & url)69 SoapSender::SoapSender(const Reference<XMultiServiceFactory> sf, const OUString& url) 70 : m_ServiceFactory(sf) 71 , m_Url(url) 72 { } 73 send(const SoapRequest & request) const74 void SoapSender::send(const SoapRequest& request) const 75 { 76 Reference<XTempFile> temp_file( 77 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.io.TempFile")), 78 UNO_QUERY_THROW); 79 Reference<XSimpleFileAccess> file_access( 80 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")), 81 UNO_QUERY_THROW); 82 Reference<XURLTransformer> url_trans( 83 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.util.URLTransformer")), 84 UNO_QUERY_THROW); 85 86 // writing request to tempfile 87 { 88 Reference<XOutputStream> temp_stream = temp_file->getOutputStream(); 89 request.writeTo(temp_stream); 90 temp_stream->flush(); 91 temp_stream->closeOutput(); 92 } 93 94 // parse Url 95 URL url; 96 { 97 url.Complete = m_Url; 98 url_trans->parseStrict(url); 99 } 100 101 // connect socket 102 shared_ptr<ConnectorSocket> socket(new ConnectorSocket(osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream)); 103 { 104 ::osl::SocketAddr addr(url.Server, url.Port); 105 oslSocketResult result = socket->connect(addr); 106 if(result != osl_Socket_Ok) 107 throw RuntimeException( 108 OUString::createFromAscii("unable to connect to SOAP server"), 109 Reference<XInterface>()); 110 } 111 112 // send header 113 { 114 OStringBuffer path_on_server = 115 OUStringToOString(url.Path, RTL_TEXTENCODING_ASCII_US) + 116 OUStringToOString(url.Name, RTL_TEXTENCODING_ASCII_US); 117 const OString header = getHttpPostHeader(path_on_server.makeStringAndClear(), file_access->getSize(temp_file->getUri())); 118 if(socket->write(header.getStr(), header.getLength()) != static_cast<sal_Int32>(header.getLength())) 119 throw RuntimeException( 120 OUString::createFromAscii("error while sending HTTP header"), 121 Reference<XInterface>()); 122 } 123 124 // send soap request 125 { 126 Reference<XInputStream> temp_stream = file_access->openFileRead(temp_file->getUri()); 127 const sal_Int32 bufsize = 1024; 128 sal_Int32 bytes_read; 129 Sequence<sal_Int8> buf(bufsize); 130 char buf2[bufsize]; 131 do 132 { 133 bytes_read = temp_stream->readBytes(buf, bufsize); 134 buf.realloc(bytes_read); 135 for(sal_Int32 idx = 0; idx < bytes_read; idx++) 136 buf2[idx] = buf[idx]; 137 if(socket->write(buf2, bytes_read) != bytes_read) 138 throw RuntimeException( 139 OUString::createFromAscii("error while sending SOAP request"), 140 Reference<XInterface>()); 141 } while(bytes_read == bufsize); 142 } 143 144 // receive answer 145 { 146 const sal_Int32 bufsize = 1024; 147 char buf[bufsize]; 148 sal_Int32 bytes_read = socket->read(buf, bufsize); 149 OString answer(buf, bytes_read); 150 const sal_Int32 returncode_start = answer.indexOf(' '); 151 if(returncode_start==-1 || !answer.copy(returncode_start, 4).equals(OString(" 200"))) 152 throw RuntimeException( 153 OUString::createFromAscii("SOAP server returns a error"), 154 Reference<XInterface>()); 155 } 156 } 157 } 158