All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.difi.oxalis.test.asd.AsdServlet Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
package no.difi.oxalis.test.asd;

import com.google.common.io.ByteStreams;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import no.difi.oxalis.api.header.HeaderParser;
import no.difi.oxalis.api.lang.OxalisContentException;
import no.difi.oxalis.api.lang.TimestampException;
import no.difi.oxalis.api.model.Direction;
import no.difi.oxalis.api.model.TransmissionIdentifier;
import no.difi.oxalis.api.persist.PersisterHandler;
import no.difi.oxalis.api.timestamp.Timestamp;
import no.difi.oxalis.api.timestamp.TimestampProvider;
import no.difi.vefa.peppol.common.model.Header;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Path;

/**
 * @author erlend
 */
@Singleton
public class AsdServlet extends HttpServlet {

    @Inject
    private Provider persisterHandlerProvider;

    @Inject
    private TimestampProvider timestampProvider;

    @Inject
    private HeaderParser headerParser;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("Hello ASD world!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PersisterHandler persisterHandler = persisterHandlerProvider.get();

        TransmissionIdentifier transmissionIdentifier = null;

        Header header = null;

        Path path = null;

        try {
            Timestamp timestamp = timestampProvider.generate(null, Direction.IN);

            byte[] content = ByteStreams.toByteArray(req.getInputStream());

            transmissionIdentifier = TransmissionIdentifier.of(req.getHeader(AsdHeaders.TRANSMISSION_ID));

            header = headerParser.parse(new ByteArrayInputStream(content));

            path = persisterHandler.persist(transmissionIdentifier, header, new ByteArrayInputStream(content));

            persisterHandler.persist(new AsdInboundMetadata(transmissionIdentifier, header, timestamp.getDate()), path);

            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setHeader(AsdHeaders.STATUS, "OK");
            resp.setHeader(AsdHeaders.TIMESTAMP, timestamp.getDate().toString());
        } catch (TimestampException | OxalisContentException e) {
            persisterHandler.persist(transmissionIdentifier, header, path, e);
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.setHeader(AsdHeaders.STATUS, String.format("ERROR: %s", e.getMessage()));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy