Updated handling to be CORS compatible.

This commit is contained in:
Admin
2015-11-13 11:40:13 -06:00
parent 12eab16f21
commit e3f5946c9d
3 changed files with 57 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package com.bwssystems.HABridge.devicemanagmeent; package com.bwssystems.HABridge.devicemanagmeent;
import static spark.Spark.get; import static spark.Spark.get;
import static spark.Spark.options;
import static spark.Spark.post; import static spark.Spark.post;
import static spark.Spark.put; import static spark.Spark.put;
import static spark.Spark.delete; import static spark.Spark.delete;
@@ -52,6 +53,15 @@ public class DeviceResource {
private void setupEndpoints() { private void setupEndpoints() {
log.info("HABridge device management service started.... "); log.info("HABridge device management service started.... ");
// http://ip_address:port/api/devices CORS request
options(API_CONTEXT, "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post(API_CONTEXT, "application/json", (request, response) -> { post(API_CONTEXT, "application/json", (request, response) -> {
log.debug("Create a Device - request body: " + request.body()); log.debug("Create a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class); DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
@@ -67,11 +77,21 @@ public class DeviceResource {
deviceRepository.save(device); deviceRepository.save(device);
log.debug("Created a Device: " + request.body()); log.debug("Created a Device: " + request.body());
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.status(HttpStatus.SC_CREATED); response.status(HttpStatus.SC_CREATED);
return device; return device;
}, new JsonTransformer()); }, new JsonTransformer());
// http://ip_address:port/api/devices/:id CORS request
options(API_CONTEXT + "/:id", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
put (API_CONTEXT + "/:id", "application/json", (request, response) -> { put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Edit a Device - request body: " + request.body()); log.debug("Edit a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class); DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);

View File

@@ -16,6 +16,7 @@ import com.google.gson.Gson;
import net.java.dev.eval.Expression; import net.java.dev.eval.Expression;
import static spark.Spark.get; import static spark.Spark.get;
import static spark.Spark.options;
import static spark.Spark.post; import static spark.Spark.post;
import static spark.Spark.put; import static spark.Spark.put;
@@ -85,6 +86,15 @@ public class HueMulator {
return deviceResponseMap; return deviceResponseMap;
} , new JsonTransformer()); } , new JsonTransformer());
// http://ip_address:port/api CORS request
options(HUE_CONTEXT, "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/api with body of user request returns json object for a success of user add // http://ip_address:port/api with body of user request returns json object for a success of user add
post(HUE_CONTEXT, "application/json", (request, response) -> { post(HUE_CONTEXT, "application/json", (request, response) -> {
UserCreateRequest aNewUser = null; UserCreateRequest aNewUser = null;
@@ -105,11 +115,21 @@ public class HueMulator {
aDeviceType = "<not given>"; aDeviceType = "<not given>";
log.debug("hue api user create requested for device type: " + aDeviceType + " and username: " + newUser); log.debug("hue api user create requested for device type: " + aDeviceType + " and username: " + newUser);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);
return "[{\"success\":{\"username\":\"" + newUser + "\"}}]"; return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
} ); } );
// http://ip_address:port/api/* CORS request
options(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/api/* with body of user request returns json object for a success of user add - This method is for Harmony Hub // http://ip_address:port/api/* with body of user request returns json object for a success of user add - This method is for Harmony Hub
post(HUE_CONTEXT + "/*", "application/json", (request, response) -> { post(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
UserCreateRequest aNewUser = null; UserCreateRequest aNewUser = null;
@@ -130,6 +150,7 @@ public class HueMulator {
aDeviceType = "<not given>"; aDeviceType = "<not given>";
log.debug("HH trace: hue api user create requested for device type: " + aDeviceType + " and username: " + newUser); log.debug("HH trace: hue api user create requested for device type: " + aDeviceType + " and username: " + newUser);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);
return "[{\"success\":{\"username\":\"" + newUser + "\"}}]"; return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
@@ -178,6 +199,15 @@ public class HueMulator {
return lightResponse; return lightResponse;
}, new JsonTransformer()); }, new JsonTransformer());
// http://ip_address:port/api/:userid/lights/:id/state CORS request
options(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/api/{userId}/lights/{lightId}/state uses json object to set the lights state // http://ip_address:port/api/{userId}/lights/{lightId}/state uses json object to set the lights state
put(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> { put(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> {
/** /**
@@ -260,6 +290,7 @@ public class HueMulator {
} }
} }
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);
return responseString; return responseString;

View File

@@ -103,8 +103,7 @@ app.service('bridgeService', function ($http, $window, BridgeSettings) {
if (error.data) { if (error.data) {
self.state.error = error.data.message; self.state.error = error.data.message;
} else { } else {
self.state.error = "If you're not seeing any devices, you may be running into problems with CORS. " + self.state.error = "Some error occurred.";
"You can work around this by running a fresh launch of Chrome with the --disable-web-security flag.";
} }
console.log(error); console.log(error);
} }