/* * (non-Javadoc) * * @see org.smslib.smsserver.AInterface#MessagesReceived(java.util.List) */ @Override public void MessagesReceived(Collection<InboundMessage> msgList) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); for (InboundMessage msg : msgList) { /* Check type of an message */ if ((msg.getType() == MessageTypes.INBOUND) || (msg.getType() == MessageTypes.STATUSREPORT)) { Document xmldoc = db.newDocument(); /* Add inbound message to the xml document */ addMessageToDocument(xmldoc, msg); /* Serialize xml document */ File outputFile = null; do { String fileName = fileSdf.format(new java.util.Date()) + ".xml"; outputFile = new File(this.inDirectory, fileName); if (outputFile.exists()) { Thread.sleep(100); } } while (outputFile.exists()); getService() .getLogger() .logInfo("Writing inbound files to " + outputFile.getName(), null, null); writeDocument(xmldoc, outputFile); } } }
/** * @author zouyang * @time 2017年1月18日 上午10:03:33 * @description 读取未读信息 */ public void readMessageOfUnread() { try { InboundMessage[] msgs = srv.readMessages(MessageClasses.UNREAD); for (InboundMessage msg : msgs) { // 读取未读信息 System.out.println("未读信息:" + msg.getText()); } } catch (Exception e) { e.printStackTrace(); } }
@Override public void process(AGateway gateway, MessageTypes mt, InboundMessage im) { System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId()); System.out.println("Message type" + " " + mt); System.out.println("Message" + " " + im.getText()); System.out.println("Message" + " " + im.getOriginator()); String mssgCode = im.getText().substring(0, 3); System.out.println(mssgCode); String mssgNo = im.getOriginator(); if ("REG".equals(mssgCode)) { System.out.println("reg"); System.out.println( mssgNo + " " + im.getText().substring(4, 8) + " " + im.getText().substring(9)); insertusers(mssgNo, im.getText().substring(4, 8), im.getText().substring(9)); } else if ("PST".equals(mssgCode) && searchusers(mssgNo)) { System.out.println("pst"); String postMssg = im.getText().substring(4); String postId; PostToFb post = new PostToFb(); postId = post.statusSet(Main.fbToken, namereturn(mssgNo) + " says: " + postMssg); if ("".equals(postId)) { System.out.println("post not unsuccessful"); } else { insertpostid(mssgNo, postId); System.out.println("post not successful"); } } else if ("CMT".equals(mssgCode) && searchusers(mssgNo)) { System.out.println("cmt"); String commentMssg = im.getText().substring(4); String userName; String postId = postidreturn(mssgNo); CommentToFb comment = new CommentToFb(); cmtId = comment.commentSet(Main.fbToken, namereturn(mssgNo) + " says: " + commentMssg, postId); System.out.println(postId); } try { Service.getInstance().deleteMessage(im); } catch (TimeoutException ex) { Logger.getLogger(SendAndReceiveMessage.class.getName()).log(Level.SEVERE, null, ex); } catch (GatewayException ex) { Logger.getLogger(SendAndReceiveMessage.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(SendAndReceiveMessage.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(SendAndReceiveMessage.class.getName()).log(Level.SEVERE, null, ex); } }
private void deleteMessage(InboundMessage msg) { try { Service.getInstance().deleteMessage(msg); } catch (TimeoutException | GatewayException | IOException | InterruptedException e) { LOGGER.error( "Error when trying to delete the function message: " + msg.getText().toUpperCase(), e); } }
public void readMsg() throws Exception { System.out.println("dsf"); List<InboundMessage> msgList; // // Create the notification callback method for Inbound & Status // Report // // messages. try { // Read Messages. The reading is done via the Service object and // affects all Gateway objects defined. This can also be more // directed to a specific // Gateway - look the JavaDocs for information on the Service method // calls. msgList = new ArrayList<InboundMessage>(); this.srv.readMessages(msgList, MessageClasses.ALL); for (InboundMessage msg : msgList) { // System.out.println(msg); System.out.println(); System.out.println("ddddddddddddddddddddddddddddddddd"); System.out.println(msg.getDate()); System.out.println(msg.getEncoding()); System.out.println(msg.getOriginator()); System.out.println(msg.getText()); System.out.println(msg.getMemIndex()); // if (msg.getOriginator().equalsIgnoreCase("852193193")){ // this.srv.deleteMessage(msg); //åˆ é™¤çŸä¿¡ // System.out.println("this msg has been killed"); // } } // Sleep now. Emulate real world situation and give a chance to the // notifications // methods to be called in the event of message or voice call // reception. System.out.println("Now Sleeping - Hit <enter> to terminate."); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("finish recv"); this.srv.stopService(); } }
/** * Adds the given InboundMessage to the given document. * * @param xmldoc The document in which the message is written * @param m The message to add to the docment */ private void addMessageToDocument(Document xmldoc, org.smslib.InboundMessage m) { Element message = null; Element originatorElement = null; Node originatorNode = null; Element textElement = null; Node textNode = null; Element timeElement = null; Node timeNode = null; message = xmldoc.createElement("message"); message.setAttribute("gateway_id", m.getGatewayId()); /* Type */ String msgType = null; switch (m.getType()) { case INBOUND: msgType = "I"; break; case STATUSREPORT: msgType = "S"; break; case OUTBOUND: msgType = "O"; break; case UNKNOWN: msgType = "U"; break; case WAPSI: msgType = "W"; break; } if (msgType != null) { message.setAttributeNS(null, "type", msgType); } /* Encoding */ String encoding = null; switch (m.getEncoding()) { case ENC7BIT: encoding = "7"; break; case ENC8BIT: encoding = "8"; break; case ENCUCS2: encoding = "U"; break; case ENCCUSTOM: encoding = "C"; break; } if (encoding != null) { message.setAttributeNS(null, "encoding", encoding); } /* Compose message */ originatorNode = xmldoc.createTextNode(m.getOriginator()); originatorElement = xmldoc.createElementNS(null, "originator"); originatorElement.appendChild(originatorNode); textNode = xmldoc.createTextNode(m.getText()); textElement = xmldoc.createElementNS(null, "text"); textElement.appendChild(textNode); timeNode = xmldoc.createTextNode(getDateAsISO8601(m.getDate())); timeElement = xmldoc.createElementNS(null, "receive_date"); timeElement.appendChild(timeNode); message.appendChild(originatorElement); message.appendChild(textElement); message.appendChild(timeElement); xmldoc.appendChild(message); }
@Override public void process(org.smslib.AGateway gateway, MessageTypes msgType, InboundMessage msg) { Logger.getInstance().logInfo("Received new message from: " + msg.getOriginator(), null, null); }
public void doIt() throws Exception { SerialModemGateway gateway = new SerialModemGateway("ibcs", port, boudRate, mfr, model); gateway.setInbound(true); gateway.setOutbound(true); gateway.setProtocol(Protocols.PDU); gateway.getATHandler().setStorageLocations("SM"); Service.getInstance().addGateway(gateway); Service.getInstance().startService(); SmsServiceActionProxy sm = new SmsServiceActionProxy(); while (true) { Service.getInstance().readMessages(msgList, MessageClasses.ALL); myDriver(); for (InboundMessage msg : msgList) { String sender = msg.getOriginator(); String text_msg = msg.getText(); text_msg = text_msg.trim().replaceAll(" +", " "); logger.info("\nFrom: " + sender); logger.info("Text: " + text_msg); String[] wordsList = text_msg.split(" "); String meterNo = wordsList[0]; String vendingAmount = wordsList[1]; if (meterNo.length() == 11) { meterNo = "0" + meterNo; } if (isMeterAndAmountValid(meterNo, vendingAmount)) { if (isMeterTaggedWithMobile(sender, meterNo)) { String token; try { token = sm.getToken(meterNo, vendingAmount); logger.info( "Congrats! Mobile no: " + sender + " is registerd with Meter: " + meterNo + ". Token: " + token); } catch (RemoteException e) { // TODO Auto-generated catch block logger.warn("RmExp " + e); } catch (Exception e) { // TODO Auto-generated catch block logger.warn("Exp " + e); } } else { System.out.println( "Sorry!!! Mobile no: " + sender + " is not registerd with Meter: " + meterNo); } } else { logger.warn("MeterNo and vendingAmount is not valid."); } Service.getInstance().deleteMessage(msg); } msgList.clear(); System.out.println(""); Thread.sleep(5000); } }
@Override public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg) { if (SystemPrevayler.getSystemPrevaylerModel().addReceivedMessage(msg)) { switch (msgType) { case STATUSREPORT: if (msg instanceof StatusReportMessage) { StatusReportMessage statusReportMessage = (StatusReportMessage) msg; String recipient = ContactFactory.getInstance() .createContact( Long.valueOf(statusReportMessage.getRecipient().replaceAll("\\D", "0"))) .getFormattedContact(); LOGGER.info( "Processing a " + MessageTypes.STATUSREPORT.name() + " from Recipient: " + recipient); Logger.getLogger("validContacts").info("recipient: " + recipient); } break; default: String originator = ContactFactory.getInstance() .createContact(Long.valueOf(msg.getOriginator().replaceAll("\\D", "0"))) .getFormattedContact(); LOGGER.info( "Processing a " + MessageTypes.INBOUND.name() + " Message Notification from Originator: " + originator + "; Text: " + msg.getText()); if (isFunctionMessage(msg)) { Integer requiredFunction = -1; try { requiredFunction = Integer.valueOf(msg.getText().trim()); } catch (Exception e) { LOGGER.error( "Error when parsing ReportRequiredType for value: " + msg.getText().trim()); } switch (ReportRequiredType.parse(requiredFunction)) { case SMS_RATING: new Thread(new SmsRatingReport()).start(); break; case EMAIL_RATING: new Thread(new EmailRatingReport()).start(); break; case CLEAR_RECEIVED_MESSAGES: LOGGER.error("Cleanning the received messages..."); SystemPrevayler.getSystemPrevaylerModel().clearReceivedMessages(); break; case NOT_DEFINED: new Thread(new NotSupportedReport()).start(); break; } SystemPrevayler.getSystemPrevaylerModel().getReceivedMessages().remove(msg); } break; } SystemPrevayler.takeSnapShot(); } deleteMessage(msg); }
private boolean isRecentlyMessage(InboundMessage msg) { Long dateDiff = Calendar.getInstance().getTimeInMillis() - msg.getDate().getTime(); return dateDiff < (1L * (1000L * 60L * 60L)); }
private boolean isFunctionMessage(InboundMessage msg) { Contato originator = ContactFactory.getInstance() .createContact(Long.valueOf(msg.getOriginator().replaceAll("\\D", "0"))); return isRecentlyMessage(msg) && isFromAdminContact(originator); }
public void onAcceptDeliverSm(DeliverSm deliverSm) throws ProcessRequestException { if (MessageType.SMSC_DEL_RECEIPT.containedIn(deliverSm.getEsmClass())) { try { DeliveryReceipt delReceipt = deliverSm.getShortMessageAsDeliveryReceipt(); StatusReportMessage statusReportMessage = new StatusReportMessage( delReceipt.getId(), deliverSm.getDestAddress(), deliverSm.getSourceAddr(), delReceipt.getText(), delReceipt.getSubmitDate(), delReceipt.getDoneDate()); switch (delReceipt.getFinalStatus()) { case DELIVRD: statusReportMessage.setStatus(DeliveryStatuses.DELIVERED); break; case REJECTD: case EXPIRED: case UNDELIV: statusReportMessage.setStatus(DeliveryStatuses.ABORTED); break; default: statusReportMessage.setStatus(DeliveryStatuses.UNKNOWN); } statusReportMessage.setGatewayId(getGatewayId()); Service.getInstance() .getNotifyQueueManager() .getNotifyQueue() .add( new InboundMessageNotification( getMyself(), MessageTypes.STATUSREPORT, statusReportMessage)); } catch (InvalidDeliveryReceiptException e) { Logger.getInstance().logError("Failed getting delivery receipt.", e, getGatewayId()); } } else { InboundMessage msg = new InboundMessage( new java.util.Date(), deliverSm.getSourceAddr(), new String(deliverSm.getShortMessage()), 0, null); msg.setGatewayId(JSMPPGateway.this.getGatewayId()); if (Alphabet.ALPHA_DEFAULT.value() == deliverSm.getDataCoding()) { msg.setEncoding(MessageEncodings.ENC7BIT); } else if (Alphabet.ALPHA_8_BIT.value() == deliverSm.getDataCoding()) { msg.setEncoding(MessageEncodings.ENC8BIT); } else if (Alphabet.ALPHA_UCS2.value() == deliverSm.getDataCoding()) { msg.setEncoding(MessageEncodings.ENCUCS2); } else { msg.setEncoding(MessageEncodings.ENCCUSTOM); } incInboundMessageCount(); Service.getInstance() .getNotifyQueueManager() .getNotifyQueue() .add(new InboundMessageNotification(getMyself(), MessageTypes.INBOUND, msg)); } }