Child: [r4] (diff)

Download this file

controller.java    155 lines (130 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (http://triplecheck.de)
* Created: 2013-12-02T00:00:00Z
* LicenseName: NOASSERTION
* FileName: controller.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright (c) 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> This class is responsible for receiving requests from
* users, dispatching the correct actions to process the request and then
* provide an answer. The reason why it is called "controller" is because it
* stays in the middle between the communication. The controller receives
* web requests and knows where they should be displayed. </text>
*/
package main;
import definitions.is;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import www.RequestOrigin;
import www.WebRequest;
import www.WebServer;
/**
*
* @author Nuno Brito, 21st of December 2013 in Darsmtadt, Germany
*/
public class controller {
/**
* Places a web request on the queue to be processed. This is an important
* step to ensure that each web request is handled in the same manner. There
* is no more direct contact with the location where the web request is
* displayed, there is this controller class that will know and decide when
* to present the message.
* @param request The object with details about the request
*/
public static void display(WebRequest request) {
// preflight checking, we need an answer to be available
if(request.hasAnswer() == false){
return;
}
// is this request intended for the web or for the Swing UI?
if(request.requestOrigin == RequestOrigin.GUI){
displayRequestGUI(request);
return;
}
// is this request intended for the web or for the Swing UI?
if(request.requestOrigin == RequestOrigin.GUI_tree){
displayRequestGUI(request);
return;
}
// Was this request made from a web browser?
if(request.requestOrigin == RequestOrigin.BROWSER){
displayRequestBrowser(request);
return;
}
System.err.println("WR002 - Unknown origin for request");
}
/**
* Handle a given request that we want to display on the Swing UI
* @param request
*/
private static void displayRequestGUI(WebRequest request) {
core.studio.editorPane(is.contentHTML, false, 0,
request.getAnswer(), Boolean.TRUE, request);
}
/**
* Display the provided request on the web browser
* @param request
*/
private static void displayRequestBrowser(WebRequest request) {
PrintStream body;
// a request has arrived, let's process this
try {
body = request.responseSimple.getPrintStream();
body.println(request.getAnswer());
// all done
body.close();
// close our connection after serving the text
//response.set("Connection", "close");
request.responseSimple.close();
} catch (IOException ex) {
Logger.getLogger(WebServer.class.getName()).log
(Level.SEVERE, null, ex);
}
}
/**
* The front-end of the controller class to receive request and forward this
* request to the appropriate script to later collect the expected answer.
* @param newRequest
* @return The HTML code that will be presented to the end-user
*/
public static String process(WebRequest newRequest) {
// if the user clicked on a tree node, we know what to do
// if(
// (newRequest.requestOrigin == RequestOrigin.GUI_tree)
// || (newRequest.requestOrigin == RequestOrigin.GUI)
//
// ){
// if there is no method selected, just call the default one
if(newRequest.scriptMethod.isEmpty()){
newRequest.scriptMethod = is.methodDefault;
}
// do a unique id for this request
long randomTime = System.currentTimeMillis();
String randomId = "request" + randomTime;
// place this request in our memory
core.temp.put(randomId, newRequest);
// prepare our tricky command
// basically, we first retrieve the object and then pass this as
// parameter for the requested method
String command = ""
+ "WebRequest webRequest = (WebRequest) core.temp.get(\""
+ randomId
+"\");\n"
+ "plugin." + newRequest.scriptMethod + "(webRequest);";
// run the script file
core.script.runJava(newRequest.scriptFile, command, is.plugin);
WebRequest processedRequest = (WebRequest) core.temp.get(randomId);
String result = processedRequest.getAnswer();
// remove it from the memory
core.temp.remove(randomId);
return result;
}
// return;
// }
// System.err.println("CN001 - Request not processed");
// }
}