use of org.openremote.model.AbstractValueHolder in project openremote by openremote.
the class Protocol method getLinkedAttributeMessageFilters.
/**
* Extract the {@link MessageFilter}s from the specified {@link Attribute}
*/
static Optional<List<MessageFilter>> getLinkedAttributeMessageFilters(Attribute attribute) {
if (attribute == null) {
return Optional.empty();
}
Optional<ArrayValue> arrayValueOptional = attribute.getMetaItem(META_PROTOCOL_FILTERS).flatMap(AbstractValueHolder::getValueAsArray);
if (!arrayValueOptional.isPresent()) {
return Optional.empty();
}
try {
ArrayValue arrayValue = arrayValueOptional.get();
List<MessageFilter> messageFilters = new ArrayList<>(arrayValue.length());
for (int i = 0; i < arrayValue.length(); i++) {
ObjectValue objValue = arrayValue.getObject(i).orElseThrow(() -> new IllegalArgumentException("Attribute protocol filters meta item is invalid"));
MessageFilter filter = deserialiseMessageFilter(objValue);
messageFilters.add(filter);
}
return messageFilters.isEmpty() ? Optional.empty() : Optional.of(messageFilters);
} catch (IllegalArgumentException e) {
LOG.log(Level.WARNING, e.getMessage(), e);
}
return Optional.empty();
}
use of org.openremote.model.AbstractValueHolder in project openremote by openremote.
the class AbstractHttpServerProtocol method createDeployment.
protected ResteasyDeployment createDeployment(Application application, AssetAttribute protocolConfiguration) {
ResteasyDeployment resteasyDeployment = new ResteasyDeployment();
resteasyDeployment.setApplication(application);
List<String> allowedOrigins;
if (devMode) {
allowedOrigins = Collections.singletonList("*");
} else {
Optional<MetaItem> allowedOriginsMeta = protocolConfiguration.getMetaItem(META_PROTOCOL_ALLOWED_ORIGINS);
allowedOrigins = allowedOriginsMeta.flatMap(AbstractValueHolder::getValueAsString).map(Collections::singletonList).orElseGet(() -> allowedOriginsMeta.flatMap(AbstractValueHolder::getValueAsArray).flatMap(arrayValue -> Values.getArrayElements(arrayValue, StringValue.class, true, false, StringValue::getString)).orElse(null));
}
if (allowedOrigins != null) {
String allowedMethods = protocolConfiguration.getMetaItem(META_PROTOCOL_ALLOWED_METHODS).flatMap(AbstractValueHolder::getValueAsString).orElse(DEFAULT_ALLOWED_METHODS);
if (TextUtil.isNullOrEmpty(allowedMethods)) {
throw new IllegalArgumentException("Allowed methods meta item must be a non empty string: " + META_PROTOCOL_ALLOWED_METHODS);
}
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().addAll(allowedOrigins);
corsFilter.setAllowedMethods(allowedMethods);
resteasyDeployment.getProviders().add(corsFilter);
}
return resteasyDeployment;
}
use of org.openremote.model.AbstractValueHolder in project openremote by openremote.
the class HttpClientProtocol method doLinkAttribute.
@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
AttributeRef protocolConfigurationRef = protocolConfiguration.getReferenceOrThrow();
Pair<ResteasyWebTarget, List<Integer>> clientAndFailureCodes;
clientAndFailureCodes = clientMap.get(protocolConfigurationRef);
WebTarget client = clientAndFailureCodes != null ? clientAndFailureCodes.key : null;
if (client == null) {
LOG.warning("Attempt to link attribute to non existent protocol configuration: " + attribute.getReferenceOrThrow());
return;
}
String method = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_METHOD, StringValue.class, false, true).map(StringValue::getString).orElse(DEFAULT_HTTP_METHOD);
String path = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_PATH, StringValue.class, false, true).map(StringValue::getString).orElse(null);
String contentType = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_CONTENT_TYPE, StringValue.class, false, true).map(StringValue::getString).orElse(null);
Value body = attribute.getMetaItem(META_ATTRIBUTE_BODY).flatMap(AbstractValueHolder::getValue).orElse(null);
List<Integer> failureCodes = attribute.getMetaItem(META_FAILURE_CODES).flatMap(AbstractValueHolder::getValueAsArray).flatMap(arrayValue -> Values.getArrayElements(arrayValue, NumberValue.class, true, false, number -> Values.getIntegerCoerced(number).orElse(null))).map(fCodes -> {
if (clientAndFailureCodes.value != null) {
fCodes.addAll(clientAndFailureCodes.value);
}
return fCodes;
}).orElseGet(() -> {
if (clientAndFailureCodes.value != null) {
return clientAndFailureCodes.value;
}
return null;
});
MultivaluedMap<String, String> headers = Values.getMetaItemValueOrThrow(attribute, META_HEADERS, ObjectValue.class, false, true).flatMap(objectValue -> getMultivaluedMap(objectValue, true)).orElse(null);
MultivaluedMap<String, String> queryParams = Values.getMetaItemValueOrThrow(attribute, META_QUERY_PARAMETERS, ObjectValue.class, false, true).flatMap(objectValue -> getMultivaluedMap(objectValue, false)).orElse(null);
Optional<Integer> pollingSeconds = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_POLLING_SECONDS, NumberValue.class, false, true).map(polling -> Values.getIntegerCoerced(polling).map(seconds -> seconds < 1 ? null : seconds).orElseThrow(() -> new IllegalArgumentException("Polling seconds meta item must be an integer >= 1")));
final AttributeRef attributeRef = attribute.getReferenceOrThrow();
boolean updateConnectionStatus = !pollingMap.containsKey(protocolConfigurationRef);
HttpClientRequest clientRequest = buildClientRequest(client, path, method, headers, queryParams, failureCodes, updateConnectionStatus, body, contentType);
LOG.fine("Creating HTTP request for linked attribute '" + clientRequest + "': " + attributeRef);
requestMap.put(attributeRef, clientRequest);
pollingSeconds.ifPresent(seconds -> pollingMap.put(attributeRef, schedulePollingRequest(attributeRef, protocolConfigurationRef, clientRequest, seconds)));
}
use of org.openremote.model.AbstractValueHolder in project openremote by openremote.
the class HttpClientProtocol method doLinkProtocolConfiguration.
@Override
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
if (!protocolConfiguration.isEnabled()) {
updateStatus(protocolRef, ConnectionStatus.DISABLED);
return;
}
String baseUri = protocolConfiguration.getMetaItem(META_PROTOCOL_BASE_URI).flatMap(AbstractValueHolder::getValueAsString).orElseThrow(() -> new IllegalArgumentException("Missing or invalid require meta item: " + META_PROTOCOL_BASE_URI));
/* We're going to fail hard and fast if optional meta items are incorrectly configured */
Optional<OAuthGrant> oAuthGrant = getOAuthGrant(protocolConfiguration);
Optional<StringValue> username = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_USERNAME, StringValue.class, false, true);
Optional<StringValue> password = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_PASSWORD, StringValue.class, false, true);
boolean followRedirects = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_FOLLOW_REDIRECTS, BooleanValue.class, false, true).map(BooleanValue::getBoolean).orElse(false);
List<Integer> failureCodes = protocolConfiguration.getMetaItem(META_FAILURE_CODES).flatMap(AbstractValueHolder::getValueAsArray).flatMap(arrayValue -> Values.getArrayElements(arrayValue, NumberValue.class, true, false, number -> Values.getIntegerCoerced(number).orElse(null))).orElse(null);
Optional<MultivaluedMap<String, String>> headers = Values.getMetaItemValueOrThrow(protocolConfiguration, META_HEADERS, ObjectValue.class, false, true).flatMap(objectValue -> getMultivaluedMap(objectValue, true));
Optional<MultivaluedMap<String, String>> queryParams = Values.getMetaItemValueOrThrow(protocolConfiguration, META_QUERY_PARAMETERS, ObjectValue.class, false, true).flatMap(objectValue -> getMultivaluedMap(objectValue, false));
String pingPath = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_PING_PATH, StringValue.class, false, true).map(StringValue::getString).orElse(null);
if ((username.isPresent() && !password.isPresent()) || (!username.isPresent() && password.isPresent())) {
throw new IllegalArgumentException("Both username and password must be set for basic authentication");
}
WebTargetBuilder webTargetBuilder = new WebTargetBuilder(baseUri);
if (oAuthGrant.isPresent()) {
LOG.info("Adding OAuth");
webTargetBuilder.setOAuthAuthentication(oAuthGrant.get());
} else {
// noinspection ConstantConditions
username.ifPresent(stringValue -> {
LOG.info("Adding Basic Authentication");
webTargetBuilder.setBasicAuthentication(stringValue.getString(), password.get().getString());
});
}
headers.ifPresent(webTargetBuilder::setInjectHeaders);
queryParams.ifPresent(webTargetBuilder::setInjectQueryParameters);
webTargetBuilder.followRedirects(followRedirects);
LOG.fine("Creating web target client '" + baseUri + "'");
ResteasyWebTarget client = webTargetBuilder.build();
clientMap.put(protocolRef, new Pair<>(client, failureCodes));
updateStatus(protocolRef, ConnectionStatus.UNKNOWN);
if (pingPath == null) {
return;
}
String pingMethod = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_PING_METHOD, StringValue.class, false, true).map(StringValue::getString).orElse(DEFAULT_HTTP_METHOD);
Value pingBody = protocolConfiguration.getMetaItem(META_PROTOCOL_PING_BODY).flatMap(AbstractValueHolder::getValue).orElse(null);
MultivaluedMap<String, String> pingQueryParams = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_PING_QUERY_PARAMETERS, ObjectValue.class, false, true).flatMap(objectValue -> getMultivaluedMap(objectValue, false)).orElse(null);
Integer pingPollingSeconds = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_PING_SECONDS, NumberValue.class, false, true).map(polling -> Values.getIntegerCoerced(polling).map(seconds -> seconds < 1 ? null : seconds).orElseThrow(() -> new IllegalArgumentException("Ping polling seconds meta item must be an integer >= 1"))).orElse(DEFAULT_PING_SECONDS);
String contentType = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_PING_CONTENT_TYPE, StringValue.class, false, true).map(StringValue::getString).orElse(null);
HttpClientRequest pingRequest = buildClientRequest(client, pingPath, pingMethod, null, pingQueryParams, null, true, pingBody, contentType);
LOG.info("Creating ping polling request '" + pingRequest + "'");
requestMap.put(protocolRef, pingRequest);
pollingMap.put(protocolRef, schedulePollingRequest(null, protocolRef, pingRequest, pingPollingSeconds));
}
use of org.openremote.model.AbstractValueHolder in project openremote by openremote.
the class UpnpProtocol method doLinkProtocolConfiguration.
@Override
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
boolean isEnabled = protocolConfiguration.isEnabled();
if (!isEnabled) {
updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISABLED);
return;
}
protocolConfiguration.getAssetId().ifPresent(agentId -> {
// The parent is either the configured asset to group all discovered devices or the agent
protocolConfigsGroupAssetId.put(protocolConfiguration.getReferenceOrThrow(), protocolConfiguration.getMetaItem(GROUP_ASSET_ID).flatMap(AbstractValueHolder::getValueAsString).orElse(agentId));
Collection<Device> devices = getService().getRegistry().getDevices();
updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.CONNECTED);
if (devices.size() > 0) {
for (Device device : getService().getRegistry().getDevices()) {
storeAssets(protocolConfiguration, device);
}
}
});
}
Aggregations