| 1 |
=begin |
|---|
| 2 |
SOAP4R |
|---|
| 3 |
Copyright (C) 2000 NAKAMURA Hiroshi. |
|---|
| 4 |
|
|---|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
|---|
| 6 |
the terms of the GNU General Public License as published by the Free Software |
|---|
| 7 |
Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 8 |
version. |
|---|
| 9 |
|
|---|
| 10 |
This program is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
|---|
| 12 |
PRATICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 13 |
|
|---|
| 14 |
You should have received a copy of the GNU General Public License along with |
|---|
| 15 |
this program; if not, write to the Free Software Foundation, Inc., 675 Mass |
|---|
| 16 |
Ave, Cambridge, MA 02139, USA. |
|---|
| 17 |
=end |
|---|
| 18 |
|
|---|
| 19 |
require 'socket' |
|---|
| 20 |
require 'timeout' |
|---|
| 21 |
|
|---|
| 22 |
require 'SOAP' |
|---|
| 23 |
require 'http-access' |
|---|
| 24 |
require 'uri' |
|---|
| 25 |
|
|---|
| 26 |
class SOAPStreamHandler |
|---|
| 27 |
public |
|---|
| 28 |
|
|---|
| 29 |
attr_reader :endPoint |
|---|
| 30 |
|
|---|
| 31 |
def initialize( endPoint ) |
|---|
| 32 |
@endPoint = endPoint |
|---|
| 33 |
end |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
class SOAPHTTPPostStreamHandler < SOAPStreamHandler |
|---|
| 38 |
public |
|---|
| 39 |
|
|---|
| 40 |
MediaType = 'text/xml' |
|---|
| 41 |
|
|---|
| 42 |
NofRetry = 10 # [times] |
|---|
| 43 |
CallTimeout = 300 # [sec] |
|---|
| 44 |
ReadTimeout = 300 # [sec] |
|---|
| 45 |
|
|---|
| 46 |
def initialize( endPointUri, proxy = nil ) |
|---|
| 47 |
super( endPointUri ) |
|---|
| 48 |
@server = URIModule::URI.create( endPointUri ) |
|---|
| 49 |
@proxy = proxy |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
def send( methodNamespace, methodName, soapString ) |
|---|
| 53 |
begin |
|---|
| 54 |
s = sendPOST( methodNamespace, methodName, soapString ) |
|---|
| 55 |
rescue PostUnavailableError |
|---|
| 56 |
begin |
|---|
| 57 |
s = sendMPOST( methodNamespace, methodName, soapString ) |
|---|
| 58 |
rescue MPostUnavailableError |
|---|
| 59 |
raise HTTPStreamError.new( $! ) |
|---|
| 60 |
end |
|---|
| 61 |
end |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
private |
|---|
| 65 |
|
|---|
| 66 |
def sendPOST( methodNamespace, methodName, soapString ) |
|---|
| 67 |
retryNo = NofRetry |
|---|
| 68 |
drv = nil |
|---|
| 69 |
begin |
|---|
| 70 |
drv = HTTPAccess.new( @server.host, @server.port, @proxy ) |
|---|
| 71 |
rescue |
|---|
| 72 |
retryNo -= 1 |
|---|
| 73 |
if retryNo > 0 |
|---|
| 74 |
puts 'Retrying connection ...' if $DEBUG |
|---|
| 75 |
retry |
|---|
| 76 |
end |
|---|
| 77 |
raise |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
action = methodNamespace.dup << '#' << methodName |
|---|
| 81 |
requestHeaders = { 'SOAPAction' => action, 'Content-Type' => MediaType } |
|---|
| 82 |
|
|---|
| 83 |
begin |
|---|
| 84 |
timeout( CallTimeout ) do |
|---|
| 85 |
drv.request_post( @server.path, soapString, requestHeaders ) |
|---|
| 86 |
rh = drv.get_header |
|---|
| 87 |
responseHeaders = {} |
|---|
| 88 |
rh.each do | header | |
|---|
| 89 |
/^([^:]+):\s*([^\r]*)\r?\n/ =~ header |
|---|
| 90 |
responseHeaders[ $1.downcase ] = $2 |
|---|
| 91 |
end |
|---|
| 92 |
if ( drv.code == '405' ) |
|---|
| 93 |
# 405: Method Not Allowed |
|---|
| 94 |
raise PostUnavailableError.new( "#{ drv.code }: #{ drv.message }" ) |
|---|
| 95 |
elsif ( drv.code != '200' ) |
|---|
| 96 |
raise HTTPStreamError.new( "#{ drv.code }: #{ drv.message }" ) |
|---|
| 97 |
elsif ( !responseHeaders.has_key?( 'content-type' )) |
|---|
| 98 |
raise HTTPStreamError.new( 'Content-type not found.' ) |
|---|
| 99 |
elsif ( /^#{ MediaType }(?:;.*)?/ !~ responseHeaders[ 'content-type' ] ) |
|---|
| 100 |
raise HTTPStreamError.new( 'Illegal content-type: ' << responseHeaders[ 'content-type' ] ) |
|---|
| 101 |
end |
|---|
| 102 |
end |
|---|
| 103 |
rescue TimeoutError |
|---|
| 104 |
raise HTTPStreamError.new( 'Call timeout' ) |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
receiveString = '' |
|---|
| 108 |
begin |
|---|
| 109 |
timeout( ReadTimeout ) do |
|---|
| 110 |
drv.get_data( 8124 ) do | data | |
|---|
| 111 |
receiveString << data |
|---|
| 112 |
end |
|---|
| 113 |
end |
|---|
| 114 |
rescue TimeoutError |
|---|
| 115 |
raise HTTPStreamError.new( 'Read timeout' ) |
|---|
| 116 |
end |
|---|
| 117 |
|
|---|
| 118 |
receiveString |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def sendMPOST( methodNamespace, methodName, soapString ) |
|---|
| 122 |
raise NotImplementError.new() |
|---|
| 123 |
|
|---|
| 124 |
s = nil |
|---|
| 125 |
if (( status == '501' ) or ( status == '510' )) |
|---|
| 126 |
# 501: Not Implemented |
|---|
| 127 |
# 510: Not Extended |
|---|
| 128 |
raise MPostUnavailableError.new( "Status: #{ status }, Reason-phrase: #{ reason }" ) |
|---|
| 129 |
elsif ( status != '200' ) |
|---|
| 130 |
raise HTTPStreamError.new( "#{ status }: #{ reason }" ) |
|---|
| 131 |
end |
|---|
| 132 |
end |
|---|
| 133 |
end |
|---|