1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 2001
20
* the Initial Developer. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
40
#include "nsISOAPParameter.h"
41
#include "nsSOAPMessage.h"
42
#include "nsISOAPEncoder.h"
43
#include "nsISOAPDecoder.h"
44
#include "nsSOAPEncoding.h"
45
#include "nsSOAPUtils.h"
46
#include "nsSOAPException.h"
47
#include "nsIServiceManager.h"
48
#include "nsIComponentManager.h"
49
#include "nsIDOMNodeList.h"
50
#include "nsISchema.h"
51
#include "nsISchemaLoader.h"
52
#include "nsSOAPUtils.h"
55
// callback for deleting the encodings from the nsObjectHashtable, mEncodings,
56
// in the nsSOAPEncodingRegistry
58
static PRBool PR_CALLBACK
59
DeleteEncodingEntry(nsHashKey *aKey, void *aData, void *aClosure)
61
NS_DELETEXPCOM((nsSOAPEncoding*)aData);
66
// First the registry, which is shared between associated encodings
67
// but is never seen by xpconnect.
70
NS_IMPL_ISUPPORTS1(nsSOAPEncodingRegistry, nsISOAPEncodingRegistry)
72
nsSOAPEncodingRegistry::nsSOAPEncodingRegistry(nsISOAPEncoding *aEncoding)
73
: mEncodings(nsnull, nsnull, DeleteEncodingEntry, nsnull, 4)
76
aEncoding->GetStyleURI(style);
77
NS_ASSERTION(!style.IsEmpty(), "nsSOAPEncoding Regsitry constructed without style");
79
nsStringKey styleKey(style);
80
mEncodings.Put(&styleKey, aEncoding);
84
nsSOAPEncodingRegistry::GetAssociatedEncoding(const nsAString & aStyleURI,
86
nsISOAPEncoding **aEncoding)
88
NS_SOAP_ENSURE_ARG_STRING(aStyleURI);
89
NS_ENSURE_ARG_POINTER(aEncoding);
91
nsStringKey styleKey(aStyleURI);
92
*aEncoding = (nsISOAPEncoding *) mEncodings.Get(&styleKey);
94
nsCOMPtr < nsISOAPEncoding > defaultEncoding;
95
nsCAutoString encodingContractid;
96
encodingContractid.Assign(NS_SOAPENCODING_CONTRACTID_PREFIX);
97
encodingContractid.Append(NS_ConvertUCS2toUTF8(aStyleURI));
98
defaultEncoding = do_GetService(encodingContractid.get());
99
if (defaultEncoding || aCreateIf) {
100
nsCOMPtr < nsISOAPEncoding > encoding = new nsSOAPEncoding(aStyleURI,this,defaultEncoding);
101
*aEncoding = encoding;
103
NS_ADDREF(*aEncoding);
104
mEncodings.Put(&styleKey, encoding);
107
return NS_ERROR_FAILURE;
112
// need to AddRef the *-style pointer coming from mEncodings
113
NS_ADDREF(*aEncoding);
119
nsSOAPEncodingRegistry::SetSchemaCollection(nsISchemaCollection *
122
NS_ENSURE_ARG(aSchemaCollection);
123
mSchemaCollection = aSchemaCollection;
128
nsSOAPEncodingRegistry::GetSchemaCollection(nsISchemaCollection **
131
NS_ENSURE_ARG_POINTER(aSchemaCollection);
132
if (!mSchemaCollection) {
134
nsCOMPtr < nsISchemaLoader > loader =
135
do_CreateInstance(NS_SCHEMALOADER_CONTRACTID, &rv);
138
mSchemaCollection = do_QueryInterface(loader);
139
if (!mSchemaCollection)
140
return NS_ERROR_FAILURE;
142
*aSchemaCollection = mSchemaCollection;
143
NS_ADDREF(*aSchemaCollection);
148
// Second, the encodings themselves.
150
NS_IMPL_CI_INTERFACE_GETTER1(nsSOAPEncoding, nsISOAPEncoding)
151
NS_IMPL_QUERY_INTERFACE1_CI(nsSOAPEncoding, nsISOAPEncoding)
153
// Due to circular referencing with the registry we abdicate all ref counting
154
// to the registry itself. When the registry reaches zero it destroys all
155
// the encodings with itself.
156
NS_IMETHODIMP_(nsrefcnt)
157
nsSOAPEncoding::AddRef(void)
160
return mRegistry->AddRef();
164
NS_IMETHODIMP_(nsrefcnt)
165
nsSOAPEncoding::Release()
168
return mRegistry->Release();
172
nsSOAPEncoding::nsSOAPEncoding() : mEncoders(),
177
mStyleURI.Assign(gSOAPStrings->kSOAPEncURI11);
178
mRegistry = new nsSOAPEncodingRegistry(this);
179
mDefaultEncoding = do_GetService(NS_DEFAULTSOAPENCODING_1_1_CONTRACTID);
182
nsSOAPEncoding::nsSOAPEncoding(const nsAString & aStyleURI,
183
nsSOAPEncodingRegistry * aRegistry,
184
nsISOAPEncoding * aDefaultEncoding)
190
mStyleURI.Assign(aStyleURI);
191
mRegistry = aRegistry;
192
mDefaultEncoding = aDefaultEncoding;
196
nsSOAPEncoding::SetSchemaCollection(nsISchemaCollection *
199
NS_ENSURE_ARG(aSchemaCollection);
201
return NS_ERROR_FAILURE;
202
return mRegistry->SetSchemaCollection(aSchemaCollection);
206
nsSOAPEncoding::GetSchemaCollection(nsISchemaCollection **
209
NS_ENSURE_ARG_POINTER(aSchemaCollection);
211
return NS_ERROR_FAILURE;
212
return mRegistry->GetSchemaCollection(aSchemaCollection);
215
/* readonly attribute AString styleURI; */
216
NS_IMETHODIMP nsSOAPEncoding::GetStyleURI(nsAString & aStyleURI)
218
NS_ENSURE_ARG_POINTER(&aStyleURI);
219
aStyleURI.Assign(mStyleURI);
223
/* nsISOAPEncoding getAssociatedEncoding (in AString aStyleURI, in boolean aCreateIf); */
225
nsSOAPEncoding::GetAssociatedEncoding(const nsAString & aStyleURI,
227
nsISOAPEncoding ** _retval)
229
NS_SOAP_ENSURE_ARG_STRING(aStyleURI);
230
NS_ENSURE_ARG_POINTER(_retval);
232
return NS_ERROR_FAILURE;
233
return mRegistry->GetAssociatedEncoding(aStyleURI, aCreateIf, _retval);
236
/* nsISOAPEncoder setEncoder (in AString aKey, in nsISOAPEncoder aEncoder); */
238
nsSOAPEncoding::SetEncoder(const nsAString & aKey,
239
nsISOAPEncoder * aEncoder)
241
NS_SOAP_ENSURE_ARG_STRING(aKey);
242
NS_ENSURE_ARG(aEncoder);
243
nsStringKey nameKey(aKey);
245
mEncoders.Put(&nameKey, aEncoder, nsnull);
247
mEncoders.Remove(&nameKey, nsnull);
252
/* nsISOAPEncoder getEncoder (in AString aKey); */
254
nsSOAPEncoding::GetEncoder(const nsAString & aKey,
255
nsISOAPEncoder ** _retval)
257
NS_SOAP_ENSURE_ARG_STRING(aKey);
258
NS_ENSURE_ARG_POINTER(_retval);
259
nsStringKey nameKey(aKey);
260
*_retval = (nsISOAPEncoder *) mEncoders.Get(&nameKey);
261
if (*_retval == nsnull && mDefaultEncoding) {
262
return mDefaultEncoding->GetEncoder(aKey, _retval);
267
/* nsISOAPDecoder setDecoder (in AString aKey, in nsISOAPDecoder aDecoder); */
269
nsSOAPEncoding::SetDecoder(const nsAString & aKey,
270
nsISOAPDecoder * aDecoder)
272
NS_SOAP_ENSURE_ARG_STRING(aKey);
273
NS_ENSURE_ARG(aDecoder);
274
nsStringKey nameKey(aKey);
276
mDecoders.Put(&nameKey, aDecoder, nsnull);
278
mDecoders.Remove(&nameKey, nsnull);
283
/* nsISOAPDecoder getDecoder (in AString aKey); */
285
nsSOAPEncoding::GetDecoder(const nsAString & aKey,
286
nsISOAPDecoder ** _retval)
288
NS_SOAP_ENSURE_ARG_STRING(aKey);
289
NS_ENSURE_ARG_POINTER(_retval);
290
nsStringKey nameKey(aKey);
291
*_retval = (nsISOAPDecoder *) mDecoders.Get(&nameKey);
292
if (*_retval == nsnull && mDefaultEncoding) {
293
return mDefaultEncoding->GetDecoder(aKey, _retval);
298
/* nsIDOMElement encode (in nsIVariant aSource, in AString aNamespaceURI, in AString aName, in nsISchemaType aSchemaType, in nsISOAPAttachments aAttachments, in nsIDOMElement aDestination); */
300
nsSOAPEncoding::Encode(nsIVariant * aSource,
301
const nsAString & aNamespaceURI,
302
const nsAString & aName,
303
nsISchemaType * aSchemaType,
304
nsISOAPAttachments * aAttachments,
305
nsIDOMElement * aDestination,
306
nsIDOMElement ** _retval)
308
NS_ENSURE_ARG(aSource);
309
NS_ENSURE_ARG_POINTER(_retval);
311
nsCOMPtr < nsISOAPEncoder > encoder;
312
nsresult rv = GetDefaultEncoder(getter_AddRefs(encoder));
316
return encoder->Encode(this, aSource, aNamespaceURI, aName,
317
aSchemaType, aAttachments, aDestination,
321
return SOAP_EXCEPTION(NS_ERROR_NOT_IMPLEMENTED,"SOAP_DEFAULT_ENCODER", "Encoding style does not have a default encoder.");
324
/* nsIVariant decode (in nsIDOMElement aSource, in nsISchemaType aSchemaType, in nsISOAPAttachments aAttachments); */
326
nsSOAPEncoding::Decode(nsIDOMElement * aSource,
327
nsISchemaType * aSchemaType,
328
nsISOAPAttachments * aAttachments,
329
nsIVariant ** _retval)
331
NS_ENSURE_ARG(aSource);
332
NS_ENSURE_ARG_POINTER(_retval);
333
nsCOMPtr < nsISOAPDecoder > decoder;
334
nsresult rv = GetDefaultDecoder(getter_AddRefs(decoder));
338
return decoder->Decode(this, aSource, aSchemaType, aAttachments,
342
return SOAP_EXCEPTION(NS_ERROR_NOT_IMPLEMENTED,"SOAP_DEFAULT_ENCODER", "Encoding style does not have a default decoder.");
345
/* attribute nsISOAPEncoder defaultEncoder; */
347
nsSOAPEncoding::GetDefaultEncoder(nsISOAPEncoder * *aDefaultEncoder)
349
NS_ENSURE_ARG_POINTER(aDefaultEncoder);
350
if (mDefaultEncoding && !mDefaultEncoder) {
351
return mDefaultEncoding->GetDefaultEncoder(aDefaultEncoder);
353
*aDefaultEncoder = mDefaultEncoder;
354
NS_IF_ADDREF(*aDefaultEncoder);
359
nsSOAPEncoding::SetDefaultEncoder(nsISOAPEncoder * aDefaultEncoder)
361
mDefaultEncoder = aDefaultEncoder;
365
/* attribute nsISOAPDecoder defaultDecoder; */
367
nsSOAPEncoding::GetDefaultDecoder(nsISOAPDecoder * *aDefaultDecoder)
369
NS_ENSURE_ARG_POINTER(aDefaultDecoder);
370
if (mDefaultEncoding && !mDefaultDecoder) {
371
return mDefaultEncoding->GetDefaultDecoder(aDefaultDecoder);
373
*aDefaultDecoder = mDefaultDecoder;
374
NS_IF_ADDREF(*aDefaultDecoder);
379
nsSOAPEncoding::SetDefaultDecoder(nsISOAPDecoder * aDefaultDecoder)
381
mDefaultDecoder = aDefaultDecoder;
386
nsSOAPEncoding::MapSchemaURI(const nsAString & aExternalURI,
387
const nsAString & aInternalURI,
391
NS_ENSURE_ARG_POINTER(&aExternalURI);
392
NS_ENSURE_ARG_POINTER(&aInternalURI);
393
if (aExternalURI.IsEmpty() || aInternalURI.IsEmpty()) // Permit no empty URIs.
394
return SOAP_EXCEPTION(NS_ERROR_ILLEGAL_VALUE,"SOAP_SCHEMA_URI_MAPPING", "No schema URI mapping possible of empty strings.");
395
nsStringKey externalKey(aExternalURI);
396
if (mMappedExternal.Exists(&externalKey)) {
397
*_retval = PR_FALSE; // Do not permit duplicate external
401
nsStringKey internalKey(aInternalURI);
402
if (mMappedInternal.Exists(&internalKey)) {
403
*_retval = PR_FALSE; // Do not permit duplicate internal
407
nsCOMPtr < nsIWritableVariant > p =
408
do_CreateInstance(NS_VARIANT_CONTRACTID, &rc);
411
p->SetAsAString(aExternalURI);
414
mMappedInternal.Put(&internalKey, p);
417
nsCOMPtr < nsIWritableVariant > p =
418
do_CreateInstance(NS_VARIANT_CONTRACTID, &rc);
421
p->SetAsAString(aInternalURI);
424
mMappedExternal.Put(&externalKey, p);
430
/* boolean unmapSchemaURI (in AString aExternalURI); */
431
NS_IMETHODIMP nsSOAPEncoding::UnmapSchemaURI(const nsAString & aExternalURI, PRBool *_retval)
433
NS_ENSURE_ARG_POINTER(&aExternalURI);
434
nsStringKey externalKey(aExternalURI);
435
nsCOMPtr<nsIVariant> internal = dont_AddRef(NS_STATIC_CAST(nsIVariant*,mMappedExternal.Get(&externalKey)));
437
nsAutoString internalstr;
438
nsresult rc = internal->GetAsAString(internalstr);
441
nsStringKey internalKey(internalstr);
442
mMappedExternal.Remove(&externalKey);
443
mMappedInternal.Remove(&internalKey);
454
/* AString getInternalSchemaURI (in AString aExternalURI); */
455
NS_IMETHODIMP nsSOAPEncoding::GetInternalSchemaURI(const nsAString & aExternalURI, nsAString & _retval)
457
NS_ENSURE_ARG_POINTER(&aExternalURI);
458
NS_ENSURE_ARG_POINTER(&_retval);
459
if (mMappedExternal.Count()) {
460
nsStringKey externalKey(aExternalURI);
461
nsCOMPtr<nsIVariant> internal = dont_AddRef(NS_STATIC_CAST(nsIVariant*,mMappedExternal.Get(&externalKey)));
463
return internal->GetAsAString(_retval);
466
if (mDefaultEncoding) {
467
return mDefaultEncoding->GetInternalSchemaURI(aExternalURI, _retval);
469
_retval.Assign(aExternalURI);
473
/* AString getExternalSchemaURI (in AString aInternalURI); */
474
NS_IMETHODIMP nsSOAPEncoding::GetExternalSchemaURI(const nsAString & aInternalURI, nsAString & _retval)
476
NS_ENSURE_ARG_POINTER(&aInternalURI);
477
NS_ENSURE_ARG_POINTER(&_retval);
478
if (mMappedInternal.Count()) {
479
nsStringKey internalKey(aInternalURI);
480
nsCOMPtr<nsIVariant> external = dont_AddRef(NS_STATIC_CAST(nsIVariant*,mMappedInternal.Get(&internalKey)));
482
return external->GetAsAString(_retval);
485
if (mDefaultEncoding) {
486
return mDefaultEncoding->GetExternalSchemaURI(aInternalURI, _retval);
488
_retval.Assign(aInternalURI);