Skip to content

Commit 6ba9e42

Browse files
add working version
1 parent 753a24b commit 6ba9e42

File tree

9 files changed

+125
-55
lines changed

9 files changed

+125
-55
lines changed

src/main/java/org/openubl/jms/SendFileQueueConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SendFileQueueConsumer implements Runnable {
2525
@Inject
2626
ConnectionFactory connectionFactory;
2727

28-
@ConfigProperty(name = "openubl.sendFileQueue")
28+
@ConfigProperty(name = "openubl.jms.sendFileQueue")
2929
String sendFileQueue;
3030

3131
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

src/main/java/org/openubl/jms/SendTicketQueueConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SendTicketQueueConsumer implements Runnable {
2525
@Inject
2626
ConnectionFactory connectionFactory;
2727

28-
@ConfigProperty(name = "openubl.ticketQueue")
28+
@ConfigProperty(name = "openubl.jms.ticketQueue")
2929
String ticketQueue;
3030

3131
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

src/main/java/org/openubl/managers/DocumentsManager.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public class DocumentsManager {
3434
public static final Pattern FACTURA_SERIE_REGEX = Pattern.compile("^[F|f].*$");
3535
public static final Pattern BOLETA_SERIE_REGEX = Pattern.compile("^[B|b].*$");
3636

37-
@ConfigProperty(name = "openubl.sunatUrl1")
38-
String sunatUrl1;
37+
@ConfigProperty(name = "openubl.jms.delay")
38+
Long messageDelay;
3939

40-
@ConfigProperty(name = "openubl.sendFileQueue")
40+
@ConfigProperty(name = "openubl.jms.sendFileQueue")
4141
String sendFileQueue;
4242

43-
@ConfigProperty(name = "openubl.message.delay")
44-
Long messageDelay;
43+
@ConfigProperty(name = "openubl.sunat.url1")
44+
String sunatUrl1;
4545

4646
@Inject
4747
FilesManager filesManager;
@@ -62,7 +62,7 @@ public class DocumentsManager {
6262
*/
6363
@Transactional
6464
public FileDeliveryEntity createFileDeliveryAndSchedule(
65-
byte[] file, String customId
65+
byte[] file, String username, String password, String customId
6666
) throws InvalidXMLFileException, UnsupportedDocumentTypeException, StorageException {
6767
// Read file
6868
XmlContentModel xmlContentModel;
@@ -74,29 +74,31 @@ public FileDeliveryEntity createFileDeliveryAndSchedule(
7474

7575
// Check document type is supported
7676
Optional<DocumentType> documentTypeOptional = DocumentType.valueFromDocumentType(xmlContentModel.getDocumentType());
77-
if (!documentTypeOptional.isPresent()) {
77+
if (documentTypeOptional.isEmpty()) {
7878
throw new UnsupportedDocumentTypeException(xmlContentModel.getDocumentType() + " is not supported yet");
7979
}
8080

8181
DocumentType documentType = documentTypeOptional.get();
8282
String serverUrl = getServerUrl(documentType);
83-
String fileName = getFileName(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID());
83+
String fileNameWithoutExtension = getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID());
8484

8585
// Save XML File
86-
String fileID = filesManager.upload(file, fileName, FileType.XML);
86+
String fileID = filesManager.uploadFile(file, fileNameWithoutExtension + ".xml" ,FileType.XML);
8787
if (fileID == null) {
8888
throw new StorageException("Could not save xml file in storage");
8989
}
9090

9191
// Create Entity in DB
9292
FileDeliveryEntity deliveryEntity = FileDeliveryEntity.Builder.aFileDeliveryEntity()
9393
.withFileID(fileID)
94-
.withFilename(fileName)
94+
.withFilename(fileNameWithoutExtension)
9595
.withRuc(xmlContentModel.getRuc())
9696
.withDocumentID(xmlContentModel.getDocumentID())
9797
.withDocumentType(documentType)
9898
.withDeliveryStatus(FileDeliveryStatusType.SCHEDULED_TO_DELIVER)
9999
.withServerUrl(serverUrl)
100+
.withSunatUsername(username)
101+
.withSunatPassword(password)
100102
.withCustomId(customId)
101103
.build();
102104

@@ -126,7 +128,7 @@ private String getServerUrl(DocumentType documentType) {
126128
return sunatUrl1;
127129
}
128130

129-
private String getFileName(DocumentType type, String ruc, String documentID) {
131+
private String getFileNameWithoutExtension(DocumentType type, String ruc, String documentID) {
130132
String codigoDocumento;
131133
switch (type) {
132134
case INVOICE:
@@ -138,16 +140,16 @@ private String getFileName(DocumentType type, String ruc, String documentID) {
138140
throw new IllegalStateException("Invalid Serie, can not detect code");
139141
}
140142

141-
return MessageFormat.format("{0}-{1}-{2}.xml", ruc, codigoDocumento, documentID);
143+
return MessageFormat.format("{0}-{1}-{2}", ruc, codigoDocumento, documentID);
142144
case CREDIT_NOTE:
143145
codigoDocumento = "07";
144-
return MessageFormat.format("{0}-{1}-{2}.xml", ruc, codigoDocumento, documentID);
146+
return MessageFormat.format("{0}-{1}-{2}", ruc, codigoDocumento, documentID);
145147
case DEBIT_NOTE:
146148
codigoDocumento = "08";
147-
return MessageFormat.format("{0}-{1}-{2}.xml", ruc, codigoDocumento, documentID);
149+
return MessageFormat.format("{0}-{1}-{2}", ruc, codigoDocumento, documentID);
148150
case VOIDED_DOCUMENT:
149151
case SUMMARY_DOCUMENT:
150-
return MessageFormat.format("{0}-{1}.xml", ruc, documentID);
152+
return MessageFormat.format("{0}-{1}", ruc, documentID);
151153
default:
152154
throw new IllegalStateException("Invalid type of UBL Document, can not extract Serie-Numero to create fileName");
153155
}

src/main/java/org/openubl/managers/FilesManager.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,35 @@ public class FilesManager {
2222
@ConfigProperty(name = "openubl.storage.type")
2323
String storageType;
2424

25-
public String upload(byte[] file, String filename, FileType fileType) {
25+
/**
26+
* Uploads a file and zip it if necessary
27+
*/
28+
public String uploadFile(byte[] file, String fileName, FileType fileType) {
2629
Map<String, Object> headers = new HashMap<>();
27-
headers.put(Exchange.FILE_NAME, filename);
28-
headers.put("fileType", fileType);
29-
30-
LOG.debug("Using storageType=" + storageType);
30+
headers.put("isZipFile", fileType.equals(FileType.ZIP));
31+
headers.put(Exchange.FILE_NAME, fileName);
3132

3233
return camelContext
3334
.createProducerTemplate()
3435
.requestBodyAndHeaders("direct:" + storageType + "-save-file", file, headers, String.class);
3536
}
3637

37-
public byte[] getFileAsBytes(String fileID) {
38-
LOG.debug("Using storageType=" + storageType);
38+
public byte[] getFileAsBytesAfterUnzip(String fileID) {
39+
Map<String, Object> headers = new HashMap<>();
40+
headers.put("shouldUnzip", true);
41+
42+
return camelContext
43+
.createProducerTemplate()
44+
.requestBodyAndHeaders("direct:" + storageType + "-get-file", fileID, headers, byte[].class);
45+
}
46+
47+
public byte[] getFileAsBytesWithoutUnzipping(String fileID) {
48+
Map<String, Object> headers = new HashMap<>();
49+
headers.put("shouldUnzip", true);
3950

4051
return camelContext
4152
.createProducerTemplate()
42-
.requestBody("direct:" + storageType + "-get-file", fileID, byte[].class);
53+
.requestBodyAndHeaders("direct:" + storageType + "-get-file", fileID, headers, byte[].class);
4354
}
4455

4556
public void delete(String fileID) {

src/main/java/org/openubl/models/jpa/entities/FileDeliveryEntity.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public class FileDeliveryEntity extends PanacheEntity {
4444
@Column(name = "crd_id")
4545
public String cdrID;
4646

47+
@Column(name = "sunat_username")
48+
public String sunatUsername;
49+
50+
@Column(name = "sunat_password")
51+
public String sunatPassword;
52+
4753
@Column(name = "sunat_ticket")
4854
public String sunatTicket;
4955

@@ -68,6 +74,8 @@ public static final class Builder {
6874
public FileDeliveryStatusType deliveryStatus;
6975
public String serverUrl;
7076
public String cdrID;
77+
public String sunatUsername;
78+
public String sunatPassword;
7179
public String sunatTicket;
7280
public String sunatStatus;
7381
public Integer sunatCode;
@@ -121,6 +129,16 @@ public Builder withCdrID(String cdrID) {
121129
return this;
122130
}
123131

132+
public Builder withSunatUsername(String sunatUsername) {
133+
this.sunatUsername = sunatUsername;
134+
return this;
135+
}
136+
137+
public Builder withSunatPassword(String sunatPassword) {
138+
this.sunatPassword = sunatPassword;
139+
return this;
140+
}
141+
124142
public Builder withSunatTicket(String sunatTicket) {
125143
this.sunatTicket = sunatTicket;
126144
return this;
@@ -148,19 +166,21 @@ public Builder withCustomId(String customId) {
148166

149167
public FileDeliveryEntity build() {
150168
FileDeliveryEntity fileDeliveryEntity = new FileDeliveryEntity();
151-
fileDeliveryEntity.documentType = this.documentType;
169+
fileDeliveryEntity.sunatPassword = this.sunatPassword;
152170
fileDeliveryEntity.sunatStatus = this.sunatStatus;
153-
fileDeliveryEntity.filename = this.filename;
154-
fileDeliveryEntity.cdrID = this.cdrID;
171+
fileDeliveryEntity.fileID = this.fileID;
155172
fileDeliveryEntity.serverUrl = this.serverUrl;
156-
fileDeliveryEntity.deliveryStatus = this.deliveryStatus;
157-
fileDeliveryEntity.sunatTicket = this.sunatTicket;
158-
fileDeliveryEntity.customId = this.customId;
159173
fileDeliveryEntity.ruc = this.ruc;
174+
fileDeliveryEntity.sunatUsername = this.sunatUsername;
175+
fileDeliveryEntity.filename = this.filename;
160176
fileDeliveryEntity.sunatDescription = this.sunatDescription;
161-
fileDeliveryEntity.documentID = this.documentID;
162-
fileDeliveryEntity.fileID = this.fileID;
163177
fileDeliveryEntity.sunatCode = this.sunatCode;
178+
fileDeliveryEntity.documentID = this.documentID;
179+
fileDeliveryEntity.cdrID = this.cdrID;
180+
fileDeliveryEntity.customId = this.customId;
181+
fileDeliveryEntity.documentType = this.documentType;
182+
fileDeliveryEntity.deliveryStatus = this.deliveryStatus;
183+
fileDeliveryEntity.sunatTicket = this.sunatTicket;
164184
return fileDeliveryEntity;
165185
}
166186
}

src/main/java/org/openubl/providers/WSProvider.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import javax.jms.*;
1919
import javax.transaction.Transactional;
2020
import java.io.IOException;
21+
import java.lang.IllegalStateException;
22+
import java.util.Optional;
2123

2224
@ApplicationScoped
2325
public class WSProvider {
@@ -33,17 +35,20 @@ public class WSProvider {
3335
@Inject
3436
ConnectionFactory connectionFactory;
3537

36-
@ConfigProperty(name = "openubl.callbackQueue")
38+
@ConfigProperty(name = "openubl.jms.delay")
39+
Long messageDelay;
40+
41+
@ConfigProperty(name = "openubl.jms.callbackQueue")
3742
String callbackQueue;
3843

39-
@ConfigProperty(name = "openubl.ticketQueue")
44+
@ConfigProperty(name = "openubl.jms.ticketQueue")
4045
String ticketQueue;
4146

42-
@ConfigProperty(name = "openubl.username")
43-
String username;
47+
@ConfigProperty(name = "openubl.sunat.username")
48+
Optional<String> sunatUsername;
4449

45-
@ConfigProperty(name = "openubl.password")
46-
String password;
50+
@ConfigProperty(name = "openubl.sunat.password")
51+
Optional<String> sunatPassword;
4752

4853
/**
4954
* @return true if message was processed and don't need to be redelivered
@@ -58,13 +63,13 @@ public boolean sendFileDelivery(Long id) {
5863

5964
DocumentType documentType = deliveryEntity.documentType;
6065

61-
byte[] file = filesManager.getFileAsBytes(deliveryEntity.fileID);
66+
byte[] file = filesManager.getFileAsBytesWithoutUnzipping(deliveryEntity.fileID);
6267

6368
// Send to SUNAT
6469
ServiceConfig config = new ServiceConfig.Builder()
6570
.url(deliveryEntity.serverUrl)
66-
.username(username)
67-
.password(password)
71+
.username(deliveryEntity.sunatUsername != null ? deliveryEntity.sunatUsername : sunatUsername.orElseThrow(IllegalStateException::new))
72+
.password(deliveryEntity.sunatPassword != null ? deliveryEntity.sunatPassword : sunatPassword.orElseThrow(IllegalStateException::new))
6873
.build();
6974

7075
BillServiceModel billServiceModel;
@@ -73,12 +78,12 @@ public boolean sendFileDelivery(Long id) {
7378
case INVOICE:
7479
case CREDIT_NOTE:
7580
case DEBIT_NOTE:
76-
billServiceModel = BillServiceManager.sendBill(deliveryEntity.filename, file, config);
81+
billServiceModel = BillServiceManager.sendBill(deliveryEntity.filename + ".zip", file, config);
7782
processCDR(billServiceModel, deliveryEntity);
7883
break;
7984
case VOIDED_DOCUMENT:
8085
case SUMMARY_DOCUMENT:
81-
billServiceModel = BillServiceManager.sendSummary(deliveryEntity.filename, file, config);
86+
billServiceModel = BillServiceManager.sendSummary(deliveryEntity.filename + ".zip", file, config);
8287
processTicket(billServiceModel, deliveryEntity);
8388
break;
8489
default:
@@ -116,8 +121,14 @@ public boolean checkTicket(long fileDeliveryID) {
116121
try {
117122
ServiceConfig config = new ServiceConfig.Builder()
118123
.url(deliveryEntity.serverUrl)
119-
.username(username)
120-
.password(password)
124+
.username(deliveryEntity.sunatUsername != null
125+
? deliveryEntity.sunatUsername
126+
: sunatUsername.orElseThrow(() -> new IllegalStateException("Could not find a username for sending to SUNAT"))
127+
)
128+
.password(deliveryEntity.sunatPassword != null
129+
? deliveryEntity.sunatPassword
130+
: sunatPassword.orElseThrow(() -> new IllegalStateException("Could not find a username for sending to SUNAT"))
131+
)
121132
.build();
122133
billServiceModel = BillServiceManager.getStatus(deliveryEntity.sunatTicket, config);
123134
} catch (UnknownWebServiceException e) {
@@ -139,7 +150,8 @@ private void processCDR(BillServiceModel billServiceModel, FileDeliveryEntity de
139150
// Save CDR in storage
140151
String cdrID = null;
141152
if (billServiceModel.getCdr() != null) {
142-
cdrID = filesManager.upload(billServiceModel.getCdr(), deliveryEntity.filename + ".cdr.zip", FileType.ZIP);
153+
// The filename does not really matter here
154+
cdrID = filesManager.uploadFile(billServiceModel.getCdr(), deliveryEntity.filename, FileType.ZIP);
143155
}
144156

145157
// Update DB
@@ -170,6 +182,7 @@ private void processTicket(BillServiceModel billServiceModel, FileDeliveryEntity
170182
public void produceTicketMessage(FileDeliveryEntity deliveryEntity) {
171183
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
172184
JMSProducer producer = context.createProducer();
185+
producer.setDeliveryDelay(messageDelay);
173186
Queue queue = context.createQueue(ticketQueue);
174187
Message message = context.createTextMessage(deliveryEntity.id.toString());
175188
producer.send(queue, message);
@@ -179,6 +192,7 @@ public void produceTicketMessage(FileDeliveryEntity deliveryEntity) {
179192
public void produceCallbackMessage(FileDeliveryEntity deliveryEntity) {
180193
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
181194
JMSProducer producer = context.createProducer();
195+
producer.setDeliveryDelay(messageDelay);
182196
Queue queue = context.createQueue(callbackQueue);
183197
Message message = context.createTextMessage(deliveryEntity.id.toString());
184198
producer.send(queue, message);

src/main/java/org/openubl/resources/DocumentsResource.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public Response sendXML(MultipartFormDataInput input) {
3636
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
3737

3838
List<InputPart> fileInputParts = uploadForm.get("file");
39+
List<InputPart> usernameInputParts = uploadForm.get("username");
40+
List<InputPart> passwordInputParts = uploadForm.get("password");
3941
List<InputPart> customIdInputParts = uploadForm.get("customId");
4042

4143
if (fileInputParts == null) {
@@ -44,6 +46,8 @@ public Response sendXML(MultipartFormDataInput input) {
4446
}
4547

4648
byte[] xmlFile = null;
49+
String username = null;
50+
String password = null;
4751
String customId = null;
4852

4953
try {
@@ -52,6 +56,18 @@ public Response sendXML(MultipartFormDataInput input) {
5256
xmlFile = IOUtils.toByteArray(fileInputStream);
5357
}
5458

59+
if (usernameInputParts != null) {
60+
for (InputPart inputPart : usernameInputParts) {
61+
username = inputPart.getBodyAsString();
62+
}
63+
}
64+
65+
if (passwordInputParts != null) {
66+
for (InputPart inputPart : passwordInputParts) {
67+
password = inputPart.getBodyAsString();
68+
}
69+
}
70+
5571
if (customIdInputParts != null) {
5672
for (InputPart inputPart : customIdInputParts) {
5773
customId = inputPart.getBodyAsString();
@@ -68,7 +84,7 @@ public Response sendXML(MultipartFormDataInput input) {
6884

6985
FileDeliveryEntity fileDeliveryEntity;
7086
try {
71-
fileDeliveryEntity = documentsManager.createFileDeliveryAndSchedule(xmlFile, customId);
87+
fileDeliveryEntity = documentsManager.createFileDeliveryAndSchedule(xmlFile, username, password, customId);
7288
} catch (InvalidXMLFileException e) {
7389
LOG.error(e);
7490
ErrorRepresentation error = new ErrorRepresentation("Form[file] is not a valid XML file or is corrupted");

0 commit comments

Comments
 (0)