Rework the authentication
[clamp.git] / src / main / java / org / onap / clamp / clds / service / LogServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import javax.mail.Message;
32 import javax.mail.MessagingException;
33 import javax.mail.Session;
34 import javax.mail.Transport;
35 import javax.mail.internet.InternetAddress;
36 import javax.mail.internet.MimeMessage;
37 import javax.ws.rs.core.Context;
38
39 import org.apache.commons.mail.Email;
40 import org.apache.commons.mail.SimpleEmail;
41 import org.apache.cxf.jaxrs.ext.MessageContext;
42 import org.camunda.bpm.engine.HistoryService;
43 import org.camunda.bpm.engine.RuntimeService;
44 import org.camunda.bpm.engine.history.HistoricActivityInstance;
45 import org.camunda.bpm.engine.impl.history.event.HistoricActivityInstanceEventEntity;
46 import org.camunda.bpm.engine.runtime.ProcessInstance;
47 import org.onap.clamp.clds.common.LogMessages;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.mail.MailException;
50 import org.springframework.mail.SimpleMailMessage;
51 import org.springframework.mail.javamail.JavaMailSenderImpl;
52 import org.springframework.stereotype.Service;
53
54 import com.att.ajsc.camunda.core.AttCamundaHistoryEvent;
55 import com.att.ajsc.camunda.core.AttCamundaService;
56 import com.att.eelf.configuration.EELFLogger;
57 import com.att.eelf.configuration.EELFManager;
58 import com.google.gson.Gson;
59
60 @Service
61 public class LogServiceImpl implements LogService {
62     protected static final EELFLogger       logger      = EELFManager.getInstance().getLogger(LogServiceImpl.class);
63     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
64
65     @Autowired
66     private RuntimeService          runtimeService;
67
68     @Autowired
69     private HistoryService          historyService;
70
71     @Context
72     private MessageContext          context;
73
74     public void setRuntimeService(RuntimeService runtimeService) {
75         this.runtimeService = runtimeService;
76     }
77
78     public LogServiceImpl() {
79         // needed for instantiation
80     }
81
82     @Override
83     public String logMessage(String logMessageText, String javamail, String springmail, String commonsmail) {
84         logger.info("Value of contexxt : " + context);
85         String convId = null;
86         if (context != null) {
87             convId = context.getHttpServletRequest().getHeader("X-CSI-ConversationId");
88             if (convId == null) {
89                 convId = (String) context.getHttpServletRequest().getAttribute("X-CSI-ConversationId");
90             }
91             context.getHttpServletRequest().setAttribute("CALL_TYPE", "Testing");
92             AttCamundaService.setHttpRequest(context.getHttpServletRequest());
93         }
94         // input variables to example camunda process
95         Map<String, Object> variables = new HashMap<>();
96         variables.put("logMessageText", logMessageText);
97         if (convId != null) {
98             variables.put("conversationId", convId);
99         }
100
101         // BEGIN - added for send mail testing
102         // also added the following to the method signature: ,
103         // @QueryParam("javamail") String javamail, @QueryParam("springmail")
104         // String springmail, @QueryParam("commonsmail") String commonsmail
105         // if javamail parameter provided, assume it contains an email address.
106         // use Java Mail to send an email from that address, to that address
107         if (javamail != null && javamail.length() > 0) {
108             variables.put("javamail", javamail);
109             try {
110                 Properties props = new Properties();
111                 props.put("mail.smtp.host", "smtp.sbc.com"); // eMail.setHostName
112                 Session session = Session.getInstance(props);
113                 MimeMessage msg = new MimeMessage(session);
114
115                 msg.setFrom(new InternetAddress(javamail)); // eMail.setFrom
116
117                 InternetAddress[] fromAddresses = { new InternetAddress(javamail) };
118                 msg.setReplyTo(fromAddresses); // eMail.addReplyTo
119                 msg.setSubject("test message using javax.mail"); // eMail.setSubject
120                 msg.setText(logMessageText); // eMail.setMsg
121
122                 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(javamail)); // eMail.addTo
123                 Transport.send(msg);
124             } catch (MessagingException e) {
125                 logger.error(LogMessages.LOGSERVICE_EMAIL_ERROR, e);
126             }
127         }
128
129         // if springmail parameter provided, assume it contains an email
130         // address.
131         // use Spring Mail to send an email from that address, to that address
132         if (springmail != null && springmail.length() > 0) {
133             variables.put("springmail", springmail);
134             JavaMailSenderImpl sender = new JavaMailSenderImpl();
135             SimpleMailMessage smsg = new SimpleMailMessage();
136
137             try {
138                 sender.setHost("smtp.sbc.com"); // eMail.setHostName
139                 smsg.setFrom(springmail); // eMail.setFrom
140                 smsg.setReplyTo(springmail); // eMail.addReplyTo
141                 smsg.setSubject("test message using spring mail"); // eMail.setSubject
142                 smsg.setText(logMessageText); // eMail.setMsg
143                 smsg.setTo(springmail); // eMail.addTo
144                 sender.send(smsg);
145             } catch (MailException e) {
146                 logger.error(LogMessages.LOGSERVICE_EMAIL_ERROR, e);
147             }
148         }
149
150         // if commonsmail parameter provided, assume it contains an email
151         // address.
152         // use Apache Commons Mail to send an email from that address, to that
153         // address
154         if (commonsmail != null && commonsmail.length() > 0) {
155             variables.put("commonsmail", commonsmail);
156             Email email = new SimpleEmail();
157             try {
158                 email.setHostName("smtp.sbc.com");
159                 email.setFrom(commonsmail);
160                 email.addReplyTo(commonsmail);
161                 email.setSubject("test message using commons mail");
162                 email.setMsg(logMessageText);
163                 email.addTo(commonsmail);
164                 java.net.URL classUrl = this.getClass().getResource("com.sun.mail.util.TraceInputStream");
165                 if (classUrl != null) {
166                     logger.info(LogMessages.LOGSERVICE_EMAIL_CLASS, classUrl.getFile());
167                 } else {
168                     logger.info(LogMessages.LOGSERVICE_EMAIL_CLASS, classUrl.getFile());
169                     logger.info(LogMessages.LOGSERVICE_EMAIL_CLASS_NULL);
170                 }
171                 email.send();
172             } catch (Exception e) {
173                 logger.error(LogMessages.LOGSERVICE_EMAIL_ERROR, e);
174             }
175         }
176         // END - added for send mail testing
177
178         // execute example camunda process, log-message-wf
179         ProcessInstance pi = runtimeService.startProcessInstanceByKey("log-message-wf", variables);
180         AttCamundaService.setHttpRequest(null);
181         // return text message of what was done
182         return "Started processDefinitionId=" + pi.getProcessDefinitionId() + ", processInstanceId="
183                 + pi.getProcessInstanceId() + ", to log message: " + logMessageText;
184     }
185
186     @Override
187     public String postLogMessage(String histEventList) {
188         String message = "no logs Created";
189         logger.info("value of history events:" + histEventList);
190         Gson gson = new Gson();
191         AttCamundaHistoryEvent attCamundaHistoryEvent = gson.fromJson(histEventList, AttCamundaHistoryEvent.class);
192         if (attCamundaHistoryEvent != null && attCamundaHistoryEvent.getProcInstId() != null) {
193             logger.info(LogMessages.PROCESS_INSTANCE_ID, attCamundaHistoryEvent.getProcInstId());
194             if (context != null && context.getHttpServletRequest() != null
195                     && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
196                 context.getHttpServletRequest().setAttribute("CALL_TYPE", "Testing");
197                 List<HistoricActivityInstance> histActInstList = historyService.createHistoricActivityInstanceQuery()
198                         .processInstanceId(attCamundaHistoryEvent.getProcInstId()).list();
199
200                 if (histActInstList != null && histActInstList.size() > 0) {
201                     for (HistoricActivityInstance currHistoricActivityInstance : histActInstList) {
202                         if (currHistoricActivityInstance != null
203                                 && currHistoricActivityInstance.getActivityName() != null
204                                 && currHistoricActivityInstance.getStartTime() != null
205                                 && currHistoricActivityInstance.getEndTime() != null) {
206                             logger.info("value of serviceTrack:" + currHistoricActivityInstance);
207                             message = "Log Entry Created";
208                             logger.info(message);
209                         }
210                     }
211                 }
212                 if (attCamundaHistoryEvent.getHistoryEventList() != null
213                         && attCamundaHistoryEvent.getHistoryEventList().size() > 0) {
214                     List<HistoricActivityInstanceEventEntity> historyEventList = attCamundaHistoryEvent
215                             .getHistoryEventList();
216                     for (HistoricActivityInstanceEventEntity actiEvent : historyEventList) {
217                         // resolve null pointer exception if
218                         // actiEvent.getActivityName()
219                         message = "Log Entry Created";
220                     }
221                 }
222             }
223         }
224         return message;
225     }
226
227     @Override
228     public String createLogMessage(String startTime, String endTime, String serviceName) {
229         String message = "no logs Created";
230
231         if (context != null && context.getHttpServletRequest() != null
232                 && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
233             context.getHttpServletRequest().setAttribute("X-CSI-ClientApp", "AJSC-CSI~sdsds");
234             /*
235              * PerformanceTrackingBean trackingBean =(PerformanceTrackingBean)
236              * context.getHttpServletRequest().getAttribute(
237              * "PERFORMANCE_TRACKER_BEAN");
238              * PerformanceTracking.addInvokeServiceTrack(trackingBean,
239              * serviceName, Long.valueOf(startTime), Long.valueOf(endTime),
240              * "Completed", 500, 1000) ;
241              */
242             message = "Log Entry Created";
243         }
244         // return text message of what was done
245         return message;
246     }
247
248     @Override
249     public String createLogMessageUsingHistory(String procInstId, String histEventList) {
250         String message = "no logs Created";
251         logger.info("value of history events:" + histEventList);
252         logger.info("value of events:" + histEventList + ":" + histEventList);
253         if (context != null && context.getHttpServletRequest() != null
254                 && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
255             context.getHttpServletRequest().setAttribute("CALL_TYPE", "Testing");
256             List<HistoricActivityInstance> histActInstList = historyService.createHistoricActivityInstanceQuery()
257                     .processInstanceId(procInstId).list();
258
259             if (histActInstList != null && histActInstList.size() > 0) {
260                 for (HistoricActivityInstance currHistoricActivityInstance : histActInstList) {
261                     if (currHistoricActivityInstance != null && currHistoricActivityInstance.getActivityName() != null
262                             && currHistoricActivityInstance.getStartTime() != null
263                             && currHistoricActivityInstance.getEndTime() != null) {
264                         logger.info("value of serviceTrack:" + currHistoricActivityInstance);
265                         message = "Log Entry Created";
266                         logger.info(message);
267                     }
268                 }
269             }
270         }
271         return message;
272     }
273
274     @Override
275     public String CreateHistLog(String procInstId) {
276         String message = "no logs Created";
277         if (context != null && context.getHttpServletRequest() != null
278                 && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
279             List<HistoricActivityInstance> histActInstList = historyService.createHistoricActivityInstanceQuery()
280                     .processInstanceId(procInstId).list();
281
282             if (histActInstList != null && histActInstList.size() > 0) {
283                 for (HistoricActivityInstance currHistoricActivityInstance : histActInstList) {
284                     if (currHistoricActivityInstance != null && currHistoricActivityInstance.getActivityName() != null
285                             && currHistoricActivityInstance.getStartTime() != null
286                             && currHistoricActivityInstance.getEndTime() != null) {
287                         logger.info("value of serviceTrack:" + currHistoricActivityInstance);
288                         context.getHttpServletRequest().setAttribute("X-CSI-ClientApp", "AJSC-CSI~sdsds");
289                         message = "Log Entry Created";
290                     }
291                 }
292             }
293         }
294         return message;
295     }
296 }