Skip to content

Commit

Permalink
Fixed v1.0/Set(id)/property not returning 204 for null properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Nov 16, 2018
1 parent 3bcbb1b commit 4144266
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public default ResourcePath getPath() {
epe.setEntityType(type);
epe.setId(getId());
ResourcePath resourcePath = new ResourcePath();
resourcePath.getPathElements().add(epe);
resourcePath.setMainElement(epe);
resourcePath.addPathElement(epe, true, false);
return resourcePath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public boolean matches(PersistenceManager persistenceManager, Entity newEntity,
protected void generateFilter(int pathElementOffset) {
EntityType lastType = getEntityType();
List<Property> properties = new ArrayList<>();
for (int i = path.getPathElements().size() - 1 - pathElementOffset; i >= 0; i--) {
ResourcePathElement element = path.getPathElements().get(i);
for (int i = path.size() - 1 - pathElementOffset; i >= 0; i--) {
ResourcePathElement element = path.get(i);
if (!(element instanceof EntityPathElement)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ private void init() {
throw new IllegalArgumentException("Invalid subscription to: '" + topic + "': query options not allowed for subscription on an entitiy.");
}
entityType = ((EntityPathElement) path.getLastElement()).getEntityType();
if (path.getPathElements().size() == 2
&& path.getPathElements().get(path.getPathElements().size() - 2) instanceof EntitySetPathElement) {
final int size = path.size();
if (size == 2 && path.get(0) instanceof EntitySetPathElement) {
Id id = ((EntityPathElement) path.getLastElement()).getId();
matcher = x -> x.getProperty(EntityProperty.ID).equals(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ private void init() {
if (!SubscriptionFactory.getQueryFromTopic(topic).isEmpty()) {
throw new IllegalArgumentException("Invalid subscription to: '" + topic + "': query options not allowed for subscription on a property.");
}
entityType = ((EntityPathElement) path.getPathElements().get(path.getPathElements().size() - 2)).getEntityType();
property = ((PropertyPathElement) path.getPathElements().get(path.getPathElements().size() - 1)).getProperty();
final int size = path.size();
entityType = ((EntityPathElement) path.get(size - 2)).getEntityType();
property = ((PropertyPathElement) path.get(size - 1)).getProperty();
if (path.getIdentifiedElement() != null) {
Id id = path.getIdentifiedElement().getId();
matcher = x -> x.getProperty(EntityProperty.ID).equals(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ public Subscription get(String topic) {
internalTopic = topic.substring(topicPrefix.length());
}
ResourcePath path = parsePath(getPathFromTopic(internalTopic));
if (path == null || path.getPathElements().isEmpty()) {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException(errorMsg + "invalid path.");
}
path.setServiceRootUrl(settings.getServiceRootUrl());
path.compress();
final int size = path.size();
if (path.getLastElement() instanceof EntitySetPathElement) {
// SensorThings Standard 14.2.1 - Subscribe to EntitySet
return new EntitySetSubscription(topic, path, settings.getServiceRootUrl());
} else if (path.getLastElement() instanceof EntityPathElement) {
// SensorThings Standard 14.2.2 - Subscribe to Entity
return new EntitySubscription(topic, path, settings.getServiceRootUrl());
} else if (path.getPathElements().size() >= 2
&& path.getPathElements().get(path.getPathElements().size() - 2) instanceof EntityPathElement
&& path.getPathElements().get(path.getPathElements().size() - 1) instanceof PropertyPathElement) {
} else if (size >= 2
&& path.get(size - 2) instanceof EntityPathElement
&& path.get(size - 1) instanceof PropertyPathElement) {
// SensorThings Standard 14.2.3 - Subscribe to Property
return new PropertySubscription(topic, path, settings.getServiceRootUrl());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -80,7 +79,6 @@ public static ResourcePath parsePath(IdManager idmanager, String serviceRootUrl,
ResourcePath resourcePath = new ResourcePath();
resourcePath.setServiceRootUrl(serviceRootUrl);
resourcePath.setPathUrl(path);
resourcePath.setPathElements(new ArrayList<>());
if (path == null) {
return resourcePath;
}
Expand Down Expand Up @@ -121,30 +119,28 @@ private void addAsEntitiy(ResourcePath rp, SimpleNode node, EntityType type) {
rp.setIdentifiedElement(epa);
}
epa.setParent(rp.getLastElement());
rp.getPathElements().add(epa);
rp.setMainElement(epa);
rp.addPathElement(epa, true, false);
}

private void addAsEntitiySet(ResourcePath rp, EntityType type) {
EntitySetPathElement espa = new EntitySetPathElement();
espa.setEntityType(type);
espa.setParent(rp.getLastElement());
rp.getPathElements().add(espa);
rp.setMainElement(espa);
rp.addPathElement(espa, true, false);
}

private void addAsEntitiyProperty(ResourcePath rp, EntityProperty type) {
PropertyPathElement ppe = new PropertyPathElement();
ppe.setProperty(type);
ppe.setParent(rp.getLastElement());
rp.getPathElements().add(ppe);
rp.addPathElement(ppe);
}

private void addAsCustomProperty(ResourcePath rp, SimpleNode node) {
CustomPropertyPathElement cppa = new CustomPropertyPathElement();
cppa.setName(node.value.toString());
cppa.setParent(rp.getLastElement());
rp.getPathElements().add(cppa);
rp.addPathElement(cppa);
}

private void addAsArrayIndex(ResourcePath rp, SimpleNode node) {
Expand All @@ -158,7 +154,7 @@ private void addAsArrayIndex(ResourcePath rp, SimpleNode node) {
int index = Integer.parseInt(numberString);
cpai.setIndex(index);
cpai.setParent(rp.getLastElement());
rp.getPathElements().add(cpai);
rp.addPathElement(cpai);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Array indices must be integer values. Failed to parse: " + image);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class ResourcePath {
* Flag indicating there was a $value at the end of the path.
*/
private boolean value;
/**
* Flag indicating the path points to an entityProperty
* (EntitySet(id)/entityProperty).
*/
private boolean entityProperty;
/**
* The elements in this path.
*/
Expand All @@ -67,16 +72,74 @@ public ResourcePath(String serviceRootUrl, String pathUrl) {
this.pathUrl = pathUrl;
}

/**
* Flag indicating there was a $ref at the end of the path.
*
* @return the ref
*/
public boolean isRef() {
return ref;
}

/**
* Flag indicating there was a $ref at the end of the path.
*
* @param ref the ref to set
*/
public void setRef(boolean ref) {
this.ref = ref;
}

/**
* Flag indicating there was a $value at the end of the path.
*
* @return the value
*/
public boolean isValue() {
return value;
}

public List<ResourcePathElement> getPathElements() {
return pathElements;
/**
* Flag indicating there was a $value at the end of the path.
*
* @param value the value to set
*/
public void setValue(boolean value) {
this.value = value;
}

/**
* Flag indicating the path points to an entityProperty
* (EntitySet(id)/entityProperty).
*
* @return the entityProperty
*/
public boolean isEntityProperty() {
return entityProperty;
}

/**
* Returns the number of elements in the path.
*
* @return The size of the path.
*/
public int size() {
return pathElements.size();
}

public boolean isEmpty() {
return pathElements.isEmpty();
}

/**
* Get the element with the given index, where the first element has index
* 0.
*
* @param index The index of the element to get.
* @return The element with the given index.
*/
public ResourcePathElement get(int index) {
return pathElements.get(index);
}

public ResourcePathElement getMainElement() {
Expand Down Expand Up @@ -106,18 +169,6 @@ public EntityPathElement getIdentifiedElement() {
return identifiedElement;
}

public void setRef(boolean ref) {
this.ref = ref;
}

public void setValue(boolean value) {
this.value = value;
}

public void setPathElements(List<ResourcePathElement> pathElements) {
this.pathElements = pathElements;
}

public void setMainElement(ResourcePathElement mainElementType) {
this.mainElement = mainElementType;
}
Expand All @@ -126,16 +177,38 @@ public void setIdentifiedElement(EntityPathElement identifiedElement) {
this.identifiedElement = identifiedElement;
}

/**
* Add the given element at the given index.
*
* @param index The position in the path to put the element.
* @param pe The element to add.
*/
public void addPathElement(int index, ResourcePathElement pe) {
pathElements.add(index, pe);
}

public void addPathElement(ResourcePathElement pe) {
addPathElement(pe, false, false);
}

/**
* Add the given path element, optionally setting it as the main element, or
* as the identifying element.
*
* @param pe The element to add.
* @param isMain Flag indicating it is the main element.
* @param isIdentifier Flag indicating it is the identifying element.
*/
public void addPathElement(ResourcePathElement pe, boolean isMain, boolean isIdentifier) {
getPathElements().add(pe);
if (isMain && pe instanceof EntityPathElement) {
EntityPathElement epe = (EntityPathElement) pe;
setMainElement(epe);
pathElements.add(pe);
if (isMain && pe instanceof EntityPathElement || pe instanceof EntitySetPathElement) {
setMainElement(pe);
}
if (isIdentifier && pe instanceof EntityPathElement) {
EntityPathElement epe = (EntityPathElement) pe;
setIdentifiedElement(epe);
}
this.entityProperty = (pe instanceof PropertyPathElement);
}

public void compress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,16 @@ private <T> ServiceResponse<T> handleGet(PersistenceManager pm, ServiceRequest r
return response;
}
if (object == null) {
response.setStatus(404, "Nothing found.");
maybeCommitAndClose();
return response;
if (path.isValue() || path.isEntityProperty()) {
response.setStatus(204, "No Content");
} else {
response.setStatus(404, "Nothing found.");
}
} else {
response.setResult(object);
response.setResultFormatted(request.getFormatter().format(path, query, object, settings.isUseAbsoluteNavigationLinks()));
response.setCode(200);
}
response.setResult(object);
response.setResultFormatted(request.getFormatter().format(path, query, object, settings.isUseAbsoluteNavigationLinks()));
response.setCode(200);
maybeCommitAndClose();
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@
import de.fraunhofer.iosb.ilt.sta.query.Query;
import de.fraunhofer.iosb.ilt.sta.settings.CoreSettings;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author jab
*/
public class ParserHelper {

/**
* The logger for this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ParserHelper.class);

public static final class PathQuery {

public final ResourcePath path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import de.fraunhofer.iosb.ilt.sta.persistence.IdManagerlong;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void testParsePath_setThingsRef() {
expResult.setMainElement(espe);
expResult.setRef(true);

assert (result.equals(expResult));
Assert.assertEquals(expResult, result);
}

private void testThing(long id) {
Expand Down
23 changes: 11 additions & 12 deletions FROST-Server.Core/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%16thread] %-5level %30logger{30} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%16thread] %-5level %30logger{30} - %msg%n</pattern>
</encoder>
</appender>

<logger name="de.fraunhofer.iosb.ilt.sta.parser.path" level="INFO"/>
<logger name="de.fraunhofer.iosb.ilt.sta.parser.query" level="INFO"/>
<logger name="de.fraunhofer.iosb.ilt.sta.persistence.postgres.PostgresPersistenceManager" level="INFO"/>
<logger name="com.querydsl" level="INFO"/>
<logger name="de.fraunhofer.iosb.ilt.sta.parser.path" level="OFF"/>
<logger name="de.fraunhofer.iosb.ilt.sta.parser.query" level="OFF"/>
<logger name="com.querydsl" level="OFF"/>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<root level="OFF">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ private void executeService(RequestType requestType, HttpServletRequest request,
CoreSettings coreSettings = (CoreSettings) request.getServletContext().getAttribute(AbstractContextListener.TAG_CORE_SETTINGS);
Service service = new Service(coreSettings);
sendResponse(service.execute(serviceRequestFromHttpRequest(request, requestType)), response);
} catch (Exception e) {
LOGGER.error("", e);
sendResponse(new ServiceResponse(500, e.getMessage()), response);
} catch (Exception exc) {
LOGGER.error("", exc);
sendResponse(new ServiceResponse(500, exc.getMessage()), response);
}
}

Expand Down
2 changes: 1 addition & 1 deletion FROST-Server.HTTP/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<logger name="de.fraunhofer.iosb.ilt.sta.parser.path" level="INFO"/>
<logger name="de.fraunhofer.iosb.ilt.sta.parser.query" level="INFO"/>
<logger name="de.fraunhofer.iosb.ilt.sta.persistence.postgres.PostgresPersistenceManager" level="INFO"/>
<logger name="de.fraunhofer.iosb.ilt.sta.persistence.postgres" level="INFO"/>
<logger name="com.querydsl" level="INFO"/>
<logger name="io.moquette.server.netty.NettyMQTTHandler" level="WARN"/>
<logger name="io.moquette.spi" level="WARN"/>
Expand Down
Loading

0 comments on commit 4144266

Please sign in to comment.