Skip to content

Commit

Permalink
Close streams properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pfeil committed Jul 18, 2022
1 parent 9f168b9 commit b76d99d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
*/
public class RorProvider {

private RorProvider() {}

/**
* The method that parses a ror entry to a crate entity.
*
* @param url the url of the ror entry.
* @return the created Organization entity.
*/
public static OrganizationEntity getOrganization(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
if (!url.startsWith("https://ror.org/")) {
throw new IllegalArgumentException("Should provide ror url");
}
String newUrl = "https://api.ror.org/organizations/" + url.replaceAll("https://ror.org/", "");
HttpGet request = new HttpGet(newUrl);

try {
CloseableHttpResponse response = httpClient.execute(request);
ObjectNode jsonNode = MyObjectMapper.getMapper().readValue(response.getEntity().getContent(),
ObjectNode.class);
return new OrganizationEntity.OrganizationEntityBuilder()
.setId(jsonNode.get("id").asText())
.addProperty("name", jsonNode.get("name"))
.addProperty("email", jsonNode.get("email_address"))
.addProperty("url", jsonNode.get("links"))
.build();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
CloseableHttpResponse response = httpClient.execute(request);
ObjectNode jsonNode = MyObjectMapper.getMapper().readValue(response.getEntity().getContent(),
ObjectNode.class);
return new OrganizationEntity.OrganizationEntityBuilder()
.setId(jsonNode.get("id").asText())
.addProperty("name", jsonNode.get("name"))
.addProperty("email", jsonNode.get("email_address"))
.addProperty("url", jsonNode.get("links"))
.build();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/edu/kit/datamanager/ro_crate/writer/ZipWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -25,9 +24,25 @@ public class ZipWriter implements WriterStrategy {

@Override
public void save(Crate crate, String destination) {
File file = new File(destination);
ZipFile zipFile = new ZipFile(destination);
try (ZipFile zipFile = new ZipFile(destination)) {
saveMetadataJson(crate, zipFile);
saveDataEntities(crate, zipFile);
} catch (IOException e) {
// can not close ZipFile (threw Exception)
}
}

private void saveDataEntities(Crate crate, ZipFile zipFile) {
for (DataEntity dataEntity : crate.getAllDataEntities()) {
try {
dataEntity.saveToZip(zipFile);
} catch (ZipException e) {
System.out.println("could not save " + dataEntity.getId() + " to zip file!");
}
}
}

private void saveMetadataJson(Crate crate, ZipFile zipFile) {
try {
// write the metadata.json file
ZipParameters zipParameters = new ZipParameters();
Expand All @@ -49,14 +64,5 @@ public void save(Crate crate, String destination) {
} catch (IOException e) {
e.printStackTrace();
}

// save all the data entities
for (DataEntity dataEntity : crate.getAllDataEntities()) {
try {
dataEntity.saveToZip(zipFile);
} catch (ZipException e) {
System.out.println("could not save " + dataEntity.getId() + " to zip file!");
}
}
}
}

0 comments on commit b76d99d

Please sign in to comment.