Advertisement
OldCode101

APIServlet

May 16th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.34 KB | None | 0 0
  1. package servlets;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Enumeration;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8.  
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpSession;
  12.  
  13. import local.mpic.tools.XMLUtils;
  14. import local.mpic.tools.keys.PublicKey;
  15.  
  16. import org.w3c.dom.Document;
  17. import org.w3c.dom.Element;
  18.  
  19. import tools.CSVUtils;
  20.  
  21. public class APIServlet extends HttpServlet {
  22.   public static final long serialVersionUID = 1L;
  23.   public static final String FORMAT_JSON = "json";
  24.   public static final String FORMAT_JSON_CONTENTTYPE = "application/json";
  25.   public static final String FORMAT_XML = "xml";
  26.   public static final String FORMAT_XML_CONTENTTYPE = "text/xml";
  27.   public static final String FORMAT_CSV = "csv";
  28.   public static final String FORMAT_CSV_CONTENTTYPE = "text/plain";
  29.   public static final String[] FORMAT_OPTIONS = {FORMAT_XML, FORMAT_JSON, FORMAT_CSV};
  30.   public static final String FORMAT_DEFAULT = FORMAT_XML;
  31.   public static final String FORMAT_DEFAULT_CONTENTTYPE = FORMAT_XML_CONTENTTYPE;
  32.  
  33.   public static final String POST = "post";
  34.   public static final String GET = "get";
  35.  
  36.   public static final int SECURE_PORT = 8443;
  37.  
  38.   public static final String STATUS_OK = "OK";
  39.   public static final String STATUS_ERROR = "ERROR";
  40.   public static final String STATUS_FAILED = "FAILED";
  41.   public static final String[] STATUS_OPTIONS = {STATUS_OK, STATUS_ERROR, STATUS_FAILED};
  42.   public static final String STATUS_DEFAULT = STATUS_FAILED;
  43.  
  44.   public static final String PARAM_FORMAT = "format";
  45.   public static final String PARAM_PUBLICKEY = "key";
  46.  
  47.   public static final String COMMAND_DEFAULT = "helloWorld";
  48.  
  49.   public static final String ELEMENT_ROOT = "ResultSet";
  50.   public static final String ELEMENT_COMMAND = "Command";
  51.   public static final String ELEMENT_PARAMS = "Params";
  52.   public static final String ELEMENT_STATUS = "Status";
  53.   public static final String ELEMENT_STATUSMESSAGE = "StatusMessage";
  54.   public static final String ELEMENT_SECURE = "Secure";
  55.   public static final String ELEMENT_RESULT = "Result";
  56.  
  57.   private Document resultSet;
  58.   private String command;
  59.   private HashMap<String, String> params;
  60.   private String format;
  61.   private boolean secure;
  62.   private String status;
  63.   private String statusMessage;
  64.   private int size;
  65.   private ArrayList<HashMap<String,String>> result;
  66.   private PublicKey key;
  67.  
  68.   APIServlet(){
  69.     this.resultSet = XMLUtils.makeDoc(ELEMENT_ROOT);
  70.     this.setCommand(COMMAND_DEFAULT);
  71.     this.setFormat(FORMAT_DEFAULT);  
  72.     this.setResult(new ArrayList<HashMap<String,String>>());
  73.   }
  74.  
  75.  
  76.   public Document toDocument() {
  77.     Element root = XMLUtils.getRootElement(resultSet);
  78.     root.appendChild(getCommandElement());
  79.     root.appendChild(getParamsElement());
  80.     root.appendChild(getStatusElement());
  81.     root.appendChild(getStatusMessageElement());
  82.     root.appendChild(getSecureElement());
  83.     root.appendChild(getResultElement());
  84.     return resultSet;
  85.   }
  86.  
  87.   public String toString() {
  88.     Document doc = this.toDocument();
  89.     String output = XMLUtils.toString(doc);
  90.     if(this.getFormat().equals(FORMAT_JSON)) {
  91.       output = XMLUtils.toJSONString(output);
  92.     }
  93.     else if(getFormat().equals(FORMAT_CSV)) {
  94.       output = CSVUtils.parseDocument(doc);
  95.     }
  96.     return output;
  97.   }
  98.  
  99.   public String getContentType(String format) {
  100.     String contentType = FORMAT_DEFAULT_CONTENTTYPE;
  101.     if(format.equals(FORMAT_XML)) {
  102.       contentType = FORMAT_XML_CONTENTTYPE;
  103.     }
  104.     else if(format.equals(FORMAT_JSON)) {
  105.       contentType = FORMAT_JSON_CONTENTTYPE;
  106.     }
  107.     else if(format.equals(FORMAT_CSV)) {
  108.       contentType = FORMAT_CSV_CONTENTTYPE;
  109.     }
  110.     return contentType;
  111.   }
  112.  
  113.   public String getContentType() {
  114.     return this.getContentType(this.getFormat());
  115.   }
  116.  
  117.   public void killMe(HttpServletRequest req) {
  118.     HttpSession theSession = req.getSession( false );
  119.     if( theSession != null ) {
  120.       synchronized( theSession ) {
  121.         theSession.invalidate();
  122.       }
  123.     }
  124.   }
  125.  
  126.   //[GETTERS/SETTERS]
  127.   public void setCommand(String command) {
  128.     if(command.length() > 0) {
  129.       this.command = command;
  130.     }
  131.   }
  132.  
  133.   public String getCommand() {
  134.     return command;
  135.   }
  136.  
  137.   public Element getCommandElement() {
  138.     Element e = resultSet.createElement(ELEMENT_COMMAND);
  139.     e.setTextContent(this.getCommand());
  140.     return e;
  141.   }
  142.  
  143.   public void setFormat(String format) {
  144.     if(isFormat(format)) {
  145.       this.format = format;
  146.     }
  147.   }
  148.  
  149.   public boolean isFormat(String format) {
  150.     boolean isFormat = false;
  151.     for(int i=0; i<=FORMAT_OPTIONS.length; i++) {
  152.       if(FORMAT_OPTIONS[i].equals(format)) {
  153.         isFormat = true;
  154.         break;
  155.       }
  156.     }
  157.     return isFormat;
  158.   }
  159.  
  160.   public String getFormat() {
  161.     return format;
  162.   }
  163.  
  164.  
  165.   public void setSecure(boolean secure) {
  166.     this.secure = secure;
  167.   }
  168.  
  169.   public void setSecure(int port, String method) {
  170.     if(method.equals(POST) && port == SECURE_PORT) {
  171.       this.setSecure(true);
  172.     }
  173.   }
  174.  
  175.   public boolean isSecure() {
  176.     return secure;
  177.   }
  178.   public Element getSecureElement() {
  179.     Element e = resultSet.createElement(ELEMENT_SECURE);
  180.     e.setTextContent(this.isSecure() ? "1" : "0");
  181.     return e;
  182.   }
  183.  
  184.   public void setStatus(String status) {
  185.     this.status = status;
  186.   }
  187.  
  188.   public String getStatus() {
  189.     return status;
  190.   }
  191.  
  192.   public Element getStatusElement() {
  193.     Element e = resultSet.createElement(ELEMENT_STATUS);
  194.     e.setTextContent(this.getStatus());
  195.     return e;
  196.   }
  197.  
  198.   public void setParams(HashMap<String, String> params) {
  199.     this.params = params;
  200.     if(this.params.containsKey(PARAM_FORMAT)) {
  201.       this.setFormat(this.params.get(PARAM_FORMAT));
  202.     }
  203.     if(this.params.containsKey(PARAM_PUBLICKEY)) {
  204.       this.setKey(new PublicKey(this.params.get(PARAM_PUBLICKEY)));
  205.     }
  206.    
  207.   }
  208.   public void setParams(HttpServletRequest request) {
  209.     HashMap<String, String> p = new HashMap<String,String>();
  210.     Enumeration<?> en = request.getParameterNames();
  211.     while (en.hasMoreElements()) {
  212.         String paramName = (String) en.nextElement();
  213.         p.put(paramName, request.getParameter(paramName));
  214.     }
  215.     this.setParams(p);
  216.   }
  217.  
  218.   public HashMap<String, String> getParams() {
  219.     return params;
  220.   }
  221.  
  222.   public String getParamsAsString() {
  223.     return this.params.toString();
  224.   }
  225.  
  226.   public Element getParamsElement() {
  227.     Element e = resultSet.createElement(ELEMENT_PARAMS);
  228.     e.setTextContent(getParamsAsString());
  229.     return e;
  230.   }
  231.  
  232.   public void setResult(ArrayList<HashMap<String,String>> result) {
  233.     this.result = result;
  234.   }
  235.   public void setResult(HashMap<String, String> result) {
  236.     this.result.add(result);
  237.   }
  238.   public void setResult(String key, String value) {
  239.     HashMap<String, String> hm = new HashMap<String,String>();
  240.     hm.put(key, value);
  241.     this.setResult(hm);
  242.   }
  243.  
  244.   public ArrayList<HashMap<String,String>> getResult() {
  245.     return result;
  246.   }
  247.  
  248.   public Element getResultElement() {
  249.     Element e = resultSet.createElement(ELEMENT_RESULT);
  250.     ArrayList<HashMap<String,String>> result = this.getResult();
  251.     HashMap<String,String> record=null;
  252.     for(int i=0; i<result.size(); i++) {
  253.       Element r = resultSet.createElement("record");
  254.       r.setAttribute("id", "record_"+i);
  255.       record = result.get(i);
  256.       Iterator<String> ri = record.keySet().iterator();
  257.       while(ri.hasNext()) {
  258.         String key = ri.next();
  259.         String value = record.get(key);
  260.         Element fe = resultSet.createElement(key);
  261.         fe.setTextContent(value);
  262.         r.appendChild(fe);
  263.       }
  264.       e.appendChild(r);
  265.     }
  266.     return e;
  267.   }
  268.   public void setStatusMessage(String statusMessage) {
  269.     this.statusMessage = statusMessage;
  270.   }
  271.  
  272.   public String getStatusMessage() {
  273.     return statusMessage;
  274.   }
  275.  
  276.   public Element getStatusMessageElement() {
  277.     Element e = resultSet.createElement(ELEMENT_STATUSMESSAGE);
  278.     e.setTextContent(this.getStatusMessage());
  279.     return e;
  280.   }
  281.  
  282.   public void setSize(int size) {
  283.     this.size = size;
  284.   }
  285.  
  286.  
  287.   public int getSize() {
  288.     return size;
  289.   }
  290.  
  291.  
  292.   public void setKey(PublicKey key) {
  293.     this.key = key;
  294.   }
  295.  
  296.  
  297.   public PublicKey getKey() {
  298.     return key;
  299.   }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement