/** * Gets the endpoint name of the context's resource. * * @return The endpoint name. */ String getEndpoint() { return resource.getEndpoint(); }
private Endpoint getEndpoint(final ResourceIdentifier targetAddress) { return endpoints.get(targetAddress.getEndpoint()); }
/** * Gets the name of the endpoint that the message has been published to. * * @return The name or {@code null} if the endpoint cannot * be determined from the message's topic. */ public String endpoint() { return Optional.ofNullable(topic).map(t -> t.getEndpoint()).orElse(null); } }
/** * Gets the canonical name of the endpoint that the * message has been published to. * <p> * The name is determined from the endpoint path segment of the * topic that the message has been published to. * * @return The endpoint name or {@code null} if the message does not * contain a topic. */ public String endpoint() { return Optional.ofNullable(topic) .map(t -> t.getEndpoint()) .orElse(null); }
/** * Gets the endpoint registered for handling a specific target address. * * @param targetAddress The address. * @return The endpoint for handling the address or {@code null} if no endpoint is registered * for the target address. */ protected final AmqpEndpoint getEndpoint(final ResourceIdentifier targetAddress) { return getEndpoint(targetAddress.getEndpoint()); }
/** * Gets the endpoint registered for handling a specific target address. * * @param targetAddress The address. * @return The endpoint for handling the address or {@code null} if no endpoint is registered * for the target address. */ protected final AmqpEndpoint getEndpoint(final ResourceIdentifier targetAddress) { return getEndpoint(targetAddress.getEndpoint()); }
private void createStringRepresentation() { resource = createStringRepresentation(0); final StringBuilder b = new StringBuilder(getEndpoint()); if (getTenantId() != null) { b.append("/").append(getTenantId()); } basePath = b.toString(); }
private void createStringRepresentation() { resource = createStringRepresentation(0); final StringBuilder b = new StringBuilder(getEndpoint()); if (getTenantId() != null) { b.append("/").append(getTenantId()); } basePath = b.toString(); }
private static String getTenantOnlyTargetAddress(final String address) { ResourceIdentifier targetAddress = ResourceIdentifier.fromString(address); return String.format("%s/%s", targetAddress.getEndpoint(), targetAddress.getTenantId()); }
private ResourceIdentifier(final ResourceIdentifier resourceIdentifier, final String tenantId, final String resourceId) { String[] path = resourceIdentifier.getResourcePath(); if (path.length < 3) { path = new String[3]; path[IDX_ENDPOINT] = resourceIdentifier.getEndpoint(); } path[IDX_TENANT_ID] = tenantId; path[IDX_RESOURCE_ID] = resourceId; setResourcePath(path); }
private ResourceIdentifier(final ResourceIdentifier resourceIdentifier, final String tenantId, final String resourceId) { String[] path = resourceIdentifier.getResourcePath(); if (path.length < 3) { path = new String[3]; path[IDX_ENDPOINT] = resourceIdentifier.getEndpoint(); } path[IDX_TENANT_ID] = tenantId; path[IDX_RESOURCE_ID] = resourceId; setResourcePath(path); }
@Override public boolean isAuthorized(final ResourceIdentifier resource, final Activity intent) { boolean allowed = false; if (resource.getResourceId() != null) { allowed = isAuthorized(String.format(resTemplate, resource.toString()), intent); } if (!allowed && resource.getTenantId() != null) { allowed = isAuthorized(String.format(resTemplate, resource.getEndpoint() + "/" + resource.getTenantId()), intent) || isAuthorized(String.format(resTemplate, resource.getEndpoint() + "/*"), intent); } if (!allowed) { allowed = isAuthorized(String.format(resTemplate, resource.getEndpoint()), intent) || isAuthorized(String.format(resTemplate, "*"), intent); } return allowed; }
@Override public boolean isAuthorized(final ResourceIdentifier resource, final Activity intent) { boolean allowed = false; if (resource.getResourceId() != null) { allowed = isAuthorized(String.format(resTemplate, resource.toString()), intent); } if (!allowed && resource.getTenantId() != null) { allowed = isAuthorized(String.format(resTemplate, resource.getEndpoint() + "/" + resource.getTenantId()), intent) || isAuthorized(String.format(resTemplate, resource.getEndpoint() + "/*"), intent); } if (!allowed) { allowed = isAuthorized(String.format(resTemplate, resource.getEndpoint()), intent) || isAuthorized(String.format(resTemplate, "*"), intent); } return allowed; }
@Override public boolean isAuthorized(final ResourceIdentifier resource, final String operation) { boolean allowed = false; if (resource.getResourceId() != null) { allowed = isAuthorized(String.format(opTemplate, resource.toString(), operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.toString(), "*"), Activity.EXECUTE); } if (!allowed && resource.getTenantId() != null) { allowed = isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/" + resource.getTenantId(), operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/" + resource.getTenantId(), "*"), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/*", operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/*", "*"), Activity.EXECUTE); } if (!allowed) { allowed = isAuthorized(String.format(opTemplate, resource.getEndpoint(), operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint(), "*"), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, "*", operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, "*", "*"), Activity.EXECUTE); } return allowed; }
@Override public boolean isAuthorized(final ResourceIdentifier resource, final String operation) { boolean allowed = false; if (resource.getResourceId() != null) { allowed = isAuthorized(String.format(opTemplate, resource.toString(), operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.toString(), "*"), Activity.EXECUTE); } if (!allowed && resource.getTenantId() != null) { allowed = isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/" + resource.getTenantId(), operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/" + resource.getTenantId(), "*"), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/*", operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint() + "/*", "*"), Activity.EXECUTE); } if (!allowed) { allowed = isAuthorized(String.format(opTemplate, resource.getEndpoint(), operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, resource.getEndpoint(), "*"), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, "*", operation), Activity.EXECUTE) || isAuthorized(String.format(opTemplate, "*", "*"), Activity.EXECUTE); } return allowed; }
private boolean hasPermissionForTenant(final String subject, final ResourceIdentifier resource, final Permission permission) { if (resource.getResourceId() != null) { final ResourceIdentifier tenantResource = ResourceIdentifier.from(resource.getEndpoint(), resource.getTenantId(), null); return hasPermissionInternal(subject, tenantResource, permission); } return false; }
/** * Checks whether an AMQP message contains required standard properties. * <p> * This method checks if the message contains a valid device identifier using * the {@link #hasValidDeviceId(ResourceIdentifier, Message)} method. * <p> * After successful verification the following properties are added to the message's <em>annotations</em>: * <ul> * <li>{@link MessageHelper#APP_PROPERTY_DEVICE_ID} - the ID of the device that reported the data.</li> * <li>{@link MessageHelper#APP_PROPERTY_TENANT_ID} - the ID of the tenant as indicated by the link target's second segment.</li> * <li>{@link MessageHelper#APP_PROPERTY_RESOURCE} - the full resource path including the endpoint, the tenant and the device ID.</li> * </ul> * * @param linkTarget The resource path to check the message's properties against for consistency. * @param msg The AMQP 1.0 message to perform the checks on. * @return {@code true} if the message passes all checks. */ protected static final boolean verifyStandardProperties(final ResourceIdentifier linkTarget, final Message msg) { if (!hasValidDeviceId(linkTarget, msg)) { return false; } else { final ResourceIdentifier targetResource = ResourceIdentifier .from(linkTarget.getEndpoint(), linkTarget.getTenantId(), MessageHelper.getDeviceId(msg)); MessageHelper.annotate(msg, targetResource); return true; } }
/** * Checks whether an AMQP message contains required standard properties. * <p> * This method checks if the message contains a valid device identifier using * the {@link #hasValidDeviceId(ResourceIdentifier, Message)} method. * <p> * After successful verification the following properties are added to the message's <em>annotations</em>: * <ul> * <li>{@link MessageHelper#APP_PROPERTY_DEVICE_ID} - the ID of the device that reported the data.</li> * <li>{@link MessageHelper#APP_PROPERTY_TENANT_ID} - the ID of the tenant as indicated by the link target's second segment.</li> * <li>{@link MessageHelper#APP_PROPERTY_RESOURCE} - the full resource path including the endpoint, the tenant and the device ID.</li> * </ul> * * @param linkTarget The resource path to check the message's properties against for consistency. * @param msg The AMQP 1.0 message to perform the checks on. * @return {@code true} if the message passes all checks. */ protected static final boolean verifyStandardProperties(final ResourceIdentifier linkTarget, final Message msg) { if (!hasValidDeviceId(linkTarget, msg)) { return false; } else { final ResourceIdentifier targetResource = ResourceIdentifier .from(linkTarget.getEndpoint(), linkTarget.getTenantId(), MessageHelper.getDeviceId(msg)); MessageHelper.annotate(msg, targetResource); return true; } }
Future<ResourceIdentifier> mapTopic(final MqttContext ctx) { final Future<ResourceIdentifier> result = Future.future(); final ResourceIdentifier topic = ctx.topic(); ResourceIdentifier mappedTopic = null; if (getConfig().getControlPrefix().equals(topic.getEndpoint())) { // this is a "control" message ctx.setContentType(getConfig().getCtrlMsgContentType()); final String[] mappedPath = Arrays.copyOf(topic.getResourcePath(), topic.getResourcePath().length); mappedPath[0] = getEndpoint(ctx.message().qosLevel()); mappedTopic = ResourceIdentifier.fromPath(mappedPath); } else { // map "data" messages based on QoS ctx.setContentType(getConfig().getDataMsgContentType()); final String[] mappedPath = new String[topic.getResourcePath().length + 1]; System.arraycopy(topic.getResourcePath(), 0, mappedPath, 1, topic.getResourcePath().length); mappedPath[0] = getEndpoint(ctx.message().qosLevel()); mappedTopic = ResourceIdentifier.fromPath(mappedPath); } if (mappedTopic.getResourcePath().length < 3) { // topic does not contain account_name and client_id result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "topic does not comply with Kura format")); } else { LOG.debug("mapped Kura message [topic: {}, QoS: {}] to Hono message [to: {}, device_id: {}, content-type: {}]", topic, ctx.message().qosLevel(), mappedTopic.getBasePath(), mappedTopic.getResourceId(), ctx.contentType()); result.complete(mappedTopic); } return result; }
Future<ResourceIdentifier> mapTopic(final MqttContext ctx) { final Future<ResourceIdentifier> result = Future.future(); final ResourceIdentifier topic = ctx.topic(); ResourceIdentifier mappedTopic = null; if (getConfig().getControlPrefix().equals(topic.getEndpoint())) { // this is a "control" message ctx.setContentType(getConfig().getCtrlMsgContentType()); final String[] mappedPath = Arrays.copyOf(topic.getResourcePath(), topic.getResourcePath().length); mappedPath[0] = getEndpoint(ctx.message().qosLevel()); mappedTopic = ResourceIdentifier.fromPath(mappedPath); } else { // map "data" messages based on QoS ctx.setContentType(getConfig().getDataMsgContentType()); final String[] mappedPath = new String[topic.getResourcePath().length + 1]; System.arraycopy(topic.getResourcePath(), 0, mappedPath, 1, topic.getResourcePath().length); mappedPath[0] = getEndpoint(ctx.message().qosLevel()); mappedTopic = ResourceIdentifier.fromPath(mappedPath); } if (mappedTopic.getResourcePath().length < 3) { // topic does not contain account_name and client_id result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "topic does not comply with Kura format")); } else { LOG.debug("mapped Kura message [topic: {}, QoS: {}] to Hono message [to: {}, device_id: {}, content-type: {}]", topic, ctx.message().qosLevel(), mappedTopic.getBasePath(), mappedTopic.getResourceId(), ctx.contentType()); result.complete(mappedTopic); } return result; }