summaryrefslogtreecommitdiffstats
path: root/axiom/src/attachments/data_handler.c
blob: 3be8a9dc70257942d42ee928b760bf4c3c691306 (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axiom_data_handler.h>
#include <stdio.h>
#include <sys/stat.h>
#include <axiom_mime_part.h>

struct axiom_data_handler
{
    /* The content type */
    axis2_char_t *mime_type;

    /* If in a file then the file name*/
    axis2_char_t *file_name;

    /* If it is in a buffer then the buffer */
    axis2_byte_t *buffer;

    /* The length of the buffer */
    size_t buffer_len;

    /* Is this a data_handler with a file name or a buffer*/
    axiom_data_handler_type_t data_handler_type;

    /* When parsing whether we have cached it or not */
    axis2_bool_t cached;

    /* The Content Id */
    axis2_char_t *mime_id;

    /* In the case of sending callback this is required */
    void *user_param;

};

/* Creates the data_handler. The file name is not mandatory */

AXIS2_EXTERN axiom_data_handler_t *AXIS2_CALL
axiom_data_handler_create(
    const axutil_env_t *env,
    const axis2_char_t *file_name,
    const axis2_char_t *mime_type)
{
    axiom_data_handler_t *data_handler = NULL;

    AXIS2_ENV_CHECK(env, NULL);
    data_handler = (axiom_data_handler_t *)AXIS2_MALLOC(env->allocator,
        sizeof(axiom_data_handler_t));

    if(!data_handler)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "No memory. Cannot create data handler");
        return NULL;
    }

    data_handler->mime_type = NULL;
    data_handler->file_name = NULL;
    data_handler->buffer = NULL;
    data_handler->buffer_len = 0;
    /* By default, a Data Handler is of type Buffer */
    data_handler->data_handler_type = AXIOM_DATA_HANDLER_TYPE_BUFFER;
    data_handler->cached = AXIS2_FALSE;
    data_handler->mime_id = NULL;
    data_handler->user_param = NULL;

    if(mime_type)
    {
        data_handler->mime_type = axutil_strdup(env, mime_type);
        if(!(data_handler->mime_type))
        {
            axiom_data_handler_free(data_handler, env);
            return NULL;
        }
    }
    if(file_name)
    {
        data_handler->file_name = axutil_strdup(env, file_name);
        if(!(data_handler->file_name))
        {
            axiom_data_handler_free(data_handler, env);
            return NULL;
        }
        data_handler->data_handler_type = AXIOM_DATA_HANDLER_TYPE_FILE;
    }

    return data_handler;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_data_handler_free(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    if(data_handler->file_name)
    {
        AXIS2_FREE(env->allocator, data_handler->file_name);
    }

    if(data_handler->mime_type)
    {
        AXIS2_FREE(env->allocator, data_handler->mime_type);
    }

    if(data_handler->buffer)
    {
        AXIS2_FREE(env->allocator, data_handler->buffer);
    }

    if(data_handler->mime_id)
    {
        AXIS2_FREE(env->allocator, data_handler->mime_id);
    }

    if(data_handler)
    {
        AXIS2_FREE(env->allocator, data_handler);
    }

    return;
}

AXIS2_EXTERN axis2_char_t *AXIS2_CALL
axiom_data_handler_get_content_type(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->mime_type;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_set_content_type(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    const axis2_char_t *mime_type)
{
    if(data_handler->mime_type)
    {
        AXIS2_FREE(env->allocator, data_handler->mime_type);
    }
    data_handler->mime_type = axutil_strdup(env, mime_type);
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axiom_data_handler_get_cached(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->cached;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_data_handler_set_cached(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    axis2_bool_t cached)
{
    data_handler->cached = cached;
}

AXIS2_EXTERN axis2_byte_t *AXIS2_CALL
axiom_data_handler_get_input_stream(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->buffer;
}

AXIS2_EXTERN size_t AXIS2_CALL
axiom_data_handler_get_input_stream_len(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->buffer_len;
}

/* With MTOM caching support this function is no longer used
 * Because this will load whole file in to buffer. So for large 
 * attachment this is not wise */

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_read_from(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    axis2_byte_t **output_stream,
    size_t *output_stream_size)
{
    if(data_handler->data_handler_type == AXIOM_DATA_HANDLER_TYPE_BUFFER)
    {
        *output_stream = data_handler->buffer;
        *output_stream_size = data_handler->buffer_len;
    }
    else if(data_handler->data_handler_type == AXIOM_DATA_HANDLER_TYPE_FILE
        && data_handler->file_name)
    {
        FILE *f = NULL;
        axis2_byte_t *byte_stream = NULL;
        axis2_byte_t *temp_byte_stream = NULL;
        axis2_byte_t *read_stream = NULL;
        int byte_stream_size = 0;
        int temp_byte_stream_size = 0;
        int read_stream_size = 0;
        int count = 0;
        struct stat stat_p;

        f = fopen(data_handler->file_name, "rb");
        if(!f)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error opening file %s for reading",
                data_handler->file_name);
            return AXIS2_FAILURE;
        }

        if(stat(data_handler->file_name, &stat_p) == -1)
        {
            fclose(f);
            return AXIS2_FAILURE;
        }
        else if(stat_p.st_size == 0)
        {
            fclose(f);
            *output_stream = NULL;
            *output_stream_size = 0;
            return AXIS2_SUCCESS;
        }

        do
        {
            read_stream_size = stat_p.st_size;
            read_stream = AXIS2_MALLOC(env->allocator, (read_stream_size) * sizeof(axis2_byte_t));
            if(!read_stream)
            {
                AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "No memory. Cannot create binary stream");
                if(byte_stream)
                {
                    AXIS2_FREE(env->allocator, byte_stream);
                }
                fclose(f);
                return AXIS2_FAILURE;
            }
            count = (int)fread(read_stream, 1, read_stream_size, f);
            /* The count lies within the int range */
            if(ferror(f))
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error in reading file %s",
                    data_handler->file_name);
                if(byte_stream)
                {
                    AXIS2_FREE(env->allocator, byte_stream);
                }
                if(read_stream)
                {
                    AXIS2_FREE(env->allocator, read_stream);
                }
                fclose(f);
                return AXIS2_FAILURE;
            }

            /* copy the read bytes */
            if(count > 0)
            {
                if(byte_stream)
                {
                    temp_byte_stream = byte_stream;
                    temp_byte_stream_size = byte_stream_size;
                    byte_stream_size = temp_byte_stream_size + count;
                    byte_stream = AXIS2_MALLOC(env->allocator, (byte_stream_size)
                        * sizeof(axis2_byte_t));
                    if(!byte_stream)
                    {
                        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
                        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
                            "No memory. Cannot create binary stream");
                        if(read_stream)
                        {
                            AXIS2_FREE(env->allocator, read_stream);
                        }
                        if(temp_byte_stream)
                        {
                            AXIS2_FREE(env->allocator, temp_byte_stream);
                        }
                        fclose(f);
                        return AXIS2_FAILURE;
                    }

                    memcpy(byte_stream, temp_byte_stream, temp_byte_stream_size);
                    memcpy(byte_stream + temp_byte_stream_size, read_stream, count);

                    if(read_stream)
                    {
                        AXIS2_FREE(env->allocator, read_stream);
                        read_stream_size = 0;
                    }
                    if(temp_byte_stream)
                    {
                        AXIS2_FREE(env->allocator, temp_byte_stream);
                        temp_byte_stream = NULL;
                        temp_byte_stream_size = 0;
                    }
                }
                else
                {
                    byte_stream = read_stream;
                    byte_stream_size = read_stream_size;
                    read_stream = NULL;
                    read_stream_size = 0;
                }
            }
            else if(read_stream)
            {
                AXIS2_FREE(env->allocator, read_stream);
            }
        }
        while(!feof(f));

        fclose(f);
        data_handler->buffer = byte_stream;
        data_handler->buffer_len = byte_stream_size;
        *output_stream = byte_stream;
        *output_stream_size = byte_stream_size;
    }
    else
    {
        /* Data Handler File Name is missing */
        return AXIS2_FAILURE;
    }

    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_set_binary_data(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    axis2_byte_t *input_stream,
    size_t input_stream_len)
{
    data_handler->buffer = input_stream;
    data_handler->buffer_len = input_stream_len;
    return AXIS2_SUCCESS;
}

/* This function will write the data in the buffer 
 * to a file. When caching is being used this will 
 * not be called , because the parser it self cache 
 * the attachment while parsing */

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_write_to(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    if(data_handler->file_name)
    {
        FILE *f = NULL;
        int count = 0;

        f = fopen(data_handler->file_name, "wb");
        if(!f)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error opening file %s for writing",
                data_handler->file_name);
            return AXIS2_FAILURE;
        }

        count = (int)fwrite(data_handler->buffer, 1, data_handler->buffer_len, f);
        /* The count lies within the int range */

        if(ferror(f))
        {
            fclose(f);
            return AXIS2_FAILURE;
        }
        fflush(f);
        fclose(f);
    }

    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_set_file_name(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    axis2_char_t *file_name)
{
    if(data_handler->file_name)
    {
        AXIS2_FREE(env->allocator, data_handler->file_name);
        data_handler->file_name = NULL;
    }

    if(file_name)
    {
        data_handler->file_name = axutil_strdup(env, file_name);
        if(!(data_handler->file_name))
        {
            return AXIS2_FAILURE;
        }
    }

    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_char_t *AXIS2_CALL
axiom_data_handler_get_file_name(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    if(data_handler->file_name)
    {
        return data_handler->file_name;
    }
    else
    {
        return NULL;
    }
}

/* This method will add the data_handler binary data to the array_list.
 * If it is a buffer the part type is buffer. otherwise it is a file. In the
 * case of file the array_list have just the file name and the size. The content
 * is not loaded to the memory.
 */

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_add_binary_data(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    axutil_array_list_t *list)

{
    axiom_mime_part_t *binary_part = NULL;

    binary_part = axiom_mime_part_create(env);

    if(!binary_part)
    {
        return AXIS2_FAILURE;
    }

    if(data_handler->data_handler_type == AXIOM_DATA_HANDLER_TYPE_BUFFER)
    {
        binary_part->part = (axis2_byte_t *)AXIS2_MALLOC(env->allocator, (data_handler->buffer_len)
            * sizeof(axis2_byte_t));
        memcpy(binary_part->part, data_handler->buffer, data_handler->buffer_len);

        binary_part->part_size = data_handler->buffer_len;
        binary_part->type = AXIOM_MIME_PART_BUFFER;
    }

    /* In the case of file we first calculate the file size
     * and then add the file name */

    else if(data_handler->data_handler_type == AXIOM_DATA_HANDLER_TYPE_FILE
        && data_handler->file_name)
    {
        struct stat stat_p;

        if(stat(data_handler->file_name, &stat_p) == -1)
        {
            return AXIS2_FAILURE;
        }
        else if(stat_p.st_size == 0)
        {
            return AXIS2_SUCCESS;
        }
        else
        {
            binary_part->file_name = (axis2_char_t *)axutil_strdup(env, data_handler->file_name);
            binary_part->part_size = stat_p.st_size;
            binary_part->type = AXIOM_MIME_PART_FILE;
        }
    }
    /* In the case of Callback the user should specify the callback name in the
     * configuration file. We just set the correct type. Inside the transport 
     * it will load the callback and send the attachment appropriately */

    else if(data_handler->data_handler_type == AXIOM_DATA_HANDLER_TYPE_CALLBACK)
    {
        binary_part->type = AXIOM_MIME_PART_CALLBACK;
        binary_part->user_param = data_handler->user_param;
    }

    else
    {
        /* Data Handler File Name is missing */
        return AXIS2_FAILURE;
    }

    /* Finaly we add the binary details to the list */

    axutil_array_list_add(list, env, binary_part);

    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_char_t *AXIS2_CALL
axiom_data_handler_get_mime_id(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->mime_id;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axiom_data_handler_set_mime_id(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    const axis2_char_t *mime_id)
{
    if(data_handler->mime_id)
    {
        AXIS2_FREE(env->allocator, data_handler->mime_id);
    }
    data_handler->mime_id = axutil_strdup(env, mime_id);
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axiom_data_handler_type_t AXIS2_CALL
axiom_data_handler_get_data_handler_type(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->data_handler_type;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_data_handler_set_data_handler_type(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    axiom_data_handler_type_t data_handler_type)
{
    data_handler->data_handler_type = data_handler_type;
    return;
}

AXIS2_EXTERN void *AXIS2_CALL
axiom_data_handler_get_user_param(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env)
{
    return data_handler->user_param;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_data_handler_set_user_param(
    axiom_data_handler_t *data_handler,
    const axutil_env_t *env,
    void *user_param)
{
    data_handler->user_param = user_param;
    return;
}