Parent: [r3] (diff)

Child: [r15] (diff)

Download this file

WebRequest.java    226 lines (190 with data), 6.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* 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: WebRequest.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright (c) 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> This class is used for interpreting requests deriving
* from the GUI or from a web browser. </text>
*/
package www;
import definitions.is;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.controller;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import script.log;
/**
*
* @author Nuno Brito, 2nd of December 2013 in Darsmtadt, Germany
*/
public class WebRequest {
// the set of parameters in key/value style
public ArrayList<String[]> parameters = new ArrayList<String[]>();
// what we reply when there is no answer?
private final String noAnswerReply = "No answer";
// helps to know if this request has already been handled or not
private boolean
wasAnswered = false;
public String
originIP; // if deriving from the web, what is the IP Address?
private String
answer; // what should be provide as answer (in HTML)
// defines the specific type of request that is being handled here
public RequestType
requestType = RequestType.UNKNOWN;
// defines from where this request is coming from
// the idea is to help the processing adjust the answer accordingly
public RequestOrigin
requestOrigin = RequestOrigin.UNKNOWN;
// Define the root folder where the related files are located?
public File BaseFolder;
// these two variables help to measure the processing time for each request
private final long
timeStart;
private long
timeEnd;
// in case we want to run a file with a script
public File
scriptFile = null,
scriptFolder = null;
// if a file with a script was added, which method should be run?
public String scriptMethod;
// used when we want to set up a page for this request
private URL page;
// used only for the SimpleFramework web server
public Request requestSimple;
public Response responseSimple;
/**
* The constructor, let's setup this object to track the time when it was
* created and other initial settings.
*/
public WebRequest(){
timeStart = System.currentTimeMillis();
}
/**
* Has this request already been processed?
* @return True if there is an answer, false if none was yet done
*/
public boolean hasAnswer(){
return wasAnswered;
}
/**
* When we have an answer ready, the next step is storing it inside
* using this method.
* @param text The HTML text that will be provided as answer
*/
public void setAnswer(String text){
// don't allow more than one answer to exist
if(wasAnswered){
System.err.println("WR001 - Answer for request was already given");
return;
}
// set the answer text
answer = text;
wasAnswered = true;
timeEnd = System.currentTimeMillis();
// place this answer on the queue to be solved
controller.display(this);
// all done in our side
}
public String getAnswer() {
if(answer == null){
setAnswer(noAnswerReply);
return answer;
}
if(answer.isEmpty()){
setAnswer(noAnswerReply);
}
return answer;
}
public long getTimeStart() {
return timeStart;
}
public long getTimeEnd() {
return timeEnd;
}
/**
* Adds a key value parameter to this request
* @param key The title
* @param value The value of the parameter
*/
public void addParameter(String key, String value){
String[] parameter = new String[]{key, value};
// initialize this array if necessary
if(parameters == null){
parameters = new ArrayList<String[]>();
}
parameters.add(parameter);
}
/**
* If there is a page available, give it back
* @return The URL object with the respective Page object
*/
public URL getPage() {
return page;
}
/**
* If there is a page that we want to setup, this is the place
* @param page
*/
public void setPage(URL page) {
this.page = page;
}
/**
* If there is a page that we want to setup, this is the place
* @param page
*/
public void setPage(File page) {
try {
setPage(page.toURI().toURL());
} catch (MalformedURLException ex) {
Logger.getLogger(WebRequest.class.getName()).log(Level.SEVERE, null, ex);
}
String fileText = utils.files.readAsString(page);
setAnswer(fileText);
}
/**
* If there is a page that we want to setup, this is the place
* @param page An HTML page on the same folder where the script is running
*/
public void setPage(String page) {
log.write(is.COMPLETED, "Displaying web page: %1", page);
File file = new File(BaseFolder, page);
setPage(file);
}
/**
* Helps to clarify if a page was loaded or not
* @return true when there is a page available
*/
public boolean hasPage(){
return page != null;
}
/**
* Retrieves a specific parameter from this request
* @param key the title of the parameter
* @return A string with the requested parameter or null as reply
*/
public String getParameter(String key){
String result = null;
// go through all parameters
for(String[] parameter : parameters){
// don't be picky, we accept both lower or upper case combinations
if(parameter[0].equalsIgnoreCase(key)){
// we have a match, get its value
result = parameter[1];
// all done, no need to continue
break;
}
}
return result;
}
}