-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMS.java
More file actions
232 lines (176 loc) · 7.97 KB
/
Copy pathCMS.java
File metadata and controls
232 lines (176 loc) · 7.97 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
226
227
228
229
230
231
232
package javaxt.demo.express;
//Java imports
import java.util.*;
//JavaXT imports
import javaxt.io.Directory;
import javaxt.http.servlet.*;
import static javaxt.utils.Console.*;
import static javaxt.demo.express.Utils.*;
//******************************************************************************
//** CMS Demo
//******************************************************************************
/**
* Used to run CMS-related demos and scripts
*
******************************************************************************/
public class CMS {
//**************************************************************************
//** start
//**************************************************************************
/** Used to start a CMS demo
*/
public static void start(Directory cmsDir, HashMap<String, String> args){
//Process args
String demo = args.get("-demo");
if (demo==null) demo = "";
String auth = args.get("-auth");
if (auth==null) auth = "";
//Start demo
if (auth.equalsIgnoreCase("true") || demo.equalsIgnoreCase("Editing")){
authDemo(cmsDir, args);
}
else{
basicDemo(cmsDir, args);
}
}
//**************************************************************************
//** basicDemo
//**************************************************************************
/** Used to support a basic CMS demo. Features a custom getContent() method
* to generate custom html for the "wiki" homepage and a custom websocket
* interface to notify clients whenever any files are created, updated, or
* deleted on the server.
*/
public static void basicDemo(Directory cmsDir, HashMap<String, String> args){
//Instantiate CMS servlet
var servlet = initServlet(cmsDir, null);
//Start web server
startServer(args, servlet);
}
//**************************************************************************
//** authDemo
//**************************************************************************
/** Extension of the basic CMS demo that layers in HTTP Basic Authentication.
* The "admin" tab requires authentication; all other tabs are public.
*/
public static void authDemo(Directory cmsDir, HashMap<String, String> args){
//Prompt user to define a valid username and password
String username = args.get("-user");
String password = args.get("-password");
if (username==null || password==null){
username = getInput("Username: ");
password = getPassword("Password: ");
}
//Create an approved/authorized User
var authorizedUser = new User(username, password);
System.out.println(username + " user created!");
//Create authenticator
var authenticator = new javaxt.express.Authenticator(){
public java.security.Principal getPrinciple(){
User user = null;
try{
String[] credentials = getCredentials();
String u = credentials[0];
String p = credentials[1];
if (u!=null && p!=null){
if (u.equalsIgnoreCase(authorizedUser.username) &&
p.equals(authorizedUser.password)){
user = authorizedUser;
}
}
}
catch(Exception e){}
setUser(user);
return user;
}
};
//Instantiate CMS servlet
var servlet = initServlet(cmsDir, authenticator);
//Start web server
startServer(args, servlet);
}
//**************************************************************************
//** initServlet
//**************************************************************************
private static javaxt.express.cms.WebSite initServlet(Directory cmsDir, javaxt.express.Authenticator authenticator){
//Instantiate CMS servlet
var servlet = new javaxt.express.cms.WebSite(cmsDir){
/* Set website properties using static braces
*/
{
if (authenticator!=null) this.setAuthenticator(authenticator);
this.setCompanyName("You");
this.setAuthor("You");
}
/* Override the processRequest method to handle authentication
*/
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
//Get path from url, excluding servlet path and leading "/" character
String path = request.getPathInfo();
if (path!=null) path = path.substring(1);
//Get first "directory" in the path
String service = path==null ? "" : path.toLowerCase();
if (service.contains("/")) service = service.substring(0, service.indexOf("/"));
if (authenticator!=null){
//Pass the request to the authenticator for login/logout/whoami
var auth = (javaxt.express.Authenticator) getAuthenticator(request);
if (auth.handleRequest(service, response)) return;
}
super.processRequest(request, response);
}
/* Override canEdit to allow authenticated users to edit content.
*/
protected boolean canEdit(HttpServletRequest request){
if (authenticator==null) return false;
try{
var auth = (javaxt.express.Authenticator) getAuthenticator(request);
return auth.getPrinciple()!=null;
}
catch(Exception e){
return false;
}
}
protected void onFileUpdate(javaxt.io.File file, String action){
//System.out.println(action + ": " + file);
}
/* Override the getContent method to generate custom HTML for wiki pages.
* Performs custom keyword substitution for the "<%=index%>" tag.
*/
public javaxt.express.cms.Content getContent(
javaxt.http.servlet.HttpServletRequest request, javaxt.io.File file){
//Get path from url
String path = request.getURL().getPath().toLowerCase();
//Remove leading and trailing "/" characters
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length()-1);
//Return content
if (path.startsWith("admin") && authenticator==null){
String html = "<h1>Admin</h1>" +
"<p>Authentication is not enabled in this demo. " +
"To see the admin login flow, start the server " +
"with the <code>-auth</code> flag:</p>" +
"<pre>java -jar express-demo.jar -start cms -demo Basic -auth true</pre>";
return new javaxt.express.cms.Content(html, new Date());
}
else if (path.equals("wiki")){ //replace index tag
String html = file.getText();
Date date = file.getDate();
if (file.getName(false).equals("index")){
javaxt.express.cms.Content content = getIndex(file);
javaxt.utils.Date d = new javaxt.utils.Date(content.getDate());
if (d.isAfter(new javaxt.utils.Date(date))){
date = d.getDate();
}
html = html.replace("<%=index%>", content.getHTML());
}
return new javaxt.express.cms.Content(html, date);
}
else{
return super.getContent(request, file);
}
}
};
return servlet;
}
}