Skip to content

Commit

Permalink
modularize
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Apr 24, 2020
1 parent d934129 commit 16de7ef
Show file tree
Hide file tree
Showing 30 changed files with 1,055 additions and 180 deletions.
66 changes: 66 additions & 0 deletions api-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0"?>
<!--
Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Eclipse Public License - v 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.eclipse.org/legal/epl-2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.project-openubl</groupId>
<artifactId>xml-sender</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>xml-sender-api-core</artifactId>
<name>XML Sender :: API Core</name>

<dependencies>
<!--Quarkus-->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>

<!--Test-->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xmlsender.resources;

import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition;
import org.eclipse.microprofile.openapi.annotations.info.Contact;
import org.eclipse.microprofile.openapi.annotations.info.Info;
import org.eclipse.microprofile.openapi.annotations.info.License;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@OpenAPIDefinition(
info = @Info(
title = "XML Sender API",
version = "1.0.0",
contact = @Contact(
name = "XML Sender API Support",
url = "https://github.com/project-openubl/xml-sender/issues",
email = "projectopenubl+subscribe@googlegroups.com"
),
license = @License(
name = "Eclipse Public License - v 2.0",
url = "https://www.eclipse.org/legal/epl-2.0/"
)
)
)
@ApplicationPath(ApiApplication.API_BASE)
public class ApiApplication extends Application {
public static final String API_BASE = "/api";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xmlsender.resources;

import org.jboss.resteasy.api.validation.ResteasyConstraintViolation;
import org.jboss.resteasy.api.validation.ResteasyViolationException;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.HashMap;
import java.util.Map;

@Provider
public class ValidationExceptionHandler implements ExceptionMapper<ResteasyViolationException> {
@Override
public Response toResponse(ResteasyViolationException e) {
Map<String, String> violationMessages = new HashMap<>();
for (ResteasyConstraintViolation violation : e.getViolations()) {
String key = violation.getPath();
violationMessages.put(key, "[" + violation.getValue() + "] " + violation.getMessage());
}

return Response.status(400)
.type(MediaType.APPLICATION_JSON)
.entity(violationMessages)
.build();
}
}
5 changes: 5 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<version>${sunat-web-services.version}</version>
</dependency>

<dependency>
<groupId>io.github.project-openubl</groupId>
<artifactId>xml-sender-core</artifactId>
</dependency>

<!--Quarkus-->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xmlsender.routes;
package io.github.project.openubl.xmlsender.camel;

import io.github.project.openubl.xmlsender.models.FileType;
import org.apache.camel.LoggingLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xmlsender.routes;
package io.github.project.openubl.xmlsender.camel;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
package io.github.project.openubl.xmlsender.exceptions;

public class StorageException extends Exception {
public StorageException(Exception e) {
super(e);
}

public StorageException(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
package io.github.project.openubl.xmlsender.exceptions;

public class UnsupportedDocumentTypeException extends Exception {
public UnsupportedDocumentTypeException(Exception e) {
super(e);
}

public UnsupportedDocumentTypeException(String messasge) {
super(messasge);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.quarkus.runtime.StartupEvent;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import io.github.project.openubl.xmlsender.providers.WSProvider;
import io.github.project.openubl.xmlsender.ws.WSSunatClient;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
Expand All @@ -36,7 +36,7 @@ public class SendFileQueueConsumer implements Runnable {
private static final Logger LOG = Logger.getLogger(SendFileQueueConsumer.class);

@Inject
WSProvider wsProvider;
WSSunatClient wsSunatClient;

@Inject
ConnectionFactory connectionFactory;
Expand Down Expand Up @@ -74,8 +74,8 @@ public void run() {
return;
}

String entityId = textMessage.getText();
boolean result = wsProvider.sendFileDelivery(Long.valueOf(entityId));
String documentId = textMessage.getText();
boolean result = wsSunatClient.sendDocument(Long.valueOf(documentId));

if (result) {
message.acknowledge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.quarkus.runtime.StartupEvent;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import io.github.project.openubl.xmlsender.providers.WSProvider;
import io.github.project.openubl.xmlsender.ws.WSSunatClient;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
Expand All @@ -36,7 +36,7 @@ public class SendTicketQueueConsumer implements Runnable {
private static final Logger LOG = Logger.getLogger(SendTicketQueueConsumer.class);

@Inject
WSProvider wsProvider;
WSSunatClient wsSunatClient;

@Inject
ConnectionFactory connectionFactory;
Expand Down Expand Up @@ -75,7 +75,7 @@ public void run() {
}

String entityId = textMessage.getText();
boolean result = wsProvider.checkTicket(Long.parseLong(entityId));
boolean result = wsSunatClient.checkDocumentTicket(Long.parseLong(entityId));

if (result) {
message.acknowledge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@
import io.github.project.openubl.xmlsender.exceptions.StorageException;
import io.github.project.openubl.xmlsender.exceptions.UnsupportedDocumentTypeException;
import io.github.project.openubl.xmlsender.models.DocumentType;
import io.github.project.openubl.xmlsender.models.FileDeliveryStatusType;
import io.github.project.openubl.xmlsender.models.DeliveryStatusType;
import io.github.project.openubl.xmlsender.models.FileType;
import io.github.project.openubl.xmlsender.models.jpa.FileDeliveryRepository;
import io.github.project.openubl.xmlsender.models.jpa.entities.FileDeliveryEntity;
import io.github.project.openubl.xmlsender.models.jpa.DocumentRepository;
import io.github.project.openubl.xmlsender.models.jpa.entities.DocumentEntity;
import io.github.project.openubl.xmlsender.xml.ubl.XmlContentModel;
import io.github.project.openubl.xmlsender.xml.ubl.XmlContentProvider;
import org.xml.sax.SAXException;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.jms.*;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.transaction.Transactional;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
Expand All @@ -42,6 +47,7 @@
import java.util.Optional;
import java.util.regex.Pattern;

@Transactional
@ApplicationScoped
public class DocumentsManager {

Expand Down Expand Up @@ -69,15 +75,9 @@ public class DocumentsManager {
XmlContentProvider xmlContentProvider;

@Inject
FileDeliveryRepository fileDeliveryRepository;
DocumentRepository documentRepository;


/**
* @param file file to be sent to SUNAT
* @return true if file was scheduled to be send
*/
@Transactional
public FileDeliveryEntity createFileDeliveryAndSchedule(
public DocumentEntity createDocumentAndScheduleDelivery(
byte[] file, String username, String password, String customId
) throws InvalidXMLFileException, UnsupportedDocumentTypeException, StorageException {
// Read file
Expand All @@ -95,39 +95,39 @@ public FileDeliveryEntity createFileDeliveryAndSchedule(
}

DocumentType documentType = documentTypeOptional.get();
String serverUrl = getServerUrl(documentType);
String deliveryURL = getDeliveryURL(documentType);
String fileNameWithoutExtension = getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID());

// Save XML File
String fileID = filesManager.uploadFile(file, fileNameWithoutExtension + ".xml" ,FileType.XML);
String fileID = filesManager.createFile(file, FileType.getFilename(fileNameWithoutExtension, FileType.XML), FileType.XML);
if (fileID == null) {
throw new StorageException("Could not save xml file in storage");
}

// Create Entity in DB
FileDeliveryEntity deliveryEntity = FileDeliveryEntity.Builder.aFileDeliveryEntity()
DocumentEntity documentEntity = DocumentEntity.Builder.aDocumentEntity()
.withFileID(fileID)
.withFilename(fileNameWithoutExtension)
.withFilenameWithoutExtension(fileNameWithoutExtension)
.withRuc(xmlContentModel.getRuc())
.withDocumentID(xmlContentModel.getDocumentID())
.withDocumentType(documentType)
.withDeliveryStatus(FileDeliveryStatusType.SCHEDULED_TO_DELIVER)
.withServerUrl(serverUrl)
.withDeliveryStatus(DeliveryStatusType.SCHEDULED_TO_DELIVER)
.withDeliveryURL(deliveryURL)
.withSunatUsername(username)
.withSunatPassword(password)
.withCustomId(customId)
.build();

fileDeliveryRepository.persist(deliveryEntity);
documentRepository.persist(documentEntity);

// Send JSM File
produceMessage(deliveryEntity);
produceMessage(documentEntity);

// return result
return deliveryEntity;
return documentEntity;
}

private void produceMessage(FileDeliveryEntity deliveryEntity) {
private void produceMessage(DocumentEntity deliveryEntity) {
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
JMSProducer producer = context.createProducer();
producer.setDeliveryDelay(messageDelay);
Expand All @@ -140,7 +140,7 @@ private void produceMessage(FileDeliveryEntity deliveryEntity) {
}
}

private String getServerUrl(DocumentType documentType) {
private String getDeliveryURL(DocumentType documentType) {
return sunatUrl1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
@ApplicationScoped
public class FilesManager {

private static final Logger LOG = Logger.getLogger(FilesManager.class);

@Inject
CamelContext camelContext;

Expand All @@ -41,7 +39,7 @@ public class FilesManager {
/**
* Uploads a file and zip it if necessary
*/
public String uploadFile(byte[] file, String fileName, FileType fileType) {
public String createFile(byte[] file, String fileName, FileType fileType) {
Map<String, Object> headers = new HashMap<>();
headers.put("isZipFile", fileType.equals(FileType.ZIP));
headers.put(Exchange.FILE_NAME, fileName);
Expand Down
Loading

0 comments on commit 16de7ef

Please sign in to comment.