Search in sources :

Example 1 with Alphabet

use of org.jsmpp.bean.Alphabet in project camel by apache.

the class SmppBinding method createSmppMessage.

/**
     * Create a new SmppMessage from the inbound deliver sm or deliver receipt
     */
public SmppMessage createSmppMessage(DeliverSm deliverSm) throws Exception {
    SmppMessage smppMessage = new SmppMessage(deliverSm, configuration);
    String messagePayload = null;
    if (deliverSm.getShortMessage() == null && deliverSm.getOptionalParameters() != null) {
        List<OptionalParameter> oplist = Arrays.asList(deliverSm.getOptionalParameters());
        for (OptionalParameter optPara : oplist) {
            if (OptionalParameter.Tag.MESSAGE_PAYLOAD.code() == optPara.tag && OctetString.class.isInstance(optPara)) {
                messagePayload = ((OctetString) optPara).getValueAsString();
                break;
            }
        }
    }
    if (deliverSm.isSmscDeliveryReceipt()) {
        smppMessage.setHeader(SmppConstants.MESSAGE_TYPE, SmppMessageType.DeliveryReceipt.toString());
        DeliveryReceipt smscDeliveryReceipt = null;
        if (deliverSm.getShortMessage() != null) {
            smscDeliveryReceipt = deliverSm.getShortMessageAsDeliveryReceipt();
        } else if (messagePayload != null) {
            smscDeliveryReceipt = DefaultDecomposer.getInstance().deliveryReceipt(messagePayload);
        }
        if (smscDeliveryReceipt != null) {
            smppMessage.setBody(smscDeliveryReceipt.getText());
            smppMessage.setHeader(SmppConstants.ID, smscDeliveryReceipt.getId());
            smppMessage.setHeader(SmppConstants.DELIVERED, smscDeliveryReceipt.getDelivered());
            smppMessage.setHeader(SmppConstants.DONE_DATE, smscDeliveryReceipt.getDoneDate());
            if (!"000".equals(smscDeliveryReceipt.getError())) {
                smppMessage.setHeader(SmppConstants.ERROR, smscDeliveryReceipt.getError());
            }
            smppMessage.setHeader(SmppConstants.SUBMIT_DATE, smscDeliveryReceipt.getSubmitDate());
            smppMessage.setHeader(SmppConstants.SUBMITTED, smscDeliveryReceipt.getSubmitted());
            smppMessage.setHeader(SmppConstants.FINAL_STATUS, smscDeliveryReceipt.getFinalStatus());
        }
        if (deliverSm.getOptionalParameters() != null && deliverSm.getOptionalParameters().length > 0) {
            // the deprecated way
            Map<String, Object> optionalParameters = createOptionalParameterByName(deliverSm);
            smppMessage.setHeader(SmppConstants.OPTIONAL_PARAMETERS, optionalParameters);
            // the new way
            Map<Short, Object> optionalParameter = createOptionalParameterByCode(deliverSm);
            smppMessage.setHeader(SmppConstants.OPTIONAL_PARAMETER, optionalParameter);
        }
    } else {
        smppMessage.setHeader(SmppConstants.MESSAGE_TYPE, SmppMessageType.DeliverSm.toString());
        if (deliverSm.getShortMessage() != null) {
            Alphabet alphabet = Alphabet.parseDataCoding(deliverSm.getDataCoding());
            if (SmppUtils.is8Bit(alphabet)) {
                smppMessage.setBody(deliverSm.getShortMessage());
            } else {
                smppMessage.setBody(String.valueOf(new String(deliverSm.getShortMessage(), configuration.getEncoding())));
            }
        } else if (messagePayload != null) {
            smppMessage.setBody(messagePayload);
        }
        smppMessage.setHeader(SmppConstants.SEQUENCE_NUMBER, deliverSm.getSequenceNumber());
        smppMessage.setHeader(SmppConstants.COMMAND_ID, deliverSm.getCommandId());
        smppMessage.setHeader(SmppConstants.SOURCE_ADDR, deliverSm.getSourceAddr());
        smppMessage.setHeader(SmppConstants.SOURCE_ADDR_NPI, deliverSm.getSourceAddrNpi());
        smppMessage.setHeader(SmppConstants.SOURCE_ADDR_TON, deliverSm.getSourceAddrTon());
        smppMessage.setHeader(SmppConstants.DATA_CODING, deliverSm.getDataCoding());
        smppMessage.setHeader(SmppConstants.DEST_ADDR, deliverSm.getDestAddress());
        smppMessage.setHeader(SmppConstants.DEST_ADDR_NPI, deliverSm.getDestAddrNpi());
        smppMessage.setHeader(SmppConstants.DEST_ADDR_TON, deliverSm.getDestAddrTon());
        smppMessage.setHeader(SmppConstants.SCHEDULE_DELIVERY_TIME, deliverSm.getScheduleDeliveryTime());
        smppMessage.setHeader(SmppConstants.VALIDITY_PERIOD, deliverSm.getValidityPeriod());
        smppMessage.setHeader(SmppConstants.SERVICE_TYPE, deliverSm.getServiceType());
    }
    return smppMessage;
}
Also used : OptionalParameter(org.jsmpp.bean.OptionalParameter) COctetString(org.jsmpp.bean.OptionalParameter.COctetString) OctetString(org.jsmpp.bean.OptionalParameter.OctetString) Alphabet(org.jsmpp.bean.Alphabet) DeliveryReceipt(org.jsmpp.bean.DeliveryReceipt) COctetString(org.jsmpp.bean.OptionalParameter.COctetString) OctetString(org.jsmpp.bean.OptionalParameter.OctetString)

Example 2 with Alphabet

use of org.jsmpp.bean.Alphabet in project camel by apache.

the class SmppMessage method createBody.

@Override
protected Object createBody() {
    if (command instanceof MessageRequest) {
        MessageRequest msgRequest = (MessageRequest) command;
        byte[] shortMessage = msgRequest.getShortMessage();
        if (shortMessage == null || shortMessage.length == 0) {
            return null;
        }
        Alphabet alphabet = Alphabet.parseDataCoding(msgRequest.getDataCoding());
        if (SmppUtils.is8Bit(alphabet)) {
            return shortMessage;
        }
        String encoding = IOHelper.getCharsetName(getExchange(), false);
        if (ObjectHelper.isEmpty(encoding) || !Charset.isSupported(encoding)) {
            encoding = configuration.getEncoding();
        }
        try {
            return new String(shortMessage, encoding);
        } catch (UnsupportedEncodingException e) {
            LOG.info("Unsupported encoding \"{}\". Using system default encoding.", encoding);
        }
        return new String(shortMessage);
    }
    return null;
}
Also used : Alphabet(org.jsmpp.bean.Alphabet) MessageRequest(org.jsmpp.bean.MessageRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with Alphabet

use of org.jsmpp.bean.Alphabet in project camel by apache.

the class SmppSmCommand method createSplitter.

protected SmppSplitter createSplitter(Message message) throws SmppException {
    SmppSplitter splitter;
    // use the splitter if provided via header
    if (message.getHeaders().containsKey(SmppConstants.DATA_SPLITTER)) {
        splitter = message.getHeader(SmppConstants.DATA_SPLITTER, SmppSplitter.class);
        if (null != splitter) {
            return splitter;
        }
        throw new SmppException("Invalid splitter given. Must be instance of SmppSplitter");
    }
    Alphabet alphabet = determineAlphabet(message);
    String body = message.getBody(String.class);
    if (SmppUtils.is8Bit(alphabet)) {
        splitter = new Smpp8BitSplitter(body.length());
    } else if (alphabet == Alphabet.ALPHA_UCS2) {
        splitter = new SmppUcs2Splitter(body.length());
    } else {
        splitter = new SmppDefaultSplitter(body.length());
    }
    return splitter;
}
Also used : Alphabet(org.jsmpp.bean.Alphabet)

Example 4 with Alphabet

use of org.jsmpp.bean.Alphabet in project camel by apache.

the class SmppSmCommand method determineAlphabet.

private Alphabet determineAlphabet(Message message) {
    String body = message.getBody(String.class);
    byte alphabet = getProvidedAlphabet(message);
    Charset charset = getCharsetForMessage(message);
    if (charset == null) {
        charset = defaultCharset;
    }
    Alphabet alphabetObj;
    if (alphabet == SmppConstants.UNKNOWN_ALPHABET) {
        alphabetObj = Alphabet.ALPHA_UCS2;
        if (isLatin1Compatible(charset)) {
            byte[] messageBytes = body.getBytes(charset);
            if (SmppUtils.isGsm0338Encodeable(messageBytes)) {
                alphabetObj = Alphabet.ALPHA_DEFAULT;
            }
        }
    } else {
        alphabetObj = Alphabet.valueOf(alphabet);
    }
    return alphabetObj;
}
Also used : Alphabet(org.jsmpp.bean.Alphabet) Charset(java.nio.charset.Charset)

Example 5 with Alphabet

use of org.jsmpp.bean.Alphabet in project camel by apache.

the class SmppSmCommand method getShortMessage.

protected final byte[] getShortMessage(Message message) {
    if (has8bitDataCoding(message)) {
        return message.getBody(byte[].class);
    } else {
        byte providedAlphabet = getProvidedAlphabet(message);
        Alphabet determinedAlphabet = determineAlphabet(message);
        Charset charset = determineCharset(message, providedAlphabet, determinedAlphabet.value());
        String body = message.getBody(String.class);
        return body.getBytes(charset);
    }
}
Also used : Alphabet(org.jsmpp.bean.Alphabet) Charset(java.nio.charset.Charset)

Aggregations

Alphabet (org.jsmpp.bean.Alphabet)5 Charset (java.nio.charset.Charset)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DeliveryReceipt (org.jsmpp.bean.DeliveryReceipt)1 MessageRequest (org.jsmpp.bean.MessageRequest)1 OptionalParameter (org.jsmpp.bean.OptionalParameter)1 COctetString (org.jsmpp.bean.OptionalParameter.COctetString)1 OctetString (org.jsmpp.bean.OptionalParameter.OctetString)1