Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/tags/RELEASE_1_0_1/lib/SOAPStreamHandler.rb

Revision 6, 3.6 kB (checked in by nakahiro, 8 years ago)

Dump request/response string if -d was given.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to author date id revision
Line 
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     action = methodNamespace.dup << '#' << methodName
80     requestHeaders = { 'SOAPAction' => action, 'Content-Type' => MediaType }
81
82     puts soapString if $DEBUG
83
84     begin
85       timeout( CallTimeout ) do
86         drv.request_post( @server.path, soapString, requestHeaders )
87         rh = drv.get_header
88         responseHeaders = {}
89         rh.each do | header |
90           /^([^:]+):\s*([^\r]*)\r?\n/ =~ header
91           responseHeaders[ $1.downcase ] = $2
92         end
93         if ( drv.code == '405' )
94           # 405: Method Not Allowed
95           raise PostUnavailableError.new( "#{ drv.code }: #{ drv.message }" )
96         elsif ( drv.code != '200' )
97           raise HTTPStreamError.new( "#{ drv.code }: #{ drv.message }" )
98         elsif ( !responseHeaders.has_key?( 'content-type' ))
99           raise HTTPStreamError.new( 'Content-type not found.' )
100         elsif ( /^#{ MediaType }(?:;.*)?/ !~ responseHeaders[ 'content-type' ] )
101           raise HTTPStreamError.new( 'Illegal content-type: ' << responseHeaders[ 'content-type' ] )
102         end
103       end
104     rescue TimeoutError
105       raise HTTPStreamError.new( 'Call timeout' )
106     end
107
108     receiveString = ''
109     begin
110       timeout( ReadTimeout ) do
111         drv.get_data( 8124 ) do | data |
112           receiveString << data
113         end
114       end
115     rescue TimeoutError
116       raise HTTPStreamError.new( 'Read timeout' )
117     end
118
119     puts receiveString if $DEBUG
120     receiveString
121   end
122
123   def sendMPOST( methodNamespace, methodName, soapString )
124     raise NotImplementError.new()
125
126     s = nil
127     if (( status == '501' ) or ( status == '510' ))
128       # 501: Not Implemented
129       # 510: Not Extended
130       raise MPostUnavailableError.new( "Status: #{ status }, Reason-phrase: #{ reason }" )
131     elsif ( status != '200' )
132       raise HTTPStreamError.new( "#{ status }: #{ reason }" )
133     end
134   end
135 end
Note: See TracBrowser for help on using the browser.