--- /dev/null
+require('shelljs/global');
+
+// "constants"
+var constants = {
+ opendaylight : {
+ source : pwd()+'/../main/src/main/resources',
+ target : pwd()+'/../main/target/main-osgipackage/opendaylight'
+ }
+};
+constants.opendaylight.cors = {
+ source : constants.opendaylight.source+'/cors.json',
+ target : constants.opendaylight.target+'/cors.json'
+}
+var cors = {};
+
+// help
+var help = 'Usage: node provision.js [ --scaffold <your-ip> | --teardown | --status ]' +
+ '\n\n' +
+ 'Options:' +
+ '\n -s, --scaffold <your-ip> setup cors filter with <your-ip> address' +
+ '\n -t, --teardown tear down cors filter' +
+ '\n -a, --status display current state' +
+ '\n -h, --help display this help';
+
+// basic args check
+if (process.argv.length <= 2) {
+ console.log(help);
+ return;
+}
+
+// help args check
+if (process.argv.length === 3
+ && (process.argv[2] == '-h'
+ || process.argv[2] == '--help')) {
+ console.log(help);
+ return;
+}
+
+// status
+function statusCheck() {
+ if (test('-f', constants.opendaylight.cors.target)) {
+ console.log('Scaffold setup in '+constants.opendaylight.cors.target);
+ } else if (test('-f', constants.opendaylight.cors.source)) {
+ console.log('Scaffold setup in '+constants.opendaylight.cors.source);
+ } else {
+ console.log('Currently not scaffolded');
+ }
+}
+
+if (process.argv.length === 3
+ && (process.argv[2] == '-a'
+ || process.argv[2] == '--status')) {
+ statusCheck();
+ return;
+}
+
+// scaffold
+function scaffold(ip) {
+ cors.address = ip;
+ // check if cors already exists
+ if (test('-f', constants.opendaylight.cors.target)
+ || test('-f', constants.opendaylight.cors.source)) {
+ console.log('Scaffolding already exists');
+ return;
+ }
+ // scaffold
+ if (test('-d', constants.opendaylight.target)) {
+ JSON.stringify(cors).to(constants.opendaylight.cors.target);
+ console.log('Scaffolding initialized in '+constants.opendaylight.cors.target);
+ } else if (test('-d', constants.opendaylight.source)) {
+ JSON.stringify(cors).to(constants.opendaylight.cors.source);
+ console.log('Scaffolding initialized in '+constants.opendaylight.cors.source);
+ } else {
+ console.log('Error: Unable to initialize scaffolding; your distribution directory doesn\'t seem to exist');
+ }
+}
+
+if (process.argv.length === 3
+ && (process.argv[2] == '-s'
+ || process.argv[2] == '--scaffold')) {
+ console.log('Error: specify your IP address');
+ return;
+}
+
+if (process.argv.length === 4
+ && (process.argv[2] == '-s'
+ || process.argv[2] == '--scaffold')) {
+ scaffold(process.argv[3]);
+ return;
+}
+
+// teardown
+function teardown() {
+ if (test('-f', constants.opendaylight.cors.target)
+ || test('-f', constants.opendaylight.cors.source)) {
+ rm('-f', constants.opendaylight.cors.target);
+ rm('-f', constants.opendaylight.cors.source);
+ console.log('Teardown scaffold');
+ } else {
+ console.log('Currently not scaffolded');
+ }
+}
+
+if (process.argv.length === 3
+ && (process.argv[2] == '-t'
+ || process.argv[2] == '--teardown')) {
+ teardown();
+ return;
+}
+
+// assume failed state (invalid args)
+console.log('Error: unrecognized flag '+process.argv[2]+
+'\nTry --help for options');
+++ /dev/null
-// Enter all relative paths to your app's files / directories here
-
-var appname = 'simpleappfoo';
-
-var Config = {
- index: "../" + appname + "/src/main/resources/WEB-INF/jsp/main.jsp",
- basepath: "../web/src/main/resources/", // common web bundle assets
- apppath: "../" + appname + "/src/main/resources/", // app bundle assets
- appdir_to_remove: appname + "/web",
-
-
-}
-
-module.exports = Config;
+++ /dev/null
-var url = require('url'),
- path = require('path'),
- fs = require('fs'),
- mime = require('mime'),
- config = require('./config');
-
-
-
-String.prototype.endsWith = function(suffix) {
- return this.indexOf(suffix, this.length - suffix.length) !== -1;
-};
-
-var Static = function(request, response) {
- var uri = url.parse(request.url).pathname;
- var cwd = process.cwd();
- var found = false;
-
- if (uri.endsWith("/")) {
- if (uri == "/") {
- found = true;
- return returnFile(request, response, path.join(cwd, config.index), "text/html");
- } else {
- uri = uri + "index.html";
- }
- }
-
- var fn = "";
- if (uri.indexOf("/" + config.appdir_to_remove) > -1) {
- uri = uri.replace("/" + config.appdir_to_remove, "");
- fn = path.join(path.join(cwd, config.apppath), uri);
- } else {
- fn = path.join(path.join(cwd, config.basepath), uri);
- }
-
- if (fs.existsSync(fn)) {
- return returnFile(request, response, fn);
- } else {
-
- console.log("404 - " + uri);
- response.writeHead(404, {"Content-Type": "text/plain"});
- response.write("404 Not Found\n");
- response.end();
- }
-}
-
-function returnFile(request, response, filename, mimetype) {
-
- var mimetyp = typeof mimetype !== 'undefined' ? mimetype : mime.lookup(filename);
-
- fs.readFile(filename, "binary", function(err, file) {
- if(err) {
- console.log("500 " + filename);
- response.writeHead(500, {"Content-Type": "text/plain"});
- response.write(err + "\n");
- response.end();
- return;
- }
-
- console.log("200 " + filename);
- response.writeHead(200, {
- "Content-Type": mimetyp,
- "Access-Control-Allow-Origin": "*",
- });
- response.write(file, "binary");
- response.end();
- });
-
-}
-module.exports = Static;
</Bundle-Activator>
</instructions>
<manifestLocation>${project.basedir}/src/main/resources/META-INF</manifestLocation>
- <buildDirectory>../main/target/main-osgipackage/opendaylight/plugins/</buildDirectory> <!-- TODO use pom var -->
+ <buildDirectory>../../main/target/main-osgipackage/opendaylight/plugins/</buildDirectory> <!-- TODO use pom var -->
</configuration>
</plugin>
</plugins>
--- /dev/null
+// Enter all relative paths to your app's files / directories here
+
+// TODO andrew - init app name from file
+
+var appname = 'simple';
+
+var Config = {
+ index: '../../samples/' + appname + '/src/main/resources/WEB-INF/jsp/main.jsp',
+ basepath: '../../web/src/main/resources/', // common web bundle assets
+ apppath: '../../samples/' + appname + '/src/main/resources/', // app bundle assets
+ appdir_to_remove: appname + '/web',
+}
+
+module.exports = Config;
--- /dev/null
+var url = require('url'),
+path = require('path'),
+fs = require('fs'),
+mime = require('mime'),
+config = require('./config');
+
+// functions
+String.prototype.endsWith = function(suffix) {
+ return this.indexOf(suffix, this.length - suffix.length) !== -1;
+};
+
+function returnFile(request, response, filename, mimetype) {
+ var mimetyp = typeof mimetype !== 'undefined' ? mimetype : mime.lookup(filename);
+
+ fs.readFile(filename, 'binary', function(err, file) {
+ if (err) {
+ console.log('500 ' + filename);
+ response.writeHead(500, {'Content-Type': 'text/plain'});
+ response.write(err + "\n");
+ response.end();
+ return;
+ }
+
+ console.log('200 ' + filename);
+ response.writeHead(200, {
+ 'Content-Type': mimetyp,
+ 'Access-Control-Allow-Origin': '*',
+ });
+
+ response.write(file, 'binary');
+ response.end();
+ });
+}
+
+// Static
+var Static = function(request, response) {
+ var uri = url.parse(request.url).pathname;
+ var cwd = process.cwd();
+ var found = false;
+
+ if (uri.endsWith('/')) {
+ if (uri == '/') {
+ found = true;
+ return returnFile(request, response, path.join(cwd, config.index), 'text/html');
+ } else {
+ uri += 'index.html';
+ }
+ }
+
+ var fn = '';
+ if (uri.indexOf('/' + config.appdir_to_remove) > -1) {
+ uri = uri.replace('/' + config.appdir_to_remove, '');
+ fn = path.join(path.join(cwd, config.apppath), uri);
+ } else {
+ fn = path.join(path.join(cwd, config.basepath), uri);
+ }
+
+ if (fs.existsSync(fn)) {
+ return returnFile(request, response, fn);
+ } else {
+ console.log('404 ' + uri);
+ response.writeHead(404, {'Content-Type': 'text/plain'});
+ response.write('404 Not Found\n');
+ response.end();
+ }
+}
+
+module.exports = Static;
org.opendaylight.controller.connectionmanager,
org.opendaylight.controller.switchmanager,
com.google.gson,
+ com.google.gson.reflect,
javax.annotation,
javax.naming,
javax.servlet,
package org.opendaylight.toolkit.web;
import java.io.IOException;
+import java.lang.reflect.Type;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.opendaylight.toolkit.web.bean.CorsBean;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+
/**
- * Temporary workaround because built-in tomcat cors filter
- * isn't working at the moment
+ * CORS Filter for easier web application development
*
* @author Andrew Kim
*
*/
public class CorsFilter implements Filter {
public static String VALID_METHODS = "DELETE, HEAD, GET, OPTIONS, POST, PUT";
+ private static Type CORS_BEAN = new TypeToken<CorsBean>(){}.getType();
+ private static Gson gson = new Gson();
@Override
public void destroy() {
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
- /* Uncomment next line */
- //response.setHeader("Access-Control-Allow-Origin", "http://<<your-ip>>:8000");
- response.setHeader("Access-Control-Allow-Credentials", "true");
- response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
- response.setHeader("Access-Control-Max-Age", "3600");
- response.setHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers");
+
+ // determine allowed origin
+ StringBuffer http = new StringBuffer("http://");
+ String allowedOrigin = "localhost";
+ StringBuffer path = new StringBuffer(System.getProperty("user.dir"));
+ path.append("/cors.json");
+ try {
+ String corsBean = readFile(path.toString());
+ CorsBean bean = gson.fromJson(corsBean, CORS_BEAN);
+ allowedOrigin = bean.getAddress();
+ http.append(allowedOrigin).append(":8000");
+ response.setHeader("Access-Control-Allow-Origin", http.toString());
+
+ // everything else
+ response.setHeader("Access-Control-Allow-Credentials", "true");
+ response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
+ response.setHeader("Access-Control-Max-Age", "3600");
+ response.setHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers");
+ } catch (IOException e) {
+ // just skip cors if no cors.json found
+ //System.err.println("Failed to find cors.json in "+path.toString()+" defaulting to localhost");
+ }
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
+
+ private String readFile(String path) throws IOException {
+ byte[] encoded = Files.readAllBytes(Paths.get(path));
+ return new String(encoded, Charset.defaultCharset());
+ }
}
--- /dev/null
+package org.opendaylight.toolkit.web.bean;
+
+import java.io.Serializable;
+
+public class CorsBean implements Serializable {
+ private static final long serialVersionUID = 1L;
+ private String address;
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+}