-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#451: Fixed translation of FilterSets between DSMLv2 and JSON
- Loading branch information
Showing
6 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
.../src/main/java/org/openehealth/ipf/commons/ihe/hpd/stub/json/ControlListDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openehealth.ipf.commons.ihe.hpd.stub.json; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.openehealth.ipf.commons.ihe.hpd.controls.ControlUtils; | ||
import org.openehealth.ipf.commons.ihe.hpd.controls.sorting.SortControl2; | ||
import org.openehealth.ipf.commons.ihe.hpd.controls.sorting.SortResponseControl2; | ||
import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.Control; | ||
|
||
import javax.naming.ldap.*; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* JSON deserializer for {@link List}<{@link Control}>. | ||
* | ||
* @author Dmytro Rud | ||
*/ | ||
public class ControlListDeserializer extends JsonDeserializer<List> { | ||
|
||
@Override | ||
public List deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException { | ||
JsonNode arrayNode = jsonParser.readValueAsTree(); | ||
if (!arrayNode.isArray()) { | ||
throw new IllegalArgumentException("'controls' shall be a JSON array"); | ||
} | ||
List<Control> result = new ArrayList<>(); | ||
Iterator<JsonNode> controlNodes = arrayNode.elements(); | ||
while (controlNodes.hasNext()) { | ||
result.add(ControlUtils.toDsmlv2(deserializeControl(controlNodes.next()))); | ||
} | ||
return result; | ||
} | ||
|
||
private static BasicControl deserializeControl(JsonNode node) throws IOException { | ||
switch (node.get("type").textValue()) { | ||
case PagedResultsControl.OID: | ||
return new PagedResultsControl(node.get("size").intValue(), node.get("cookie").binaryValue(), node.get("critical").booleanValue()); | ||
case SortControl.OID: | ||
ArrayList<SortKey> keys = new ArrayList<>(); | ||
JsonNode keysNode = node.get("keys"); | ||
Iterator<JsonNode> keyNodes = keysNode.elements(); | ||
while (keyNodes.hasNext()) { | ||
JsonNode keyNode = keyNodes.next(); | ||
keys.add(new SortKey(keyNode.get("attrId").textValue(), keyNode.get("ascending").booleanValue(), keyNode.get("matchingRuleId").textValue())); | ||
} | ||
return new SortControl2(node.get("critical").asBoolean(), keys.toArray(new SortKey[0])); | ||
case SortResponseControl.OID: | ||
return new SortResponseControl2(node.get("resultCode").intValue(), node.get("failedAttrId").asText(), node.get("critical").booleanValue()); | ||
default: | ||
throw new NotImplementedException("Cannot handle control type " + node.get("type").asText()); | ||
} | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
...pd/src/main/java/org/openehealth/ipf/commons/ihe/hpd/stub/json/ControlListSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openehealth.ipf.commons.ihe.hpd.stub.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.openehealth.ipf.commons.ihe.hpd.controls.ControlUtils; | ||
import org.openehealth.ipf.commons.ihe.hpd.controls.sorting.SortControl2; | ||
import org.openehealth.ipf.commons.ihe.hpd.controls.sorting.SortResponseControl2; | ||
import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.Control; | ||
|
||
import javax.naming.ldap.*; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* JSON serializer for {@link List}<{@link Control}>. | ||
* | ||
* @author Dmytro Rud | ||
*/ | ||
public class ControlListSerializer extends StdSerializer<List> { | ||
|
||
public ControlListSerializer() { | ||
super(List.class); | ||
} | ||
|
||
@Override | ||
public void serialize(List list, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
gen.writeStartArray(list); | ||
for (Object object : list) { | ||
Control dsmlcontrol = (Control) object; | ||
BasicControl control = ControlUtils.extractControl((byte[]) dsmlcontrol.getControlValue(), dsmlcontrol.getType(), dsmlcontrol.isCriticality()); | ||
gen.writeStartObject(); | ||
gen.writeStringField("type", control.getID()); | ||
gen.writeBooleanField("critical", control.isCritical()); | ||
switch (dsmlcontrol.getType()) { | ||
case PagedResultsControl.OID: | ||
PagedResultsResponseControl paginationControl = (PagedResultsResponseControl) control; | ||
gen.writeNumberField("size", paginationControl.getResultSize()); | ||
if (paginationControl.getCookie() != null) { | ||
gen.writeBinaryField("cookie", paginationControl.getCookie()); | ||
} | ||
break; | ||
case SortControl.OID: | ||
SortControl2 sortControl = (SortControl2) control; | ||
gen.writeArrayFieldStart("keys"); | ||
for (SortKey sortKey : sortControl.getKeys()) { | ||
gen.writeStartObject(); | ||
gen.writeStringField("attrId", sortKey.getAttributeID()); | ||
gen.writeStringField("matchingRuleId", sortKey.getMatchingRuleID()); | ||
gen.writeBooleanField("ascending", sortKey.isAscending()); | ||
gen.writeEndObject(); | ||
} | ||
gen.writeEndArray(); | ||
break; | ||
case SortResponseControl.OID: | ||
SortResponseControl2 sortResponseControl = (SortResponseControl2) control; | ||
gen.writeNumberField("resultCode", sortResponseControl.getResultCode()); | ||
if (sortResponseControl.getFailedAttributeName() != null) { | ||
gen.writeStringField("failedAttrId", sortResponseControl.getFailedAttributeName()); | ||
} | ||
break; | ||
default: | ||
throw new NotImplementedException("Cannot handle control type " + dsmlcontrol.getType()); | ||
} | ||
gen.writeEndObject(); | ||
} | ||
gen.writeEndArray(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters