@@ -95,6 +95,41 @@ class EcpWebService {
9595 }
9696 }
9797
98+ /**
99+ * This method is used when the response is expected to be a stream, e.g. for downloading files.
100+ * It returns a Map with keys [statusCode: <http status code>, error: <error message if applicable>]
101+ * The supplied outputStream will be written to with the response from the web service if the call is successful.
102+ * The caller is responsible for closing the outputStream.
103+ */
104+ Map readToStream (String url , OutputStream outputStream , boolean includeUserId = true , Integer timeout = null ) {
105+ HttpURLConnection conn = null
106+ Map resp = [:]
107+ try {
108+ conn = configureConnection(url, includeUserId, timeout)
109+ try (InputStream inputStream = conn. inputStream) {
110+ outputStream << inputStream
111+ }
112+ resp = [statusCode : conn. responseCode]
113+
114+ } catch (SocketTimeoutException e) {
115+ int statusCode = conn ? (conn. responseCode ?: HttpStatus . SC_GATEWAY_TIMEOUT ) : HttpStatus . SC_GATEWAY_TIMEOUT
116+ resp = [error : " Timed out calling web service. URL= ${ url} ." , statusCode : statusCode]
117+ log. error " Timed out calling web service. URL= ${ url} ." , e
118+ } catch (SocketException se) {
119+ int statusCode = conn ? (conn. responseCode ?: HttpStatus . SC_GATEWAY_TIMEOUT ) : HttpStatus . SC_GATEWAY_TIMEOUT
120+ resp = [error : " Timed out calling web service. URL= ${ url} ." , statusCode : statusCode]
121+
122+ log. warn " Socket connection closed. ${ se.getMessage()} URL= ${ url} ." , se
123+ } catch (Exception e) {
124+ int statusCode = conn ? (conn. responseCode ?: HttpStatus . SC_INTERNAL_SERVER_ERROR ) : HttpStatus . SC_INTERNAL_SERVER_ERROR
125+ resp = [error : " Timed out calling web service. URL= ${ url} ." , statusCode : statusCode]
126+
127+ log. error " Failed calling web service. ${ e.getClass()} ${ e.getMessage()} URL= ${ url} ." , e
128+ }
129+
130+ resp
131+ }
132+
98133 Map getString (String url , boolean includeAuth ) {
99134 URLConnection conn = null
100135 Map resp = [:]
@@ -104,7 +139,7 @@ class EcpWebService {
104139 resp. statusCode = conn. responseCode
105140 } catch (SocketTimeoutException e) {
106141 resp. error = " Timed out calling web service. URL= ${ url} ."
107- resp. statusCode = HttpStatus . CONNECTION_TIMED_OUT
142+ resp. statusCode = HttpStatus . SC_GATEWAY_TIMEOUT
108143 log. warn resp. error
109144 } catch (SocketException se) {
110145 resp. error = " Socket connection closed. ${ se.getMessage()} URL= ${ url} ."
@@ -607,9 +642,10 @@ class EcpWebService {
607642 }
608643
609644 def user = userService. getUser()
610- String userId = user. userId
611- String userIdHeader = grailsApplication. config. getProperty(' app.http.header.userId' )
645+
612646 if (user) {
647+ String userIdHeader = grailsApplication. config. getProperty(' app.http.header.userId' )
648+ String userId = user. userId
613649 requestBuilder. addHeader(userIdHeader, userId)
614650 } else {
615651 log. warn(" No user associated with request: ${ url} " )
0 commit comments