|
18 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
19 | 19 | import org.springframework.http.HttpEntity; |
20 | 20 | import org.springframework.http.HttpHeaders; |
| 21 | +import org.springframework.http.ResponseEntity; |
21 | 22 | import org.springframework.stereotype.Component; |
22 | 23 | import org.springframework.web.client.RestTemplate; |
23 | 24 | import org.springframework.web.util.UriComponentsBuilder; |
|
29 | 30 | import it.finanze.sanita.fse2.ms.edsclient.dto.EdsResponseDTO; |
30 | 31 | import it.finanze.sanita.fse2.ms.edsclient.dto.request.BrokerRequestDTO; |
31 | 32 | import it.finanze.sanita.fse2.ms.edsclient.dto.response.DocumentResponseDTO; |
| 33 | +import it.finanze.sanita.fse2.ms.edsclient.dto.response.GetDocumentReferenceResDTO; |
32 | 34 | import it.finanze.sanita.fse2.ms.edsclient.enums.ProcessorOperationEnum; |
33 | 35 | import it.finanze.sanita.fse2.ms.edsclient.enums.ResultLogEnum; |
34 | 36 | import it.finanze.sanita.fse2.ms.edsclient.exceptions.BusinessException; |
|
41 | 43 | @Component |
42 | 44 | public class BrokerClient implements IBrokerClient { |
43 | 45 |
|
44 | | - private static final String MSG_UNSUPPORTED = "Unsupported exception"; |
45 | | - |
46 | | - @Autowired |
47 | | - private RestTemplate restTemplate; |
48 | | - |
49 | | - @Autowired |
50 | | - private LoggerHelper logger; |
51 | | - |
52 | | - @Autowired |
53 | | - private BrokerCfg brokerCfg; |
54 | | - |
55 | | - @Override |
56 | | - public EdsResponseDTO dispatchAndSendData(BrokerRequestDTO brokerRequestDTO) { |
57 | | - |
58 | | - EdsResponseDTO output = new EdsResponseDTO(); |
59 | | - final Date startingDate = new Date(); |
60 | | - final String successLog = "Informazioni inviate al broker"; |
61 | | - final String errorLog = "Errore riscontrato durante l'invio delle informazioni al broker"; |
62 | | - |
63 | | - URI url = UriComponentsBuilder |
64 | | - .fromUriString(brokerCfg.getBrokerHost() + "/v1/ingestion/document" |
65 | | - + buildRequestPath(brokerRequestDTO.getOperation(), |
66 | | - brokerRequestDTO.getIdentifier(), brokerRequestDTO.getWorkflowInstanceId(), |
67 | | - brokerRequestDTO.getFiscalCode())) |
68 | | - .build().toUri(); |
69 | | - |
70 | | - HttpHeaders headers = new HttpHeaders(); |
71 | | - headers.set("Content-Type", "application/json"); |
72 | | - |
73 | | - try { |
74 | | - DocumentDTO requestBody = buildRequestBody(brokerRequestDTO); |
75 | | - HttpEntity<?> entity = new HttpEntity<>(requestBody, headers); |
76 | | - |
77 | | - restTemplate.exchange(url, Constants.AppConstants.methodMap.get(brokerRequestDTO.getOperation()), entity, |
78 | | - DocumentResponseDTO.class); |
79 | | - |
80 | | - logger.info(successLog, brokerRequestDTO.getOperation().getOperationLogEnum(), ResultLogEnum.OK, |
81 | | - startingDate); |
82 | | - output.setEsito(true); |
83 | | - } catch (Exception ex) { |
84 | | - logger.error(errorLog, brokerRequestDTO.getOperation().getOperationLogEnum(), ResultLogEnum.KO, |
85 | | - startingDate, brokerRequestDTO.getOperation().getErrorLogEnum()); |
86 | | - output.setExClassCanonicalName(ExceptionUtils.getRootCause(ex).getClass().getCanonicalName()); |
87 | | - output.setMessageError(ex.getMessage()); |
88 | | - } |
89 | | - |
90 | | - return output; |
91 | | - } |
92 | | - |
93 | | - private DocumentDTO buildRequestBody(BrokerRequestDTO brokerRequestDTO) { |
94 | | - DocumentDTO requestBody = null; |
95 | | - IniEdsInvocationETY ety = brokerRequestDTO.getIniEdsInvocationETY() != null |
96 | | - ? brokerRequestDTO.getIniEdsInvocationETY() |
97 | | - : null; |
98 | | - |
99 | | - switch (brokerRequestDTO.getOperation()) { |
100 | | - case UPDATE: |
101 | | - if (brokerRequestDTO.getUpdateReqDTO() == null) { |
102 | | - // bad request |
103 | | - throw new BusinessException(MSG_UNSUPPORTED); |
104 | | - } |
105 | | - requestBody = new DocumentDTO(); |
106 | | - requestBody.setIdentifier(brokerRequestDTO.getIdentifier()); |
107 | | - requestBody.setOperation(ProcessorOperationEnum.UPDATE); |
108 | | - requestBody.setJsonString(JsonUtility.objectToJson(brokerRequestDTO.getUpdateReqDTO())); |
109 | | - requestBody.setFiscalCode(brokerRequestDTO.getFiscalCode()); |
110 | | - requestBody.setRde(brokerRequestDTO.getIniEdsInvocationETY().getRde()); |
111 | | - break; |
112 | | - case REPLACE: |
113 | | - requestBody = new DocumentDTO(); |
114 | | - requestBody.setIdentifier(brokerRequestDTO.getIdentifier()); |
115 | | - requestBody.setOperation(ProcessorOperationEnum.REPLACE); |
116 | | - requestBody.setFiscalCode(brokerRequestDTO.getIniEdsInvocationETY().getFiscalCode()); |
117 | | - requestBody.setRde(brokerRequestDTO.getIniEdsInvocationETY().getRde()); |
118 | | - if (ety != null && ety.getData() != null) { |
119 | | - requestBody.setJsonString(JsonUtility.objectToJson(ety.getData())); |
120 | | - } else { |
121 | | - throw new BusinessException(MSG_UNSUPPORTED); |
122 | | - } |
123 | | - break; |
124 | | - |
125 | | - case DELETE: |
126 | | - break; |
127 | | - |
128 | | - case PUBLISH: |
129 | | - default: |
130 | | - requestBody = new DocumentDTO(); |
131 | | - requestBody.setIdentifier(brokerRequestDTO.getIdentifier()); |
132 | | - requestBody.setOperation(ProcessorOperationEnum.PUBLISH); |
133 | | - requestBody.setFiscalCode(brokerRequestDTO.getIniEdsInvocationETY().getFiscalCode()); |
134 | | - requestBody.setRde(brokerRequestDTO.getIniEdsInvocationETY().getRde()); |
135 | | - if (ety != null && ety.getData() != null) { |
136 | | - requestBody.setJsonString(JsonUtility.objectToJson(ety.getData())); |
137 | | - } else { |
138 | | - throw new BusinessException(MSG_UNSUPPORTED); |
139 | | - } |
140 | | - break; |
141 | | - } |
142 | | - |
143 | | - return requestBody; |
144 | | - |
145 | | - } |
146 | | - |
147 | | - private String buildRequestPath(final ProcessorOperationEnum operation, final String identifier, |
148 | | - final String workflowInstanceId, |
149 | | - final String fiscalCode) { |
150 | | - String requestPath = ""; |
151 | | - |
152 | | - switch (operation) { |
153 | | - case UPDATE: |
154 | | - requestPath = "/metadata"; |
155 | | - break; |
156 | | - case DELETE: |
157 | | - requestPath = "/identifier/" + identifier + "/" + fiscalCode; |
158 | | - break; |
159 | | - case REPLACE: |
160 | | - case PUBLISH: |
161 | | - requestPath = "/workflowinstanceid/" + workflowInstanceId; |
162 | | - break; |
163 | | - default: |
164 | | - break; |
165 | | - } |
166 | | - return requestPath; |
167 | | - } |
| 46 | + private static final String MSG_UNSUPPORTED = "Unsupported exception"; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private RestTemplate restTemplate; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private LoggerHelper logger; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private BrokerCfg brokerCfg; |
| 56 | + |
| 57 | + @Override |
| 58 | + public EdsResponseDTO dispatchAndSendData(BrokerRequestDTO brokerRequestDTO) { |
| 59 | + |
| 60 | + EdsResponseDTO output = new EdsResponseDTO(); |
| 61 | + final Date startingDate = new Date(); |
| 62 | + final String successLog = "Informazioni inviate al broker"; |
| 63 | + final String errorLog = "Errore riscontrato durante l'invio delle informazioni al broker"; |
| 64 | + |
| 65 | + URI url = UriComponentsBuilder |
| 66 | + .fromUriString(brokerCfg.getBrokerHost() + "/v1/ingestion/document" |
| 67 | + + buildRequestPath(brokerRequestDTO.getOperation(), |
| 68 | + brokerRequestDTO.getIdentifier(), brokerRequestDTO.getWorkflowInstanceId(), |
| 69 | + brokerRequestDTO.getFiscalCode())) |
| 70 | + .build().toUri(); |
| 71 | + |
| 72 | + HttpHeaders headers = new HttpHeaders(); |
| 73 | + headers.set("Content-Type", "application/json"); |
| 74 | + |
| 75 | + try { |
| 76 | + DocumentDTO requestBody = buildRequestBody(brokerRequestDTO); |
| 77 | + HttpEntity<?> entity = new HttpEntity<>(requestBody, headers); |
| 78 | + |
| 79 | + restTemplate.exchange(url, Constants.AppConstants.methodMap.get(brokerRequestDTO.getOperation()), entity, |
| 80 | + DocumentResponseDTO.class); |
| 81 | + |
| 82 | + logger.info(successLog, brokerRequestDTO.getOperation().getOperationLogEnum(), ResultLogEnum.OK, |
| 83 | + startingDate); |
| 84 | + output.setEsito(true); |
| 85 | + } catch (Exception ex) { |
| 86 | + logger.error(errorLog, brokerRequestDTO.getOperation().getOperationLogEnum(), ResultLogEnum.KO, |
| 87 | + startingDate, brokerRequestDTO.getOperation().getErrorLogEnum()); |
| 88 | + output.setExClassCanonicalName(ExceptionUtils.getRootCause(ex).getClass().getCanonicalName()); |
| 89 | + output.setMessageError(ex.getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + return output; |
| 93 | + } |
| 94 | + |
| 95 | + private DocumentDTO buildRequestBody(BrokerRequestDTO brokerRequestDTO) { |
| 96 | + DocumentDTO requestBody = null; |
| 97 | + IniEdsInvocationETY ety = brokerRequestDTO.getIniEdsInvocationETY() != null |
| 98 | + ? brokerRequestDTO.getIniEdsInvocationETY() |
| 99 | + : null; |
| 100 | + |
| 101 | + switch (brokerRequestDTO.getOperation()) { |
| 102 | + case UPDATE: |
| 103 | + if (brokerRequestDTO.getUpdateReqDTO() == null) { |
| 104 | + // bad request |
| 105 | + throw new BusinessException(MSG_UNSUPPORTED); |
| 106 | + } |
| 107 | + requestBody = new DocumentDTO(); |
| 108 | + requestBody.setIdentifier(brokerRequestDTO.getIdentifier()); |
| 109 | + requestBody.setOperation(ProcessorOperationEnum.UPDATE); |
| 110 | + requestBody.setJsonString(brokerRequestDTO.getUpdateReqDTO().getDocumentReference()); |
| 111 | + requestBody.setFiscalCode(brokerRequestDTO.getFiscalCode()); |
| 112 | + // requestBody.setRde(brokerRequestDTO.getIniEdsInvocationETY().getRde()); |
| 113 | + break; |
| 114 | + case REPLACE: |
| 115 | + requestBody = new DocumentDTO(); |
| 116 | + requestBody.setIdentifier(brokerRequestDTO.getIdentifier()); |
| 117 | + requestBody.setOperation(ProcessorOperationEnum.REPLACE); |
| 118 | + requestBody.setFiscalCode(brokerRequestDTO.getIniEdsInvocationETY().getFiscalCode()); |
| 119 | + requestBody.setRde(brokerRequestDTO.getIniEdsInvocationETY().getRde()); |
| 120 | + if (ety != null && ety.getData() != null) { |
| 121 | + requestBody.setJsonString(JsonUtility.objectToJson(ety.getData())); |
| 122 | + } else { |
| 123 | + throw new BusinessException(MSG_UNSUPPORTED); |
| 124 | + } |
| 125 | + break; |
| 126 | + |
| 127 | + case DELETE: |
| 128 | + break; |
| 129 | + |
| 130 | + case PUBLISH: |
| 131 | + default: |
| 132 | + requestBody = new DocumentDTO(); |
| 133 | + requestBody.setIdentifier(brokerRequestDTO.getIdentifier()); |
| 134 | + requestBody.setOperation(ProcessorOperationEnum.PUBLISH); |
| 135 | + requestBody.setFiscalCode(brokerRequestDTO.getIniEdsInvocationETY().getFiscalCode()); |
| 136 | + requestBody.setRde(brokerRequestDTO.getIniEdsInvocationETY().getRde()); |
| 137 | + if (ety != null && ety.getData() != null) { |
| 138 | + requestBody.setJsonString(JsonUtility.objectToJson(ety.getData())); |
| 139 | + } else { |
| 140 | + throw new BusinessException(MSG_UNSUPPORTED); |
| 141 | + } |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + return requestBody; |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + private String buildRequestPath(final ProcessorOperationEnum operation, final String identifier, |
| 150 | + final String workflowInstanceId, |
| 151 | + final String fiscalCode) { |
| 152 | + String requestPath = ""; |
| 153 | + |
| 154 | + switch (operation) { |
| 155 | + case UPDATE: |
| 156 | + requestPath = "/metadata"; |
| 157 | + break; |
| 158 | + case DELETE: |
| 159 | + requestPath = "/identifier/" + identifier + "/" + fiscalCode; |
| 160 | + break; |
| 161 | + case REPLACE: |
| 162 | + case PUBLISH: |
| 163 | + requestPath = "/workflowinstanceid/" + workflowInstanceId; |
| 164 | + break; |
| 165 | + default: |
| 166 | + break; |
| 167 | + } |
| 168 | + return requestPath; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public GetDocumentReferenceResDTO getDocumentReference(String fiscalCode, String masterIdentifier) { |
| 173 | + final URI uri = UriComponentsBuilder |
| 174 | + .fromUriString(brokerCfg.getBrokerHost()) |
| 175 | + .path("/v1/ingestion/document-reference/{fiscalCode}/{masterIdentifier}") |
| 176 | + .buildAndExpand(fiscalCode, masterIdentifier) |
| 177 | + .toUri(); |
| 178 | + |
| 179 | + return restTemplate.getForObject(uri, GetDocumentReferenceResDTO.class); |
| 180 | + |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + |
168 | 185 | } |
0 commit comments