use of cz.metacentrum.perun.core.api.RTMessage in project perun by CESNET.
the class RTMessagesManagerBlImpl method sendMessageToRT.
public RTMessage sendMessageToRT(PerunSession sess, int voId, String queue, String subject, String text) throws InternalErrorException {
log.debug("Parameters of rtMessage are queue='" + queue + "', subject='{}' and text='{}'", subject, text);
//Get Email from User who get from session
String email = null;
User user = sess.getPerunPrincipal().getUser();
//try to get user/member email from user in session
if (user != null)
email = findUserPreferredEmail(sess, user);
else {
email = null;
log.error("Can't get user from session.");
}
//try to get email from additionalInformations in session (attribute mail)
if (email == null) {
Matcher emailMatcher;
Map<String, String> additionalInfo = sess.getPerunPrincipal().getAdditionalInformations();
//If there are some data in additionalInfo
if (additionalInfo != null) {
String mailInfo = additionalInfo.get("mail");
//If there is notnull attribute "mail" in map
if (mailInfo != null) {
//If attribute mail has separator ',' or ';'
if (mailInfo.contains(";")) {
String[] mailsFromInfo = mailInfo.split(";");
for (String mail : mailsFromInfo) {
emailMatcher = Utils.emailPattern.matcher(mail);
if (emailMatcher.matches()) {
email = mail;
break;
}
}
} else if (mailInfo.contains(",")) {
String[] mailsFromInfo = mailInfo.split(",");
for (String mail : mailsFromInfo) {
emailMatcher = Utils.emailPattern.matcher(mail);
if (emailMatcher.matches()) {
email = mail;
break;
}
}
} else {
//If there is no separator, test if this has format of email, if yes, save it to email
emailMatcher = Utils.emailPattern.matcher(mailInfo);
if (emailMatcher.matches()) {
email = mailInfo;
}
}
}
}
}
//Prepare sending message
HttpResponse response;
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// just like cookie-policy: ignore cookies
httpClientBuilder.disableCookieManagement();
HttpClient httpClient = httpClientBuilder.build();
StringBuilder responseMessage = new StringBuilder();
String ticketNumber = "0";
try {
response = httpClient.execute(this.prepareDataAndGetHttpRequest(sess, voId, queue, email, subject, text));
BufferedReader bw = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//Reading response from RT
String line;
while ((line = bw.readLine()) != null) {
responseMessage.append(line);
responseMessage.append('\n');
//Matcher for ticketNumber
Matcher ticketNumberMatcher = this.ticketNumberPattern.matcher(line);
if (ticketNumberMatcher.find()) {
ticketNumber = ticketNumberMatcher.group(1);
}
}
} catch (IOException ex) {
throw new InternalErrorException("IOException has been throw while executing http request.", ex);
}
//Return message if response is ok, or throw exception with bad response
int ticketNum = Integer.valueOf(ticketNumber);
if (ticketNum != 0) {
RTMessage rtmessage = new RTMessage(email, ticketNum);
log.debug("RT message was send successfully and the ticket has number: " + ticketNum);
return rtmessage;
} else {
throw new InternalErrorException("RT message was not send due to error with RT returned this message: " + responseMessage.toString());
}
}
Aggregations