Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
Update xbuilder quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Dec 7, 2020
1 parent 5d0a5bb commit 7dfa917
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 75 deletions.
4 changes: 3 additions & 1 deletion xbuilder-quickstart/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Execute Main.java
## XBuilder quickstart

Ejemplo sobre como usar XBuilder para crear y firmar comprobantes electrónicos. El presente ejemplo solo require que ejecutes la clase Main.java en cualquier IDE y ver el resultado que se imprime en la consola.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import java.math.BigDecimal;

public class UBLDefaults implements Config {
public class ConfigDefaults implements Config {
private BigDecimal igv = new BigDecimal("0.18");
private BigDecimal ivap = new BigDecimal("0.04");
private String defaultMoneda = "PEN";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,23 @@
import io.github.project.openubl.xmlbuilderlib.clock.SystemClock;
import io.github.project.openubl.xmlbuilderlib.config.Config;

import java.util.Calendar;
import java.util.TimeZone;
public class ConfigSingleton {

public class UBLConfigSingleton {

private static UBLConfigSingleton instance;
private static ConfigSingleton instance;

private Config config;
private SystemClock clock;

private UBLConfigSingleton() {
config = new UBLDefaults();
clock = new SystemClock() {
@Override
public TimeZone getTimeZone() {
return TimeZone.getTimeZone("America/Lima");
}

@Override
public Calendar getCalendarInstance() {
return Calendar.getInstance();
}
};
private ConfigSingleton() {
config = new ConfigDefaults();
clock = new DefaultSystemClock();
}

public static UBLConfigSingleton getInstance() {
public static ConfigSingleton getInstance() {
if (instance == null) {
synchronized (UBLConfigSingleton.class) {
synchronized (ConfigSingleton.class) {
if (instance == null) {
instance = new UBLConfigSingleton();
instance = new ConfigSingleton();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
* <p>
* 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
* <p>
* https://www.eclipse.org/legal/epl-2.0/
* <p>
* 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.quickstart.xbuilder;

import io.github.project.openubl.xmlbuilderlib.clock.SystemClock;

import java.util.Calendar;
import java.util.TimeZone;

public class DefaultSystemClock implements SystemClock {

@Override
public TimeZone getTimeZone() {
return TimeZone.getTimeZone("America/Lima");
}

@Override
public Calendar getCalendarInstance() {
return Calendar.getInstance();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* <p>
* 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
*
* <p>
* https://www.eclipse.org/legal/epl-2.0/
*
* <p>
* 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.
Expand All @@ -31,28 +31,53 @@
import io.github.project.openubl.xmlbuilderlib.xml.XMLSigner;
import io.github.project.openubl.xmlbuilderlib.xml.XmlSignatureHelper;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.crypto.MarshalException;
import javax.xml.crypto.dsig.XMLSignatureException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Base64;

public class Main {

public static void main(String[] args) throws Exception {
// Create XML
String xml = createUnsignedXML();

System.out.println("Your XML is:");
System.out.println(xml);

// Sign XML
Document signedXML = signXML(xml);

byte[] bytesFromDocument = XmlSignatureHelper.getBytesFromDocument(signedXML);
String signedXMLString = new String(bytesFromDocument, StandardCharsets.ISO_8859_1);

System.out.println("\n Your signed XML is:");
System.out.println(signedXMLString);
}

public static String createUnsignedXML() {
// General config
Config config = ConfigSingleton.getInstance().getConfig();
SystemClock clock = ConfigSingleton.getInstance().getClock();

// Invoice generation
InvoiceInputModel input = invoiceFactory();
DocumentWrapper<InvoiceOutputModel> result = DocumentManager.createXML(input, config, clock);
return result.getXml();
}

public static Document signXML(String xml) throws Exception {
InputStream ksInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("LLAMA-PE-CERTIFICADO-DEMO-12345678912.pfx");
CertificateDetails certificate = CertificateDetailsFactory.create(ksInputStream, "password");

X509Certificate x509Certificate = certificate.getX509Certificate();
PrivateKey privateKey = certificate.getPrivateKey();
return XMLSigner.signXML(xml, "Project OpenUBL", x509Certificate, privateKey);
}

public static InvoiceInputModel invoiceFactory() {
return InvoiceInputModel.Builder.anInvoiceInputModel()
.withSerie("F001")
Expand Down Expand Up @@ -83,40 +108,4 @@ public static InvoiceInputModel invoiceFactory() {
.build();
}

public static String createUnsignedXML() {
// General config
Config config = UBLConfigSingleton.getInstance().getConfig();
SystemClock clock = UBLConfigSingleton.getInstance().getClock();

// Invoice generation
InvoiceInputModel input = invoiceFactory();
DocumentWrapper<InvoiceOutputModel> result = DocumentManager.createXML(input, config, clock);
return result.getXml();
}

public static Document signXML(String xml) throws Exception {
InputStream ksInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("LLAMA-PE-CERTIFICADO-DEMO-12345678912.pfx");
CertificateDetails certificate = CertificateDetailsFactory.create(ksInputStream, "password");

X509Certificate x509Certificate = certificate.getX509Certificate();
PrivateKey privateKey = certificate.getPrivateKey();
return XMLSigner.signXML(xml, "Project OpenUBL", x509Certificate, privateKey);
}

public static void main(String[] args) throws Exception {
// Create XML
String xml = createUnsignedXML();

System.out.println("Your XML is:");
System.out.println(xml);

// Sign XML
Document signedXML = signXML(xml);

byte[] bytesFromDocument = XmlSignatureHelper.getBytesFromDocument(signedXML);
String signedXMLString = new String(bytesFromDocument, StandardCharsets.ISO_8859_1);

System.out.println("\n Your signed XML is:");
System.out.println(signedXMLString);
}
}

0 comments on commit 7dfa917

Please sign in to comment.