summaryrefslogtreecommitdiff
path: root/extensions/source/oooimprovement/soaprequest.cxx
blob: fa7b600035847f6a3c7ec66aa41e9a306dadb35c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/


// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_extensions.hxx"

#include "soaprequest.hxx"
#include "errormail.hxx"
#include "config.hxx"
#include <boost/shared_ptr.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <rtl/strbuf.hxx>
#include <rtl/ustring.hxx>


using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::io;
using boost::shared_ptr;
using com::sun::star::io::XOutputStream;
using com::sun::star::ucb::XSimpleFileAccess;
using rtl::OUString;
using rtl::OString;
using rtl::OStringBuffer;


namespace
{
    static unsigned long asUlong(sal_Int8 input)
    {
        return *reinterpret_cast<unsigned char *>(&input);
    };

    static Sequence<sal_Int8> base64_encode(const Sequence<sal_Int8>& input)
    {
        static const char base64_tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        Sequence<sal_Int8> result(4);
        unsigned long value = asUlong(input[0]) << 16;
        if(input.getLength() > 1) value |= asUlong(input[1]) << 8;
        if(input.getLength() > 2) value |= asUlong(input[2]);

        result[0] = static_cast<sal_Int8>(base64_tab[(value >> 18) & 0x3F]);
        result[1] = static_cast<sal_Int8>(base64_tab[(value >> 12) & 0x3F]);
        result[2] = static_cast<sal_Int8>('=');
        result[3] = static_cast<sal_Int8>('=');

        if (input.getLength() > 1)
        {
            result[2] = base64_tab[(value >> 6) & 0x3F];
            if (input.getLength() > 2)
                 result[3] = base64_tab[(value >> 0) & 0x3F];
        }
        return result;
    };

    static OString replaceAll(const OString& str, sal_Char old, const OString& replacement)
    {
        OStringBuffer result;
        sal_Int32 idx = 0;
        do {
            result.append(str.getToken(0, old, idx));
            if(idx>=0) result.append(replacement);
        } while(idx >= 0);
        return result.makeStringAndClear();
    };

    static OString xmlEncode(const OString& input)
    {
        OString result = replaceAll(input, '&', OString("&amp;"));
        result = replaceAll(result, '<', OString("&lt;"));
        return replaceAll(result, '>', OString("&gt;"));
    }

    static shared_ptr<Sequence<sal_Int8> > createSequenceFromString(const OString& str)
    {
        const sal_Int32 size = str.getLength();
        shared_ptr<Sequence<sal_Int8> > result(new Sequence<sal_Int8>(size));
        for(sal_Int32 idx=0; idx < size; idx++)
            (*result)[idx] = str[idx];
        return result;
    };

    static void writeString(const Reference<XOutputStream>& target, const OString& str)
    {
        shared_ptr<Sequence<sal_Int8> > seq = createSequenceFromString(str);
        target->writeBytes(*seq);
    };

    static void writeFile(const Reference<XMultiServiceFactory>& sf, const Reference<XOutputStream>& target, const OUString& fileurl)
    {
        Reference<XSimpleFileAccess> file_access(
            sf->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.SimpleFileAccess"))),
            UNO_QUERY);
        Reference<XInputStream> file = file_access->openFileRead(fileurl);
        const sal_Int32 bufsize = 3;
        sal_Int32 bytes_read;
        Sequence<sal_Int8> buf(bufsize);
        do
        {
            bytes_read = file->readBytes(buf, bufsize);
            if(bytes_read < buf.getLength()) buf.realloc(bytes_read);
            if(bytes_read) target->writeBytes(base64_encode(buf));
        } while(bytes_read == bufsize);
    };

    static const OString SOAP_START(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
        "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"
        "xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"\n"
        "xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\n"
        "xmlns:rds=\"urn:ReportDataService\"\n"
        "xmlns:apache=\"http://xml.apache.org/xml-soap\"\n"
        "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
        "<SOAP-ENV:Body>\n"
        "<rds:submitReport>\n");
    static const OString SOAP_ITEMS_START("<hash xsi:type=\"apache:Map\">\n");
    static const OString SOAP_ITEMS_END("</hash>\n");
    static const OString SOAP_END(
        "</rds:submitReport>\n"
        "</SOAP-ENV:Body>\n"
        "</SOAP-ENV:Envelope>\n");
    static const OString SOAP_ITEM_END("]]></value></item>\n");

    static const OString getSoapOfficeversion(const Reference<XMultiServiceFactory>& sf)
    {
        return ::rtl::OUStringToOString(oooimprovement::Config(sf).getCompleteProductname(), RTL_TEXTENCODING_ASCII_US);
    };

    static const OString getSoapSoapId(const Reference<XMultiServiceFactory>& sf, const OString& soap_id)
    {
        OStringBuffer buf;
        buf.append("<body xsi:type=\"xsd:string\">");
        buf.append(xmlEncode(soap_id)).append("\n");
        buf.append(xmlEncode(getSoapOfficeversion(sf))).append("\n");
        buf.append("</body>\n");
        return buf.makeStringAndClear();
    };

    static const OString getSoapItemStart(const OString& key)
    {
        OStringBuffer buf =
            "<item>\n"
            "<key xsi:type=\"xsd:string\">" + key + "</key>\n"
            "<value xsi:type=\"xsd:string\"><![CDATA[";
        return buf.makeStringAndClear();
    };
}

namespace oooimprovement
{
    SoapRequest::SoapRequest(const Reference<XMultiServiceFactory>& sf, const OUString& soap_id, const OUString& logfile)
        : m_ServiceFactory(sf)
        , m_SoapId(soap_id)
        , m_Logfile(logfile)
    {}

     void SoapRequest::writeTo(const Reference<XOutputStream>& target) const
    {
        writeString(target, SOAP_START);
            writeString(
                target,
                getSoapSoapId(m_ServiceFactory, rtl::OUStringToOString(m_SoapId, RTL_TEXTENCODING_ASCII_US)));
            writeString(target, SOAP_ITEMS_START);
                writeString(target, getSoapItemStart("reportmail.xml"));
                    writeString(target, Errormail(m_ServiceFactory).getXml());
                writeString(target, SOAP_ITEM_END);
                writeString(target, getSoapItemStart("data.zip"));
                    writeFile(m_ServiceFactory, target, m_Logfile);
                writeString(target, SOAP_ITEM_END);
            writeString(target, SOAP_ITEMS_END);
        writeString(target, SOAP_END);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */