[OLINGO-444] Minor improvements

This commit is contained in:
Michael Bolz 2014-10-10 07:46:22 +02:00
parent 169651531c
commit ef0553a025
3 changed files with 17 additions and 34 deletions

View File

@ -143,14 +143,14 @@ public class ContextURL {
return this; return this;
} }
public Builder keySegment(Object value) { public Builder keySegment(String value) {
if (value != null) { if (value != null) {
contextURL.keyPath = String.valueOf(value); contextURL.keyPath = value;
} }
return this; return this;
} }
public Builder keySegment(Map<String, Object> values) { public Builder keySegment(Map<String, String> values) {
if (values != null && !values.isEmpty()) { if (values != null && !values.isEmpty()) {
if (values.size() == 1) { if (values.size() == 1) {

View File

@ -111,13 +111,13 @@ public class ContextURLBuilderTest {
Mockito.when(entitySet.getName()).thenReturn("Customers"); Mockito.when(entitySet.getName()).thenReturn("Customers");
ContextURL contextURL = ContextURL.with().serviceRoot(URI.create("http://host/service/")) ContextURL contextURL = ContextURL.with().serviceRoot(URI.create("http://host/service/"))
.entitySet(entitySet) .entitySet(entitySet)
.keySegment(1) .keySegment(String.valueOf(1))
.navOrPropertyPath("Name") .navOrPropertyPath("Name")
.build(); .build();
assertEquals("http://host/service/$metadata#Customers(1)/Name", assertEquals("http://host/service/$metadata#Customers(1)/Name",
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
TreeMap<String, Object> keys = new TreeMap<String, Object>(); TreeMap<String, String> keys = new TreeMap<String, String>();
keys.put("one", 1); keys.put("one", String.valueOf(1));
keys.put("two", "'two'"); keys.put("two", "'two'");
contextURL = ContextURL.with().serviceRoot(URI.create("http://host/service/")) contextURL = ContextURL.with().serviceRoot(URI.create("http://host/service/"))
.entitySet(entitySet) .entitySet(entitySet)

View File

@ -20,10 +20,7 @@ package org.apache.olingo.server.tecsvc.processor;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.List; import java.util.*;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import org.apache.olingo.commons.api.data.ContextURL; import org.apache.olingo.commons.api.data.ContextURL;
import org.apache.olingo.commons.api.data.ContextURL.Suffix; import org.apache.olingo.commons.api.data.ContextURL.Suffix;
@ -32,7 +29,6 @@ import org.apache.olingo.commons.api.data.EntitySet;
import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveType; import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmProperty; import org.apache.olingo.commons.api.edm.EdmProperty;
@ -217,24 +213,11 @@ public class TechnicalProcessor implements EntitySetProcessor, EntityProcessor,
.build(); .build();
} }
private Map<String, Object> getKeys(EdmEntityType entityType, private Map<String, String> mapKeys(List<UriParameter> parameters)
List<UriParameter> parameters) throws ODataApplicationException { throws ODataApplicationException {
TreeMap<String, Object> keys = new TreeMap<String, Object>(); Map<String, String> keys = new LinkedHashMap<String, String>();
for (UriParameter param: parameters) { for (UriParameter param: parameters) {
final EdmProperty property = (EdmProperty) entityType.getProperty(param.getName()); keys.put(param.getName(), param.getText());
final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
try {
Object keyValue = type.valueOfString(param.getText(),
property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
property.isUnicode(), type.getDefaultType());
if (keyValue instanceof String) {
keyValue = "'"+keyValue+"'";
}
keys.put(param.getName(), keyValue);
} catch (EdmPrimitiveTypeException e) {
throw new ODataApplicationException("Invalid key found", HttpStatusCode.BAD_REQUEST.getStatusCode(),
Locale.ROOT, e);
}
} }
return keys; return keys;
} }
@ -266,7 +249,7 @@ public class TechnicalProcessor implements EntitySetProcessor, EntityProcessor,
response.setContent(serializer.entityProperty(edmProperty, property, response.setContent(serializer.entityProperty(edmProperty, property,
ODataSerializerOptions.with().contextURL(format == ODataFormat.JSON_NO_METADATA ? null : ODataSerializerOptions.with().contextURL(format == ODataFormat.JSON_NO_METADATA ? null :
ContextURL.with().entitySet(edmEntitySet) ContextURL.with().entitySet(edmEntitySet)
.keySegment(getKeys(edmEntitySet.getEntityType(), resourceEntitySet.getKeyPredicates())) .keySegment(mapKeys(resourceEntitySet.getKeyPredicates()))
.navOrPropertyPath(edmProperty.getName()) .navOrPropertyPath(edmProperty.getName())
.build()).build())); .build()).build()));
response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setStatusCode(HttpStatusCode.OK.getStatusCode());