-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.groovy
229 lines (195 loc) · 7.92 KB
/
Index.groovy
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
import java.lang.management.RuntimeMXBean;
import java.lang.management.ManagementFactory;
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat;
import java.util.logging.Logger;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.lang.Runtime;
import org.json.simple.JSONObject;
import org.codehaus.groovy.tools.shell.CommandAlias;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import org.apache.commons.lang3.StringEscapeUtils
import org.bonitasoft.engine.identity.User;
import org.bonitasoft.web.extension.page.PageContext;
import org.bonitasoft.web.extension.page.PageController;
import org.bonitasoft.web.extension.page.PageResourceProvider;
import org.bonitasoft.engine.exception.AlreadyExistsException;
import org.bonitasoft.engine.exception.BonitaHomeNotSetException;
import org.bonitasoft.engine.exception.CreationException;
import org.bonitasoft.engine.exception.DeletionException;
import org.bonitasoft.engine.exception.ServerAPIException;
import org.bonitasoft.engine.exception.UnknownAPITypeException;
import org.bonitasoft.engine.session.APISession;
public class Index implements PageController {
private static String pageName="drillcar";
private static Logger loggerCustomPage= Logger.getLogger("org.bonitasoft.custompage."+pageName+".groovy");
public static class ActionAnswer
{
/*
* if true, the answer is managed by the action (else, it should be an HTML call)
*/
public boolean isManaged=false;
/*
* if true, the response is in responseMap, and a JSON is necessary
*/
public boolean isResponseMap=true;
/*
* the response under a Map
*/
public Map<String,Object> responseMap =new HashMap<String,Object>();
public void setResponse(Map<String,Object> response )
{
responseMap = response;
isResponseMap=true;
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response, PageResourceProvider pageResourceProvider, PageContext pageContext) {
try {
String requestParamJson= request.getParameter("paramjson");
String requestParamJsonSt ="";
try
{
requestParamJsonSt = requestParamJson; // in fact, the request is decoded by the Tomcat Server
// requestParamJsonSt = (requestParamJson==null ? null : java.net.URLDecoder.decode(requestParamJson, "UTF-8"));
}
catch(Exception e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
loggerCustomPage.severe("#### "+pageName+":Groovy Exception ["+e.toString()+"] at "+exceptionDetails+" Decode["+requestParamJson+"]");
PrintWriter out = response.getWriter()
response.setCharacterEncoding("UTF-8");
response.addHeader("content-type", "application/json");
out.write( "{\"status\":\"BadjsonParam\"} " );
out.flush();
out.close();
return;
}
loggerCustomPage.info("#### "+pageName+":Groovy , requestParamJsonSt=["+requestParamJsonSt+"] (source is["+requestParamJson+"])" );
Index.ActionAnswer actionAnswer = Actions.doAction( request, requestParamJsonSt, response, pageResourceProvider, pageContext );
if (! actionAnswer.isManaged)
{
loggerCustomPage.info("#### "+pageName+"Groovy NoAction, return index.html" );
runTheBonitaIndexDoGet( request, response,pageResourceProvider,pageContext);
return;
}
loggerCustomPage.info("#### "+pageName+":Groovy , ResponseMap="+actionAnswer.responseMap.size() );
if (actionAnswer.responseMap.size()>0)
{
response.setCharacterEncoding("UTF-8");
response.addHeader("content-type", "application/json");
PrintWriter out = response.getWriter()
String jsonSt = JSONValue.toJSONString( actionAnswer.responseMap );
out.write( jsonSt );
loggerCustomPage.info("#### ##############################CustomPage: return json["+jsonSt+"]" );
out.flush();
out.close();
return;
}
// assuming the DoAction did the job (export a ZIP file for example)
loggerCustomPage.info("#### "+pageName+" ##############################CustomPage: AssumingDoAction did the job (export a file)" );
return;
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
loggerCustomPage.severe("#### "+pageName+":Groovy Exception ["+e.toString()+"] at "+exceptionDetails);
}
}
/** -------------------------------------------------------------------------
*
*getIntegerParameter
*
*/
public static getIntegerParameter(HttpServletRequest request, String paramName, Integer defaultValue)
{
String valueParamSt = request.getParameter(paramName);
if (valueParamSt==null || valueParamSt.length()==0)
{
return defaultValue;
}
try
{
return Integer.valueOf( valueParamSt );
}
catch( Exception e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
loggerCustomPage.severe("#### "+pageName+":Groovy : getinteger : Exception "+e.toString()+" on ["+valueParamSt+"] at "+exceptionDetails );
return defaultValue;
}
}
/** -------------------------------------------------------------------------
*
*getBooleanParameter
*
*/
public static Boolean getBooleanParameter(HttpServletRequest request, String paramName, Boolean defaultValue)
{
String valueParamSt = request.getParameter(paramName);
if (valueParamSt==null || valueParamSt.length()==0)
{
return defaultValue;
}
try
{
return Boolean.valueOf( valueParamSt );
}
catch( Exception e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
loggerCustomPage.severe("#### "+pageName+":Groovy : getBoolean : Exception "+e.toString()+" on ["+valueParamSt+"] at "+exceptionDetails );
return defaultValue;
}
}
/** -------------------------------------------------------------------------
*
*runTheBonitaIndexDoGet
*
*/
private void runTheBonitaIndexDoGet(HttpServletRequest request, HttpServletResponse response, PageResourceProvider pageResourceProvider, PageContext pageContext) {
try {
def String indexContent;
pageResourceProvider.getResourceAsStream("index.html").withStream { InputStream s->
indexContent = s.getText()
}
File pageDirectory = pageResourceProvider.getPageDirectory();
loggerCustomPage.info("#### "+pageName+": pageDirectory st="+pageDirectory.getAbsolutePath() );
// def String pageResource="pageResource?&page="+ request.getParameter("page")+"&location=";
// indexContent= indexContent.replace("@_USER_LOCALE_@", request.getParameter("locale"));
indexContent= indexContent.replace("@_CURRENTTIMEMILIS_@", String.valueOf(System.currentTimeMillis()));
indexContent= indexContent.replace("@_PAGEDIRECTORY_@", pageDirectory.getAbsolutePath()) ;
response.setCharacterEncoding("UTF-8");
response.addHeader("content-type", "text/html");
PrintWriter out = response.getWriter();
out.print(indexContent);
out.flush();
out.close();
// loggerCustomPage.info("#### "+pageName+": return index.hml size("+indexContent.length()+"]");
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
loggerCustomPage.severe("#### "+pageName+":Error "+e.toString()+" at "+exceptionDetails);
}
}
}