PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 2ms app.codeStats 0ms

/NameNode/src/mdfs/namenode/parser/SessionImpl.java

https://bitbucket.org/crholm/mdfs
Java | 140 lines | 76 code | 24 blank | 40 comment | 13 complexity | 4a05aa5d7d0639ac9cc1f1bc7d2a15c3 MD5 | raw file
  1. package mdfs.namenode.parser;
  2. import java.io.File;
  3. import java.io.InputStream;
  4. import mdfs.utils.Config;
  5. import mdfs.utils.Verbose;
  6. import mdfs.utils.parser.Parser;
  7. import mdfs.utils.parser.Session;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10. /**
  11. * Session is the object that contains request and respose for MDFS comunication protcol.
  12. * It contains all nessesary information to handel a request and dose so by parsing and executing
  13. * @author Rasmus Holm
  14. *
  15. */
  16. public class SessionImpl implements Session{
  17. private JSONObject request = null;
  18. private JSONObject response = null;
  19. private Parser parser;
  20. @SuppressWarnings("unused")
  21. private String status = "none";
  22. private ParserFactory parserFactory = new ParserFactory();
  23. /**
  24. * Sets the JSON response that the session holds.
  25. * @param response
  26. */
  27. public void setJsonResponse(JSONObject response){
  28. this.response = response;
  29. }
  30. /**
  31. * Enables other Objects to read and edit the request as a JSONObject
  32. * @return the request in present form as a JSONObject
  33. */
  34. public JSONObject getJsonRequest(){
  35. return request;
  36. }
  37. /**
  38. * Adds a JSON string as request for the session
  39. * @param jsonString - A string that is parsed into a JSONObject
  40. * @return false if parsing fails or if a request is already added, true if sucessfull in parsing request to JSONObject
  41. */
  42. public boolean addJsonRequest(String jsonString){
  43. if(request != null){
  44. return false;
  45. }
  46. try {
  47. request = new JSONObject(jsonString);
  48. } catch (JSONException e) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. /**
  54. * Parses a added JSON Request via a object Parser
  55. * @return false - if reqest is not set, or failing to parse request accordingly to MDFS Communication Protocol
  56. * @throws JSONException
  57. */
  58. public boolean parseRequest() throws JSONException {
  59. //Checks so that the request contains the fields To, From, Stage, Type and Mode
  60. if(request != null && request.has("To") && request.has("From") && request.has("Stage") && request.has("Mode") && request.has("Type")){
  61. //Checks so that the field To contains the current name nods address
  62. if( request.getString("To").equals(Config.getString("address"))){
  63. //Creates a parser from given information
  64. parser = parserFactory.getParser(request.getString("Stage"), request.getString("Type"), request.getString("Mode"));
  65. //Parses the request and wraps the session in it.
  66. if(!parser.parse(this)){
  67. setStatus("error");
  68. return false;
  69. }
  70. }else{
  71. setStatus("error");
  72. return false;
  73. }
  74. }else{
  75. setStatus("error");
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * Sets and edits the satus for the session
  82. * @param status
  83. */
  84. public void setStatus(String status){
  85. Verbose.print("Session Status change: " + status, this, Config.getInt("verbose")-5);
  86. this.status = status;
  87. }
  88. /**
  89. *
  90. * @return the response to request as a JSON String, null if request has not been parsed.
  91. */
  92. public String getResponse(){
  93. if(response == null){
  94. return null;
  95. }
  96. return response.toString();
  97. }
  98. @Override
  99. public InputStream getInputStreamFromRequest() {
  100. // TODO Auto-generated method stub
  101. return null;
  102. }
  103. @Override
  104. public void setInputStreamFromRequest(InputStream inputStream) {
  105. // TODO Auto-generated method stub
  106. }
  107. @Override
  108. public File getFileForResponse() {
  109. // TODO Auto-generated method stub
  110. return null;
  111. }
  112. @Override
  113. public void setFileForResponse(File file) {
  114. // TODO Auto-generated method stub
  115. }
  116. }