Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlets;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.HashMap;
- import java.util.Iterator;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import local.mpic.tools.XMLUtils;
- import local.mpic.tools.keys.PublicKey;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import tools.CSVUtils;
- public class APIServlet extends HttpServlet {
- public static final long serialVersionUID = 1L;
- public static final String FORMAT_JSON = "json";
- public static final String FORMAT_JSON_CONTENTTYPE = "application/json";
- public static final String FORMAT_XML = "xml";
- public static final String FORMAT_XML_CONTENTTYPE = "text/xml";
- public static final String FORMAT_CSV = "csv";
- public static final String FORMAT_CSV_CONTENTTYPE = "text/plain";
- public static final String[] FORMAT_OPTIONS = {FORMAT_XML, FORMAT_JSON, FORMAT_CSV};
- public static final String FORMAT_DEFAULT = FORMAT_XML;
- public static final String FORMAT_DEFAULT_CONTENTTYPE = FORMAT_XML_CONTENTTYPE;
- public static final String POST = "post";
- public static final String GET = "get";
- public static final int SECURE_PORT = 8443;
- public static final String STATUS_OK = "OK";
- public static final String STATUS_ERROR = "ERROR";
- public static final String STATUS_FAILED = "FAILED";
- public static final String[] STATUS_OPTIONS = {STATUS_OK, STATUS_ERROR, STATUS_FAILED};
- public static final String STATUS_DEFAULT = STATUS_FAILED;
- public static final String PARAM_FORMAT = "format";
- public static final String PARAM_PUBLICKEY = "key";
- public static final String COMMAND_DEFAULT = "helloWorld";
- public static final String ELEMENT_ROOT = "ResultSet";
- public static final String ELEMENT_COMMAND = "Command";
- public static final String ELEMENT_PARAMS = "Params";
- public static final String ELEMENT_STATUS = "Status";
- public static final String ELEMENT_STATUSMESSAGE = "StatusMessage";
- public static final String ELEMENT_SECURE = "Secure";
- public static final String ELEMENT_RESULT = "Result";
- private Document resultSet;
- private String command;
- private HashMap<String, String> params;
- private String format;
- private boolean secure;
- private String status;
- private String statusMessage;
- private int size;
- private ArrayList<HashMap<String,String>> result;
- private PublicKey key;
- APIServlet(){
- this.resultSet = XMLUtils.makeDoc(ELEMENT_ROOT);
- this.setCommand(COMMAND_DEFAULT);
- this.setFormat(FORMAT_DEFAULT);
- this.setResult(new ArrayList<HashMap<String,String>>());
- }
- public Document toDocument() {
- Element root = XMLUtils.getRootElement(resultSet);
- root.appendChild(getCommandElement());
- root.appendChild(getParamsElement());
- root.appendChild(getStatusElement());
- root.appendChild(getStatusMessageElement());
- root.appendChild(getSecureElement());
- root.appendChild(getResultElement());
- return resultSet;
- }
- public String toString() {
- Document doc = this.toDocument();
- String output = XMLUtils.toString(doc);
- if(this.getFormat().equals(FORMAT_JSON)) {
- output = XMLUtils.toJSONString(output);
- }
- else if(getFormat().equals(FORMAT_CSV)) {
- output = CSVUtils.parseDocument(doc);
- }
- return output;
- }
- public String getContentType(String format) {
- String contentType = FORMAT_DEFAULT_CONTENTTYPE;
- if(format.equals(FORMAT_XML)) {
- contentType = FORMAT_XML_CONTENTTYPE;
- }
- else if(format.equals(FORMAT_JSON)) {
- contentType = FORMAT_JSON_CONTENTTYPE;
- }
- else if(format.equals(FORMAT_CSV)) {
- contentType = FORMAT_CSV_CONTENTTYPE;
- }
- return contentType;
- }
- public String getContentType() {
- return this.getContentType(this.getFormat());
- }
- public void killMe(HttpServletRequest req) {
- HttpSession theSession = req.getSession( false );
- if( theSession != null ) {
- synchronized( theSession ) {
- theSession.invalidate();
- }
- }
- }
- //[GETTERS/SETTERS]
- public void setCommand(String command) {
- if(command.length() > 0) {
- this.command = command;
- }
- }
- public String getCommand() {
- return command;
- }
- public Element getCommandElement() {
- Element e = resultSet.createElement(ELEMENT_COMMAND);
- e.setTextContent(this.getCommand());
- return e;
- }
- public void setFormat(String format) {
- if(isFormat(format)) {
- this.format = format;
- }
- }
- public boolean isFormat(String format) {
- boolean isFormat = false;
- for(int i=0; i<=FORMAT_OPTIONS.length; i++) {
- if(FORMAT_OPTIONS[i].equals(format)) {
- isFormat = true;
- break;
- }
- }
- return isFormat;
- }
- public String getFormat() {
- return format;
- }
- public void setSecure(boolean secure) {
- this.secure = secure;
- }
- public void setSecure(int port, String method) {
- if(method.equals(POST) && port == SECURE_PORT) {
- this.setSecure(true);
- }
- }
- public boolean isSecure() {
- return secure;
- }
- public Element getSecureElement() {
- Element e = resultSet.createElement(ELEMENT_SECURE);
- e.setTextContent(this.isSecure() ? "1" : "0");
- return e;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- public String getStatus() {
- return status;
- }
- public Element getStatusElement() {
- Element e = resultSet.createElement(ELEMENT_STATUS);
- e.setTextContent(this.getStatus());
- return e;
- }
- public void setParams(HashMap<String, String> params) {
- this.params = params;
- if(this.params.containsKey(PARAM_FORMAT)) {
- this.setFormat(this.params.get(PARAM_FORMAT));
- }
- if(this.params.containsKey(PARAM_PUBLICKEY)) {
- this.setKey(new PublicKey(this.params.get(PARAM_PUBLICKEY)));
- }
- }
- public void setParams(HttpServletRequest request) {
- HashMap<String, String> p = new HashMap<String,String>();
- Enumeration<?> en = request.getParameterNames();
- while (en.hasMoreElements()) {
- String paramName = (String) en.nextElement();
- p.put(paramName, request.getParameter(paramName));
- }
- this.setParams(p);
- }
- public HashMap<String, String> getParams() {
- return params;
- }
- public String getParamsAsString() {
- return this.params.toString();
- }
- public Element getParamsElement() {
- Element e = resultSet.createElement(ELEMENT_PARAMS);
- e.setTextContent(getParamsAsString());
- return e;
- }
- public void setResult(ArrayList<HashMap<String,String>> result) {
- this.result = result;
- }
- public void setResult(HashMap<String, String> result) {
- this.result.add(result);
- }
- public void setResult(String key, String value) {
- HashMap<String, String> hm = new HashMap<String,String>();
- hm.put(key, value);
- this.setResult(hm);
- }
- public ArrayList<HashMap<String,String>> getResult() {
- return result;
- }
- public Element getResultElement() {
- Element e = resultSet.createElement(ELEMENT_RESULT);
- ArrayList<HashMap<String,String>> result = this.getResult();
- HashMap<String,String> record=null;
- for(int i=0; i<result.size(); i++) {
- Element r = resultSet.createElement("record");
- r.setAttribute("id", "record_"+i);
- record = result.get(i);
- Iterator<String> ri = record.keySet().iterator();
- while(ri.hasNext()) {
- String key = ri.next();
- String value = record.get(key);
- Element fe = resultSet.createElement(key);
- fe.setTextContent(value);
- r.appendChild(fe);
- }
- e.appendChild(r);
- }
- return e;
- }
- public void setStatusMessage(String statusMessage) {
- this.statusMessage = statusMessage;
- }
- public String getStatusMessage() {
- return statusMessage;
- }
- public Element getStatusMessageElement() {
- Element e = resultSet.createElement(ELEMENT_STATUSMESSAGE);
- e.setTextContent(this.getStatusMessage());
- return e;
- }
- public void setSize(int size) {
- this.size = size;
- }
- public int getSize() {
- return size;
- }
- public void setKey(PublicKey key) {
- this.key = key;
- }
- public PublicKey getKey() {
- return key;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement