Skip to content

Commit 1f03077

Browse files
authored
Merge pull request #341 from AtlasOfLivingAustralia/feature/issue3715
Added file download support to EcpWebService, fixed a bug with non-au…
2 parents d5391bd + 7d1c8cc commit 1f03077

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Install nodejs
2929
uses: actions/setup-node@v4
3030
with:
31-
node-version: '20.x'
31+
node-version: '24.x'
3232
registry-url: 'https://npm.pkg.github.com'
3333
scope: 'AtlasOfLivingAustralia'
3434

@@ -40,7 +40,7 @@ jobs:
4040
id: pre-release-version
4141
uses: adobe/update-prerelease-npm-version@4dea8f295023f5b18126afd79bae1057a5b92d6e #v1.2.0
4242

43-
- run: npm publish
43+
- run: npm publish --tag=snapshot
4444
env:
4545
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4646

grails-app/services/au/org/ala/ecodata/forms/EcpWebService.groovy

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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}")

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "9.1.0-snapshot",
44
"repository": {
55
"type": "git",
6-
"url": "https://github.com/AtlasOfLivingAustralia/ecodata-client-plugin.git"
6+
"url": "git+https://github.com/AtlasOfLivingAustralia/ecodata-client-plugin.git"
77
},
88
"publishConfig": {
99
"registry": "https://npm.pkg.github.com/"

0 commit comments

Comments
 (0)