Skip to content

Commit

Permalink
Migrated add Line from Virtual Hosts code to Configuration Tree callb…
Browse files Browse the repository at this point in the history
…acks
  • Loading branch information
Jonathan Rossi committed Dec 23, 2015
1 parent f9c5a47 commit 745647d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 145 deletions.
91 changes: 10 additions & 81 deletions src/main/java/net/apachegui/web/VirtualHostsController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package net.apachegui.web;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import apache.conf.global.Utils;
import apache.conf.parser.File;
import apache.conf.parser.Parser;
import net.apachegui.conf.ConfFiles;
import net.apachegui.conf.Configuration;
import net.apachegui.db.SettingsDao;
Expand All @@ -20,7 +14,6 @@
import net.apachegui.virtualhosts.NetworkInfo;
import net.apachegui.virtualhosts.VirtualHost;
import net.apachegui.virtualhosts.VirtualHosts;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -29,9 +22,13 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import apache.conf.global.Utils;
import apache.conf.parser.File;
import apache.conf.parser.Parser;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

@RestController
@RequestMapping("/web/VirtualHosts")
Expand Down Expand Up @@ -71,75 +68,7 @@ public String getTreeHosts() throws NullPointerException, Exception {
return summary.toString();

}

@RequestMapping(method = RequestMethod.POST, params = "option=deleteLine", produces = "application/json;charset=UTF-8")
public String deleteLine(@RequestParam(value = "file") String file,
@RequestParam(value = "lineOfStart") int lineOfStart,
@RequestParam(value = "lineOfEnd") int lineOfEnd) throws Exception {

String originalContents = ConfFiles.deleteFromConfigFile(Pattern.compile(".*", Pattern.CASE_INSENSITIVE), new File(file), lineOfStart, lineOfEnd, true);

Configuration.testChanges(file, originalContents);

JSONObject result = populateFileModifiedResponse(file);

return result.toString();
}

@RequestMapping(method = RequestMethod.POST, params = "option=editLine", produces = "application/json;charset=UTF-8")
public String editLine(@RequestParam(value = "type") String type,
@RequestParam(value = "value") String value,
@RequestParam(value = "lineType") String lineType,
@RequestParam(value = "file") String file,
@RequestParam(value = "lineOfStart") int lineOfStart,
@RequestParam(value = "lineOfEnd") int lineOfEnd) throws Exception {

String line;
if(lineType.equals("enclosure")) {
line = "<" + type + " " + value + ">";
} else {
line = type + " " + value;
}

String originalContents = ConfFiles.replaceLinesInConfigFile(new File(file), new String[]{line}, lineOfStart, lineOfEnd);

Configuration.testChanges(file, originalContents);

JSONObject result = populateFileModifiedResponse(file);

return result.toString();
}

@RequestMapping(method = RequestMethod.POST, params = "option=addLine", produces = "application/json;charset=UTF-8")
public String addLine(@RequestParam(value = "type") String type,
@RequestParam(value = "value") String value,
@RequestParam(value = "beforeLineType") String beforeLineType,
@RequestParam(value = "lineType") String lineType,
@RequestParam(value = "file") String file,
@RequestParam(value = "lineOfStart") int lineOfStart) throws Exception {

String lines[];
if(lineType.equals("enclosure")) {
lines = new String[3];
lines[0] = "<" + type + " " + value + ">";
lines[1] = "";
lines[2] = "</" + type + ">";
} else {
lines = new String[1];
lines[0] = type + " " + value;
}

boolean useWhitespaceBefore = beforeLineType.equals("directive");

String originalContents = ConfFiles.writeToConfigFile(new File(file), lines, lineOfStart, useWhitespaceBefore);

Configuration.testChanges(file, originalContents);

JSONObject result = populateFileModifiedResponse(file);

return result.toString();
}

@RequestMapping(method = RequestMethod.POST, params = "option=addHost", produces = "application/json;charset=UTF-8")
public String addHost(@RequestParam(value = "hostAddress") String hostAddress,
@RequestParam(value = "port") String port,
Expand Down
3 changes: 0 additions & 3 deletions src/main/webapp/resources/net/apachegui/ConfigurationTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,6 @@ define([
return true;
});
configTree.on("showeditdialog", function() {
return true;
});
**/
onMenuFocus: function() {
return true;
Expand Down
161 changes: 100 additions & 61 deletions src/main/webapp/resources/net/apachegui/VirtualHosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define([ "dojo/_base/declare",
{
number: 0,
tree: __tree object__,
configTree: __config tree object__,
host: __host object,
lastModified : {
file: __file path__,
Expand All @@ -53,23 +54,16 @@ define([ "dojo/_base/declare",
disableEditing : false,

addHostFileSelect: null,

inputAutoSuggest: null,


initialized : false,

init : function() {
if (this.initialized === false) {
this.populateTreeVirtualHosts();
this.addListeners();
this.initializeAutoSuggest();
this.initialized = true;
}
},

initializeAutoSuggest: function() {
this.inputAutoSuggest = new InputAutoSuggest(dom.byId('addLineType'));
},

areHostsEqual : function(host1, host2) {

Expand Down Expand Up @@ -443,6 +437,10 @@ define([ "dojo/_base/declare",
registry.byId('editLineSubmit').set('disabled', true);
registry.byId('addLineSubmit').set('disabled', true);
registry.byId('addHostSubmit').set('disabled', true);

for(var i=0; i<this.virtualHosts.length; i++) {
this.virtualHosts[i].configTree.disableEditing();
}
},

deleteLine : function() {
Expand Down Expand Up @@ -655,6 +653,7 @@ define([ "dojo/_base/declare",

},

/**
showAddLineDialog : function(type) {
if(this.disableEditing) {
Expand Down Expand Up @@ -745,6 +744,7 @@ define([ "dojo/_base/declare",
});
},
**/

reloadAllTreeHosts : function(hosts) {

Expand Down Expand Up @@ -773,7 +773,7 @@ define([ "dojo/_base/declare",

},

reloadTreeHost : function(virtualHost) {
reloadTreeHost : function() {
var that = this;

var thisdialog = net.apachegui.Util.noCloseDialog('Loading', 'Loading Tree Host...');
Expand All @@ -789,30 +789,8 @@ define([ "dojo/_base/declare",
}).response.then(function(response) {
var data = response.data;

var hosts = data.hosts;

if (!!virtualHost) {

var host;
for (var i = 0; i < hosts.length; i++) {
host = hosts[i];

if (that.areHostsEqual(virtualHost.host, host)) {

virtualHost.tree.model.store = new ItemFileWriteStore({
data : host.tree
});

virtualHost.tree.reload();

that.reloadAllTreeHosts(hosts);

break;
}
}
} else {
var hosts = data.hosts;
that.reloadAllTreeHosts(hosts);
}

thisdialog.remove();
}, function(error) {
Expand All @@ -823,31 +801,57 @@ define([ "dojo/_base/declare",
},

loadVirtualHostTreeJSON: function(callback) {
request.get('../web/GlobalTree', {
query: {
option: 'getGlobalTree'
var that = this;

var thisdialog = net.apachegui.Util.noCloseDialog('Loading', 'Loading Tree Host...');
thisdialog.show();

var virtualHost = this.currentVirtualHost;

request.get('../web/VirtualHosts', {
query : {
option : 'getTreeHosts'
},
handleAs: 'json',
preventCache: true,
sync: false
}).response.then(function (response) {
var data = response.data;
var tree = data.tree;
callback(tree);
});
handleAs : 'json',
preventCache : true,
sync : false
}).response.then(function(response) {
var data = response.data;

var hosts = data.hosts;

var host;
for (var i = 0; i < hosts.length; i++) {
host = hosts[i];

if (that.areHostsEqual(virtualHost.host, host)) {

callback(host.tree);

that.reloadAllTreeHosts(hosts);

break;
}
}

thisdialog.remove();
}, function(error) {
thisdialog.remove();
net.apachegui.Util.alert('Info', error.response.data.message);
});

},

buildTreeHost : function(host, container, pos) {
var that = this;

var id = "tree_" + this.currentTreeSummaryCount;
var id = "tree-" + this.currentTreeSummaryCount;

var virtualHostTree = new ConfigurationTree({
id: id + '_widget',
treeJSON: host.tree,
rootType: 'VirtualHost',
loadTreeJSON: this.loadVirtualHostTreeJSON
loadTreeJSON: this.loadVirtualHostTreeJSON.bind(this)
});

virtualHostTree.startup();
Expand Down Expand Up @@ -938,6 +942,7 @@ define([ "dojo/_base/declare",
return {
number : num,
tree : virtualHostTree.tree,
configTree: virtualHostTree,
host : host,
lastModified : {
file : host.file,
Expand All @@ -955,6 +960,56 @@ define([ "dojo/_base/declare",
that.currentVirtualHost = virtualHost;
});
**/
virtualHostTree.on("menufocus", function() {
that.currentVirtualHost = virtualHost;
return true;
});

virtualHostTree.on("addDisabledError", function() {
that.showDisabledError();
return false;
});

virtualHostTree.on("editDisabledError", function() {
that.showDisabledError();
return false;
});

virtualHostTree.on("deleteDisabledError", function() {
that.showDisabledError();
return false;
});

virtualHostTree.on("beforeAddLine", function(type, value) {
if (type == "ServerName") {

var host = that.currentVirtualHost.host;
var currHost = that.getHost(value, host.NetworkInfo);
if (!!currHost) {
that.showHostExistsError(currHost);
return false;
}
}

return true;
});

virtualHostTree.on("afterAddLine", function(type, value, response) {
if (type == "ServerName") {
var host = that.currentVirtualHost.host;
host.ServerName = value;
dom.byId('heading-' + that.getCurrentTreeContainerId()).innerHTML = that.buildTreeHeadingFromHost(host);
that.buildTreeHostSelect();
}

var data = response.data;
that.updateLastModifiedTime(data.file, data.lastModifiedTime);
});

virtualHostTree.on("addLineError", function(type, value, response) {
that.updateLastModifiedTime(that.currentVirtualHost.host.file);
});

this.currentTreeSummaryCount++;

return div;
Expand Down Expand Up @@ -1368,22 +1423,6 @@ define([ "dojo/_base/declare",

addListeners : function() {
var that = this;

on(registry.byId('editLineSubmit'), 'click', function() {
that.submitEditLine();
});

on(registry.byId('editLineCancel'), 'click', function() {
registry.byId('editLineDialog').hide();
});

on(registry.byId('addLineSubmit'), 'click', function() {
that.submitAddLine();
});

on(registry.byId('addLineCancel'), 'click', function() {
registry.byId('addLineDialog').hide();
});

on(registry.byId('addHostButton'), 'click', function() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//TODO add check to see if global config changed and force refresh
define([
"dojo/_base/declare",
"dojo/request",
Expand Down

0 comments on commit 745647d

Please sign in to comment.