Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package no.altibox.aip.remedy.processor;
- import static no.altibox.aip.remedy.routebuilder.BaseRouteBuilder.REMEDY;
- import static no.altibox.aip.remedy.routebuilder.BaseRouteBuilder.URL;
- import static no.altibox.aip.remedy.routebuilder.UpdateWorkInfoRouteBuilder.GET_BILLING_ACCOUNT_ID;
- import static no.altibox.aip.remedy.routebuilder.UpdateWorkInfoRouteBuilder.IS_INC_REMEDY_REQUEST;
- import static no.altibox.aip.remedy.routebuilder.UpdateWorkInfoRouteBuilder.IS_TASK_REMEDY_REQUEST;
- import static no.altibox.aip.remedy.routebuilder.UpdateWorkInfoRouteBuilder.IS_VALID_REQUEST;
- import static no.altibox.aip.remedy.routebuilder.UpdateWorkInfoRouteBuilder.RTE_DIRECT_REMEDY_UPDATE_INCIDENT_WORK_INFO;
- import static no.altibox.aip.remedy.routebuilder.UpdateWorkInfoRouteBuilder.UPDATE_WORK_INFO;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.Base64;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.activation.DataHandler;
- import javax.mail.BodyPart;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.internet.MimeUtility;
- import org.apache.camel.CamelContext;
- import org.apache.camel.Exchange;
- import org.apache.camel.Processor;
- import org.apache.camel.Route;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang.StringUtils;
- import org.jboss.logging.Logger;
- import no.altibox.aip.common.config.Config;
- import no.altibox.aip.common.config.Config.Scope;
- import no.altibox.aip.common.exception.AipException;
- import no.altibox.aip.remedy.model.updateworkinfo.UpdateWorkInfo;
- import no.altibox.aip.remedy.util.SpecialCharacterConverter;
- /**
- * @author VN0070954
- *
- */
- public class UpdateWorkInfoProcessor implements Processor {
- private static final Logger LOGGER = Logger.getLogger(UpdateWorkInfoProcessor.class);
- private static final String INCIDENT_NUMBER = "INC";
- private static final String TASK_NUMBER = "TAS";
- private static final String GET_PARTNERID = "getPartnerId";
- private static final String INBOUND_NOTIFICATION = "inboundNotification";
- private static final String URI_POLLING_PROTOCOL = "protocol";
- private static final String URI_POLLING_PORT = "port";
- private static final String URI_POLLING_USERNAME = "username_imap";
- private static final String URI_POLLING_SERVERNAME = "servername";
- private static final String URI_POLLING_PASSWD = "password_imap";
- private static final String URI_POLLING_DELAY = "delay";
- private static final String OSS_CALL = "ossCallForPartnerId";
- private static final String FROM = "From";
- private static final String SUBJECT = "Subject";
- private static final String CC = "Cc";
- private static final String BODY = "Body";
- private static final String TROUBLE_TICKET = "TroubleTicket";
- private static final String CUSTOMER_REPLY = "customer_reply";
- private static final String POLLING_ROUTE = "PollingRoute";
- private static final String REGULAR_EXPRESSION = "regExp";
- private static final String REGULAR_EXPRESSION_R6 = "regExpR6";
- private static final String EMPTY = "";
- @Override
- public void process(Exchange exchange) throws Exception {
- boolean isTaskRemedyRequest = false;
- boolean isIncRemedyRequest = false;
- extractDetails(exchange);
- UpdateWorkInfo updateWorkInfo = exchange.getProperty(UPDATE_WORK_INFO, UpdateWorkInfo.class);
- String subject = updateWorkInfo.getSubject();
- StringBuilder detailedDescription = new StringBuilder(FROM);
- detailedDescription.append(": ").append(updateWorkInfo.getFrom()).append(", ").append(SUBJECT).append(": ").append(subject);
- if (null != updateWorkInfo.getCc()) {
- detailedDescription.append(", ").append(CC).append(": ").append(updateWorkInfo.getCc());
- }
- detailedDescription.append(",\n").append(BODY).append(":\n").append(processMessageBody(exchange.getIn().getBody(), false));
- updateWorkInfo.setDetailedDescription(MimeUtility.decodeText(detailedDescription.toString()));
- updateWorkInfo.setMessageBody(processMessageBody(exchange.getIn().getBody(), true));
- if (subject.contains(INCIDENT_NUMBER)) {
- isIncRemedyRequest = true;
- exchange.setProperty(IS_INC_REMEDY_REQUEST, isIncRemedyRequest);
- } else if (subject.contains(TASK_NUMBER)) {
- isTaskRemedyRequest = true;
- exchange.setProperty(IS_TASK_REMEDY_REQUEST, isTaskRemedyRequest);
- }
- if (subject.contains(INCIDENT_NUMBER) || subject.contains(TASK_NUMBER)) {
- updateWorkInfo.setNotificationType(TROUBLE_TICKET);
- exchange.setProperty(GET_BILLING_ACCOUNT_ID, updateWorkInfo.getBillingAccountID());
- } else if (StringUtils.isNotBlank(updateWorkInfo.getBillingAccountID())) {
- exchange.setProperty(IS_VALID_REQUEST, true);
- updateWorkInfo.setNotificationType(CUSTOMER_REPLY);
- exchange.setProperty(GET_BILLING_ACCOUNT_ID, updateWorkInfo.getBillingAccountID());
- }
- exchange.getOut().setBody(updateWorkInfo);
- }
- /**
- * checkSubject method is checking whether request is from Remedy or R6. If subject starts with INC which means request is from Remedy and boolean
- * isRemedyRequest is set to true.
- *
- * @throws UnsupportedEncodingException
- **/
- private void extractDetails(Exchange exchange) throws UnsupportedEncodingException {
- UpdateWorkInfo updateWorkInfo = new UpdateWorkInfo();
- String subject = exchange.getIn().getHeader(SUBJECT, String.class);
- String cc = exchange.getIn().getHeader(CC, String.class);
- String from = exchange.getIn().getHeader(FROM, String.class);
- if (StringUtils.isBlank(subject)) {
- LOGGER.error("Subject is NULL in Incoming Email");
- throw new AipException(400);
- } else {
- updateWorkInfo.setSubject(MimeUtility.decodeText(subject));
- extractSubjectDetails(updateWorkInfo);
- updateWorkInfo.setCc(cc);
- updateWorkInfo.setFrom(SpecialCharacterConverter.convertSpecialCharacterToUTF8String(MimeUtility.decodeText(from)));
- exchange.setProperty(UPDATE_WORK_INFO, updateWorkInfo);
- }
- }
- /**
- * setPartnerId method is used to set PartnerId in InboundNotification object which is used for UpdateNotificationHistory service EAI call.
- *
- * @param exchange
- */
- public void setPartnerId(Exchange exchange) {
- UpdateWorkInfo updateWorkInfo = exchange.getProperty(UPDATE_WORK_INFO, UpdateWorkInfo.class);
- updateWorkInfo.setPartnerId(exchange.getProperty(GET_PARTNERID, String.class));
- exchange.setProperty(INBOUND_NOTIFICATION, updateWorkInfo);
- }
- /**
- * getPartnerId method is used to get PartnerId from OSS, which is used for UpdateNotificationHistory service EAI call.
- *
- * @param exchange
- */
- public void getPartnerId(Exchange exchange) {
- String url = Config.scope(REMEDY, OSS_CALL).get(URL);
- String queryParam = (String) exchange.getIn().getBody();
- StringBuilder strBuilder = new StringBuilder().append(url).append(queryParam);
- exchange.getOut().setBody(strBuilder);
- }
- /**
- * removeRoute method is used for remove route processing.
- *
- * @param exchange
- */
- public void removeRoute(Exchange exchange) {
- CamelContext context = exchange.getContext();
- // Check if the route exist
- // if yes, remove the current route
- List<Route> routesList = context.getRoutes();
- // Add iterator to get the route with id "pollingRoute" and remove it from context
- Iterator<Route> iterator = routesList.iterator();
- while (iterator.hasNext()) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("routes iterating");
- }
- Route route = iterator.next();
- if (POLLING_ROUTE.equalsIgnoreCase(route.getId())) {
- routesList.remove(route);
- break;
- }
- }
- }
- /**
- * loadRoute method is used for load route processing.
- *
- * @param exchange
- */
- public void loadRoute(Exchange exchange) {
- Scope workInfo = Config.scope(REMEDY, UPDATE_WORK_INFO);
- CamelContext context = exchange.getContext();
- // Create new route with id as "pollingRoute"
- String pollingUrl = (new StringBuilder()).append(workInfo.get(URI_POLLING_PROTOCOL)).append(workInfo.get(URI_POLLING_SERVERNAME)).append(":")
- .append(workInfo.get(URI_POLLING_PORT)).append("?password=").append(workInfo.get(URI_POLLING_PASSWD)).append("&username=")
- .append(workInfo.get(URI_POLLING_USERNAME)).append("&consumer.delay=").append(workInfo.get(URI_POLLING_DELAY)).toString();
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(String.format("pollingUrl:: %s ", pollingUrl));
- }
- RouteBuilder pollingRouteBuilder = new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- LOGGER.debug(String.format("pollingUrl in Route:: %s", pollingUrl));
- from(pollingUrl).id(POLLING_ROUTE).to(RTE_DIRECT_REMEDY_UPDATE_INCIDENT_WORK_INFO);
- }
- };
- try {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Adding pollingRouteBuilder");
- }
- context.addRoutes(pollingRouteBuilder);
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(String.format("Adding below route definition to bean: %s", pollingUrl));
- LOGGER.debug("Added pollingRouteBuilder");
- }
- } catch (Exception exception) {
- LOGGER.error("Error while creation of route", exception);
- }
- }
- private void extractSubjectDetails(UpdateWorkInfo updateWorkInfo) {
- Pattern pattern = Pattern.compile(Config.scope(REMEDY, UPDATE_WORK_INFO).get(REGULAR_EXPRESSION));
- Matcher matcher = pattern.matcher(updateWorkInfo.getSubject());
- if (matcher.find()) {
- if (matcher.group(2).contains(INCIDENT_NUMBER)) {
- updateWorkInfo.setIncidentNumber(matcher.group(2));
- } else if (matcher.group(2).contains(TASK_NUMBER)) {
- updateWorkInfo.setTaskId(matcher.group(2));
- }
- }
- pattern = Pattern.compile(Config.scope(REMEDY, UPDATE_WORK_INFO).get(REGULAR_EXPRESSION_R6));
- matcher = pattern.matcher(updateWorkInfo.getSubject());
- if (matcher.find()) {
- updateWorkInfo.setBillingAccountID(matcher.group(2));
- }
- }
- private String processMessageBody(Object content, boolean msgProcessedFlag) {
- try {
- String msg = "";
- if (content instanceof String) {
- msg = (String) content;
- } else if (content instanceof Multipart) {
- Multipart multiPart = (Multipart) content;
- msg = procesMultiPart(multiPart, msgProcessedFlag);
- } else if (content instanceof InputStream) {
- InputStream inStream = (InputStream) content;
- int ch;
- StringBuilder msgStrBuilder = new StringBuilder();
- while ((ch = inStream.read()) != -1) {
- msgStrBuilder.append(ch);
- }
- msg = msgStrBuilder.toString();
- }
- return msg;
- } catch (IOException exception) {
- LOGGER.error("Error while reading message", exception);
- throw new AipException(500);
- }
- }
- private String procesMultiPart(Multipart content, boolean msgProcessedFlag) {
- try {
- int multiPartCount = content.getCount();
- StringBuilder msgBuilder = new StringBuilder();
- for (int iCount = 0; iCount < multiPartCount; iCount++) {
- BodyPart part = content.getBodyPart(iCount);
- Object o = part.getContent();
- if (!msgProcessedFlag && part.isMimeType("text/plain")) {
- msgBuilder.append((String) part.getContent());
- } else if (msgProcessedFlag && part.isMimeType("text/html")) {
- msgBuilder.append((String) part.getContent());
- } else if (o instanceof Multipart) {
- msgBuilder.append(procesMultiPart((Multipart) o, msgProcessedFlag));
- }
- }
- return msgBuilder.toString();
- } catch (IOException exception) {
- LOGGER.error("Error while reading message", exception);
- throw new AipException(500);
- } catch (MessagingException exception) {
- LOGGER.error("Error while reading message", exception);
- throw new AipException(500);
- }
- }
- /**
- * Read the attachments received in the mail. Only 3 attachments will be sent to remedy
- *
- * @param exchange
- */
- public void readAttachments(Exchange exchange) {
- LOGGER.debug("Reading email attachments " + exchange.getIn().getAttachmentNames().size());
- // read attachments with their data handlers
- Map<String, DataHandler> mapAttachments = exchange.getIn().getAttachments();
- // set attachments in header
- if (null != mapAttachments) {
- exchange.setProperty("EMAIL_ATTACHMENT", mapAttachments);
- }
- LOGGER.debug("Reading email attachments end");
- }
- /**
- * Set attachment data in header to pass it remedy
- *
- * @param exchange
- * @throws IOException
- */
- public void setAttachmentData(Exchange exchange) throws IOException {
- // read attachments with their data handlers
- Map<String, DataHandler> mapAttachments = (Map<String, DataHandler>) exchange.getProperty("EMAIL_ATTACHMENT");
- // get the exchange property
- UpdateWorkInfo updateWorkInfo = exchange.getProperty(UPDATE_WORK_INFO, UpdateWorkInfo.class);
- // counter to read 3 attachments
- int count = 0;
- for (Entry<String, DataHandler> etyAttachments : mapAttachments.entrySet()) {
- // increment counter
- count++;
- // counter is greater than 3 break the loop
- if (count > 3) {
- break;
- }
- if(count == 1){
- // setting file name
- updateWorkInfo.setFileName1(etyAttachments.getKey());
- // setting file content
- updateWorkInfo.setFileContent1(convertInputStreamToBase64String(etyAttachments.getValue().getInputStream()));
- // setting file size default
- updateWorkInfo.setFileSize1("0");
- } else if (count == 2){
- // setting file name
- updateWorkInfo.setFileName2(etyAttachments.getKey());
- // setting file content
- updateWorkInfo.setFileContent2(convertInputStreamToBase64String(etyAttachments.getValue().getInputStream()));
- // setting file size default
- updateWorkInfo.setFileSize2("0");
- } else if (count == 3){
- // setting file name
- updateWorkInfo.setFileName3(etyAttachments.getKey());
- // setting file content
- updateWorkInfo.setFileContent3(convertInputStreamToBase64String(etyAttachments.getValue().getInputStream()));
- // setting file size default
- updateWorkInfo.setFileSize3("0");
- }
- LOGGER.debug("Attachment : " + count + " : " + etyAttachments.getKey());
- LOGGER.debug("AttachmentCount : " + count);
- }
- // setting the count
- exchange.getIn().setHeader("attachmentCount", count>3?3:count);
- // setting update work info object
- exchange.setProperty(UPDATE_WORK_INFO, updateWorkInfo);
- // removing attachments
- exchange.removeProperty("EMAIL_ATTACHMENT");
- }
- /** remove the header/exchange properties
- *
- */
- public void removeEmailAttachmentExchangeProperties(Exchange exchange){
- // remove attachment count
- exchange.getIn().removeHeader("attachmentCount");
- }
- /**
- * Convert file content to Base64String
- *
- * @param inputStream
- * @return base64String
- * @throws IOException
- */
- private String convertInputStreamToBase64String(InputStream inputStream) {
- try {
- String base64String = Base64.getEncoder().encodeToString(IOUtils.toByteArray(inputStream));
- LOGGER.debug("AttachmentData Encoded String " + base64String);
- return base64String;
- } catch (IOException ioException) {
- if(LOGGER.isDebugEnabled())
- LOGGER.debug("Exception is being thrown while converting into base64String");
- }
- return EMPTY;
- }
- }
Add Comment
Please, Sign In to add comment