Rev 414 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 414 | espaco | 1 | package nfe; |
| 2 | |||
| 3 | import br.com.swconsultoria.nfe.dom.ConfiguracoesNfe; |
||
| 4 | import br.com.swconsultoria.nfe.dom.enuns.AssinaturaEnum; |
||
| 5 | import br.com.swconsultoria.nfe.dom.enuns.DocumentoEnum; |
||
| 6 | import br.com.swconsultoria.nfe.dom.enuns.EstadosEnum; |
||
| 7 | import br.com.swconsultoria.nfe.dom.enuns.ServicosEnum; |
||
| 8 | import br.com.swconsultoria.nfe.exception.NfeException; |
||
| 9 | import br.com.swconsultoria.nfe.schema_4.enviNFe.TEnviNFe; |
||
| 10 | import br.com.swconsultoria.nfe.schema_4.enviNFe.TRetEnviNFe; |
||
| 11 | import br.com.swconsultoria.nfe.util.ObjetoUtil; |
||
| 12 | import br.com.swconsultoria.nfe.util.WebServiceUtil; |
||
| 13 | import br.com.swconsultoria.nfe.util.XmlNfeUtil; |
||
| 14 | import br.com.swconsultoria.nfe.ws.RetryParameter; |
||
| 15 | import br.com.swconsultoria.nfe.wsdl.NFeAutorizacao.NFeAutorizacao4Stub; |
||
| 16 | import lombok.extern.java.Log; |
||
| 17 | import org.apache.axiom.om.OMAbstractFactory; |
||
| 18 | import org.apache.axiom.om.OMElement; |
||
| 19 | import org.apache.axiom.om.OMFactory; |
||
| 20 | import org.apache.axiom.om.util.AXIOMUtil; |
||
| 21 | import org.apache.axiom.om.util.StAXParserConfiguration; |
||
| 22 | import org.apache.axis2.transport.http.HTTPConstants; |
||
| 23 | import org.xml.sax.InputSource; |
||
| 24 | |||
| 25 | import javax.xml.bind.JAXBException; |
||
| 26 | import javax.xml.stream.XMLStreamException; |
||
| 27 | import java.io.StringReader; |
||
| 28 | import java.rmi.RemoteException; |
||
| 29 | import java.util.Iterator; |
||
| 415 | espaco | 30 | import java.util.logging.Logger; |
| 414 | espaco | 31 | |
| 32 | /** |
||
| 33 | * Classe Responsavel por Enviar o XML. |
||
| 34 | * |
||
| 35 | * @author Samuel Oliveira - samuel@swconsultoria.com.br - www.swconsultoria.com.br |
||
| 36 | */ |
||
| 37 | @Log |
||
| 38 | class Enviar { |
||
| 415 | espaco | 39 | |
| 40 | private final static Logger log = Logger.getLogger(WebServiceUtil.class.getName()); |
||
| 414 | espaco | 41 | |
| 42 | /** |
||
| 43 | * Metodo para Montar a NFE |
||
| 44 | * |
||
| 45 | * @param enviNFe |
||
| 46 | * @param valida |
||
| 47 | * @return |
||
| 48 | * @throws NfeException |
||
| 49 | */ |
||
| 50 | static TEnviNFe montaNfe(ConfiguracoesNfe config, TEnviNFe enviNFe, boolean valida) throws NfeException { |
||
| 51 | |||
| 52 | try { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Cria o xml |
||
| 56 | */ |
||
| 57 | String xml = XmlNfeUtil.objectToXml(enviNFe, config.getEncode()); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Assina o Xml |
||
| 61 | */ |
||
| 62 | xml = Assinar.assinaNfe(config, xml, AssinaturaEnum.NFE); |
||
| 63 | |||
| 64 | //Retira Quebra de Linha |
||
| 65 | xml = xml.replaceAll(System.lineSeparator(), ""); |
||
| 66 | |||
| 67 | log.info("[XML-ASSINADO]: " + xml); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Valida o Xml caso sejá selecionado True |
||
| 71 | */ |
||
| 72 | if (valida) { |
||
| 73 | new Validar().validaXml(config, xml, ServicosEnum.ENVIO); |
||
| 74 | } |
||
| 75 | |||
| 76 | return XmlNfeUtil.xmlToObject(xml, TEnviNFe.class); |
||
| 77 | |||
| 78 | } catch (Exception e) { |
||
| 79 | throw new NfeException(e.getMessage(),e); |
||
| 80 | } |
||
| 81 | |||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Metodo para Enviar a NFE. |
||
| 86 | * |
||
| 87 | * @param enviNFe |
||
| 88 | * @param tipoDocumento |
||
| 89 | * @return |
||
| 90 | * @throws NfeException |
||
| 91 | */ |
||
| 92 | static TRetEnviNFe enviaNfe(ConfiguracoesNfe config, TEnviNFe enviNFe, DocumentoEnum tipoDocumento) throws NfeException { |
||
| 93 | |||
| 94 | try { |
||
| 95 | |||
| 96 | String xml = XmlNfeUtil.objectToXml(enviNFe, config.getEncode()); |
||
| 97 | |||
| 98 | OMElement ome; |
||
| 99 | if (tipoDocumento.equals(DocumentoEnum.NFE)) { |
||
| 100 | ome = AXIOMUtil.stringToOM(xml); |
||
| 101 | } else { |
||
| 102 | OMFactory factory = OMAbstractFactory.getOMFactory(); |
||
| 103 | ome = factory.getMetaFactory().createOMBuilder(factory, StAXParserConfiguration.NON_COALESCING, new InputSource(new StringReader(xml))).getDocumentElement(); |
||
| 104 | } |
||
| 105 | |||
| 106 | Iterator<?> children = ome.getChildrenWithLocalName("NFe"); |
||
| 107 | while (children.hasNext()) { |
||
| 108 | OMElement omElementNFe = (OMElement) children.next(); |
||
| 109 | if ((omElementNFe != null) && ("NFe".equals(omElementNFe.getLocalName()))) { |
||
| 110 | omElementNFe.addAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe", null); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | log.info("[XML-ENVIO]: " + xml); |
||
| 115 | |||
| 116 | NFeAutorizacao4Stub.NfeDadosMsg dadosMsg = new NFeAutorizacao4Stub.NfeDadosMsg(); |
||
| 117 | dadosMsg.setExtraElement(ome); |
||
| 118 | |||
| 119 | NFeAutorizacao4Stub stub = new NFeAutorizacao4Stub(WebServiceUtil.getUrl(config, tipoDocumento, ServicosEnum.ENVIO)); |
||
| 120 | |||
| 121 | // Timeout |
||
| 122 | if (ObjetoUtil.verifica(config.getTimeout()).isPresent()) { |
||
| 123 | stub._getServiceClient().getOptions().setProperty(HTTPConstants.SO_TIMEOUT, config.getTimeout()); |
||
| 124 | stub._getServiceClient().getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT, config.getTimeout()); |
||
| 125 | } |
||
| 126 | |||
| 127 | //Erro 411 MG |
||
| 128 | if (tipoDocumento.equals(DocumentoEnum.NFCE) && config.getEstado().equals(EstadosEnum.MG)) { |
||
| 129 | stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, false); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (ObjetoUtil.verifica(config.getRetry()).isPresent()) { |
||
| 133 | RetryParameter.populateRetry(stub, config.getRetry()); |
||
| 134 | } |
||
| 135 | |||
| 136 | NFeAutorizacao4Stub.NfeResultMsg result = stub.nfeAutorizacaoLote(dadosMsg); |
||
| 137 | log.info("[XML-RETORNO]: " + result.getExtraElement().toString()); |
||
| 138 | return XmlNfeUtil.xmlToObject(result.getExtraElement().toString(), TRetEnviNFe.class); |
||
| 139 | |||
| 140 | } catch (RemoteException | XMLStreamException | JAXBException e) { |
||
| 141 | throw new NfeException(e.getMessage(), e); |
||
| 142 | } |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 146 | } |