@Override
 public boolean sendMessage(OutboundMessage msg)
     throws TimeoutException, GatewayException, IOException, InterruptedException {
   URL url = null;
   List<HttpHeader> request = new ArrayList<HttpHeader>();
   List<String> response;
   String reqLine;
   boolean ok = false;
   request.add(new HttpHeader("username", this.username, false));
   request.add(new HttpHeader("password", this.password, false));
   request.add(new HttpHeader("message", msg.getText(), false));
   request.add(new HttpHeader("msisdn", msg.getRecipient().substring(1), false));
   request.add(new HttpHeader("allow_concat_text_sms", "1", false));
   if (msg.getStatusReport()) request.add(new HttpHeader("want_report", "1", false));
   if (msg.getFlashSms()) request.add(new HttpHeader("msg_class", "0", false));
   if (msg.getFrom() != null && msg.getFrom().length() != 0)
     request.add(new HttpHeader("source_id", msg.getFrom(), false));
   else if (getFrom() != null && getFrom().length() != 0)
     request.add(new HttpHeader("source_id", getFrom(), false));
   reqLine = ExpandHttpHeaders(request);
   url = new URL(this.providerUrl + "/eapi/submission/send_sms/2/2.0" + "?" + reqLine);
   synchronized (this.SYNC_Commander) {
     response = HttpGet(url);
   }
   if (response.get(0).charAt(0) == '0') {
     StringTokenizer tokens = new StringTokenizer(response.get(0), "|");
     tokens.nextToken();
     tokens.nextToken();
     msg.setRefNo(tokens.nextToken());
     msg.setDispatchDate(new Date());
     msg.setGatewayId(getGatewayId());
     msg.setMessageStatus(MessageStatuses.SENT);
     incOutboundMessageCount();
     ok = true;
   } else {
     StringTokenizer tokens = new StringTokenizer(response.get(0), "|");
     switch (Integer.parseInt(tokens.nextToken())) {
       case 22:
         msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
         break;
       case 23:
         msg.setFailureCause(FailureCauses.GATEWAY_AUTH);
         break;
       case 24:
         msg.setFailureCause(FailureCauses.BAD_FORMAT);
         break;
       case 25:
       case 26:
       case 27:
       case 28:
         msg.setFailureCause(FailureCauses.NO_CREDIT);
         break;
       case 40:
         msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
         break;
     }
     msg.setRefNo(null);
     msg.setDispatchDate(null);
     msg.setMessageStatus(MessageStatuses.FAILED);
     ok = false;
   }
   return ok;
 }
Exemple #2
0
 /**
  * Reads a given node and tries to parse it
  *
  * @param n The node to parse
  * @return An outboundmessage if the node was parsable or null
  */
 private OutboundMessage readNode(Node n) {
   if ("message".equals(n.getNodeName())) {
     String recipient = null;
     String text = null;
     String originator = null;
     Element e = (Element) n;
     NodeList cnl = n.getChildNodes();
     /* Read required fields */
     for (int i = 0; i < cnl.getLength(); i++) {
       Node en = cnl.item(i);
       if ("recipient".equals(cnl.item(i).getNodeName())) {
         recipient = en.getTextContent();
       } else if ("text".equals(cnl.item(i).getNodeName())) {
         text = en.getTextContent();
       } else if ("originator".equals(cnl.item(i).getNodeName())) {
         originator = en.getTextContent();
       }
     }
     /* Create outbound message */
     OutboundMessage outMsg = new OutboundMessage(recipient, text);
     /* Set required fields */
     outMsg.setFrom(originator);
     if (!"".equals(e.getAttribute("create_date"))) {
       outMsg.setDate(getISO8601AsDate(e.getAttribute("create_date")));
     }
     if (!"".equals(e.getAttribute("gateway_id"))) {
       outMsg.setGatewayId(e.getAttribute("gateway_id"));
     }
     /* Read optional fields - priority */
     String priority = e.getAttribute("priority");
     if ("L".equalsIgnoreCase(priority)) {
       outMsg.setPriority(-1);
     } else if ("N".equalsIgnoreCase(priority)) {
       outMsg.setPriority(0);
     } else if ("H".equalsIgnoreCase(priority)) {
       outMsg.setPriority(+1);
     }
     /* Read optional fields - encoding */
     String encoding = e.getAttribute("encoding");
     if ("7".equals(encoding)) {
       outMsg.setEncoding(MessageEncodings.ENC7BIT);
     } else if ("8".equals(encoding)) {
       outMsg.setEncoding(MessageEncodings.ENC8BIT);
     } else {
       outMsg.setEncoding(MessageEncodings.ENCUCS2);
     }
     /* Read optinal fields - status_report */
     if ("1".equals(e.getAttribute("status_report"))) {
       outMsg.setStatusReport(true);
     }
     /* Read optinal fields - flash_sms */
     if ("1".equals(e.getAttribute("flash_sms"))) {
       outMsg.setFlashSms(true);
     }
     /* Read optinal fields - src_port */
     if (!"".equals(e.getAttribute("src_port"))) {
       outMsg.setSrcPort(Integer.parseInt(e.getAttribute("src_port")));
     }
     /* Read optinal fields - dst_port */
     if (!"".equals(e.getAttribute("dst_port"))) {
       outMsg.setDstPort(Integer.parseInt(e.getAttribute("dst_port")));
     }
     return outMsg;
   }
   return null;
 }
Exemple #3
0
  @Override
  public boolean sendMessage(OutboundMessage msg)
      throws TimeoutException, GatewayException, IOException, InterruptedException {

    Alphabet encoding = Alphabet.ALPHA_DEFAULT;

    switch (msg.getEncoding()) {
      case ENC8BIT:
        encoding = Alphabet.ALPHA_8_BIT;
        break;
      case ENCUCS2:
        encoding = Alphabet.ALPHA_UCS2;
        break;
      case ENCCUSTOM:
        encoding = Alphabet.ALPHA_RESERVED;
        break;
    }
    GeneralDataCoding dataCoding;

    switch (msg.getDCSMessageClass()) {
      case MSGCLASS_FLASH:
        dataCoding = new GeneralDataCoding(false, true, MessageClass.CLASS0, encoding);
        break;
      case MSGCLASS_ME:
        dataCoding = new GeneralDataCoding(false, true, MessageClass.CLASS1, encoding);
        break;
      case MSGCLASS_SIM:
        dataCoding = new GeneralDataCoding(false, true, MessageClass.CLASS2, encoding);
        break;
      case MSGCLASS_TE:
        dataCoding = new GeneralDataCoding(false, true, MessageClass.CLASS3, encoding);
        break;
      default:
        dataCoding = new GeneralDataCoding();
        dataCoding.setAlphabet(encoding);
    }
    try {
      final RegisteredDelivery registeredDelivery = new RegisteredDelivery();
      registeredDelivery.setSMSCDeliveryReceipt(
          (msg.getStatusReport())
              ? SMSCDeliveryReceipt.SUCCESS_FAILURE
              : SMSCDeliveryReceipt.DEFAULT);

      String msgId =
          session.submitShortMessage(
              bindAttributes.getSystemType(),
              TypeOfNumber.valueOf(sourceAddress.getTypeOfNumber().value()),
              NumberingPlanIndicator.valueOf(sourceAddress.getNumberingPlanIndicator().value()),
              (msg.getFrom() != null) ? msg.getFrom() : getFrom(),
              TypeOfNumber.valueOf(destinationAddress.getTypeOfNumber().value()),
              NumberingPlanIndicator.valueOf(
                  destinationAddress.getNumberingPlanIndicator().value()),
              msg.getRecipient(),
              new ESMClass(),
              (byte) 0,
              (byte) msg.getPriority(),
              null,
              formatTimeFromHours(msg.getValidityPeriod()),
              registeredDelivery,
              (byte) 0,
              dataCoding,
              (byte) 0,
              msg.getText().getBytes());
      msg.setRefNo(msgId);
      msg.setDispatchDate(new Date());
      msg.setGatewayId(getGatewayId());
      msg.setMessageStatus(MessageStatuses.SENT);
      incOutboundMessageCount();
    } catch (PDUException e) {
      msg.setGatewayId(getGatewayId());
      msg.setMessageStatus(MessageStatuses.FAILED);
      msg.setFailureCause(FailureCauses.BAD_FORMAT);
      Logger.getInstance().logError("Message Format not accepted.", e, getGatewayId());
      return false;
    } catch (ResponseTimeoutException e) {
      Logger.getInstance().logError("Message could not be sent.", e, getGatewayId());
      throw new TimeoutException(e.getMessage());
    } catch (InvalidResponseException e) {
      Logger.getInstance().logError("Message could not be sent.", e, getGatewayId());
      throw new IOException("InvalidResponseException: ", e);
    } catch (NegativeResponseException e) {
      Logger.getInstance().logError("Message could not be sent.", e, getGatewayId());
      throw new IOException("NegativeResponseException: ", e);
    }

    return true;
  }