summaryrefslogtreecommitdiffstats
path: root/lasso/id-wsf/discovery.c
blob: 47bcbd7e2de1678b9f072de3a66fa9b74954526d (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* $Id$
 *
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004 Entr'ouvert
 * http://lasso.entrouvert.org
 * 
 * Authors: Nicolas Clapies <nclapies@entrouvert.com>
 *          Valery Febvre <vfebvre@easter-eggs.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <lasso/xml/disco_modify.h>
#include <lasso/xml/disco_modify_response.h>
#include <lasso/xml/disco_query.h>
#include <lasso/xml/disco_query_response.h>

#include <lasso/xml/disco_insert_entry.h>
#include <lasso/xml/disco_remove_entry.h>

#include <lasso/id-wsf/discovery.h>

struct _LassoDiscoveryPrivate
{
	gboolean dispose_has_run;
};

/*****************************************************************************/
/* static methods/functions */
/*****************************************************************************/

/**
 * lasso_discovery_init_request:
 * @discovery: a LassoDiscovery
 * @resourceOffering: a LassoDiscoResourceOffering
 * @description: a LassoDiscoDescription
 * 
 * Generic static method used by lasso_discovery_init_modify() and lasso_discovery_init_query() 
 * 
 * Return value: 0 on success and a negative value if an error occurs.
 **/
static gint
lasso_discovery_init_request(LassoDiscovery             *discovery,
			     LassoDiscoResourceOffering *resourceOffering,
			     LassoDiscoDescription      *description)
{
	LassoWsfProfile *profile = LASSO_WSF_PROFILE(discovery);

	/* verify that description is present in resourceOffering->ServiceInstance->Description */
	if (g_list_find(resourceOffering->ServiceInstance->Description, description) == NULL) {
		message(G_LOG_LEVEL_CRITICAL, lasso_strerror(LASSO_PARAM_ERROR_INVALID_VALUE));
	}
	/* get ResourceID/EncryptedResourceID in description */
	if (resourceOffering->ResourceID != NULL) {
		if (LASSO_IS_DISCO_MODIFY(profile->request)) {
			LASSO_DISCO_MODIFY(profile->request)->ResourceID = \
				resourceOffering->ResourceID;
		}
		else if (LASSO_IS_DISCO_QUERY(profile->request)) {
			LASSO_DISCO_QUERY(profile->request)->ResourceID = \
				resourceOffering->ResourceID;
		}
	}
	else if (resourceOffering->EncryptedResourceID != NULL) {
		if (LASSO_IS_DISCO_MODIFY(profile->request)) {
			LASSO_DISCO_MODIFY(profile->request)->EncryptedResourceID = \
				resourceOffering->EncryptedResourceID;
		}
		else if (LASSO_IS_DISCO_QUERY(profile->request)) {
			LASSO_DISCO_QUERY(profile->request)->EncryptedResourceID = \
				resourceOffering->EncryptedResourceID;
		}
	}
	if (description->Endpoint != NULL) {
		profile->msg_url = description->Endpoint;
	}
	else if (description->WsdlURI != NULL) {
		/* TODO: get Endpoint at WsdlURI */
	}

	return 0;
}

/*****************************************************************************/
/* public methods */
/*****************************************************************************/

LassoDiscoInsertEntry*
lasso_discovery_add_insert_entry(LassoDiscovery                *discovery,
				 const gchar                   *serviceType,
				 const gchar                   *providerID,
				 GList                         *descriptions,
				 LassoDiscoResourceID          *resourceID,
				 LassoDiscoEncryptedResourceID *encryptedResourceID,
				 GList                         *options)
{
	LassoDiscoModify *modify;
	LassoDiscoInsertEntry *entry;
	LassoDiscoResourceOffering *resource;
	LassoDiscoServiceInstance *service;
	LassoDiscoOptions *opts;

	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), NULL);
	g_return_val_if_fail(serviceType!= NULL, NULL);
	g_return_val_if_fail(providerID != NULL, NULL);
	/* only one description is required */
	g_return_val_if_fail(g_list_length(descriptions) >= 1, NULL);
	/* resourceID/encryptedResourceID and options are optionals */
	g_return_val_if_fail((resourceID == NULL && encryptedResourceID == NULL) || \
			     (LASSO_IS_DISCO_RESOURCE_ID(resourceID) ^	\
			      LASSO_IS_DISCO_ENCRYPTED_RESOURCE_ID(encryptedResourceID)), NULL);

	modify = LASSO_DISCO_MODIFY(LASSO_WSF_PROFILE(discovery)->request);

	/* create InsertEntry */
	entry = lasso_disco_insert_entry_new();
	/* create ServiceInstance */
	service = lasso_disco_service_instance_new(serviceType, providerID, descriptions);
	/* create ResourceOffering */
	resource = lasso_disco_resource_offering_new(service);
	resource->ResourceID = resourceID;
	resource->EncryptedResourceID = encryptedResourceID;

	/* optionals data */
	/* create Options */
	if (options != NULL) {
		opts = lasso_disco_options_new();
		while (options != NULL) {
			opts->Option = g_list_append(opts->Option, options->data);
			options = g_list_next(options);
		}
		resource->Options = opts;
	}
	entry->ResourceOffering = resource;

	/* add InsertEntry */
	modify->InsertEntry = g_list_append(modify->InsertEntry, (gpointer)entry);

	return entry;
}

gint
lasso_discovery_add_remove_entry(LassoDiscovery *discovery,
				 const gchar    *entryID)
{
	LassoDiscoModify *modify;

	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(entryID != NULL, LASSO_PARAM_ERROR_INVALID_VALUE);

	modify = LASSO_DISCO_MODIFY(LASSO_WSF_PROFILE(discovery)->request);
	/* add RemoveEntry */
	modify->RemoveEntry = g_list_append(modify->RemoveEntry,
					    (gpointer)lasso_disco_remove_entry_new(entryID));

	return 0;
}

LassoDiscoRequestedServiceType*
lasso_discovery_add_requested_service_type(LassoDiscovery *discovery,
					   const gchar    *serviceType,
					   GList          *options)
{
	LassoDiscoQuery *query;
	LassoDiscoRequestedServiceType *rst;
	LassoDiscoOptions *opts;

	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), NULL);
	g_return_val_if_fail(serviceType != NULL, NULL);
	/* options is optional */

	query = LASSO_DISCO_QUERY(LASSO_WSF_PROFILE(discovery)->request);

	rst = lasso_disco_requested_service_type_new(serviceType);

	/* optionals data */
	/* create Options */
	if (options != NULL) {
		opts = lasso_disco_options_new();
		while (options != NULL) {
			opts->Option = g_list_append(opts->Option, options->data);
			options = g_list_next(options);
		}
		rst->Options = opts;
	}

	/* add RequestedServiceType */
	query->RequestedServiceType = g_list_append(query->RequestedServiceType, (gpointer)rst);

	return rst;
}

gint
lasso_discovery_add_resource_offering(LassoDiscovery             *discovery,
				      LassoDiscoResourceOffering *resourceOffering)
{
	LassoDiscoQueryResponse *query_response;

	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(LASSO_IS_DISCO_RESOURCE_OFFERING(resourceOffering),
			     LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);

	query_response = LASSO_DISCO_QUERY_RESPONSE(LASSO_WSF_PROFILE(discovery)->response);

	/* add ResourceOffering */
	query_response->ResourceOffering = g_list_append(query_response->ResourceOffering,
							 (gpointer)resourceOffering);
	
	return 0;
}

/**
 * lasso_discovery_destroy:
 * @discovery: a LassoDiscovery
 * 
 * Destroys LassoDiscovery objects created with lasso_discovery_new() or
 * lasso_discovery_new_from_dump().
 **/
void
lasso_discovery_destroy(LassoDiscovery *discovery)
{
	g_object_unref(G_OBJECT(discovery));
}

gint
lasso_discovery_init_modify(LassoDiscovery                *discovery,
			    LassoDiscoResourceOffering    *resourceOffering,
			    LassoDiscoDescription         *description)
{
	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(LASSO_IS_DISCO_RESOURCE_OFFERING(resourceOffering),
			     LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(LASSO_IS_DISCO_DESCRIPTION(description),
			     LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	
	LASSO_WSF_PROFILE(discovery)->request = LASSO_NODE(lasso_disco_modify_new());
	/*
	 * after the call of this method, app must add InsertEntry and RemoveEntry
	 */
	return lasso_discovery_init_request(discovery, resourceOffering, description);
}

gint
lasso_discovery_init_query(LassoDiscovery                *discovery,
			   LassoDiscoResourceOffering    *resourceOffering,
			   LassoDiscoDescription         *description)
{
	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(LASSO_IS_DISCO_RESOURCE_OFFERING(resourceOffering),
			     LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(LASSO_IS_DISCO_DESCRIPTION(description),
			     LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	
	LASSO_WSF_PROFILE(discovery)->request = LASSO_NODE(lasso_disco_query_new());
	/*
	 * after the call of this method, app must add requested service types
	 */
	return lasso_discovery_init_request(discovery, resourceOffering, description);
}

gint
lasso_discovery_process_modify_msg(LassoDiscovery *discovery,
				   const gchar    *message)
{
	LassoDiscoModify *request;
	LassoDiscoModifyResponse *response;
	LassoUtilityStatus *status;

	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(message != NULL, LASSO_PARAM_ERROR_INVALID_VALUE);

	request = lasso_disco_modify_new_from_message(message);
	LASSO_WSF_PROFILE(discovery)->request = LASSO_NODE(request);
	/* App should process insert entries and remove entries (in ResourceOffering) */

	status = lasso_utility_status_new(LASSO_DST_STATUS_CODE_OK);
	response = lasso_disco_modify_response_new(status);
	LASSO_WSF_PROFILE(discovery)->response = LASSO_NODE(response);

	return 0;
}

gint
lasso_discovery_process_modify_response_msg(LassoDiscovery *discovery, const gchar *message)
{
	LASSO_WSF_PROFILE(discovery)->response =
		LASSO_NODE(lasso_disco_modify_new_from_message(message));

	return 0;
}

gint
lasso_discovery_process_query_msg(LassoDiscovery *discovery, const gchar *message)
{
	LassoDiscoQuery *request;
	LassoDiscoQueryResponse *response;
	LassoUtilityStatus *status;

	g_return_val_if_fail(LASSO_IS_DISCOVERY(discovery), LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
	g_return_val_if_fail(message != NULL, LASSO_PARAM_ERROR_INVALID_VALUE);

	request = lasso_disco_query_new_from_message(message);
	LASSO_WSF_PROFILE(discovery)->request = LASSO_NODE(request);

	status = lasso_utility_status_new(LASSO_DST_STATUS_CODE_OK);
	response = lasso_disco_query_response_new(status);
	LASSO_WSF_PROFILE(discovery)->response = LASSO_NODE(response);

	/*
	 * after the call of this method, app must add ResourceOffering
	 */

	return 0;
}

/*****************************************************************************/
/* private methods                                                           */
/*****************************************************************************/

static LassoNodeClass *parent_class = NULL;

static xmlNode*
get_xmlNode(LassoNode *node)
{
	xmlNode *xmlnode;
	LassoDiscovery *discovery = LASSO_DISCOVERY(node);

	xmlnode = parent_class->get_xmlNode(node);
	xmlNodeSetName(xmlnode, "Discovery");
	xmlSetProp(xmlnode, "DiscoveryDumpVersion", "2");

	return xmlnode;
}

static int
init_from_xml(LassoNode *node, xmlNode *xmlnode)
{
	LassoDiscovery *discovery = LASSO_DISCOVERY(node);
	xmlNode *t;
	int rc;

	rc = parent_class->init_from_xml(node, xmlnode);
	if (rc) return rc;

	return 0;
}

/*****************************************************************************/
/* overrided parent class methods */
/*****************************************************************************/

static void
dispose(GObject *object)
{
	LassoDiscovery *discovery = LASSO_DISCOVERY(object);

	if (discovery->private_data->dispose_has_run == TRUE) {
		return;
	}
	discovery->private_data->dispose_has_run = TRUE;

	debug("Discovery object 0x%x disposed ...", discovery);

	/* unref reference counted objects */

	G_OBJECT_CLASS(parent_class)->dispose(object);
}

static void
finalize(GObject *object)
{ 
	LassoDiscovery *discovery = LASSO_DISCOVERY(object);

	debug("Discovery object 0x%x finalized ...", discovery);

	G_OBJECT_CLASS(parent_class)->finalize(object);
}

/*****************************************************************************/
/* instance and class init functions */
/*****************************************************************************/

static void
instance_init(LassoDiscovery *discovery)
{
	discovery->private_data = g_new(LassoDiscoveryPrivate, 1);
	discovery->private_data->dispose_has_run = FALSE;
}

static void
class_init(LassoDiscoveryClass *klass)
{
	parent_class = g_type_class_peek_parent(klass);

	LASSO_NODE_CLASS(klass)->get_xmlNode = get_xmlNode;
	LASSO_NODE_CLASS(klass)->init_from_xml = init_from_xml;

	G_OBJECT_CLASS(klass)->dispose = dispose;
	G_OBJECT_CLASS(klass)->finalize = finalize;
}

GType
lasso_discovery_get_type()
{
	static GType this_type = 0;

	if (!this_type) {
		static const GTypeInfo this_info = {
			sizeof(LassoDiscoveryClass),
			NULL,
			NULL,
			(GClassInitFunc) class_init,
			NULL,
			NULL,
			sizeof(LassoDiscovery),
			0,
			(GInstanceInitFunc) instance_init,
		};

		this_type = g_type_register_static(LASSO_TYPE_WSF_PROFILE,
						   "LassoDiscovery", &this_info, 0);
	}
	return this_type;
}

LassoDiscovery*
lasso_discovery_new(LassoServer *server)
{
	LassoDiscovery *discovery = NULL;

	g_return_val_if_fail(LASSO_IS_SERVER(server), NULL);

	discovery = g_object_new(LASSO_TYPE_DISCOVERY, NULL);
	LASSO_WSF_PROFILE(discovery)->server = server;

	return discovery;
}

LassoDiscovery*
lasso_discovery_new_from_dump(LassoServer *server, const gchar *dump)
{
	LassoDiscovery *discovery;
	xmlDoc *doc;

	discovery = g_object_new(LASSO_TYPE_DISCOVERY, NULL);
	doc = xmlParseMemory(dump, strlen(dump));
	init_from_xml(LASSO_NODE(discovery), xmlDocGetRootElement(doc)); 
	LASSO_WSF_PROFILE(discovery)->server = server;

	return discovery;
}

gchar*
lasso_discovery_dump(LassoDiscovery *discovery)
{
	return lasso_node_dump(LASSO_NODE(discovery), NULL, 1);
}