• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / tigerdata-app / 113bb38e-bc49-4a70-acb4-9db5de96ae3b

14 Aug 2025 03:32PM UTC coverage: 76.482%. Remained the same
113bb38e-bc49-4a70-acb4-9db5de96ae3b

push

circleci

web-flow
Indicate session expired in the exception message (#1749)

0 of 1 new or added line in 1 file covered. (0.0%)

2270 of 2968 relevant lines covered (76.48%)

176.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.86
/app/models/mediaflux/request.rb
1
# frozen_string_literal: true
2
module Mediaflux
1✔
3
    class Request
1✔
4

5
      # As this is an abstract class, this should be overridden to specify the Mediaflux API service
6
      def self.service
1✔
7
        raise(NotImplementedError, "#{self} is an abstract class, please override #{self}.service")
1✔
8
      end
9

10
      # The default request URL path for the Mediaflux API
11
      # @return [String]
12
      def self.request_path
1✔
13
        "/__mflux_svc__"
2,818✔
14
      end
15

16
      def self.uri
1✔
17
        # Setting the URI to the Ansible build or Docker build of mediaflux depending on the environment
18
        URI("#{Connection.transport}://#{Connection.host}:#{Connection.port}#{request_path}")
1,413✔
19
      end
20

21
      # Constructs a new HTTP POST request for usage with the Mediaflux API
22
      # @return [Net::HTTP::Post]
23
      def self.build_post_request
1✔
24
        Net::HTTP::Post.new(request_path)
1,405✔
25
      end
26

27
      # The default XML namespace which should be used for building the XML
28
      #   Document transmitted in the body of the HTTP request
29
      # @return [String]
30
      def self.default_xml_namespace
1✔
31
        "tigerdata"
1✔
32
      end
33

34
      def self.default_xml_namespace_uri
1✔
35
        "http://tigerdata.princeton.edu"
1✔
36
      end
37

38
      # Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
39
      # @returns http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
40
      def self.find_or_create_http_client
1✔
41
        HttpConnection.instance.http_client
1,409✔
42
      end
43

44
      attr_reader :session_token
1✔
45

46
      # Constructor
47
      # @param file [File] any upload file required for the POST request
48
      # @param session_token [String] the API token for the authenticated session
49
      # @param http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
50
      def initialize(file: nil, session_token: nil, http_client: nil, session_user: nil)
1✔
51
        @http_client = http_client || self.class.find_or_create_http_client
1,409✔
52
        @file = file
1,409✔
53
        @session_token = session_token
1,409✔
54
        @session_user = session_user
1,409✔
55
      end
56

57
      # Resolves the HTTP request against the Mediaflux API
58
      # @return [Net::HTTP]
59
      def resolve
1✔
60
        @http_response = @http_client.request self.class.uri, http_request
1,404✔
61
      end
62

63
      # Determines whether or not the request has been resolved
64
      # @return [Boolean]
65
      def resolved?
1✔
66
        @http_response.present?
1,959✔
67
      end
68

69
      # Resolves the HTTP request, and returns the XML Document parsed from the response body
70
      # @return [Nokogiri::XML::Document]
71
      def response_xml
1✔
72
        resolve unless resolved?
1,957✔
73
        Rails.logger.debug(response_body)
1,957✔
74
        @response_xml ||= Nokogiri::XML.parse(response_body)
1,957✔
75
        Rails.logger.debug(@response_xml)
1,957✔
76
        if @response_xml.xpath("//message").text == "session is not valid"
1,957✔
NEW
77
          raise Mediaflux::SessionExpired, "Session expired for token #{session_token}"
×
78
        end
79

80
        @response_xml
1,957✔
81
      end
82

83
      # The response body of the mediaflux call
84
      def response_body
1✔
85
        @response_body ||= http_response.body
3,353✔
86
      end
87

88
      def error?
1✔
89
        response_error.present?
561✔
90
      end
91

92
      def response_error
1✔
93
        xml = response_xml
566✔
94
        return nil if xml.xpath("/response/reply/error").count == 0
566✔
95
        error = {
96
          title: xml.xpath("/response/reply/error").text,
9✔
97
          message: xml.xpath("/response/reply/message").text
98
        }
99
        Rails.logger.error "MediaFlux error: #{error[:title]}, #{error[:message]}"
9✔
100
        error
9✔
101
      end
102

103
      delegate :to_s, to: :response_xml
1✔
104

105
      def xml_payload( name: self.class.service)
1✔
106
        body = build_http_request_body(name: )
2,811✔
107
        xml_payload = body.to_xml
2,811✔
108
      end
109

110
      # The output of this routine can be passed to xtoshell in aterm.  The output of which can be sent to service.execute
111
      # @param [String] name name of the service this request will send
112
      # @return [String] xml that can be passed to xtoshell without manipulation
113
      def xtoshell_xml( name: self.class.service)
1✔
114
        xml_builder = build_http_request_body(name: )
×
115
        xml_builder.doc.xpath("//request/service/@session").remove
×
116
        xml = xml_builder.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
×
117
        xml.strip.gsub("\"","'").gsub("<args>","").gsub("</args>","")
×
118
      end
119

120
      # This method is used for transforming iso8601 dates to dates that MediaFlux likes
121
      # Take a string like "2024-02-26T10:33:11-05:00" and convert this string to "22-FEB-2024 13:57:19"
122
      def self.format_date_for_mediaflux(iso8601_date)
1✔
123
        return if iso8601_date.nil?
×
124
        Time.format_date_for_mediaflux(iso8601_date)
×
125
      end
126

127
      private
1✔
128

129
        def http_request
1✔
130
          @http_request ||= build_http_request(name: self.class.service, form_file: @file)
1,406✔
131
        end
132

133
        def http_response
1✔
134
          @http_response ||= resolve
1,394✔
135
        end
136

137
        def build_http_request_body(name:)
1✔
138
          args = { name: name }
2,811✔
139
                                                # must use @session_token here instead of session_token
140
                                                #  for login to not go into an infinaite loop
141
          args[:session] = session_token unless @session_token.nil?
2,811✔
142

143
          Nokogiri::XML::Builder.new(namespace_inheritance: false) do |xml|
2,811✔
144
            xml.request do
2,811✔
145
              xml.service(**args) do
2,811✔
146
                yield xml if block_given?
2,811✔
147
              end
148
            end
149
          end
150
        end
151

152
        # rubocop:disable Metrics/MethodLength
153
        def build_http_request(name:, form_file: nil)
1✔
154
          request = self.class.build_post_request
1,405✔
155

156
          log_xml_request(xml_payload)
1,405✔
157
          set_authentication_headers(request)
1,405✔
158

159
          if form_file.nil?
1,405✔
160
            request["Content-Type"] = "text/xml; charset=utf-8"
1,404✔
161
            request.body = xml_payload(name:)
1,404✔
162
          else
163
            request["Content-Type"] = "multipart/form-data"
1✔
164
            request.set_form({ "request" => xml_payload,
1✔
165
                               "nb-data-attachments" => "1",
166
                               "file_0" => form_file },
167
                          "multipart/form-data",
168
                          "charset" => "UTF-8")
169
          end
170

171
          request
1,405✔
172
        end
173
      # rubocop:enable Metrics/MethodLength
174

175
      def log_xml_request(xml_payload)
1✔
176
        password_element = xml_payload.match(/\<password\>.*\<\/password\>/)
1,405✔
177
        if password_element.nil?
1,405✔
178
          Rails.logger.debug(xml_payload)
475✔
179
        else
180
          # Don't log the password
181
          Rails.logger.debug(xml_payload.gsub(password_element.to_s, "<password>***</password>"))
930✔
182
        end
183
      end
184

185
      # Authentication code to push a few custom HTTP headers to Mediaflux
186
      # Eventually the `session_user` will need to be an object that provides the timeout value.
187
      def set_authentication_headers(request)
1✔
188
        return if @session_user.nil?
1,405✔
189

190
        request["mediaflux.sso.user"] = @session_user.uid
1✔
191
      end
192
    end
193
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc