datahub/web/app/controllers/Application.java

271 lines
9.4 KiB
Java
Raw Normal View History

2015-11-19 14:39:21 -08:00
/**
* Copyright 2015 LinkedIn Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package controllers;
2016-03-15 18:48:18 -07:00
import dao.FlowsDAO;
import dao.MetricsDAO;
2015-11-19 14:39:21 -08:00
import dao.UserDAO;
import play.Play;
2015-11-19 14:39:21 -08:00
import play.data.DynamicForm;
import play.mvc.Controller;
import play.mvc.Result;
import play.Logger;
import play.mvc.Security;
import utils.Tree;
import views.html.*;
2015-11-19 14:39:21 -08:00
import static play.data.Form.form;
import org.apache.commons.lang3.StringUtils;
import security.AuthenticationManager;
public class Application extends Controller
{
private static String TREE_NAME_SUBFIX = ".tree.name";
private static String LINKEDIN_INTERNAL_KEY = "linkedin.internal";
private static String PIWIK_SITE_ID = "tracking.piwik.siteid";
2015-11-19 14:39:21 -08:00
@Security.Authenticated(Secured.class)
public static Result index()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
//You cann generate the Csrf token such as String csrfToken = SecurityPlugin.getInstance().getCsrfToken();
String csrfToken = "";
return ok(index.render(username, csrfToken, isInternal, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result lineage()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
return ok(lineage.render(username, isInternal, "chains", 0, null, null, null, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result datasetLineage(int id)
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
return ok(lineage.render(username, isInternal, "dataset", id, null, null, null, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result metricLineage(int id)
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
return ok(lineage.render(username, isInternal, "metric", id, null, null, null, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result flowLineage(String application, String project, String flow)
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
String type = "azkaban";
if (StringUtils.isNotBlank(application) && (application.toLowerCase().indexOf("appworx") != -1))
{
type = "appworx";
}
return ok(lineage.render(username, isInternal, type, 0, application.replace(" ", "."), project, flow, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result schemaHistory()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
return ok(schemaHistory.render(username, isInternal, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
2016-05-06 11:29:22 -07:00
@Security.Authenticated(Secured.class)
public static Result scriptFinder()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2016-05-06 11:29:22 -07:00
String username = session("user");
if (username == null)
{
username = "";
}
return ok(scriptFinder.render(username, isInternal, piwikSiteId));
2016-05-06 11:29:22 -07:00
}
2016-02-10 19:17:47 -08:00
@Security.Authenticated(Secured.class)
public static Result idpc()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2016-02-10 19:17:47 -08:00
String username = session("user");
if (username == null)
{
username = "";
}
return ok(idpc.render(username, isInternal, piwikSiteId));
2016-02-10 19:17:47 -08:00
}
@Security.Authenticated(Secured.class)
public static Result dashboard()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
String username = session("user");
if (username == null)
{
username = "";
}
return ok(dashboard.render(username, isInternal, piwikSiteId));
}
2015-11-19 14:39:21 -08:00
public static Result login()
{
Boolean isInternal = Play.application().configuration().getBoolean(LINKEDIN_INTERNAL_KEY, false);
Integer piwikSiteId = Play.application().configuration().getInt(PIWIK_SITE_ID);
2015-11-19 14:39:21 -08:00
//You cann generate the Csrf token such as String csrfToken = SecurityPlugin.getInstance().getCsrfToken();
String csrfToken = "";
return ok(login.render(csrfToken, isInternal, piwikSiteId));
2015-11-19 14:39:21 -08:00
}
public static Result authenticate()
{
DynamicForm loginForm = form().bindFromRequest();
String username = loginForm.get("username");
String password = loginForm.get("password");
if (StringUtils.isBlank(username) || StringUtils.isBlank(password))
{
flash("error", "Invalid username or password");
return redirect(controllers.routes.Application.login());
}
try
{
AuthenticationManager.authenticateUser(username, password);
}
catch (Exception e)
{
Logger.error("Authentication failed for user " + username);
Logger.error(e.getMessage());
flash("error", "Invalid username or password");
return redirect(controllers.routes.Application.login());
}
session().clear();
session("user", username);
return redirect(controllers.routes.Application.index());
}
public static Result signUp()
{
DynamicForm loginForm = form().bindFromRequest();
String username = loginForm.get("inputName");
String firstName = loginForm.get("inputFirstName");
String lastName = loginForm.get("inputLastName");
String email = loginForm.get("inputEmail");
String password = loginForm.get("inputPassword");
String errorMessage = "";
try
{
errorMessage = UserDAO.signUp(username, firstName, lastName, email, password);
if (StringUtils.isNotBlank(errorMessage))
{
flash("error", errorMessage);
}
else
{
flash("success", "Congratulations! Your account has been created. Please login.");
}
}
catch (Exception e)
{
flash("error", e.getMessage());
}
return redirect(controllers.routes.Application.login());
}
public static Result logout()
{
session().clear();
flash("success", "You've been logged out");
return redirect(controllers.routes.Application.login());
}
public static Result loadTree(String key)
{
2016-03-15 18:48:18 -07:00
if (StringUtils.isNotBlank(key) && key.equalsIgnoreCase("flows"))
{
return ok(FlowsDAO.getFlowApplicationNodes());
}
else if (StringUtils.isNotBlank(key) && key.equalsIgnoreCase("metrics"))
{
return ok(MetricsDAO.getMetricDashboardNodes());
}
2015-11-19 14:39:21 -08:00
return ok(Tree.loadTreeJsonNode(key + TREE_NAME_SUBFIX));
}
2016-03-15 18:48:18 -07:00
public static Result loadFlowProjects(String app)
{
return ok(FlowsDAO.getFlowProjectNodes(app));
}
public static Result loadFlowNodes(String app, String project)
{
return ok(FlowsDAO.getFlowNodes(app, project));
}
public static Result loadMetricGroups(String dashboard)
{
return ok(MetricsDAO.getMetricGroupNodes(dashboard));
}
public static Result loadMetricNodes(String dashboard, String group)
{
return ok(MetricsDAO.getMetricNodes(dashboard, group));
}
2015-11-19 14:39:21 -08:00
}