Update ooo320-m1
[ooovba.git] / extensions / source / oooimprovement / soaprequest.cxx
bloba67fd8053068d18f9746dce80ebb37a4d6e37a61
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: soaprequest.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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #include "soaprequest.hxx"
35 #include "errormail.hxx"
36 #include "config.hxx"
37 #include <boost/shared_ptr.hpp>
38 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
41 #include <rtl/strbuf.hxx>
42 #include <rtl/ustring.hxx>
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::XOutputStream;
50 using com::sun::star::ucb::XSimpleFileAccess;
51 using rtl::OUString;
52 using rtl::OString;
53 using rtl::OStringBuffer;
56 namespace
58 static unsigned long asUlong(sal_Int8 input)
60 return *reinterpret_cast<unsigned char *>(&input);
63 static Sequence<sal_Int8> base64_encode(const Sequence<sal_Int8>& input)
65 static const char base64_tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66 Sequence<sal_Int8> result(4);
67 unsigned long value = asUlong(input[0]) << 16;
68 if(input.getLength() > 1) value |= asUlong(input[1]) << 8;
69 if(input.getLength() > 2) value |= asUlong(input[2]);
71 result[0] = static_cast<sal_Int8>(base64_tab[(value >> 18) & 0x3F]);
72 result[1] = static_cast<sal_Int8>(base64_tab[(value >> 12) & 0x3F]);
73 result[2] = static_cast<sal_Int8>('=');
74 result[3] = static_cast<sal_Int8>('=');
76 if (input.getLength() > 1)
78 result[2] = base64_tab[(value >> 6) & 0x3F];
79 if (input.getLength() > 2)
80 result[3] = base64_tab[(value >> 0) & 0x3F];
82 return result;
85 static OString replaceAll(const OString& str, sal_Char old, const OString& replacement)
87 OStringBuffer result;
88 sal_Int32 idx = 0;
89 do {
90 result.append(str.getToken(0, old, idx));
91 if(idx>=0) result.append(replacement);
92 } while(idx >= 0);
93 return result.makeStringAndClear();
96 static OString xmlEncode(const OString& input)
98 OString result = replaceAll(input, '&', OString("&amp;"));
99 result = replaceAll(result, '<', OString("&lt;"));
100 return replaceAll(result, '>', OString("&gt;"));
103 static shared_ptr<Sequence<sal_Int8> > createSequenceFromString(const OString& str)
105 const sal_Int32 size = str.getLength();
106 shared_ptr<Sequence<sal_Int8> > result(new Sequence<sal_Int8>(size));
107 for(sal_Int32 idx=0; idx < size; idx++)
108 (*result)[idx] = str[idx];
109 return result;
112 static void writeString(const Reference<XOutputStream>& target, const OString& str)
114 shared_ptr<Sequence<sal_Int8> > seq = createSequenceFromString(str);
115 target->writeBytes(*seq);
118 static void writeFile(const Reference<XMultiServiceFactory>& sf, const Reference<XOutputStream>& target, const OUString& fileurl)
120 Reference<XSimpleFileAccess> file_access(
121 sf->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
122 UNO_QUERY);
123 Reference<XInputStream> file = file_access->openFileRead(fileurl);
124 const sal_Int32 bufsize = 3;
125 sal_Int32 bytes_read;
126 Sequence<sal_Int8> buf(bufsize);
129 bytes_read = file->readBytes(buf, bufsize);
130 if(bytes_read < buf.getLength()) buf.realloc(bytes_read);
131 if(bytes_read) target->writeBytes(base64_encode(buf));
132 } while(bytes_read == bufsize);
135 static const OString SOAP_START(
136 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
137 "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
138 "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"
139 "xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"\n"
140 "xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\n"
141 "xmlns:rds=\"urn:ReportDataService\"\n"
142 "xmlns:apache=\"http://xml.apache.org/xml-soap\"\n"
143 "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
144 "<SOAP-ENV:Body>\n"
145 "<rds:submitReport>\n");
146 static const OString SOAP_ITEMS_START("<hash xsi:type=\"apache:Map\">\n");
147 static const OString SOAP_ITEMS_END("</hash>\n");
148 static const OString SOAP_END(
149 "</rds:submitReport>\n"
150 "</SOAP-ENV:Body>\n"
151 "</SOAP-ENV:Envelope>\n");
152 static const OString SOAP_ITEM_END("]]></value></item>\n");
154 static const OString getSoapOfficeversion(const Reference<XMultiServiceFactory>& sf)
156 return ::rtl::OUStringToOString(oooimprovement::Config(sf).getCompleteProductname(), RTL_TEXTENCODING_ASCII_US);
159 static const OString getSoapSoapId(const Reference<XMultiServiceFactory>& sf, const OString& soap_id)
161 OStringBuffer buf;
162 buf.append("<body xsi:type=\"xsd:string\">");
163 buf.append(xmlEncode(soap_id)).append("\n");
164 buf.append(xmlEncode(getSoapOfficeversion(sf))).append("\n");
165 buf.append("</body>\n");
166 return buf.makeStringAndClear();
169 static const OString getSoapItemStart(const OString& key)
171 OStringBuffer buf =
172 "<item>\n"
173 "<key xsi:type=\"xsd:string\">" + key + "</key>\n"
174 "<value xsi:type=\"xsd:string\"><![CDATA[";
175 return buf.makeStringAndClear();
179 namespace oooimprovement
181 SoapRequest::SoapRequest(const Reference<XMultiServiceFactory>& sf, const OUString& soap_id, const OUString& logfile)
182 : m_ServiceFactory(sf)
183 , m_SoapId(soap_id)
184 , m_Logfile(logfile)
187 void SoapRequest::writeTo(const Reference<XOutputStream>& target) const
189 writeString(target, SOAP_START);
190 writeString(
191 target,
192 getSoapSoapId(m_ServiceFactory, rtl::OUStringToOString(m_SoapId, RTL_TEXTENCODING_ASCII_US)));
193 writeString(target, SOAP_ITEMS_START);
194 writeString(target, getSoapItemStart("reportmail.xml"));
195 writeString(target, Errormail(m_ServiceFactory).getXml());
196 writeString(target, SOAP_ITEM_END);
197 writeString(target, getSoapItemStart("data.zip"));
198 writeFile(m_ServiceFactory, target, m_Logfile);
199 writeString(target, SOAP_ITEM_END);
200 writeString(target, SOAP_ITEMS_END);
201 writeString(target, SOAP_END);