Skip to content

Sp/template #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target/
.idea/
*.iml
src/main/resources/static/node_modules/
uploads/
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class AppProperties {
@Value("${app.companyAddress}")
private String companyAddress;

@Value("${app.jarUploadPath}")
private String jarUploadPath;

private Map<String, String> globalProperties;

public String getName() {
Expand All @@ -47,6 +50,10 @@ public String getCompanyName() {
return companyName;
}

public String getJarUploadPath() {
return jarUploadPath;
}

public Map<String, String> getGlobalProperties() {
if (globalProperties == null) {
Map<String, String> tmpMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.darksci.kafkaview.configuration;

import com.darksci.kafkaview.manager.plugin.DeserializerLoader;
import com.darksci.kafkaview.manager.plugin.PluginSecurityPolicy;
import com.darksci.kafkaview.manager.plugin.PluginUploadManager;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import java.security.Policy;

@Component
public class PluginConfig implements ApplicationListener<ApplicationReadyEvent> {

private final Policy pluginSecurityPolicy = new PluginSecurityPolicy();

@Bean
public Policy getPluginSecurityPolicy() {
return pluginSecurityPolicy;
}

@Bean
public PluginUploadManager getPluginUploadManager(final AppProperties appProperties) {
return new PluginUploadManager(appProperties.getJarUploadPath());
}

@Bean
public DeserializerLoader getDeserializerLoader(final AppProperties appProperties) {
return new DeserializerLoader(appProperties.getJarUploadPath() + "/deserializers");
}

@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
// // Setup plugin policy on startup
// Policy.setPolicy(getPluginSecurityPolicy());
// System.setSecurityManager(new SecurityManager());
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/darksci/kafkaview/configuration/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,20 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/css/**")
.addResourceLocations("classpath:/static/css/");

// js resource
registry
.addResourceHandler("/js/**")
.addResourceLocations("classpath:/static/js/");

// js resource
registry
.addResourceHandler("/node_modules/**")
.addResourceLocations("classpath:/static/node_modules/");

// img resource
registry
.addResourceHandler("/img/**")
.addResourceLocations("classpath:/static/img/");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.darksci.kafkaview.controller.configuration.cluster;

import com.darksci.kafkaview.controller.BaseController;
import com.darksci.kafkaview.controller.configuration.cluster.forms.ClusterForm;
import com.darksci.kafkaview.manager.ui.FlashMessage;
import com.darksci.kafkaview.model.Cluster;
import com.darksci.kafkaview.repository.ClusterRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/configuration/cluster")
public class ClusterController extends BaseController {
private final static Logger logger = LoggerFactory.getLogger(ClusterController.class);

@Autowired
private ClusterRepository clusterRepository;

/**
* GET Displays main configuration index.
*/
@RequestMapping(path = "", method = RequestMethod.GET)
public String index(final Model model) {
// Retrieve all clusters
final Iterable<Cluster> clusterList = clusterRepository.findAll();
model.addAttribute("clusterList", clusterList);

return "configuration/cluster/index";
}

/**
* GET Displays create cluster form.
*/
@RequestMapping(path = "/create", method = RequestMethod.GET)
public String createClusterForm(final ClusterForm clusterForm) {
return "configuration/cluster/create";
}

/**
* GET Displays edit cluster form.
*/
@RequestMapping(path = "/edit/{id}", method = RequestMethod.GET)
public String editClusterForm(
final @PathVariable Long id,
final ClusterForm clusterForm,
final RedirectAttributes redirectAttributes) {

// Retrieve by id
final Cluster cluster = clusterRepository.findOne(id);
if (cluster == null) {
// redirect
// Set flash message
final FlashMessage flashMessage = FlashMessage.newWarning("Unable to find cluster!");
redirectAttributes.addFlashAttribute("FlashMessage", flashMessage);

// redirect to cluster index
return "redirect:/configuration/cluster";
}

// Build form
clusterForm.setId(cluster.getId());
clusterForm.setName(cluster.getName());
clusterForm.setBrokerHosts(cluster.getBrokerHosts());

// Display template
return "configuration/cluster/create";
}

/**
* Handles both Update and Creating clusters.
*/
@RequestMapping(path = "/update", method = RequestMethod.POST)
public String clusterUpdate(
@Valid final ClusterForm clusterForm,
final BindingResult bindingResult,
final RedirectAttributes redirectAttributes) {

final boolean updateExisting = clusterForm.exists();

// Ensure that cluster name is not already used.
final Cluster existingCluster = clusterRepository.findByName(clusterForm.getName());
if (existingCluster != null) {
// If we're updating, exclude our own id.
if (!updateExisting ||
(updateExisting && !clusterForm.getId().equals(existingCluster.getId()))) {
bindingResult.addError(new FieldError(
"clusterForm", "name", clusterForm.getName(), true, null, null, "Name is already used")
);
}
}

// If we have errors
if (bindingResult.hasErrors()) {
logger.info("Result: {}", clusterForm);
return "configuration/cluster/create";
}

// If we're updating
final Cluster cluster;
final String successMessage;
if (updateExisting) {
// Retrieve it
cluster = clusterRepository.findOne(clusterForm.getId());
if (cluster == null) {
// redirect
// Set flash message
final FlashMessage flashMessage = FlashMessage.newWarning("Unable to find cluster!");
redirectAttributes.addFlashAttribute("FlashMessage", flashMessage);

// redirect to cluster index
return "redirect:/configuration/cluster";
}

successMessage = "Updated cluster successfully!";
} else {
cluster = new Cluster();
successMessage = "Created new cluster!";
}

// Update properties
cluster.setName(clusterForm.getName());
cluster.setBrokerHosts(clusterForm.getBrokerHosts());
cluster.setValid(false);
clusterRepository.save(cluster);

// Set flash message
final FlashMessage flashMessage = FlashMessage.newSuccess(successMessage);
redirectAttributes.addFlashAttribute("FlashMessage", flashMessage);

// redirect to cluster index
return "redirect:/configuration/cluster";
}

/**
* POST deletes the selected cluster
*/
@RequestMapping(path = "/delete/{id}", method = RequestMethod.POST)
public String deleteCluster(final @PathVariable Long id, final RedirectAttributes redirectAttributes) {
// Retrieve it
final Cluster cluster = clusterRepository.findOne(id);
if (cluster == null) {
// Set flash message & redirect
redirectAttributes.addFlashAttribute("FlashMessage", FlashMessage.newWarning("Unable to find cluster!"));
} else {
// Delete it
clusterRepository.delete(id);

redirectAttributes.addFlashAttribute("FlashMessage", FlashMessage.newSuccess("Deleted cluster!"));
}

// redirect to cluster index
return "redirect:/configuration/cluster";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.darksci.kafkaview.controller.configuration.cluster.forms;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class ClusterForm {
private Long id = null;

@NotNull(message = "Enter a unique name")
@Size(min = 2, max = 255)
private String name;

@NotNull(message = "Enter kafka broker hosts")
@Size(min = 2, max = 255)
private String brokerHosts;

public Long getId() {
return id;
}

public void setId(final Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

public String getBrokerHosts() {
return brokerHosts;
}

public void setBrokerHosts(final String brokerHosts) {
this.brokerHosts = brokerHosts;
}

public boolean exists() {
return getId() != null;
}

@Override
public String toString() {
return "ClusterForm{" +
"name='" + name + '\'' +
", brokerHosts='" + brokerHosts + '\'' +
'}';
}
}
Loading