use of org.mobicents.smsc.slee.services.http.server.tx.exceptions.HttpApiException in project smscgateway by RestComm.
the class HttpRequestUtils method extractParametersFromPost.
/**
* Extract parameters from HTTP POST.
*
* @param logger the logger
* @param aRequestContentLength the request content length
* @param aRequestBodyInputStream the request body input stream
* @return the map
* @throws HttpApiException the http API exception
*/
public static Map<String, String[]> extractParametersFromPost(final Tracer logger, final int aRequestContentLength, final InputStream aRequestBodyInputStream) throws HttpApiException {
final byte[] bodyBuff = new byte[aRequestContentLength];
try {
int bodyOffset = 0;
while (bodyOffset < aRequestContentLength) {
if (logger.isFinestEnabled()) {
logger.finest("### Reading body from POST Request. Offset: " + bodyOffset + ". Content Length: " + aRequestContentLength + ".");
}
final int bodyBytesRead = aRequestBodyInputStream.read(bodyBuff, bodyOffset, aRequestContentLength - bodyOffset);
if (logger.isFinestEnabled()) {
logger.finest("### Reading body from POST Request. Read bytes count: " + bodyBytesRead + ".");
}
if (bodyBytesRead < 0) {
break;
}
bodyOffset += bodyBytesRead;
}
} catch (final IOException e) {
throw new HttpApiException("IOException while reading the body of the HttpServletRequest. Message: " + e.getMessage() + ".", e);
}
final String body = new String(bodyBuff);
if (logger.isFinestEnabled()) {
logger.finest("### POST Request body: '" + body + "'.");
}
final List<String> paramsList = new ArrayList<>(Arrays.asList(P_MSG, P_SENDER, P_TO, P_USERID, P_PASSWORD, P_MSGID, P_FORMAT, P_SMSC_ENCODING, P_MESSAGE_BODY_ENCODING));
final String[] splitted = body.split("&");
final List<String> splittedList = new ArrayList<>();
boolean isParam = false;
for (int i = 0; i < splitted.length; i++) {
String item = splitted[i];
for (final String param : paramsList) {
isParam = false;
if (item.startsWith(param)) {
splittedList.add(item);
isParam = true;
break;
}
}
if (!isParam && i > 0) {
item = splittedList.get(splittedList.size() - 1) + "&" + item;
splittedList.set(splittedList.size() - 1, item);
}
}
final Map<String, String[]> map = new HashMap<>(0);
for (final String item : splittedList) {
final String[] pair = item.split("=");
if (pair.length == 1) {
if (logger.isFineEnabled()) {
logger.fine("Empty value for key " + pair[0]);
}
} else {
if (pair.length > 2) {
for (int i = 2; i < pair.length; i++) {
pair[1] += "=" + pair[i];
}
}
final String first = pair[0];
final String second = pair[1];
if (P_TO.equals(first) && second.contains(",")) {
map.put(first, second.split(","));
} else {
map.put(first, new String[] { second });
}
}
}
return map;
}
use of org.mobicents.smsc.slee.services.http.server.tx.exceptions.HttpApiException in project smscgateway by RestComm.
the class TxHttpServerSbb method createSendMessageIncomingData.
private HttpSendMessageIncomingData createSendMessageIncomingData(HttpServletRequest request) throws HttpApiException, UnauthorizedException {
logger.fine("createSendMessageIncomingData");
if (GET.equals(request.getMethod())) {
final String userId = request.getParameter(RequestParameter.USER_ID.getName());
final String password = request.getParameter(RequestParameter.PASSWORD.getName());
final String encodedMsg = request.getParameter(RequestParameter.MESSAGE_BODY.getName());
final String format = request.getParameter(RequestParameter.FORMAT.getName());
final String msgEncoding = request.getParameter(RequestParameter.SMSC_ENCODING.getName());
final String bodyEncoding = request.getParameter(RequestParameter.MESSAGE_BODY_ENCODING.getName());
final String senderId = request.getParameter(RequestParameter.SENDER.getName());
final String destAddressParam = request.getParameter(RequestParameter.TO.getName());
final String senderTon = request.getParameter(RequestParameter.SENDER_TON.getName());
final String senderNpi = request.getParameter(RequestParameter.SENDER_NPI.getName());
final String udhStr = request.getParameter(RequestParameter.UDH.getName());
final String[] destAddresses = destAddressParam != null ? destAddressParam.split(",") : new String[] {};
return new HttpSendMessageIncomingData(userId, password, encodedMsg, format, msgEncoding, bodyEncoding, senderId, senderTon, senderNpi, destAddresses, smscPropertiesManagement, httpUsersManagement, udhStr);
} else if (POST.equals(request.getMethod())) {
String userId = request.getParameter(RequestParameter.USER_ID.getName());
String password = request.getParameter(RequestParameter.PASSWORD.getName());
String encodedMsg = request.getParameter(RequestParameter.MESSAGE_BODY.getName());
String format = request.getParameter(RequestParameter.FORMAT.getName());
String msgEncoding = request.getParameter(RequestParameter.SMSC_ENCODING.getName());
String bodyEncoding = request.getParameter(RequestParameter.MESSAGE_BODY_ENCODING.getName());
String senderId = request.getParameter(RequestParameter.SENDER.getName());
String senderTon = request.getParameter(RequestParameter.SENDER_TON.getName());
String senderNpi = request.getParameter(RequestParameter.SENDER_NPI.getName());
String destAddressParam = request.getParameter(RequestParameter.TO.getName());
String udhStr = request.getParameter(RequestParameter.UDH.getName());
String[] destAddresses = destAddressParam != null ? destAddressParam.split(",") : new String[] {};
Map<String, String[]> map = HttpRequestUtils.extractParametersFromPost(logger, request.getContentLength(), HttpRequestUtils.getInputStream(request));
if (userId == null || userId.isEmpty()) {
userId = getValueFromMap(map, RequestParameter.USER_ID.getName());
}
if (password == null || password.isEmpty()) {
password = getValueFromMap(map, RequestParameter.PASSWORD.getName());
}
if (encodedMsg == null || encodedMsg.isEmpty()) {
encodedMsg = getValueFromMap(map, RequestParameter.MESSAGE_BODY.getName());
}
if (format == null || format.isEmpty()) {
format = getValueFromMap(map, RequestParameter.FORMAT.getName());
}
if (msgEncoding == null || msgEncoding.isEmpty()) {
msgEncoding = getValueFromMap(map, RequestParameter.SMSC_ENCODING.getName());
}
if (bodyEncoding == null || bodyEncoding.isEmpty()) {
bodyEncoding = getValueFromMap(map, RequestParameter.MESSAGE_BODY_ENCODING.getName());
}
if (senderId == null || senderId.isEmpty()) {
senderId = getValueFromMap(map, RequestParameter.SENDER.getName());
}
if (senderTon == null || senderTon.isEmpty()) {
senderTon = getValueFromMap(map, RequestParameter.SENDER_TON.getName());
}
if (senderNpi == null || senderNpi.isEmpty()) {
senderNpi = getValueFromMap(map, RequestParameter.SENDER_NPI.getName());
}
if (udhStr == null || udhStr.isEmpty()) {
udhStr = getValueFromMap(map, RequestParameter.UDH.getName());
}
if (destAddresses == null || destAddresses.length < 1) {
String[] tmp = map.get(RequestParameter.TO.getName());
destAddresses = (tmp == null ? new String[] { "" } : tmp);
}
HttpSendMessageIncomingData incomingData = new HttpSendMessageIncomingData(userId, password, encodedMsg, format, msgEncoding, bodyEncoding, senderId, senderTon, senderNpi, destAddresses, smscPropertiesManagement, httpUsersManagement, udhStr);
return incomingData;
} else {
throw new HttpApiException("Unsupported method of the Http Request. Method is: " + request.getMethod());
}
}
use of org.mobicents.smsc.slee.services.http.server.tx.exceptions.HttpApiException in project smscgateway by RestComm.
the class TxHttpServerSbb method onHttpGet.
public void onHttpGet(HttpServletRequestEvent event, ActivityContextInterface aci) {
logger.fine("onHttpGet");
HttpServletRequest request = event.getRequest();
String remoteAddrAndPort = null;
if (request != null)
remoteAddrAndPort = request.getRemoteAddr() + ":" + request.getRemotePort();
long timestampB = 0L;
// decision if getStatus or sendMessage
try {
if (checkCharging()) {
final String message = "The operation is forbidden";
HttpUtils.sendErrorResponse(logger, event.getResponse(), HttpServletResponse.SC_FORBIDDEN, message);
timestampB = System.currentTimeMillis();
if (smscPropertiesManagement.isGenerateRejectionCdr()) {
generateCDR(request, CdrGenerator.CDR_SUBMIT_FAILED_HTTP, message, true);
}
CdrDetailedGenerator.generateDetailedCdr(false, null, null, null, timestampB, null, null, null, EventType.IN_HTTP_REJECT_FORBIDDEN, ErrorCode.REJECT_INCOMING, CdrDetailedGenerator.CDR_MSG_TYPE_HTTP, HttpServletResponse.SC_FORBIDDEN, -1, remoteAddrAndPort, null, -1, smscPropertiesManagement.getGenerateReceiptCdr(), smscPropertiesManagement.getGenerateDetailedCdr());
} else {
String requestURL = request.getRequestURL().toString();
String[] tmp = requestURL.split("\\?");
if (tmp[0].endsWith(SEND_SMS)) {
processHttpSendMessageEvent(event, aci);
} else if (tmp[0].endsWith(MSG_QUERY)) {
processHttpGetMessageIdStatusEvent(event, aci);
} else {
throw new HttpApiException("Unknown operation on the HTTP API");
}
}
} catch (HttpApiException e) {
try {
if (logger.isWarningEnabled()) {
logger.warning(e.getMessage());
}
sendErrorResponse(event, e);
} catch (Exception ex) {
logger.severe("Error while sending error response", ex);
}
} catch (UnauthorizedException e) {
try {
if (logger.isWarningEnabled()) {
logger.warning(e.getMessage() + " UserName:" + e.getUserName() + " Password:" + e.getPassword());
}
HttpUtils.sendErrorResponse(logger, event.getResponse(), HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
timestampB = System.currentTimeMillis();
if (smscPropertiesManagement.isGenerateRejectionCdr()) {
generateCDR(request, CdrGenerator.CDR_SUBMIT_FAILED_HTTP, e.getMessage(), true);
}
CdrDetailedGenerator.generateDetailedCdr(false, null, null, null, timestampB, null, null, null, EventType.IN_HTTP_REJECT_FORBIDDEN, ErrorCode.REJECT_INCOMING, CdrDetailedGenerator.CDR_MSG_TYPE_HTTP, HttpServletResponse.SC_UNAUTHORIZED, -1, remoteAddrAndPort, null, -1, smscPropertiesManagement.getGenerateReceiptCdr(), smscPropertiesManagement.getGenerateDetailedCdr());
} catch (Exception ex) {
logger.severe("Error while sending error response", ex);
}
} catch (Exception e) {
try {
if (logger.isWarningEnabled()) {
logger.warning(e.getMessage());
}
sendErrorResponse(event, e);
} catch (Exception ex) {
logger.severe("Error while sending error response", ex);
}
}
}
use of org.mobicents.smsc.slee.services.http.server.tx.exceptions.HttpApiException in project smscgateway by RestComm.
the class TxHttpServerSbb method onHttpPost.
public void onHttpPost(HttpServletRequestEvent event, ActivityContextInterface aci) {
logger.fine("onHttpPost");
HttpServletRequest request = event.getRequest();
String remoteAddrAndPort = null;
if (request != null)
remoteAddrAndPort = request.getRemoteAddr() + ":" + request.getRemotePort();
long timestampB = 0L;
// decision if getStatus or sendMessage
try {
if (checkCharging()) {
final String message = "The operation is forbidden";
HttpUtils.sendErrorResponse(logger, event.getResponse(), HttpServletResponse.SC_FORBIDDEN, message);
timestampB = System.currentTimeMillis();
if (smscPropertiesManagement.isGenerateRejectionCdr()) {
generateCDR(request, CdrGenerator.CDR_SUBMIT_FAILED_HTTP, message, true);
}
CdrDetailedGenerator.generateDetailedCdr(false, null, null, null, timestampB, null, null, null, EventType.IN_HTTP_REJECT_FORBIDDEN, ErrorCode.REJECT_INCOMING, CdrDetailedGenerator.CDR_MSG_TYPE_HTTP, HttpServletResponse.SC_FORBIDDEN, -1, remoteAddrAndPort, null, -1, smscPropertiesManagement.getGenerateReceiptCdr(), smscPropertiesManagement.getGenerateDetailedCdr());
} else {
String requestURL = request.getRequestURL().toString();
requestURL.endsWith(SEND_SMS);
if (requestURL.endsWith(SEND_SMS)) {
processHttpSendMessageEvent(event, aci);
} else if (requestURL.endsWith(MSG_QUERY)) {
processHttpGetMessageIdStatusEvent(event, aci);
} else {
throw new HttpApiException("Unknown operation on the HTTP API. Parameter set from the request does not match any of the HTTP API services.");
}
}
} catch (HttpApiException e) {
try {
if (logger.isWarningEnabled()) {
logger.warning(e.getMessage());
}
sendErrorResponse(event, e);
} catch (Exception ex) {
logger.severe("Error while sending error response", ex);
}
} catch (UnauthorizedException e) {
try {
if (logger.isWarningEnabled()) {
logger.warning(e.getMessage() + " UserName:" + e.getUserName() + " Password:" + e.getPassword());
}
HttpUtils.sendErrorResponse(logger, event.getResponse(), HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
timestampB = System.currentTimeMillis();
if (smscPropertiesManagement.isGenerateRejectionCdr()) {
generateCDR(request, CdrGenerator.CDR_SUBMIT_FAILED_HTTP, e.getMessage(), true);
}
CdrDetailedGenerator.generateDetailedCdr(false, null, null, null, timestampB, null, null, null, EventType.IN_HTTP_REJECT_FORBIDDEN, ErrorCode.REJECT_INCOMING, CdrDetailedGenerator.CDR_MSG_TYPE_HTTP, HttpServletResponse.SC_UNAUTHORIZED, -1, remoteAddrAndPort, null, -1, smscPropertiesManagement.getGenerateReceiptCdr(), smscPropertiesManagement.getGenerateDetailedCdr());
} catch (Exception ex) {
logger.severe("Error while sending error response", ex);
}
} catch (Exception e) {
logger.warning("Unable to handle HttpPost. Message: " + e.getMessage() + ".", e);
try {
sendErrorResponse(event, e);
} catch (Exception ex) {
logger.severe("Error while sending error response. Message: " + ex.getMessage() + ".", ex);
}
}
}
use of org.mobicents.smsc.slee.services.http.server.tx.exceptions.HttpApiException in project smscgateway by RestComm.
the class TxHttpServerSbb method getMessageIdStatus.
private void getMessageIdStatus(HttpServletRequestEvent event, HttpGetMessageIdStatusIncomingData incomingData, ActivityContextInterface aci) throws HttpApiException {
if (logger.isFineEnabled()) {
logger.fine("Received getMessageIdStatus = " + incomingData);
}
final Long messageId = incomingData.getMsgId();
QuerySmResponse querySmResponse = null;
MessageState messageState = null;
HttpGetMessageIdStatusOutgoingData outgoingData;
try {
final long msgId = messageId.longValue();
querySmResponse = persistence.c2_getQuerySmResponse(msgId);
if (querySmResponse == null) {
throw new HttpApiException("Cannot retrieve QuerySmResponse from database. Returned object is null.");
}
messageState = querySmResponse.getMessageState();
outgoingData = new HttpGetMessageIdStatusOutgoingData();
outgoingData.setStatus(Status.SUCCESS);
outgoingData.setStatusMessage(messageState.toString());
HttpUtils.sendOkResponseWithContent(logger, event.getResponse(), ResponseFormatter.format(outgoingData, incomingData.getFormat()), incomingData.getFormat());
} catch (PersistenceException e) {
throw new HttpApiException("PersistenceException while obtaining message status from the database for the " + "message with id: " + incomingData.getMsgId());
} catch (IOException e) {
throw new HttpApiException("IOException while trying to send response ok message with content");
}
}
Aggregations