| 1 |
# soap/attachment.rb: SOAP4R - SwA implementation. |
|---|
| 2 |
# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. |
|---|
| 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 |
@content = nil |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
def contentid |
|---|
| 44 |
@contentid ||= Attachment.contentid(self) |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def contentid=(contentid) |
|---|
| 48 |
@contentid = contentid |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def mime_contentid |
|---|
| 52 |
'<' + contentid + '>' |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def content |
|---|
| 56 |
if @content == nil and @string_or_readable != nil |
|---|
| 57 |
@content = @string_or_readable.respond_to?(:read) ? |
|---|
| 58 |
@string_or_readable.read : @string_or_readable |
|---|
| 59 |
end |
|---|
| 60 |
@content |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
def to_s |
|---|
| 64 |
content |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
def write(out) |
|---|
| 68 |
out.write(content) |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|
| 71 |
def save(filename) |
|---|
| 72 |
File.open(filename, "wb") do |f| |
|---|
| 73 |
write(f) |
|---|
| 74 |
end |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
def self.contentid(obj) |
|---|
| 78 |
# this needs to be fixed |
|---|
| 79 |
[obj.__id__.to_s, Process.pid.to_s].join('.') |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
def self.mime_contentid(obj) |
|---|
| 83 |
'<' + contentid(obj) + '>' |
|---|
| 84 |
end |
|---|
| 85 |
end |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
module Mapping |
|---|
| 89 |
class AttachmentFactory < SOAP::Mapping::Factory |
|---|
| 90 |
def obj2soap(soap_class, obj, info, map) |
|---|
| 91 |
soap_obj = soap_class.new(obj) |
|---|
| 92 |
mark_marshalled_obj(obj, soap_obj) |
|---|
| 93 |
soap_obj |
|---|
| 94 |
end |
|---|
| 95 |
|
|---|
| 96 |
def soap2obj(obj_class, node, info, map) |
|---|
| 97 |
obj = node.data |
|---|
| 98 |
mark_unmarshalled_obj(node, obj) |
|---|
| 99 |
return true, obj |
|---|
| 100 |
end |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
DefaultRegistry.add(::SOAP::Attachment, ::SOAP::SOAPAttachment, |
|---|
| 104 |
AttachmentFactory.new, nil) |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
end |
|---|