-
Notifications
You must be signed in to change notification settings - Fork 4
/
Index.groovy
190 lines (162 loc) · 7.86 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
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.api.TenantAPIAccessor;
import org.bonitasoft.engine.session.APISession;
public class Index implements PageController {
private static Logger loggerCustomPage= Logger.getLogger("org.bonitasoft.custompage.meteor.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 = (requestParamJson==null ? null : java.net.URLDecoder.decode(requestParamJson, "UTF-8"));
// Token securisation : must be done action per action, else it's not possible to download a document.
Index.ActionAnswer actionAnswer = Actions.doAction( request, requestParamJsonSt, response, pageResourceProvider, pageContext );
if (! actionAnswer.isManaged) {
loggerCustomPage.fine("#### CustomPage:Groovy NoAction, return index.html" );
runTheBonitaIndexDoGet( request, response,pageResourceProvider,pageContext);
return;
}
loggerCustomPage.fine("#### CustomPage: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.fine("#### ##############################CustomPage: return json["+jsonSt+"]" );
out.flush();
out.close();
return;
}
// assuming the DoAction did the job (export a ZIP file for example)
return;
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionDetails = sw.toString();
loggerCustomPage.severe("#### LongBoardCustomPage: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("#### LongBoardCustomPage:Groovy LongBoard: 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("#### LongBoardCustomPage:Groovy LongBoard: 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()
}
// def String pageResource="pageResource?&page="+ request.getParameter("page")+"&location=";
// indexContent= indexContent.replace("@_USER_LOCALE_@", request.getParameter("locale"));
// indexContent= indexContent.replace("@_PAGE_RESOURCE_@", pageResource);
File pageDirectory = pageResourceProvider.getPageDirectory();
// def String pageResource="pageResource?&page="+ request.getParameter("page")+"&location=";
// indexContent= indexContent.replace("@_USER_LOCALE_@", request.getParameter("locale"));
// indexContent= indexContent.replace("@_PAGE_RESOURCE_@", pageResource);
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();
} catch (Exception e) {
e.printStackTrace();
}
}
}