Skip to content

Commit 22530ed

Browse files
author
Claudio Guidi
committed
added tutorial download a zip file from the internet
1 parent 40bab2b commit 22530ed

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [Advanced Scenarios](tutorials/advanced-scenarios/README.md)
2727
- [Implementing new port protocols in Jolie](tutorials/advanced-scenarios/supporting-new-protocols-in-jolie/README.md)
2828
- [Building a File Uploader Service](tutorials/advanced-scenarios/file-uploader/README.md)
29+
- [Download a zip file from the Internet](tutorials/advanced-scenarios/download-zip-from-internet/README.md)
2930

3031
# Language, tools, and standard library
3132

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Download a zip file from the Internet
2+
3+
In this simple tutorial we'll see how to download derby.jar from _apache.org_.
4+
The full code can be downloaded [here](https://github.com/jolie/examples/blob/master/Tutorials/download-a-zip-from-the-web/download-a-zip-from-the-web.ol)
5+
6+
The steps we'll follows are:
7+
8+
9+
1. Downloading the zip file `db-derby-10.14.2.0-lib.zip` with the libs from `https://archive.apache.org:443/dist/db/derby/db-derby-10.14.2.0/`
10+
1. Saving the downloaded file in the current directory
11+
1. Unzipping the file
12+
1. Extracting the file `derby.jar` and copying in teh current directory
13+
1. Removing unuseful directories
14+
15+
## Downloading the zip file from the Internet
16+
In order to download the file we need to create an outputPort as it follows:
17+
18+
```jolie
19+
...
20+
21+
interface ApacheDerbyDownloadInterface {
22+
RequestResponse:
23+
downloadDerby
24+
}
25+
26+
...
27+
28+
outputPort ApacheDerby1_10_14 {
29+
location: "socket://archive.apache.org:443/dist/db/derby/db-derby-10.14.2.0/"
30+
protocol: https {
31+
.osc.downloadDerby.alias = "db-derby-10.14.2.0-lib.zip"
32+
.osc.downloadDerby.method = "get"
33+
}
34+
interfaces: ApacheDerbyDownloadInterface
35+
}
36+
37+
...
38+
```
39+
40+
Note that:
41+
42+
1. The location is set without the name of the file, and the https token is replaced with medium `socket`
43+
1. The protocol https is defined in the parameter `protocol`
44+
1. The interface `ApacheDerbyDownloadInterface` has been attached to the outputPort where the operation `downloadDerby` is defined
45+
1. Protocol https is defines with two extra parameters `.osc.downloadDerby.alias` and `.osc.downloadDerby.method`. The former one defines an alias to use for making a request for operation _downloadDerby_, the latter one defines the http method to use when invoking the endpoint using operation _downloadDerby_
46+
47+
In the behaviour, it is sufficient to program the related solicit-response:
48+
```jolie
49+
...
50+
downloadDerby@ApacheDerby1_10_14()( derby )
51+
...
52+
```
53+
Variable `derby` contains the downloaded file.
54+
55+
## Saving the downloaded file
56+
This step is quite simple, just use the File standard library. Pay attentio to use the format `binary` for keeping safe the file encoding.
57+
```jolie
58+
writeFile@File( { filename = DOWNLOADED_ZIP, content << derby } )()
59+
```
60+
where `DOWNLOADED_ZIP` is a constant with the name of the file.
61+
62+
## Unzipping the file
63+
Now it is possibile to unzip the file by using the standard library `zip-utils`
64+
65+
```jolie
66+
from zip-utils import ZipUtils
67+
...
68+
embed ZipUtils as ZipUtils
69+
...
70+
unzip@ZipUtils( { filename = DOWNLOADED_ZIP, targetPath = TARGET_PATH_UNZIP} )()
71+
```
72+
where `TARGET_PATH_UNZIP` is a constant with the target path where saving the unzipped files.
73+
74+
## Extraction of the derby.jar file
75+
This step is quite simple because it is sufficient to exploit the standard library File
76+
77+
```jolie
78+
derbyDbJarFilename = TARGET_PATH_UNZIP + "/db-derby-10.14.2.0-lib/lib/derby.jar"
79+
readFile@File( { filename = derbyDbJarFilename, format = "binary" } )( derbyJar )
80+
writeFile@File( { filename = "derby.jar", format = "binary", content << derbyJar })()
81+
```
82+
83+
## Unuseful directories removal
84+
Also this step exploits the standard library for files
85+
```jolie
86+
delete@File( DOWNLOADED_ZIP )()
87+
deleteDir@File( TARGET_PATH_UNZIP )()
88+
```

0 commit comments

Comments
 (0)