use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.
the class TremoloTarget method executeWorkFlow.
private void executeWorkFlow(String wfName, User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
StringBuffer surl = new StringBuffer();
surl.append(this.wfUrlBase).append("/services/wf/login");
HttpGet get = new HttpGet(surl.toString());
try {
try {
httpclient.execute(get);
} catch (ClientProtocolException e1) {
} catch (IOException e1) {
}
} finally {
get.releaseConnection();
}
surl.setLength(0);
surl.append(this.wfUrlBase).append("/services/wf/execute");
HttpPost post = new HttpPost(surl.toString());
try {
TremoloUser tu = new TremoloUser();
tu.setAttributes(new ArrayList<Attribute>());
tu.setUid(user.getUserID());
tu.setUserPassword(user.getPassword());
for (String attrName : user.getAttribs().keySet()) {
Attribute attr = user.getAttribs().get(attrName);
if (attributes.size() == 0 || attributes.contains(attrName)) {
tu.getAttributes().add(attr);
}
}
WFCall wfcall = new WFCall();
wfcall.setName(wfName);
wfcall.setUidAttributeName(this.uidAttrName);
wfcall.setUser(tu);
wfcall.setRequestParams(new HashMap<String, Object>());
wfcall.getRequestParams().put(ProvisioningParams.UNISON_EXEC_TYPE, ProvisioningParams.UNISON_EXEC_SYNC);
Gson gson = new Gson();
String jsonOut = gson.toJson(wfcall);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("wfcall", jsonOut));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
StringBuffer res = new StringBuffer();
while ((line = in.readLine()) != null) {
// System.out.println(line);
res.append(line).append('\n');
}
ProvisioningResult provRes = gson.fromJson(res.toString(), ProvisioningResult.class);
if (!provRes.isSuccess()) {
throw new ProvisioningException(provRes.getError().getError());
}
} catch (Exception e) {
throw new ProvisioningException("Could not execute workflow", e);
} finally {
post.releaseConnection();
}
}
use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.
the class ExecuteWorkflow method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Gson gson = new Gson();
String wfcall = req.getParameter("wfcall");
if (wfcall == null) {
logger.error("Could not get workflow call");
resp.setStatus(500);
ProvisioningError pe = new ProvisioningError();
ProvisioningResult pres = new ProvisioningResult();
pres.setSuccess(false);
pres.setError(pe);
pe.setError("Could not get workflow call");
gson = new Gson();
resp.getOutputStream().print(gson.toJson(pres));
return;
}
String line;
StringBuffer json = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(wfcall.getBytes("UTF-8"))));
while ((line = in.readLine()) != null) {
json.append(line).append('\n');
}
WFCall wfCall = gson.fromJson(json.toString(), WFCall.class);
if (wfCall == null) {
logger.error("Could not get workflow call");
resp.setStatus(500);
ProvisioningError pe = new ProvisioningError();
pe.setError("Could not get workflow call");
ProvisioningResult pres = new ProvisioningResult();
pres.setSuccess(false);
pres.setError(pe);
gson = new Gson();
resp.getOutputStream().print(gson.toJson(pres));
return;
}
List<ApprovalData> autoApprovals = null;
try {
// TremoloContext.getContext().getConfigManager("proxy").getProvisioningEngine().getWorkFlow(wfCall.getName()).executeWorkflow(wfCall);
com.tremolosecurity.provisioning.workflow.ExecuteWorkflow exec = new com.tremolosecurity.provisioning.workflow.ExecuteWorkflow();
exec.execute(wfCall, GlobalEntries.getGlobalEntries().getConfigManager());
ProvisioningResult res = new ProvisioningResult();
res.setSuccess(true);
resp.getOutputStream().print(gson.toJson(res));
} catch (Throwable t) {
logger.error("Error executing workflow", t);
resp.setStatus(500);
ProvisioningError pe = new ProvisioningError();
pe.setError("Error executing workflow");
ProvisioningResult pres = new ProvisioningResult();
pres.setSuccess(false);
pres.setError(pe);
gson = new Gson();
resp.getOutputStream().print(gson.toJson(pres));
}
}
use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.
the class ExecutedWorkflows method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userKey = req.getParameter("user");
Session session = null;
try {
session = GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getHibernateSessionFactory().openSession();
// PreparedStatement ps = con.prepareStatement("select workflows.id,workflows.name from workflows inner join users on users.id=workflows.userid where workflows.completeTS IS NOT NULL AND userKey=?");
Query query = session.createQuery("FROM Workflows WHERE Workflows.completeTS IS NOT NULL AND Workflows.users.userKey = :user_key");
query.setParameter("user_key", userKey);
List<com.tremolosecurity.provisioning.objects.Workflows> workflows = query.list();
ArrayList<String> workflowids = new ArrayList<String>();
for (Workflows wf : workflows) {
if (wf.getApprovals().isEmpty()) {
workflowids.add(wf.getName());
} else {
boolean approved = true;
for (Approvals approval : wf.getApprovals()) {
approved = approved && (approval.getApproved() == 1 && approval.getApprovedTs() != null);
}
}
}
Gson gson = new Gson();
ProvisioningResult resObj = new ProvisioningResult();
resObj.setSuccess(true);
resObj.setWorkflowIds(workflowids);
resp.getOutputStream().println(gson.toJson(resObj));
} catch (Exception e) {
ProvisioningError pe = new ProvisioningError();
pe.setError("Could not load executed workflows : " + e.getMessage());
ProvisioningResult res = new ProvisioningResult();
res.setSuccess(false);
res.setError(pe);
Gson gson = new Gson();
resp.getWriter().write(gson.toJson(res));
} finally {
if (session != null) {
session.close();
}
}
}
use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.
the class GenerateReport method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final Gson gson = new Gson();
try {
String name = req.getParameter("name");
ReportType reportToRunTmp = null;
boolean foundReport = false;
for (ReportType report : GlobalEntries.getGlobalEntries().getConfigManager().getCfg().getProvisioning().getReports().getReport()) {
if (report.getName().equalsIgnoreCase(name)) {
reportToRunTmp = report;
foundReport = true;
break;
}
}
if (!foundReport) {
reportToRunTmp = null;
}
final ReportType reportToRun = reportToRunTmp;
if (reportToRun == null) {
throw new ProvisioningException("Could not find report");
} else {
Connection db = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Session session = GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getHibernateSessionFactory().openSession();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
try {
generateReportData(req, resp, gson, reportToRun, connection);
} catch (IOException e) {
throw new SQLException("Could not run reports", e);
}
}
});
} finally {
}
}
} catch (Exception e) {
logger.error("Could not run report", e);
resp.setStatus(500);
ProvisioningError pe = new ProvisioningError();
pe.setError("Could not run report;" + e.getMessage());
ProvisioningResult resObj = new ProvisioningResult();
resObj.setSuccess(false);
resObj.setError(pe);
resp.getOutputStream().print(gson.toJson(resObj));
}
}
use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.
the class ScaleMain method generateReport.
private void generateReport(HttpFilterRequest request, HttpFilterResponse response, Gson gson, ReportType reportToRun, AuthInfo userData, Connection db) throws SQLException, IOException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("Report SQL : '" + reportToRun.getSql() + "'");
}
ps = db.prepareStatement(reportToRun.getSql());
int i = 1;
for (String paramType : reportToRun.getParamater()) {
switch(paramType) {
case "currentUser":
String userid = userData.getAttribs().get(this.scaleConfig.getUidAttributeName()).getValues().get(0);
if (logger.isDebugEnabled()) {
logger.debug("Current User : '" + userid + "'");
}
ps.setString(i, userid);
break;
case "userKey":
if (logger.isDebugEnabled()) {
logger.debug("User Key : '" + request.getParameter("userKey") + "'");
}
ps.setString(i, request.getParameter("userKey").getValues().get(0));
break;
case "beginDate":
String beginDate = request.getParameter("beginDate").getValues().get(0);
if (logger.isDebugEnabled()) {
logger.debug("Begin Date : '" + beginDate + "'");
}
Date d = new Date(Long.parseLong(beginDate));
ps.setDate(i, d);
break;
case "endDate":
String endDate = request.getParameter("endDate").getValues().get(0);
if (logger.isDebugEnabled()) {
logger.debug("End Date : '" + endDate + "'");
}
Date de = new Date(Long.parseLong(endDate));
ps.setDate(i, de);
break;
}
i++;
}
rs = ps.executeQuery();
String groupingVal = null;
ReportResults res = new ReportResults();
res.setName(reportToRun.getName());
res.setDescription(reportToRun.getDescription());
res.setDataFields(reportToRun.getDataFields());
res.setHeaderFields(reportToRun.getHeaderFields());
res.setGrouping(new ArrayList<ReportGrouping>());
ReportGrouping grouping = null;
if (!reportToRun.isGroupings()) {
grouping = new ReportGrouping();
grouping.setData(new ArrayList<Map<String, String>>());
grouping.setHeader(new HashMap<String, String>());
res.getGrouping().add(grouping);
}
logger.debug("Running report");
while (rs.next()) {
if (logger.isDebugEnabled()) {
logger.debug("New row");
}
HashMap<String, String> row = new HashMap<String, String>();
for (String dataField : reportToRun.getDataFields()) {
if (logger.isDebugEnabled()) {
logger.debug("Field - " + dataField + "='" + rs.getString(dataField) + "'");
}
row.put(dataField, rs.getString(dataField));
}
if (reportToRun.isGroupings()) {
String rowID = rs.getString(reportToRun.getGroupBy());
if (logger.isDebugEnabled()) {
logger.debug("Grouping Val : '" + groupingVal + "'");
logger.debug("Group By : '" + reportToRun.getGroupBy() + "'");
logger.debug("Value of Group By in row : '" + rowID + "'");
}
if (groupingVal == null || !groupingVal.equals(rowID)) {
grouping = new ReportGrouping();
grouping.setData(new ArrayList<Map<String, String>>());
grouping.setHeader(new HashMap<String, String>());
res.getGrouping().add(grouping);
for (String headerField : reportToRun.getHeaderFields()) {
grouping.getHeader().put(headerField, rs.getString(headerField));
}
groupingVal = rowID;
}
}
grouping.getData().add(row);
}
if (request.getParameter("excel") != null && request.getParameter("excel").getValues().get(0).equalsIgnoreCase("true")) {
UUID id = UUID.randomUUID();
String sid = id.toString();
Map<String, String> map = new HashMap<String, String>();
map.put("reportid", sid);
request.getSession().setAttribute(sid, res);
String json = gson.toJson(map);
if (logger.isDebugEnabled()) {
logger.debug("JSON : " + json);
}
response.setContentType("application/json");
ScaleJSUtils.addCacheHeaders(response);
response.getWriter().print(json);
response.getWriter().flush();
} else {
ProvisioningResult pres = new ProvisioningResult();
pres.setSuccess(true);
pres.setReportResults(res);
String json = gson.toJson(res);
if (logger.isDebugEnabled()) {
logger.debug("JSON : " + json);
}
response.setContentType("application/json");
ScaleJSUtils.addCacheHeaders(response);
response.getWriter().print(json);
response.getWriter().flush();
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (Throwable t) {
}
}
if (ps != null) {
try {
ps.close();
} catch (Throwable t) {
}
}
if (db != null) {
try {
db.close();
} catch (Throwable t) {
}
}
}
}
Aggregations