Skip to content

Commit

Permalink
Merge pull request #18 from Jimuell12/master
Browse files Browse the repository at this point in the history
Bulk SMS Support & Server IP Display Added
  • Loading branch information
Xcreen authored Sep 5, 2024
2 parents 4c4dfa8 + 7c080c6 commit 16bec65
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
69 changes: 35 additions & 34 deletions app/src/main/java/net/xcreen/restsms/server/SMSServlet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,78 @@ import javax.servlet.http.HttpServletResponse

@WebServlet
@MultipartConfig
class SMSServlet(private val serverLogging: ServerLogging, private val authEnabled: Boolean , private val goodToken: String) : HttpServlet() {
class SMSServlet(private val serverLogging: ServerLogging, private val authEnabled: Boolean, private val goodToken: String) : HttpServlet() {
@Throws(IOException::class)
override fun doPost(request: HttpServletRequest, response: HttpServletResponse) {
val message = request.getParameter("message")
val phoneno = request.getParameter("phoneno")
val phonenos = request.getParameter("phoneno")?.split(",")?.map { it.trim() }
val token = request.getParameter("token")

var tokenLog = ""
if(token != null) {
if (token != null) {
tokenLog = " Token: $token"
}

serverLogging.log("info", "SMS-Servlet [" + request.method + "] Request /send From: " + request.remoteAddr + tokenLog)
//Init Gson/PhoneNumberUtil
// Init Gson/PhoneNumberUtil
val gsonBuilder = GsonBuilder()
val gson = gsonBuilder.create()
val phoneUtil = PhoneNumberUtil.getInstance()
//Set Response
// Set Response
response.contentType = "application/json"
response.characterEncoding = "utf-8"

//Check if authentication is enabled
// Check if authentication is enabled
if (authEnabled) {
if (token == null) {
serverLogging.log("error", "No token")
response.writer.println(gson.toJson(SMSResponse(false, "Authentication token is missing!")))
return
}
else if (token != goodToken) {
serverLogging.log("error", "Supplied Token in wrong")
} else if (token != goodToken) {
serverLogging.log("error", "Supplied Token is wrong")
response.writer.println(gson.toJson(SMSResponse(false, "Authentication token is wrong!")))
return
}
}
//Check if post-parameters exists
if (message == null || phoneno == null) {
//Return Failing JSON
// Check if post-parameters exists
if (message == null || phonenos.isNullOrEmpty()) {
// Return Failing JSON
serverLogging.log("error", "SMS-Servlet message and/or phoneno parameter is missing")
response.writer.println(gson.toJson(SMSResponse(false, "message or phoneno parameter are missing!")))
return
}
//Check if message is valid
// Check if message is valid
if (message.isEmpty()) {
serverLogging.log("error", "SMS-Servlet Message is empty")
//Return Failing JSON
// Return Failing JSON
response.writer.println(gson.toJson(SMSResponse(false, "message is empty!")))
return
}
//Check if phoneno is valid and parse it
val phoneNumber: PhoneNumber
try {
phoneNumber = phoneUtil.parse(phoneno, null)
}
catch (ex: Exception) {
serverLogging.log("error", "SMS-Servlet Failed to parse phoneno")
ex.printStackTrace()
//Return Failing JSON
response.writer.println(gson.toJson(SMSResponse(false, "Invalid phoneno (make sure you include the + with Country-Code)!")))
return
}
//Send SMS
val smsManager = if(Build.VERSION.SDK_INT >= 31) {

// Send SMS to each phone number
val smsManager = if (Build.VERSION.SDK_INT >= 31) {
AppContext.appContext.getSystemService(SmsManager::class.java)
} else {
SmsManager.getDefault()
}
val msgArray = smsManager.divideMessage(message)
smsManager.sendMultipartTextMessage(phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL), null, msgArray, null, null)
//Show Success message

for (phoneno in phonenos) {
try {
val phoneNumber = phoneUtil.parse(phoneno, null)
val formattedNumber = phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)
val msgArray = smsManager.divideMessage(message)
smsManager.sendMultipartTextMessage(formattedNumber, null, msgArray, null, null)
serverLogging.log("info", "SMS-Servlet Successfully sent to $formattedNumber")
} catch (ex: Exception) {
serverLogging.log("error", "SMS-Servlet Failed to parse or send SMS to $phoneno")
ex.printStackTrace()
response.writer.println(gson.toJson(SMSResponse(false, "Failed to send SMS to $phoneno")))
return
}
}

// Show Success message
response.writer.println(gson.toJson(SMSResponse(true, null)))
serverLogging.log("info", "SMS-Servlet Successful send!")
serverLogging.log("info", "SMS-Servlet Successfully sent to all numbers!")
}

}
}
24 changes: 21 additions & 3 deletions app/src/main/java/net/xcreen/restsms/server/SMSWelcomeServlet.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net.xcreen.restsms.server

import java.io.IOException
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.*
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
Expand All @@ -10,13 +13,28 @@ class SMSWelcomeServlet(private val serverLogging: ServerLogging) : HttpServlet(
@Throws(IOException::class)
override fun doGet(request: HttpServletRequest, response: HttpServletResponse) {
val requestURI = request.requestURI
val localIpAddress = getLocalIpAddress()

if (requestURI != null) {
serverLogging.log("info", "Welcome-Servlet [" + request.method + "] Request " + requestURI + " From: " + request.remoteAddr)
}
else {
} else {
serverLogging.log("info", "Welcome-Servlet [" + request.method + "] Request / From: " + request.remoteAddr)
}

response.writer.println("RestSMS-Server is running!")
response.writer.println("Server IP Address: $localIpAddress")
}

}
private fun getLocalIpAddress(): String {
val networkInterfaces = NetworkInterface.getNetworkInterfaces()
for (interfaceElement in Collections.list(networkInterfaces)) {
val inetAddresses = interfaceElement.inetAddresses
for (inetAddress in Collections.list(inetAddresses)) {
if (!inetAddress.isLoopbackAddress && inetAddress.hostAddress.indexOf(':') == -1) {
return inetAddress.hostAddress
}
}
}
return "Unknown IP Address"
}
}

0 comments on commit 16bec65

Please sign in to comment.