Commit 5c1a9508 authored by Kjetil Thuen's avatar Kjetil Thuen

Removed Java7-isms

parent 213d6817
package com.nesstar;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class SMSGateway extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(SMSGateway.class);
protected HttpServletResponse response;
private SMSSend sender;
public SMSGateway() {
super();
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
sender = getSenderImplementation();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug("Got request");
this.response = response;
int httpStatus = 200;
int messageID = 1234;
int serviceStatus = 0;
String statusString = "OK";
List<String> mandatoryParameters = Arrays.asList(new String[] { "DESTADDR", "MESSAGE", "USERNAME", "PASSWORD" });
try {
for (String parameter : mandatoryParameters) {
if (null == request.getParameter(parameter)) {
httpStatus = 500;
messageID = -1;
serviceStatus = 2;
statusString = "Mandatory parameter " + parameter + " missing";
}
}
if (serviceStatus == 0) {
if (authenticateUser(request)) {
String number = request.getParameter("DESTADDR");
String message = request.getParameter("MESSAGE");
logger.info(request.getRemoteHost() + " Requested sending a SMS to " + number + " with the content " + message);
sendSMS(number, message, sender);
} else {
httpStatus = 401;
messageID = -1;
serviceStatus = 10;
statusString = "Access Denied";
}
}
} catch (RuntimeException e) {
httpStatus = 500;
messageID = -1;
serviceStatus = 1;
statusString = "Failed: " + e.getMessage();
logger.error(e, e);
}
try {
response.setStatus(httpStatus);
response.setContentType("text/plain");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
out.write(messageID + "\n");
out.write(serviceStatus + "\n");
out.write(statusString + "\n");
out.flush();
out.close();
} catch (Exception e) {
logger.fatal("Unknown error", e);
}
}
private boolean authenticateUser(HttpServletRequest request) {
String username = getInitParameter("username");
String password = getInitParameter("password");
if (null != username && null != password) {
String requestUser = request.getParameter("USERNAME");
String requestPassword = request.getParameter("PASSWORD");
if (!requestUser.equals(username) || !requestPassword.equals(password)) {
return false;
}
}
return true;
}
private SMSSend getSenderImplementation() {
SMSSend sender = null;
String senderClass = getInitParameter("sms.sender.implementation");
switch (senderClass.toLowerCase()) {
case "script":
sender = new SMSByScript();
break;
case "file":
sender = new SMSByFile();
break;
default:
sender = new SMSByHand();
break;
}
getInitParameterNames();
for (String key : Collections.list((Enumeration<String>) getInitParameterNames())) {
if (key.startsWith(senderClass.toLowerCase()) || key.startsWith("global")) {
sender.setOption(key, getInitParameter(key));
}
}
return sender;
}
private void sendSMS(String number, String message, SMSSend sender) throws RuntimeException, IOException {
sender.sendSMS(number, message);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.nesstar;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class SMSGateway extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(SMSGateway.class);
protected HttpServletResponse response;
private SMSSend sender;
public SMSGateway() {
super();
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
sender = getSenderImplementation();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug("Got request");
this.response = response;
int httpStatus = 200;
int messageID = 1234;
int serviceStatus = 0;
String statusString = "OK";
List<String> mandatoryParameters = Arrays.asList(new String[] { "DESTADDR", "MESSAGE", "USERNAME", "PASSWORD" });
try {
for (String parameter : mandatoryParameters) {
if (null == request.getParameter(parameter)) {
httpStatus = 500;
messageID = -1;
serviceStatus = 2;
statusString = "Mandatory parameter " + parameter + " missing";
}
}
if (serviceStatus == 0) {
if (authenticateUser(request)) {
String number = request.getParameter("DESTADDR");
String message = request.getParameter("MESSAGE");
logger.info(request.getRemoteHost() + " Requested sending a SMS to " + number + " with the content " + message);
sendSMS(number, message, sender);
} else {
httpStatus = 401;
messageID = -1;
serviceStatus = 10;
statusString = "Access Denied";
}
}
} catch (RuntimeException e) {
httpStatus = 500;
messageID = -1;
serviceStatus = 1;
statusString = "Failed: " + e.getMessage();
logger.error(e, e);
}
try {
response.setStatus(httpStatus);
response.setContentType("text/plain");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
out.write(messageID + "\n");
out.write(serviceStatus + "\n");
out.write(statusString + "\n");
out.flush();
out.close();
} catch (Exception e) {
logger.fatal("Unknown error", e);
}
}
private boolean authenticateUser(HttpServletRequest request) {
String username = getInitParameter("username");
String password = getInitParameter("password");
if (null != username && null != password) {
String requestUser = request.getParameter("USERNAME");
String requestPassword = request.getParameter("PASSWORD");
if (!requestUser.equals(username) || !requestPassword.equals(password)) {
return false;
}
}
return true;
}
private SMSSend getSenderImplementation() {
SMSSend sender = null;
String senderClass = getInitParameter("sms.sender.implementation");
if (senderClass.toLowerCase().equals("script")) {
sender = new SMSByScript();
} else if (senderClass.toLowerCase().equals("file")) {
sender = new SMSByFile();
} else {
sender = new SMSByHand();
}
getInitParameterNames();
for (String key : Collections.list((Enumeration<String>) getInitParameterNames())) {
if (key.startsWith(senderClass.toLowerCase()) || key.startsWith("global")) {
sender.setOption(key, getInitParameter(key));
}
}
return sender;
}
private void sendSMS(String number, String message, SMSSend sender) throws RuntimeException, IOException {
sender.sendSMS(number, message);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment