3535 */
3636class Enviar {
3737
38- /**
39- * Metodo para Montar a NFE
40- *
41- * @param enviNFe
42- * @param valida
43- * @return
44- * @throws NfeException
45- */
46- static TEnviNFe montaNfe (ConfiguracoesNfe config , TEnviNFe enviNFe , boolean valida ) throws NfeException {
47-
48- try {
49-
50- /**
51- * Cria o xml
52- */
53- String xml = XmlNfeUtil .objectToXml (enviNFe );
54-
55- /**
56- * Assina o Xml
57- */
58- xml = Assinar .assinaNfe (config , xml , AssinaturaEnum .NFE );
59-
60- //Retira Quebra de Linha
61- xml = xml .replaceAll (System .lineSeparator (), "" );
62-
63- LoggerUtil .log (Enviar .class , "[XML-ASSINADO]: " + xml );
64-
65- /**
66- * Valida o Xml caso sejá selecionado True
67- */
68- if (valida ) {
69- new Validar ().validaXml (config , xml , ServicosEnum .ENVIO );
70- }
71-
72- return XmlNfeUtil .xmlToObject (xml , TEnviNFe .class );
73-
74- } catch (Exception e ) {
75- throw new NfeException (e .getMessage ());
76- }
77-
78- }
79-
80- /**
81- * Metodo para Enviar a NFE.
82- *
83- * @param enviNFe
84- * @param tipoDocumento
85- * @return
86- * @throws NfeException
87- */
38+ /**
39+ * Metodo para Montar a NFE
40+ *
41+ * @param enviNFe
42+ * @param valida
43+ * @return
44+ * @throws NfeException
45+ */
46+ static TEnviNFe montaNfe (ConfiguracoesNfe config , TEnviNFe enviNFe , boolean valida ) throws NfeException {
47+
48+ try {
49+
50+ /**
51+ * Cria o xml
52+ */
53+ String xml = XmlNfeUtil .objectToXml (enviNFe );
54+
55+ /**
56+ * Assina o Xml
57+ */
58+ xml = Assinar .assinaNfe (config , xml , AssinaturaEnum .NFE );
59+
60+ //Retira Quebra de Linha
61+ xml = xml .replaceAll (System .lineSeparator (), "" );
62+
63+ LoggerUtil .log (Enviar .class , "[XML-ASSINADO]: " + xml );
64+
65+ /**
66+ * Valida o Xml caso sejá selecionado True
67+ */
68+ if (valida ) {
69+ new Validar ().validaXml (config , xml , ServicosEnum .ENVIO );
70+ }
71+
72+ return XmlNfeUtil .xmlToObject (xml , TEnviNFe .class );
73+
74+ } catch (Exception e ) {
75+ throw new NfeException (e .getMessage ());
76+ }
77+
78+ }
79+
80+ /**
81+ * Metodo para Enviar a NFE.
82+ *
83+ * @param enviNFe
84+ * @param tipoDocumento
85+ * @return
86+ * @throws NfeException
87+ */
8888 static TRetEnviNFe enviaNfe (ConfiguracoesNfe config , TEnviNFe enviNFe , DocumentoEnum tipoDocumento ) throws NfeException {
8989
90- try {
90+ try {
9191
92- String xml = XmlNfeUtil .objectToXml (enviNFe );
92+ String xml = XmlNfeUtil .objectToXml (enviNFe );
9393
94- OMElement ome ;
95- if (tipoDocumento .equals (DocumentoEnum .NFE )) {
96- ome = AXIOMUtil .stringToOM (xml );
97- } else {
98- OMFactory factory = OMAbstractFactory .getOMFactory ();
99- ome = factory .getMetaFactory ().createOMBuilder (factory , StAXParserConfiguration .NON_COALESCING , new InputSource (new StringReader (xml ))).getDocumentElement ();
100- }
94+ OMElement ome ;
95+ if (tipoDocumento .equals (DocumentoEnum .NFE )) {
96+ ome = AXIOMUtil .stringToOM (xml );
97+ } else {
98+ OMFactory factory = OMAbstractFactory .getOMFactory ();
99+ ome = factory .getMetaFactory ().createOMBuilder (factory , StAXParserConfiguration .NON_COALESCING , new InputSource (new StringReader (xml ))).getDocumentElement ();
100+ }
101101
102- Iterator <?> children = ome .getChildrenWithLocalName ("NFe" );
103- while (children .hasNext ()) {
104- OMElement omElementNFe = (OMElement ) children .next ();
105- if ((omElementNFe != null ) && ("NFe" .equals (omElementNFe .getLocalName ()))) {
106- omElementNFe .addAttribute ("xmlns" , "http://www.portalfiscal.inf.br/nfe" , null );
107- }
108- }
102+ Iterator <?> children = ome .getChildrenWithLocalName ("NFe" );
103+ while (children .hasNext ()) {
104+ OMElement omElementNFe = (OMElement ) children .next ();
105+ if ((omElementNFe != null ) && ("NFe" .equals (omElementNFe .getLocalName ()))) {
106+ omElementNFe .addAttribute ("xmlns" , "http://www.portalfiscal.inf.br/nfe" , null );
107+ }
108+ }
109109
110- LoggerUtil .log (Enviar .class , "[XML-ENVIO]: " + xml );
110+ LoggerUtil .log (Enviar .class , "[XML-ENVIO]: " + xml );
111111
112- NFeAutorizacao4Stub .NfeDadosMsg dadosMsg = new NFeAutorizacao4Stub .NfeDadosMsg ();
113- dadosMsg .setExtraElement (ome );
112+ NFeAutorizacao4Stub .NfeDadosMsg dadosMsg = new NFeAutorizacao4Stub .NfeDadosMsg ();
113+ dadosMsg .setExtraElement (ome );
114114
115115 NFeAutorizacao4Stub stub = new NFeAutorizacao4Stub (WebServiceUtil .getUrl (config , tipoDocumento , ServicosEnum .ENVIO ));
116116
117- // Timeout
118- if (ObjetoUtil .verifica (config .getTimeout ()).isPresent ()) {
119- stub ._getServiceClient ().getOptions ().setProperty (HTTPConstants .SO_TIMEOUT , config .getTimeout ());
120- stub ._getServiceClient ().getOptions ().setProperty (HTTPConstants .CONNECTION_TIMEOUT , config .getTimeout ());
121- }
122-
123- //Erro 411 MG
124- if (tipoDocumento .equals (DocumentoEnum .NFCE ) && config .getEstado ().equals (EstadosEnum .MG )) {
125- stub ._getServiceClient ().getOptions ().setProperty (HTTPConstants .CHUNKED , false );
126- }
127-
128- if (ObjetoUtil .verifica (config .getRetry ()).isPresent ()) {
129- RetryParameter .populateRetry (stub , config .getRetry ());
130- }
131-
132-
133- NFeAutorizacao4Stub .NfeResultMsg result = stub .nfeAutorizacaoLote (dadosMsg );
134- LoggerUtil .log (Enviar .class , "[XML-RETORNO]: " + result .getExtraElement ().toString ());
135- return XmlNfeUtil .xmlToObject (result .getExtraElement ().toString (), TRetEnviNFe .class );
136-
137- } catch (RemoteException | XMLStreamException | JAXBException e ) {
138- throw new NfeException (e .getMessage ());
139- }
140-
141- }
117+ // Timeout
118+ if (ObjetoUtil .verifica (config .getTimeout ()).isPresent ()) {
119+ stub ._getServiceClient ().getOptions ().setProperty (HTTPConstants .SO_TIMEOUT , config .getTimeout ());
120+ stub ._getServiceClient ().getOptions ().setProperty (HTTPConstants .CONNECTION_TIMEOUT , config .getTimeout ());
121+ }
122+
123+ //Erro 411 MG
124+ if (tipoDocumento .equals (DocumentoEnum .NFCE ) && config .getEstado ().equals (EstadosEnum .MG )) {
125+ stub ._getServiceClient ().getOptions ().setProperty (HTTPConstants .CHUNKED , false );
126+ }
127+
128+ if (ObjetoUtil .verifica (config .getRetry ()).isPresent ()) {
129+ RetryParameter .populateRetry (stub , config .getRetry ());
130+ }
131+
132+ NFeAutorizacao4Stub .NfeResultMsg result = stub .nfeAutorizacaoLote (dadosMsg );
133+ LoggerUtil .log (Enviar .class , "[XML-RETORNO]: " + result .getExtraElement ().toString ());
134+ return XmlNfeUtil .xmlToObject (result .getExtraElement ().toString (), TRetEnviNFe .class );
135+
136+ } catch (RemoteException | XMLStreamException | JAXBException e ) {
137+ throw new NfeException (e .getMessage ());
138+ }
139+
140+ }
142141
143142}
0 commit comments