/
RestAPIv3.java
149 lines (127 loc) · 5.94 KB
/
RestAPIv3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
*
* Copyright (c) 2009-2022 Freedomotic Team http://www.freedomotic-platform.com
*
* This file is part of Freedomotic
*
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Freedomotic; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
package com.freedomotic.plugins.devices.restapiv3;
import com.freedomotic.api.EventTemplate;
import com.freedomotic.api.Protocol;
import com.freedomotic.events.MessageEvent;
import com.freedomotic.events.ObjectHasChangedBehavior;
import com.freedomotic.events.PluginHasChanged;
import com.freedomotic.events.ZoneHasChanged;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereEventResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereMessageCalloutResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereObjectChangeResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmospherePluginChangeResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereZoneChangeResource;
import com.freedomotic.reactions.Command;
import java.net.URI;
import javax.inject.Inject;
import javax.ws.rs.core.UriBuilder;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RestAPIv3 extends Protocol {
private static final Logger LOG = LoggerFactory.getLogger(RestAPIv3.class.getName());
private static final String RESOURCE_PKG = "com.freedomotic.plugins.devices.restapiv3.resources";
public static final String JERSEY_RESOURCE_PKG = RESOURCE_PKG + ".jersey";
public static final String ATMOSPHRE_RESOURCE_PKG = RESOURCE_PKG + ".atmosphere";
public static URI BASE_URI;
public static final String API_VERSION = "v3";
@Inject
private RestJettyServer jServer;
@Inject
private AtmosphereObjectChangeResource atmosphereObjectChangeResource;
@Inject
private AtmosphereZoneChangeResource atmosphereZoneChangeResource;
@Inject
private AtmospherePluginChangeResource atmospherePluginChangeResource;
@Inject
private AtmosphereMessageCalloutResource atmosphereMessageCalloutResource;
@Inject
private AtmosphereEventResource atmosphereEventResource;
// Hold a preconfigurd static web security manager which can be used by Shiro
public static DefaultWebSecurityManager defaultWebSecurityManager;
public RestAPIv3() {
super("RestAPI v3", "/restapi-v3/restapiv3-manifest.xml");
setPollingWait(-1);
// Create the preconfigured security manager
createDefaultWebSecurityManager();
}
@Override
protected void onRun() {
}
@Override
protected void onStart() {
setDescription("Plugin is starting...");
String protocol = configuration.getBooleanProperty("enable-ssl", false) ? "https" : "http";
int port = configuration.getBooleanProperty("enable-ssl", false) ? configuration.getIntProperty("https-port", 9113) : configuration.getIntProperty("http-port", 9111);
BASE_URI = UriBuilder.fromUri(protocol + "://" + configuration.getStringProperty("listen-address", "localhost") + "/").path(API_VERSION).port(port).build();
LOG.info("RestAPI v3 plugin is started at {}", BASE_URI);
try {
jServer.setMaster(this);
jServer.startServer();
setDescription("API is available at " + BASE_URI.toString().substring(0, BASE_URI.toString().length() - 2));
} catch (Exception ex) {
LOG.error(ex.getMessage());
}
addEventListener("app.event.sensor.object.behavior.change");
addEventListener("app.event.sensor.environment.zone.change");
addEventListener("app.event.sensor.plugin.change");
addEventListener("app.event.sensor.messages.callout");
addEventListener("app.event.sensor.command.change");
addEventListener("app.event.sensor.reaction.change");
addEventListener("app.event.sensor.trigger.change");
}
@Override
protected void onStop() {
LOG.info("RestAPI v3 plugin is stopped ");
setDescription("Plugin stopped");
try {
jServer.stopServer();
} catch (Exception ex) {
LOG.error(ex.getMessage());
}
}
@Override
protected void onCommand(Command c) {
LOG.info("RestAPI v3 plugin receives a command called {} with parameters {}", new Object[]{c.getName(), c.getProperties().toString()});
}
@Override
protected boolean canExecute(Command c) {
//don't mind this method for now
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
protected void onEvent(EventTemplate event) {
if (event instanceof ObjectHasChangedBehavior) {
atmosphereObjectChangeResource.broadcast(event);
} else if (event instanceof ZoneHasChanged) {
atmosphereZoneChangeResource.broadcast(event);
} else if (event instanceof PluginHasChanged) {
atmospherePluginChangeResource.broadcast(event);
} else if (event instanceof MessageEvent) {
atmosphereMessageCalloutResource.broadcast(event);
}
// broadcast all events to AtmosphereEventResource
atmosphereEventResource.broadcast(event);
}
public final void createDefaultWebSecurityManager() {
defaultWebSecurityManager = new DefaultWebSecurityManager(this.getApi().getAuth().getUserRealm());
}
}