1 | # soap/attachment.rb: SOAP4R - SwA implementation.
|
---|
2 | # Copyright (C) 2002, 2003 Jamie Herre and NAKAMURA, Hiroshi <[email protected]>.
|
---|
3 |
|
---|
4 | # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
|
---|
5 | # redistribute it and/or modify it under the same terms of Ruby's license;
|
---|
6 | # either the dual license version in 2003, or any later version.
|
---|
7 |
|
---|
8 |
|
---|
9 | require 'soap/baseData'
|
---|
10 | require 'soap/mapping'
|
---|
11 |
|
---|
12 |
|
---|
13 | module SOAP
|
---|
14 |
|
---|
15 |
|
---|
16 | class SOAPAttachment < SOAPExternalReference
|
---|
17 | attr_reader :data
|
---|
18 |
|
---|
19 | def initialize(value)
|
---|
20 | super()
|
---|
21 | @data = value
|
---|
22 | end
|
---|
23 |
|
---|
24 | private
|
---|
25 |
|
---|
26 | def external_contentid
|
---|
27 | @data.contentid
|
---|
28 | end
|
---|
29 | end
|
---|
30 |
|
---|
31 |
|
---|
32 | class Attachment
|
---|
33 | attr_reader :io
|
---|
34 | attr_accessor :contenttype
|
---|
35 |
|
---|
36 | def initialize(string_or_readable = nil)
|
---|
37 | @string_or_readable = string_or_readable
|
---|
38 | @contenttype = "application/octet-stream"
|
---|
39 | @contentid = nil
|
---|
40 | end
|
---|
41 |
|
---|
42 | def contentid
|
---|
43 | @contentid ||= Attachment.contentid(self)
|
---|
44 | end
|
---|
45 |
|
---|
46 | def contentid=(contentid)
|
---|
47 | @contentid = contentid
|
---|
48 | end
|
---|
49 |
|
---|
50 | def mime_contentid
|
---|
51 | '<' + contentid + '>'
|
---|
52 | end
|
---|
53 |
|
---|
54 | def content
|
---|
55 | if @content == nil and @string_or_readable != nil
|
---|
56 | @content = @string_or_readable.respond_to?(:read) ?
|
---|
57 | @string_or_readable.read : @string_or_readable
|
---|
58 | end
|
---|
59 | @content
|
---|
60 | end
|
---|
61 |
|
---|
62 | def to_s
|
---|
63 | content
|
---|
64 | end
|
---|
65 |
|
---|
66 | def write(out)
|
---|
67 | out.write(content)
|
---|
68 | end
|
---|
69 |
|
---|
70 | def save(filename)
|
---|
71 | File.open(filename, "wb") do |f|
|
---|
72 | write(f)
|
---|
73 | end
|
---|
74 | end
|
---|
75 |
|
---|
76 | def self.contentid(obj)
|
---|
77 | # this needs to be fixed
|
---|
78 | [obj.__id__.to_s, Process.pid.to_s].join('.')
|
---|
79 | end
|
---|
80 |
|
---|
81 | def self.mime_contentid(obj)
|
---|
82 | '<' + contentid(obj) + '>'
|
---|
83 | end
|
---|
84 | end
|
---|
85 |
|
---|
86 |
|
---|
87 | module Mapping
|
---|
88 | class AttachmentFactory < SOAP::Mapping::Factory
|
---|
89 | def obj2soap(soap_class, obj, info, map)
|
---|
90 | soap_obj = soap_class.new(obj)
|
---|
91 | mark_marshalled_obj(obj, soap_obj)
|
---|
92 | soap_obj
|
---|
93 | end
|
---|
94 |
|
---|
95 | def soap2obj(obj_class, node, info, map)
|
---|
96 | obj = node.data
|
---|
97 | mark_unmarshalled_obj(node, obj)
|
---|
98 | return true, obj
|
---|
99 | end
|
---|
100 | end
|
---|
101 |
|
---|
102 | DefaultRegistry.add(::SOAP::Attachment, ::SOAP::SOAPAttachment,
|
---|
103 | AttachmentFactory.new, nil)
|
---|
104 | end
|
---|
105 |
|
---|
106 |
|
---|
107 | end
|
---|