[OLINGO-704] Fix: OData Server supports only odata.metadata=minimal

This commit is contained in:
Christian Holzer 2015-06-24 17:31:52 +02:00
parent 9998ba6748
commit e32034bd54
3 changed files with 142 additions and 70 deletions

View File

@ -309,7 +309,7 @@ public class ActionImportITCase extends AbstractBaseTestITCase {
@Override @Override
protected ODataClient getClient() { protected ODataClient getClient() {
ODataClient odata = ODataClientFactory.getClient(); ODataClient odata = ODataClientFactory.getClient();
odata.getConfiguration().setDefaultPubFormat(ContentType.JSON_NO_METADATA); odata.getConfiguration().setDefaultPubFormat(ContentType.JSON);
return odata; return odata;
} }

View File

@ -52,17 +52,22 @@ import org.apache.olingo.server.core.uri.UriHelperImpl;
public class ODataImpl extends OData { public class ODataImpl extends OData {
@Override @Override
public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException { public ODataSerializer createSerializer(final ContentType contentType)
throws SerializerException {
ODataSerializer serializer; ODataSerializer serializer;
// odata.metadata=none, odata.metadata=minimal, odata.metadata=full // odata.metadata=none, odata.metadata=minimal, odata.metadata=full
if(contentType.isCompatible(ContentType.APPLICATION_JSON)) { if (contentType.isCompatible(ContentType.APPLICATION_JSON)
&& ContentType.VALUE_ODATA_METADATA_MINIMAL
.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA))) {
serializer = new ODataJsonSerializer(contentType); serializer = new ODataJsonSerializer(contentType);
} else if(contentType.isCompatible(ContentType.APPLICATION_XML)) { } else if (contentType.isCompatible(ContentType.APPLICATION_XML)) {
serializer = new ODataXmlSerializerImpl(); serializer = new ODataXmlSerializerImpl();
} else { } else {
throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(), throw new SerializerException("Unsupported format: "
SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString()); + contentType.toContentTypeString(),
SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
contentType.toContentTypeString());
} }
return serializer; return serializer;
@ -79,15 +84,18 @@ public class ODataImpl extends OData {
} }
@Override @Override
public ServiceMetadata createServiceMetadata(final CsdlEdmProvider edmProvider, public ServiceMetadata createServiceMetadata(
final CsdlEdmProvider edmProvider,
final List<EdmxReference> references) { final List<EdmxReference> references) {
return createServiceMetadata(edmProvider, references, null); return createServiceMetadata(edmProvider, references, null);
} }
@Override @Override
public ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider, List<EdmxReference> references, public ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider,
List<EdmxReference> references,
ServiceMetadataETagSupport serviceMetadataETagSupport) { ServiceMetadataETagSupport serviceMetadataETagSupport) {
return new ServiceMetadataImpl(edmProvider, references, serviceMetadataETagSupport); return new ServiceMetadataImpl(edmProvider, references,
serviceMetadataETagSupport);
} }
@Override @Override
@ -101,24 +109,30 @@ public class ODataImpl extends OData {
} }
@Override @Override
public ODataDeserializer createDeserializer(final ContentType contentType) throws DeserializerException { public ODataDeserializer createDeserializer(final ContentType contentType)
throws DeserializerException {
ODataDeserializer deserializer; ODataDeserializer deserializer;
// odata.metadata=none, odata.metadata=minimal, odata.metadata=full // odata.metadata=none, odata.metadata=minimal, odata.metadata=full
if(contentType.isCompatible(ContentType.JSON)) { if (contentType.isCompatible(ContentType.JSON)) {
deserializer = new ODataJsonDeserializer(contentType); deserializer = new ODataJsonDeserializer(contentType);
//} else if(contentType.isCompatible(ContentType.APPLICATION_XML)) { // } else if(contentType.isCompatible(ContentType.APPLICATION_XML))
// We do not support XML deserialization right now so this must lead to an error. // We do not support XML deserialization right now so this must lead
// to an error.
// {
} else { } else {
throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(), throw new DeserializerException("Unsupported format: "
DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString()); + contentType.toContentTypeString(),
DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
contentType.toContentTypeString());
} }
return deserializer; return deserializer;
} }
@Override @Override
public EdmPrimitiveType createPrimitiveTypeInstance(final EdmPrimitiveTypeKind kind) { public EdmPrimitiveType createPrimitiveTypeInstance(
final EdmPrimitiveTypeKind kind) {
return EdmPrimitiveTypeFactory.getInstance(kind); return EdmPrimitiveTypeFactory.getInstance(kind);
} }

View File

@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.olingo.server.core;
import static org.junit.Assert.assertNotNull;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.deserializer.ODataDeserializer;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.junit.Test;
public class ODataImplTest {
private final OData odata = OData.newInstance();
@Test(expected=SerializerException.class)
public void testJsonSerializerForOdataMetadataNone() throws SerializerException {
odata.createSerializer(ContentType.JSON_NO_METADATA);
}
@Test(expected=SerializerException.class)
public void testJsonSerializerForODataMetadataFull() throws SerializerException {
odata.createSerializer(ContentType.JSON_FULL_METADATA);
}
@Test
public void testCreateJsonSerializerForODataMetadataMinimal() throws SerializerException {
final ODataSerializer serializer = odata.createSerializer(ContentType.JSON);
assertNotNull(serializer);
}
@Test
public void testCreateJsonDeserialierForODataMetadataMinimal() throws DeserializerException {
final ODataDeserializer deserializer = odata.createDeserializer(ContentType.JSON);
assertNotNull(deserializer);
}
}