Permalink
Browse files

Temporary implementation of public assets (relates to #59)

  • Loading branch information...
richturner committed Nov 26, 2017
1 parent 0f5c785 commit c52392c94ac9aee98ec5d4133fc0963228f2e377
@@ -63,6 +63,10 @@ public String getClientRemoteAddress() {
return request.getRemoteAddr();
}
public String getRequestRealm() {
return request.getHeader(WebService.REQUEST_HEADER_REALM);
}
@SuppressWarnings("unchecked")
public AuthContext getAuthContext() {
// The securityContext is a thread-local proxy, careful when/how you call it
@@ -35,6 +35,7 @@
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueException;
import org.openremote.model.value.Values;
@@ -471,4 +472,38 @@ public void delete(RequestParams requestParams, String assetId) {
throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);
}
}
@Override
public Asset[] queryPublicAssets(RequestParams requestParams, AbstractAssetQuery query) {
String requestRealm = getRequestRealm();
if (query == null || TextUtil.isNullOrEmpty(requestRealm)) {
return EMPTY_ASSETS;
}
// Force realm to be request realm
if (query.tenantPredicate == null) {
query.tenant(new AbstractAssetQuery.TenantPredicate().realm(requestRealm));
} else {
query.tenantPredicate.realm = requestRealm;
}
// Set public only flag
if (query.select == null) {
query.select(new AssetQuery.Select().publicOnly(true));
} else {
query.select.publicOnly = true;
}
// Limit to protected attributes and meta items
query.select.filterProtected = true;
try {
List<ServerAsset> result = assetStorageService.findAll(query);
return result.toArray(new Asset[result.size()]);
} catch (IllegalStateException ex) {
throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);
}
}
}
@@ -820,6 +820,16 @@ protected String buildWhereClause(AbstractAssetQuery query, int level, List<Para
binders.add(st -> st.setString(pos, query.userId));
}
if (level == 1 && query.select.publicOnly) {
// TODO: Change this once public asset mechanism is finalised
AbstractAssetQuery.StringPredicate publicPredicate =
new AbstractAssetQuery.StringPredicate(AbstractAssetQuery.Match.BEGIN, false, "urn:openremote:public:");
sb.append(publicPredicate.caseSensitive ? " and A.ASSET_TYPE " : " and upper(A.ASSET_TYPE)");
sb.append(publicPredicate.match == AssetQuery.Match.EXACT ? " = ?" : " like ?");
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, publicPredicate.prepareValue()));
}
if (query.type != null) {
sb.append(query.type.caseSensitive ? " and A.ASSET_TYPE" : " and upper(A.ASSET_TYPE)");
sb.append(query.type.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
@@ -226,4 +226,17 @@ void deleteUserAsset(@BeanParam RequestParams requestParams,
@RolesAllowed({"read:assets"})
@SuppressWarnings("unusable-by-js")
Asset[] queryAssets(@BeanParam RequestParams requestParams, AbstractAssetQuery query);
/**
* Retrieve public assets using an {@link AbstractAssetQuery}.
* <p>
* Allows un-authenticated 'public' users to query public assets for a realm.
*/
@POST
@Path("public/query")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@SuccessStatusCode(200)
@SuppressWarnings("unusable-by-js")
Asset[] queryPublicAssets(@BeanParam RequestParams requestParams, AbstractAssetQuery query);
}
@@ -45,6 +45,7 @@
public boolean filterProtected;
public boolean recursive;
public String[] attributeNames;
public boolean publicOnly;
public Select() {
}
@@ -76,6 +77,11 @@ public Select include(Include include) {
return this;
}
public Select publicOnly(boolean publicOnly) {
this.publicOnly = publicOnly;
return this;
}
public Select filterProtected(boolean filterProtected) {
this.filterProtected = filterProtected;
return this;

0 comments on commit c52392c

Please sign in to comment.