@@ -95,6 +95,37 @@ 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+ resp = [error : " Timed out calling web service. URL= ${ url} ." , statusCode : conn?. responseCode?: " " ]
116+ log. error " Timed out calling web service. URL= ${ url} ." , e
117+ } catch (SocketException se) {
118+ resp = [error : " Timed out calling web service. URL= ${ url} ." , statusCode : conn?. responseCode?: " " ]
119+
120+ log. warn " Socket connection closed. ${ se.getMessage()} URL= ${ url} ." , se
121+ } catch (Exception e) {
122+ resp = [error : " Timed out calling web service. URL= ${ url} ." , statusCode : conn?. responseCode?: " " ]
123+
124+ log. error " Failed calling web service. ${ e.getClass()} ${ e.getMessage()} URL= ${ url} ." , e
125+ }
126+ resp
127+ }
128+
98129 Map getString (String url , boolean includeAuth ) {
99130 URLConnection conn = null
100131 Map resp = [:]
@@ -104,7 +135,7 @@ class EcpWebService {
104135 resp. statusCode = conn. responseCode
105136 } catch (SocketTimeoutException e) {
106137 resp. error = " Timed out calling web service. URL= ${ url} ."
107- resp. statusCode = HttpStatus . CONNECTION_TIMED_OUT
138+ resp. statusCode = HttpStatus . SC_GATEWAY_TIMEOUT
108139 log. warn resp. error
109140 } catch (SocketException se) {
110141 resp. error = " Socket connection closed. ${ se.getMessage()} URL= ${ url} ."
@@ -607,9 +638,10 @@ class EcpWebService {
607638 }
608639
609640 def user = userService. getUser()
610- String userId = user. userId
611- String userIdHeader = grailsApplication. config. getProperty(' app.http.header.userId' )
641+
612642 if (user) {
643+ String userIdHeader = grailsApplication. config. getProperty(' app.http.header.userId' )
644+ String userId = user. userId
613645 requestBuilder. addHeader(userIdHeader, userId)
614646 } else {
615647 log. warn(" No user associated with request: ${ url} " )
0 commit comments