354 lines
11 KiB
Java
Raw Normal View History

2015-11-19 14:39:21 -08:00
/**
* Copyright 2015 LinkedIn Corp. All rights reserved.
* <p>
2015-11-19 14:39:21 -08:00
* 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
* <p>
2015-11-19 14:39:21 -08:00
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
2015-11-19 14:39:21 -08:00
* 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;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
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 java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Constructor;
import org.apache.commons.lang3.StringUtils;
import play.Logger;
import play.Play;
2015-11-19 14:39:21 -08:00
import play.data.DynamicForm;
import play.libs.Json;
import play.mvc.BodyParser;
2015-11-19 14:39:21 -08:00
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import security.AuthenticationManager;
2015-11-19 14:39:21 -08:00
import utils.Tree;
import wherehows.dao.DaoFactory;
import static play.data.Form.*;
2015-11-19 14:39:21 -08:00
public class Application extends Controller {
2015-11-19 14:39:21 -08:00
private static String TREE_NAME_SUBFIX = ".tree.name";
private static final String WHZ_APP_ENV = System.getenv("WHZ_APP_HOME");
private static final String APP_VERSION = Play.application().configuration().getString("app.version");
private static final String PIWIK_SITE_ID = Play.application().configuration().getString("tracking.piwik.siteid");
2017-04-07 18:59:03 -07:00
private static final String PIWIK_URL = Play.application().configuration().getString("tracking.piwik.url");
private static final Boolean IS_INTERNAL = Play.application().configuration().getBoolean("linkedin.internal", false);
2015-11-19 14:39:21 -08:00
public static final DaoFactory daoFactory = createDaoFactory();
private static DaoFactory createDaoFactory() {
try {
String className =
Play.application().configuration().getString("dao.factory.class", DaoFactory.class.getCanonicalName());
Class factoryClass = Class.forName(className);
Constructor<? extends DaoFactory> ctor = factoryClass.getConstructor();
return ctor.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Serves the build output index.html for any given path
*
* @param path takes a path string, which essentially is ignored
* routing is managed client side
* @return {Result} build output index.html resource
*/
private static Result serveAsset(String path) {
2017-04-10 20:46:07 -07:00
InputStream indexHtml = Play.application().classloader().getResourceAsStream("public/index.html");
response().setHeader("Cache-Control", "no-cache");
return ok(indexHtml).as("text/html");
}
public static Result healthcheck() {
return ok("GOOD");
}
public static Result printDeps() {
String libPath = WHZ_APP_ENV + "/lib";
String commitFile = WHZ_APP_ENV + "/commit";
String libraries = "";
String commit = "";
if (WHZ_APP_ENV == null) {
return ok("WHZ_APP_HOME environmental variable not defined");
}
try {
BufferedReader br = new BufferedReader(new FileReader(commitFile));
commit = br.readLine();
} catch (IOException ioe) {
Logger.error("Error while reading commit file. Error message: " +
ioe.getMessage());
}
//get all the files from a directory
File directory = new File(libPath);
for (File file : directory.listFiles()) {
if (file.isFile()) {
libraries += file.getName() + "\n";
}
}
return ok("commit: " + commit + "\n" + libraries);
}
/**
* index Action proxies to serveAsset
*
* @param path takes a path string which is either index.html or the path segment after /
* @return {Result} response from serveAsset method
*/
public static Result index(String path) {
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
/**
* Creates a wrapping ObjectNode containing config information
*
* @return Http Result instance with app configuration attributes
*/
public static Result appConfig() {
ObjectNode response = Json.newObject();
ObjectNode config = Json.newObject();
2017-04-07 18:59:03 -07:00
config.put("appVersion", APP_VERSION);
2017-04-07 18:59:03 -07:00
config.put("isInternal", IS_INTERNAL);
config.put("tracking", trackingInfo());
response.put("status", "ok");
response.put("config", config);
return ok(response);
}
/**
* @return Json object containing the tracking configuration details
*/
private static ObjectNode trackingInfo() {
ObjectNode trackingConfig = Json.newObject();
2017-04-07 18:59:03 -07:00
ObjectNode trackers = Json.newObject();
ObjectNode piwik = Json.newObject();
Integer siteId = null;
try {
siteId = Integer.parseInt(PIWIK_SITE_ID);
} catch (NumberFormatException e) {
Logger.error("Piwik site ID must be an integer");
}
piwik.put("piwikSiteId", siteId);
2017-04-07 18:59:03 -07:00
piwik.put("piwikUrl", PIWIK_URL);
trackers.put("piwik", piwik);
trackingConfig.put("trackers", trackers);
trackingConfig.put("isEnabled", true);
return trackingConfig;
}
2015-11-19 14:39:21 -08:00
@Security.Authenticated(Secured.class)
public static Result lineage() {
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null) {
2015-11-19 14:39:21 -08:00
username = "";
}
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result datasetLineage(int id) {
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null) {
2015-11-19 14:39:21 -08:00
username = "";
}
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result metricLineage(int id) {
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null) {
2015-11-19 14:39:21 -08:00
username = "";
}
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result flowLineage(String application, String project, String flow) {
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null) {
2015-11-19 14:39:21 -08:00
username = "";
}
String type = "azkaban";
if (StringUtils.isNotBlank(application) && (application.toLowerCase().indexOf("appworx") != -1)) {
2015-11-19 14:39:21 -08:00
type = "appworx";
}
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
@Security.Authenticated(Secured.class)
public static Result schemaHistory() {
2015-11-19 14:39:21 -08:00
String username = session("user");
if (username == null) {
2015-11-19 14:39:21 -08:00
username = "";
}
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
2016-05-06 11:29:22 -07:00
@Security.Authenticated(Secured.class)
public static Result scriptFinder() {
2016-05-06 11:29:22 -07:00
String username = session("user");
if (username == null) {
2016-05-06 11:29:22 -07:00
username = "";
}
return serveAsset("");
2016-05-06 11:29:22 -07:00
}
2016-02-10 19:17:47 -08:00
@Security.Authenticated(Secured.class)
public static Result idpc() {
2016-02-10 19:17:47 -08:00
String username = session("user");
if (username == null) {
2016-02-10 19:17:47 -08:00
username = "";
}
return serveAsset("");
2016-02-10 19:17:47 -08:00
}
@Security.Authenticated(Secured.class)
public static Result dashboard() {
String username = session("user");
if (username == null) {
username = "";
}
return serveAsset("");
}
public static Result login() {
2015-11-19 14:39:21 -08:00
//You cann generate the Csrf token such as String csrfToken = SecurityPlugin.getInstance().getCsrfToken();
String csrfToken = "";
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
@BodyParser.Of(BodyParser.Json.class)
public static Result authenticate() {
JsonNode json = request().body().asJson();
// Extract username and password as String from JsonNode,
// null if they are not strings
String username = json.findPath("username").textValue();
String password = json.findPath("password").textValue();
2015-11-19 14:39:21 -08:00
if (username == null || StringUtils.isBlank(username)) {
return badRequest("Missing or invalid [username]");
}
if (password == null || StringUtils.isBlank(password)) {
return badRequest("Missing or invalid [credentials]");
2015-11-19 14:39:21 -08:00
}
session().clear();
// Create a uuid string for this session if one doesn't already exist
// to be appended to the Result object
String uuid = session("uuid");
if (uuid == null) {
uuid = java.util.UUID.randomUUID().toString();
session("uuid", uuid);
2015-11-19 14:39:21 -08:00
}
try {
AuthenticationManager.authenticateUser(username, password);
} catch (Exception e) {
return badRequest("Invalid credentials");
2015-11-19 14:39:21 -08:00
}
// Adds the username to the session cookie
session("user", username);
// Construct an ObjectNode with the username and uuid token to be sent with the response
ObjectNode data = Json.newObject();
data.put("username", username);
data.put("uuid", uuid);
// Create a new response ObjectNode to return when authenticate request is successful
ObjectNode response = Json.newObject();
response.put("status", "ok");
response.set("data", data);
return ok(response);
2015-11-19 14:39:21 -08:00
}
public static Result signUp() {
2015-11-19 14:39:21 -08:00
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 {
2015-11-19 14:39:21 -08:00
errorMessage = UserDAO.signUp(username, firstName, lastName, email, password);
if (StringUtils.isNotBlank(errorMessage)) {
2015-11-19 14:39:21 -08:00
flash("error", errorMessage);
} else {
2015-11-19 14:39:21 -08:00
flash("success", "Congratulations! Your account has been created. Please login.");
}
} catch (Exception e) {
2015-11-19 14:39:21 -08:00
flash("error", e.getMessage());
}
return serveAsset("");
2015-11-19 14:39:21 -08:00
}
public static Result logout() {
2015-11-19 14:39:21 -08:00
session().clear();
return ok();
2015-11-19 14:39:21 -08:00
}
public static Result loadTree(String key) {
if (StringUtils.isNotBlank(key) && key.equalsIgnoreCase("flows")) {
2016-03-15 18:48:18 -07:00
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) {
2016-03-15 18:48:18 -07:00
return ok(FlowsDAO.getFlowProjectNodes(app));
}
public static Result loadFlowNodes(String app, String project) {
2016-03-15 18:48:18 -07:00
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
}