Skip to content

Commit

Permalink
Externale Message per ABIL
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrycz committed Jun 12, 2019
1 parent 7d02c71 commit 158db05
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ release_artifact:
script:
- git checkout -B "$CI_BUILD_REF_NAME"
# mi metto in condizione del push per l'utente con la deploy key
- git remote set-url --push origin [email protected]:$CI_PROJECT_PATH
- git remote set-url --push origin https://[email protected]:documentale/sprint-flows
- mvn -Dresume=false release:prepare release:perform -Pprod -DpushChanges=false -DlocalCheckout=true
- git push --tags
- git commit --amend -m "[ci skip] prepare for next development iteration"
Expand Down
8 changes: 8 additions & 0 deletions .jhipster/ExternalMessage.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
{
"fieldName": "lastErrorMessage",
"fieldType": "String"
},
{
"fieldName": "application",
"fieldType": "ExternalApplication",
"fieldValues": "ABIL,SIGLA,ACE,MISSIONI,ATTESTATI,GENERIC",
"fieldValidateRules": [
"required"
]
}
],
"changelogDate": "20190320102314",
Expand Down
86 changes: 79 additions & 7 deletions src/main/java/it/cnr/si/config/ExtenalMessageSender.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package it.cnr.si.config;

import it.cnr.si.domain.ExternalMessage;
import it.cnr.si.domain.enumeration.ExternalApplication;
import it.cnr.si.domain.enumeration.ExternalMessageStatus;
import it.cnr.si.service.ExternalMessageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EnableScheduling
@Configuration
Expand All @@ -25,12 +34,37 @@ public class ExtenalMessageSender {

@Inject
private ExternalMessageService externalMessageService;

private RestTemplate restTemplate;
@Inject
private Environment env;

@PostConstruct
public void init() {
restTemplate = new RestTemplate();

// ABIL

RestTemplate abilTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = abilTemplate.getInterceptors();
interceptors.add(new AbilRequestInterceptor());
abilTemplate.setInterceptors(interceptors);

// ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
// resource.setUsername(env.getProperty("cnr.abil.username", String.class));
// resource.setPassword(env.getProperty("cnr.abil.password", String.class));
//
// resource.setScope(Arrays.asList("read", "write"));
// resource.setAccessTokenUri(env.getProperty("cnr.abil.accessTokenUri", String.class));
//// resource.setClientId("cnr.abil.clientId");
//// resource.setClientSecret(env.getProperty("cnr.abil.clientSecret", String.class));
//
// DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
// RestTemplate abil = new OAuth2RestTemplate(resource, clientContext);

ExternalApplication.ABIL.setTemplate(abilTemplate);

// GENERIC

ExternalApplication.GENERIC.setTemplate(new RestTemplate());

}

@Scheduled(fixedDelay = 600000, initialDelay = 10000) // 10m
Expand All @@ -52,7 +86,9 @@ private void send(ExternalMessage msg) {
ResponseEntity<String> response = null;
try {

response = restTemplate.exchange(
RestTemplate template = msg.getApplication().getTemplate();

response = template.exchange(
msg.getUrl(),
msg.getVerb().value(),
new HttpEntity<>(msg.getPayload()),
Expand Down Expand Up @@ -86,6 +122,42 @@ else if (response.getBody() == null)
}
}


private class AbilRequestInterceptor implements ClientHttpRequestInterceptor {

private String id_token = null;

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

request.getHeaders().add("Authorization", "Bearer "+ id_token);
ClientHttpResponse response = execution.execute(request, body);

if ( response.getStatusCode() == HttpStatus.FORBIDDEN || response.getStatusCode() == HttpStatus.UNAUTHORIZED) {

Map<String, String> auth = new HashMap<>();
auth.put("username", env.getProperty("cnr.abil.username"));
auth.put("password", env.getProperty("cnr.abil.password"));
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
RequestEntity entity = new RequestEntity(
auth,
headers,
HttpMethod.POST,
URI.create(env.getProperty("cnr.abil.loginUrl")));

ResponseEntity<Map> resp = new RestTemplate().exchange(entity, Map.class);

this.id_token = (String) resp.getBody().get("id_token");

request.getHeaders().add("Authorization", "Bearer "+ id_token);
response = execution.execute(request, body);
}


return response;
}
}
}


Expand Down
24 changes: 23 additions & 1 deletion src/main/java/it/cnr/si/domain/ExternalMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import it.cnr.si.domain.enumeration.ExternalMessageStatus;

import it.cnr.si.domain.enumeration.ExternalApplication;

/**
* A ExternalMessage.
*/
Expand Down Expand Up @@ -44,12 +46,18 @@ public class ExternalMessage implements Serializable {
@Column(name = "status", nullable = false)
private ExternalMessageStatus status;

@Column(name = "retries")
@NotNull
@Column(name = "retries", nullable = false)
private Integer retries;

@Column(name = "last_error_message")
private String lastErrorMessage;

@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "application", nullable = false)
private ExternalApplication application;

public Long getId() {
return id;
}
Expand Down Expand Up @@ -136,6 +144,19 @@ public void setLastErrorMessage(String lastErrorMessage) {
this.lastErrorMessage = lastErrorMessage;
}

public ExternalApplication getApplication() {
return application;
}

public ExternalMessage application(ExternalApplication application) {
this.application = application;
return this;
}

public void setApplication(ExternalApplication application) {
this.application = application;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -166,6 +187,7 @@ public String toString() {
", status='" + status + "'" +
", retries='" + retries + "'" +
", lastErrorMessage='" + lastErrorMessage + "'" +
", application='" + application + "'" +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import it.cnr.si.flows.ng.service.FlowsAttachmentService;
import it.cnr.si.flows.ng.service.FlowsPdfService;
import it.cnr.si.flows.ng.service.FlowsProcessInstanceService;
import it.cnr.si.flows.ng.service.FlowsTaskService;
import it.cnr.si.flows.ng.service.ProtocolloDocumentoService;
import it.cnr.si.service.ExternalMessageService;
import it.cnr.si.domain.enumeration.ExternalMessageVerb;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void restToApplicazioneAccordiBilaterali(DelegateExecution execution, Sta
put("stato", statoDomanda.name().toString());
}
};
externalMessageService.createExternalMessage(urlAccordiBilaterali, ExternalMessageVerb.POST, abilPayload);
externalMessageService.createExternalMessage(urlAccordiBilaterali, ExternalMessageVerb.POST, abilPayload, null);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class EndAcquisti {

public void onEvent(ActivitiEvent event) {
if (event.getType() == ActivitiEventType.PROCESS_COMPLETED) {
externalMessageService.createExternalMessage("url", ExternalMessageVerb.POST, new HashMap());
externalMessageService.createExternalMessage("url", ExternalMessageVerb.POST, new HashMap(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public void notify(DelegateExecution execution) throws Exception {
//TODO implementare le url a seconda del contesto
String urlSigla = "www.google.it";
Map<String, Object> siglaPayload = createSiglaPayload(execution);
externalMessageService.createExternalMessage(urlSigla, ExternalMessageVerb.POST, siglaPayload);
externalMessageService.createExternalMessage(urlSigla, ExternalMessageVerb.POST, siglaPayload, null);
prepareFilesToSigla(execution);
};break;
case "end-stipulato-end": {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/it/cnr/si/service/ExternalMessageService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.cnr.si.service;

import com.google.gson.Gson;
import it.cnr.si.domain.ExternalMessage;
import it.cnr.si.domain.enumeration.ExternalApplication;
import it.cnr.si.domain.enumeration.ExternalMessageStatus;
import it.cnr.si.domain.enumeration.ExternalMessageVerb;
import it.cnr.si.repository.ExternalMessageRepository;
Expand Down Expand Up @@ -78,14 +78,16 @@ public void delete(Long id) {
externalMessageRepository.delete(id);
}

public void createExternalMessage(String url, ExternalMessageVerb verb, Map<String, Object> payload) {
public void createExternalMessage(String url, ExternalMessageVerb verb, Map<String, Object> payload, ExternalApplication app) {


JSONObject payloadString = new JSONObject(payload);


ExternalMessage msg = new ExternalMessage();

if (app != null)
msg.setApplication(app);
else
msg.setApplication(ExternalApplication.GENERIC);

msg.setUrl(url);
msg.setVerb(verb);
msg.setPayload(payloadString.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
<column name="last_error_message" type="varchar(255)">
<constraints nullable="true" />
</column>


<column name="application" type="varchar(255)">
<constraints nullable="false" />
</column>

<!-- jhipster-needle-liquibase-add-column - Jhipster will add columns here, do not remove-->
</createTable>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ <h2><span translate="sprintApp.externalMessage.detail.title">External Message</s
<dd>
<span>{{vm.externalMessage.lastErrorMessage}}</span>
</dd>
<dt><span translate="sprintApp.externalMessage.application">Application</span></dt>
<dd>
<span translate="{{'sprintApp.ExternalApplication.' + vm.externalMessage.application}}">{{vm.externalMessage.application}}</span>
</dd>
</dl>

<button type="submit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,41 @@ <h4 class="modal-title" id="myExternalMessageLabel" translate="sprintApp.externa
<label class="control-label" translate="sprintApp.externalMessage.retries" for="field_retries">Retries</label>
<input type="number" class="form-control" name="retries" id="field_retries"
ng-model="vm.externalMessage.retries"
/>
required />
<div ng-show="editForm.retries.$invalid">
<p class="help-block"
ng-show="editForm.retries.$error.required" translate="entity.validation.required">
This field is required.
</p>
<p class="help-block"
ng-show="editForm.retries.$error.number" translate="entity.validation.number">
This field should be a number.
</p>
</div>
</div>
<div class="form-group">
<label class="control-label" translate="sprintApp.externalMessage.lastErrorMessage" for="field_lastErrorMessage">Last Error Message</label>
<input type="text" class="form-control" name="lastErrorMessage" id="field_lastErrorMessage"
ng-model="vm.externalMessage.lastErrorMessage"
/>
</div>
<div class="form-group">
<label class="control-label" translate="sprintApp.externalMessage.application" for="field_application">Application</label>
<select class="form-control" name="application" ng-model="vm.externalMessage.application" id="field_application" required>
<option value="ABIL" translate="sprintApp.ExternalApplication.ABIL">ABIL</option>
<option value="SIGLA" translate="sprintApp.ExternalApplication.SIGLA">SIGLA</option>
<option value="ACE" translate="sprintApp.ExternalApplication.ACE">ACE</option>
<option value="MISSIONI" translate="sprintApp.ExternalApplication.MISSIONI">MISSIONI</option>
<option value="ATTESTATI" translate="sprintApp.ExternalApplication.ATTESTATI">ATTESTATI</option>
<option value="GENERIC" translate="sprintApp.ExternalApplication.GENERIC">GENERIC</option>
</select>
<div ng-show="editForm.application.$invalid">
<p class="help-block"
ng-show="editForm.application.$error.required" translate="entity.validation.required">
This field is required.
</p>
</div>
</div>

</div>
<div class="modal-footer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
$translatePartialLoader.addPart('externalMessage');
$translatePartialLoader.addPart('externalMessageVerb');
$translatePartialLoader.addPart('externalMessageStatus');
$translatePartialLoader.addPart('externalApplication');
$translatePartialLoader.addPart('global');
return $translate.refresh();
}]
Expand All @@ -72,6 +73,7 @@
$translatePartialLoader.addPart('externalMessage');
$translatePartialLoader.addPart('externalMessageVerb');
$translatePartialLoader.addPart('externalMessageStatus');
$translatePartialLoader.addPart('externalApplication');
return $translate.refresh();
}],
entity: ['$stateParams', 'ExternalMessage', function($stateParams, ExternalMessage) {
Expand Down Expand Up @@ -134,6 +136,7 @@
status: null,
retries: null,
lastErrorMessage: null,
application: null,
id: null
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h2 translate="sprintApp.externalMessage.home.title">External Messages</h2>
<th jh-sort-by="status"><span translate="sprintApp.externalMessage.status">Status</span> <span class="glyphicon glyphicon-sort"></span></th>
<th jh-sort-by="retries"><span translate="sprintApp.externalMessage.retries">Retries</span> <span class="glyphicon glyphicon-sort"></span></th>
<th jh-sort-by="lastErrorMessage"><span translate="sprintApp.externalMessage.lastErrorMessage">Last Error Message</span> <span class="glyphicon glyphicon-sort"></span></th>
<th jh-sort-by="application"><span translate="sprintApp.externalMessage.application">Application</span> <span class="glyphicon glyphicon-sort"></span></th>
<th></th>
</tr>
</thead>
Expand All @@ -37,6 +38,7 @@ <h2 translate="sprintApp.externalMessage.home.title">External Messages</h2>
<td translate="{{'sprintApp.ExternalMessageStatus.' + externalMessage.status}}">{{externalMessage.status}}</td>
<td>{{externalMessage.retries}}</td>
<td>{{externalMessage.lastErrorMessage}}</td>
<td translate="{{'sprintApp.ExternalApplication.' + externalMessage.application}}">{{externalMessage.application}}</td>
<td class="text-right">
<div class="btn-group flex-btn-group-container">
<button type="submit"
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/i18n/en/externalMessage.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"payload": "Payload",
"status": "Status",
"retries": "Retries",
"lastErrorMessage": "Last Error Message"
"lastErrorMessage": "Last Error Message",
"application": "Application"
}
}
}
Loading

0 comments on commit 158db05

Please sign in to comment.