Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package nfe; |
| 2 | |||
| 3 | import java.rmi.RemoteException; |
||
| 4 | import java.util.Iterator; |
||
| 5 | |||
| 6 | import javax.xml.bind.JAXBException; |
||
| 7 | import javax.xml.stream.XMLStreamException; |
||
| 8 | |||
| 9 | import org.apache.axiom.om.OMAbstractFactory; |
||
| 10 | import org.apache.axiom.om.OMElement; |
||
| 11 | import org.apache.axiom.om.OMFactory; |
||
| 12 | import org.apache.axiom.om.OMText; |
||
| 13 | import org.apache.axiom.om.util.AXIOMUtil; |
||
| 14 | import org.apache.axis2.transport.http.HTTPConstants; |
||
| 15 | |||
| 16 | import br.inf.portalfiscal.nfe.schema_4.enviNFe.TEnviNFe; |
||
| 17 | import br.inf.portalfiscal.nfe.schema_4.enviNFe.TRetEnviNFe; |
||
| 18 | import br.inf.portalfiscal.www.nfe_400.wsdl.NFeAutorizacao.NFeAutorizacao4Stub; |
||
| 19 | import nfe.dom.ConfiguracoesIniciaisNfe; |
||
| 20 | import nfe.exception.NfeException; |
||
| 21 | import nfe.exception.NfeValidacaoException; |
||
| 22 | import nfe.util.CertificadoUtil; |
||
| 23 | import nfe.util.ConstantesUtil; |
||
| 24 | import nfe.util.ObjetoUtil; |
||
| 25 | import nfe.util.WebServiceUtil; |
||
| 26 | import nfe.util.XmlUtil; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Classe Responsavel por Enviar o XML. |
||
| 30 | * |
||
| 31 | * @author Samuel Oliveira - samuk.exe@hotmail.com - www.samuelweb.com.br |
||
| 32 | */ |
||
| 33 | class Enviar { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Metodo para Montar a NFE |
||
| 37 | * |
||
| 38 | * @param enviNFe |
||
| 39 | * @param valida |
||
| 40 | * @return |
||
| 41 | * @throws NfeException |
||
| 42 | */ |
||
| 43 | static TEnviNFe montaNfe(TEnviNFe enviNFe, boolean valida) throws NfeException { |
||
| 44 | |||
| 45 | try { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Cria o xml |
||
| 49 | */ |
||
| 50 | String xml = XmlUtil.objectToXml(enviNFe); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Assina o Xml |
||
| 54 | */ |
||
| 55 | xml = Assinar.assinaNfe(xml, "NFe"); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Valida o Xml caso sejá selecionado True |
||
| 59 | */ |
||
| 60 | if(valida){ |
||
| 61 | String erros = Validar.validaXml(xml, Validar.ENVIO); |
||
| 62 | if (!ObjetoUtil.isEmpty(erros)) { |
||
| 63 | throw new NfeValidacaoException("Erro Na Validação do Xml: " + erros); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if (ConfiguracoesIniciaisNfe.getInstance().isLog()) { |
||
| 68 | System.out.println("Xml Assinado: " + xml); |
||
| 69 | } |
||
| 70 | |||
| 71 | return XmlUtil.xmlToObject(xml, TEnviNFe.class); |
||
| 72 | |||
| 73 | } catch (JAXBException e) { |
||
| 74 | throw new NfeException(e.getMessage()); |
||
| 75 | } |
||
| 76 | |||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Metodo para Enviar a NFE. |
||
| 81 | * |
||
| 82 | * @param enviNFe |
||
| 83 | * @param tipo |
||
| 84 | * @return |
||
| 85 | * @throws NfeException |
||
| 86 | */ |
||
| 87 | static TRetEnviNFe enviaNfe(TEnviNFe enviNFe, String tipo) throws NfeException { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Informacoes do Certificado Digital. |
||
| 91 | */ |
||
| 92 | ConfiguracoesIniciaisNfe config = CertificadoUtil.iniciaConfiguracoes(); |
||
| 93 | |||
| 94 | boolean nfce = tipo.equals(ConstantesUtil.NFCE); |
||
| 95 | String qrCode = ""; |
||
| 96 | |||
| 97 | try { |
||
| 98 | if (nfce) { |
||
| 99 | qrCode = enviNFe.getNFe().get(0).getInfNFeSupl().getQrCode(); |
||
| 100 | enviNFe.getNFe().get(0).getInfNFeSupl().setQrCode(""); |
||
| 101 | } |
||
| 102 | |||
| 103 | String xml = XmlUtil.objectToXml(enviNFe); |
||
| 104 | |||
| 105 | if (nfce) { |
||
| 106 | enviNFe.getNFe().get(0).getInfNFeSupl().setQrCode(qrCode); |
||
| 107 | } |
||
| 108 | |||
| 109 | OMElement ome = AXIOMUtil.stringToOM(xml); |
||
| 110 | |||
| 111 | Iterator<?> children = ome.getChildrenWithLocalName("NFe"); |
||
| 112 | while (children.hasNext()) { |
||
| 113 | OMElement omElementNFe = (OMElement) children.next(); |
||
| 114 | if ((omElementNFe != null) && ("NFe".equals(omElementNFe.getLocalName()))) { |
||
| 115 | omElementNFe.addAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe", null); |
||
| 116 | if (nfce) { |
||
| 117 | |||
| 118 | OMFactory f = OMAbstractFactory.getOMFactory(); |
||
| 119 | OMText omt = f.createOMText(qrCode, OMElement.CDATA_SECTION_NODE); |
||
| 120 | |||
| 121 | Iterator<?> itInfSupl = omElementNFe.getChildrenWithLocalName("infNFeSupl"); |
||
| 122 | while (itInfSupl.hasNext()) { |
||
| 123 | Object elementInfSupl = itInfSupl.next(); |
||
| 124 | if (elementInfSupl instanceof OMElement) { |
||
| 125 | OMElement omElementInfSupl = (OMElement) elementInfSupl; |
||
| 126 | Iterator<?> itqrCode = omElementInfSupl.getChildrenWithLocalName("qrCode"); |
||
| 127 | while (itqrCode.hasNext()) { |
||
| 128 | Object elementQrCode = itqrCode.next(); |
||
| 129 | if (elementQrCode instanceof OMElement) { |
||
| 130 | OMElement omElementQrCode = (OMElement) elementQrCode; |
||
| 131 | omElementQrCode.addChild(omt); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | //Adicionado CDATA após OM |
||
| 141 | if (nfce) { |
||
| 142 | enviNFe.getNFe().get(0).getInfNFeSupl().setQrCode("<![CDATA["+qrCode +"]]>"); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (config.isLog()) { |
||
| 146 | System.out.println("Xml para Envio: " + ome.toString()); |
||
| 147 | } |
||
| 148 | |||
| 149 | NFeAutorizacao4Stub.NfeDadosMsg dadosMsg = new NFeAutorizacao4Stub.NfeDadosMsg(); |
||
| 150 | dadosMsg.setExtraElement(ome); |
||
| 151 | |||
| 152 | NFeAutorizacao4Stub stub = new NFeAutorizacao4Stub(nfce ? WebServiceUtil.getUrl(ConstantesUtil.NFCE, ConstantesUtil.SERVICOS.ENVIO) : WebServiceUtil.getUrl(ConstantesUtil.NFE, ConstantesUtil.SERVICOS.ENVIO)); |
||
| 153 | //Timeout |
||
| 154 | if (!ObjetoUtil.isEmpty(config.getTimeout())) { |
||
| 155 | stub._getServiceClient().getOptions().setProperty( |
||
| 156 | HTTPConstants.SO_TIMEOUT, config.getTimeout()); |
||
| 157 | stub._getServiceClient().getOptions().setProperty( |
||
| 158 | HTTPConstants.CONNECTION_TIMEOUT, config.getTimeout()); |
||
| 159 | } |
||
| 160 | NFeAutorizacao4Stub.NfeResultMsg result = stub.nfeAutorizacaoLote(dadosMsg); |
||
| 161 | |||
| 162 | return XmlUtil.xmlToObject(result.getExtraElement().toString(), TRetEnviNFe.class); |
||
| 163 | |||
| 164 | } catch (RemoteException | XMLStreamException | JAXBException e) { |
||
| 165 | throw new NfeException(e.getMessage()); |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | } |