Skip to content

Commit

Permalink
Merge pull request #352 from Opetushallitus/OK-323
Browse files Browse the repository at this point in the history
OK-323: Uuden dokumenttipalvelukirjaston käyttöönotto
  • Loading branch information
tomikat authored Dec 4, 2023
2 parents 90f9a2b + 85e827d commit c52a222
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ viestintapalvelu.dev.session-required=true

oppijanumerorekisteri-service.base=https://${host.virkailija}/oppijanumerorekisteri-service
kayttooikeus-service.base=https://${host.virkailija}/kayttooikeus-service
dokumenttipalvelu-service.rest.url=https://${host.virkailija}/dokumenttipalvelu-service/resources
organisaatio-service.rest.url=https://${host.virkailija}/organisaatio-service/rest
ryhmasahkoposti-service.email.rest.url=https://${host.virkailija}/ryhmasahkoposti-service/email
tarjonta-service.rest.url=https://${host.virkailija}/tarjonta-service/rest/v1
Expand All @@ -160,3 +159,6 @@ viestintapalvelu-service.postgresql.maxlifetimemillis=60000
viestintapalvelu.downloadfiles.s3.region={{viestintapalvelu_downloadfiles_s3_region}}
viestintapalvelu.downloadfiles.s3.bucket={{viestintapalvelu_downloadfiles_s3_bucket}}
spring.profiles.active={{viestintapalvelu_spring_profiles_active}}

aws.region={{aws_region}}
aws.bucket.name={{dokumenttipalvelu_aws_bucket_name}}
1 change: 0 additions & 1 deletion src/test/resources/oph-configuration/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ viestintapalvelu.dev.session-required=true

oppijanumerorekisteri-service.base=https://${host.virkailija}/oppijanumerorekisteri-service
kayttooikeus-service.base=https://${host.virkailija}/kayttooikeus-service
dokumenttipalvelu-service.rest.url=https://${host.virkailija}/dokumenttipalvelu-service/resources
organisaatio-service.rest.url=https://${host.virkailija}/organisaatio-service/rest
ryhmasahkoposti-service.email.rest.url=http://${server.name}:8080/ryhmasahkoposti-service/email
tarjonta-service.rest.url=https://${host.virkailija}/tarjonta-service/rest/v1
Expand Down
13 changes: 3 additions & 10 deletions viestintapalvelu-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dokumenttipalvelu.version>5.3-SNAPSHOT</dokumenttipalvelu.version>
<dtoconverter.version>2.0.3-RELEASE</dtoconverter.version>
</properties>

Expand Down Expand Up @@ -578,7 +577,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.10.23</version>
<version>2.20.162</version>
</dependency>

<!-- Apache PDFBox API library for PDF document handling -->
Expand Down Expand Up @@ -647,14 +646,8 @@
</dependency>
<dependency>
<groupId>fi.vm.sade.dokumenttipalvelu</groupId>
<artifactId>dokumenttipalvelu-api</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>cas-client-support-distributed-ehcache</artifactId>
<groupId>org.jasig.cas.client</groupId>
</exclusion>
</exclusions>
<artifactId>dokumenttipalvelu</artifactId>
<version>6.10-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jasig.cas.client</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Urls {
public static final String API_PATH = "api/v1";
public static final String ADDRESS_LABEL_RESOURCE_PATH = "addresslabel";
public static final String DOWNLOAD_RESOURCE_PATH = "download";
public static final String LATAA_RESOURCE_PATH = "lataa";
public static final String MESSAGE_RESOURCE_PATH = "message";
public static final String HYVAKSYMISKIRJE_RESOURCE_PATH = "hyvaksymiskirje";
public static final String JALKIOHJAUSKIRJE_RESOURCE_PATH = "jalkiohjauskirje";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import fi.vm.sade.valinta.dokumenttipalvelu.Dokumenttipalvelu;
import fi.vm.sade.viestintapalvelu.download.cache.DocumentId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -44,7 +44,6 @@
import com.lowagie.text.DocumentException;
import com.wordnik.swagger.annotations.*;

import fi.vm.sade.valinta.dokumenttipalvelu.resource.DokumenttiResource;
import fi.vm.sade.viestintapalvelu.AsynchronousResource;
import fi.vm.sade.viestintapalvelu.Urls;
import fi.vm.sade.viestintapalvelu.download.Download;
Expand All @@ -64,8 +63,8 @@ public class AddressLabelResource extends AsynchronousResource {
private DownloadCache downloadCache;
@Autowired
private AddressLabelBuilder labelBuilder;
@Qualifier
private DokumenttiResource dokumenttiResource;
@Autowired
private Dokumenttipalvelu dokumenttipalvelu;
@Resource(name = "otherAsyncResourceJobsExecutorService")
private ExecutorService executor;

Expand Down Expand Up @@ -119,12 +118,17 @@ public Response xls(@ApiParam(value = "Osoitetiedot", required = true) AddressLa

@POST
@Consumes("application/json")
@Produces("application/octet-stream")
@Produces({"application/octet-stream", "text/plain"})
@Path("/sync/pdf")
@ApiOperation(value = ApiPDFSync, notes = ApiPDFSync)
@ApiResponses(@ApiResponse(code = 400, message = PDFResponse400))
public InputStream syncPdf(@ApiParam(value = "Osoitetiedot", required = true) final AddressLabelBatch input) throws DocumentException, IOException {
return new ByteArrayInputStream(labelBuilder.printPDF(input));
public Response syncPdf(@ApiParam(value = "Osoitetiedot", required = true) final AddressLabelBatch input) throws DocumentException, IOException {
try {
return Response.ok(new ByteArrayInputStream(labelBuilder.printPDF(input))).build();
} catch (final Exception e) {
LOG.error("AddressLabel PDF creation failed: {}", e.getMessage(), e);
return createFailureResponse(null);
}
}

// Async routes
Expand All @@ -143,8 +147,8 @@ public void run() {
SecurityContextHolder.getContext().setAuthentication(auth);
try {
byte[] pdf = labelBuilder.printPDF(input);
dokumenttiResource.tallenna(null, filenamePrefixWithUsernameAndTimestamp("addresslabels.pdf"), now().plusDays(2).toDate().getTime(),
Arrays.asList("viestintapalvelu", "addresslabels", "pdf"), "application/pdf;charset=utf-8", new ByteArrayInputStream(pdf));
dokumenttipalvelu.save(null, filenamePrefixWithUsernameAndTimestamp("addresslabels.pdf"), now().plusDays(2).toDate(),
Arrays.asList("viestintapalvelu", "addresslabels"), "application/pdf", new ByteArrayInputStream(pdf));
} catch (Exception e) {
e.printStackTrace();
LOG.error("AddressLabel PDF failed: {}", e.getMessage());
Expand All @@ -168,8 +172,8 @@ public void run() {
SecurityContextHolder.getContext().setAuthentication(auth);
try {
byte[] csv = labelBuilder.printCSV(input);
dokumenttiResource.tallenna(null, filenamePrefixWithUsernameAndTimestamp("addresslabels.xls"), now().plusDays(2).toDate().getTime(),
Arrays.asList("viestintapalvelu", "addresslabels", "xls"), "application/vnd.ms-excel", new ByteArrayInputStream(csv));
dokumenttipalvelu.save(null, filenamePrefixWithUsernameAndTimestamp("addresslabels.xls"), now().plusDays(2).toDate(),
Arrays.asList("viestintapalvelu", "addresslabels"), "application/vnd.ms-excel", new ByteArrayInputStream(csv));
} catch (Exception e) {
e.printStackTrace();
LOG.error("AddressLabel PDF failed: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fi.vm.sade.viestintapalvelu.dokumenttipalvelu;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiParam;
import fi.vm.sade.valinta.dokumenttipalvelu.Dokumenttipalvelu;
import fi.vm.sade.valinta.dokumenttipalvelu.dto.ObjectEntity;
import fi.vm.sade.viestintapalvelu.Urls;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Service("LataaDokumenttiResource")
@Singleton
@Path(Urls.LATAA_RESOURCE_PATH)
@Api(value = "/" + Urls.API_PATH + "/" + Urls.LATAA_RESOURCE_PATH, description = "Dokumenttipalveluun (S3) tallennetun dokumentin lataaminen")
public class LataaDokumenttiResource {
private static Logger LOGGER = LoggerFactory.getLogger(LataaDokumenttiResource.class);
private final Dokumenttipalvelu dokumenttipalvelu;
@Inject
public LataaDokumenttiResource(final Dokumenttipalvelu dokumenttipalvelu) {
this.dokumenttipalvelu = dokumenttipalvelu;
}

@GET
@Path("/{key}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response lataa(@ApiParam(value = "Ladattavan dokumentin key", required = true) @PathParam("key") String key,
@Context HttpServletResponse response) {
try {
final ObjectEntity objectEntity = dokumenttipalvelu.get(key);
response.setHeader("Content-Type", objectEntity.contentType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + objectEntity.fileName + "\"");
response.setHeader("Content-Length", String.valueOf(objectEntity.contentLength));
response.setHeader("Cache-Control", "private");
return Response.ok(IOUtils.toByteArray(objectEntity.entity)).build();
} catch (final Exception e) {
LOGGER.warn("Error fetching document with key {}", key, e);
if (e.getCause() != null && e.getCause() instanceof NoSuchKeyException) {
return Response.status(Response.Status.NOT_FOUND).entity("Document not found with given key").build();
} else {
return Response.serverError().build();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import fi.vm.sade.valinta.dokumenttipalvelu.Dokumenttipalvelu;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -43,7 +43,6 @@
import com.lowagie.text.DocumentException;
import com.wordnik.swagger.annotations.*;

import fi.vm.sade.valinta.dokumenttipalvelu.resource.DokumenttiResource;
import fi.vm.sade.viestintapalvelu.AsynchronousResource;
import fi.vm.sade.viestintapalvelu.Urls;
import fi.vm.sade.viestintapalvelu.download.Download;
Expand All @@ -66,8 +65,8 @@ public class KoekutsukirjeResource extends AsynchronousResource {
private DownloadCache downloadCache;
@Autowired
private KoekutsukirjeBuilder koekutsukirjeBuilder;
@Qualifier
private DokumenttiResource dokumenttiResource;
@Autowired
private Dokumenttipalvelu dokumenttipalvelu;
@Resource(name = "otherAsyncResourceJobsExecutorService")
private ExecutorService executor;

Expand Down Expand Up @@ -156,8 +155,8 @@ public void run() {
SecurityContextHolder.getContext().setAuthentication(auth);
try {
byte[] pdf = koekutsukirjeBuilder.printPDF(input);
dokumenttiResource.tallenna(null, filenamePrefixWithUsernameAndTimestamp("koekutsukirje.pdf"), now().plusDays(2).toDate().getTime(),
Arrays.asList("viestintapalvelu", "koekutsukirje", "pdf"), "application/pdf;charset=utf-8", new ByteArrayInputStream(pdf));
dokumenttipalvelu.save(null, filenamePrefixWithUsernameAndTimestamp("koekutsukirje.pdf"), now().plusDays(2).toDate(),
Arrays.asList("viestintapalvelu", "koekutsukirje"), "application/pdf", new ByteArrayInputStream(pdf));
} catch (Exception e) {
e.printStackTrace();
LOG.error("Koekutsukirje PDF async failed: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
import javax.ws.rs.core.Response.Status;

import com.google.gson.GsonBuilder;
import fi.vm.sade.viestintapalvelu.model.types.ContentStructureType;
import fi.vm.sade.valinta.dokumenttipalvelu.Dokumenttipalvelu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.wordnik.swagger.annotations.ApiParam;

import fi.vm.sade.valinta.dokumenttipalvelu.resource.DokumenttiResource;
import fi.vm.sade.viestintapalvelu.AsynchronousResource;
import fi.vm.sade.viestintapalvelu.common.util.FilenameHelper;
import fi.vm.sade.viestintapalvelu.dao.dto.LetterBatchStatusDto;
Expand All @@ -45,8 +44,8 @@ public abstract class AbstractLetterResource extends AsynchronousResource {
@Autowired
protected LetterBuilder letterBuilder;

@Resource
protected DokumenttiResource dokumenttipalveluRestClient;
@Autowired
protected Dokumenttipalvelu dokumenttipalvelu;

@Resource(name = "otherAsyncResourceJobsExecutorService")
protected ExecutorService executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import fi.vm.sade.valinta.dokumenttipalvelu.dto.ObjectEntity;
import fi.vm.sade.valinta.dokumenttipalvelu.dto.ObjectMetadata;
import fi.vm.sade.viestintapalvelu.model.types.ContentTypes;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -22,7 +28,6 @@
import com.lowagie.text.DocumentException;
import com.wordnik.swagger.annotations.*;

import fi.vm.sade.valinta.dokumenttipalvelu.dto.MetaData;
import fi.vm.sade.viestintapalvelu.Constants;
import fi.vm.sade.viestintapalvelu.Urls;
import fi.vm.sade.viestintapalvelu.dao.dto.LetterBatchStatusDto;
Expand Down Expand Up @@ -257,33 +262,41 @@ public Response letterBatchStatus(@PathParam("letterBatchId") @ApiParam(value =
@Path("/async/letter/pdf/{letterBatchId}")
@PreAuthorize(Constants.ASIAKIRJAPALVELU_CREATE_LETTER)
@ApiOperation(value = "Palauttaa kirjelähetyksestä generoidun PDF-dokumentin")
public Response getLetterBatchPDF(@PathParam("letterBatchId") @ApiParam(value = "Kirjelähetyksen id") String prefixedLetterBatchId) {
long letterBatchId = getLetterBatchId(prefixedLetterBatchId);
public Response getLetterBatchPDF(
@PathParam("letterBatchId")
@ApiParam(value = "Kirjelähetyksen id")
final String prefixedLetterBatchId) {
final long letterBatchId = getLetterBatchId(prefixedLetterBatchId);
try {
LetterBatchStatusDto batchStatus = letterService.getBatchStatus(letterBatchId);
if(batchStatus == null || ! fi.vm.sade.viestintapalvelu.model.LetterBatch.Status.ready.equals(batchStatus.getStatus())) {
final LetterBatchStatusDto batchStatus = letterService.getBatchStatus(letterBatchId);
if (batchStatus == null || ! fi.vm.sade.viestintapalvelu.model.LetterBatch.Status.ready.equals(batchStatus.getStatus())) {
return Response.status(Status.BAD_REQUEST).build();
}

String documentId = "mergedLetterBatch"+letterBatchId;

List<String> tags = Arrays.asList("viestintapalvelu", "mergedletters.pdf", "pdf", documentId);

Collection<MetaData> documents = dokumenttipalveluRestClient.hae(tags);
if(documents.isEmpty()) {
byte[] bytes = letterService.getLetterContentsByLetterBatchID(letterBatchId);



dokumenttipalveluRestClient.tallenna(documentId, "mergedletters.pdf",
now().plusDays(2).toDate().getTime(),
final LetterBatch letterBatch = letterService.findById(letterBatchId);
final String documentId = "mergedLetterBatch" + letterBatchId;
final List<String> tags = Stream.of("viestintapalvelu", letterBatch.getFetchTarget())
.filter(Objects::nonNull).collect(Collectors.toList());
final Collection<ObjectMetadata> documents = dokumenttipalvelu.find(tags);
ObjectEntity objectEntity;
if (!documents.isEmpty()) {
objectEntity = dokumenttipalvelu.get(documents.stream().findFirst().get().key);
} else {
final byte[] bytes = letterService.getLetterContentsByLetterBatchID(letterBatchId);
final ObjectMetadata saved = dokumenttipalvelu.save(
documentId,
"mergedletters.pdf",
now().plusDays(2).toDate(),
tags,
ContentTypes.CONTENT_TYPE_PDF,
new ByteArrayInputStream(bytes));
objectEntity = dokumenttipalvelu.get(saved.key);
}
return dokumenttipalveluRestClient.lataa(documentId);
return Response.ok(IOUtils.toByteArray(objectEntity.entity))
.header("Content-Disposition", "attachment; filename=\"" + objectEntity.fileName + "\"")
.header("Content-Type", objectEntity.contentType)
.build();

} catch (Exception e) {
} catch (final Exception e) {
LOG.error("Error getting merged pdf for batch " + letterBatchId, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
Expand Down
Loading

0 comments on commit c52a222

Please sign in to comment.