[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

@ -51,85 +51,99 @@ 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)
ODataSerializer serializer; throws SerializerException {
ODataSerializer serializer;
// odata.metadata=none, odata.metadata=minimal, odata.metadata=full
if(contentType.isCompatible(ContentType.APPLICATION_JSON)) {
serializer = new ODataJsonSerializer(contentType);
} else if(contentType.isCompatible(ContentType.APPLICATION_XML)) {
serializer = new ODataXmlSerializerImpl();
} else {
throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(),
SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
}
return serializer; // odata.metadata=none, odata.metadata=minimal, odata.metadata=full
} if (contentType.isCompatible(ContentType.APPLICATION_JSON)
&& ContentType.VALUE_ODATA_METADATA_MINIMAL
.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA))) {
serializer = new ODataJsonSerializer(contentType);
} else if (contentType.isCompatible(ContentType.APPLICATION_XML)) {
serializer = new ODataXmlSerializerImpl();
} else {
throw new SerializerException("Unsupported format: "
+ contentType.toContentTypeString(),
SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
contentType.toContentTypeString());
}
@Override return serializer;
public FixedFormatSerializer createFixedFormatSerializer() { }
return new FixedFormatSerializerImpl();
}
@Override @Override
public ODataHttpHandler createHandler(final ServiceMetadata edm) { public FixedFormatSerializer createFixedFormatSerializer() {
return new ODataHttpHandlerImpl(this, edm); return new FixedFormatSerializerImpl();
} }
@Override @Override
public ServiceMetadata createServiceMetadata(final CsdlEdmProvider edmProvider, public ODataHttpHandler createHandler(final ServiceMetadata edm) {
final List<EdmxReference> references) { return new ODataHttpHandlerImpl(this, edm);
return createServiceMetadata(edmProvider, references, null); }
}
@Override @Override
public ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider, List<EdmxReference> references, public ServiceMetadata createServiceMetadata(
ServiceMetadataETagSupport serviceMetadataETagSupport) { final CsdlEdmProvider edmProvider,
return new ServiceMetadataImpl(edmProvider, references, serviceMetadataETagSupport); final List<EdmxReference> references) {
} return createServiceMetadata(edmProvider, references, null);
}
@Override @Override
public FixedFormatDeserializer createFixedFormatDeserializer() { public ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider,
return new FixedFormatDeserializerImpl(); List<EdmxReference> references,
} ServiceMetadataETagSupport serviceMetadataETagSupport) {
return new ServiceMetadataImpl(edmProvider, references,
serviceMetadataETagSupport);
}
@Override @Override
public UriHelper createUriHelper() { public FixedFormatDeserializer createFixedFormatDeserializer() {
return new UriHelperImpl(); return new FixedFormatDeserializerImpl();
} }
@Override @Override
public ODataDeserializer createDeserializer(final ContentType contentType) throws DeserializerException { public UriHelper createUriHelper() {
ODataDeserializer deserializer; return new UriHelperImpl();
}
// odata.metadata=none, odata.metadata=minimal, odata.metadata=full
if(contentType.isCompatible(ContentType.JSON)) {
deserializer = new ODataJsonDeserializer(contentType);
//} else if(contentType.isCompatible(ContentType.APPLICATION_XML)) {
// We do not support XML deserialization right now so this must lead to an error.
} else {
throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
}
return deserializer;
}
@Override @Override
public EdmPrimitiveType createPrimitiveTypeInstance(final EdmPrimitiveTypeKind kind) { public ODataDeserializer createDeserializer(final ContentType contentType)
return EdmPrimitiveTypeFactory.getInstance(kind); throws DeserializerException {
} ODataDeserializer deserializer;
@Override // odata.metadata=none, odata.metadata=minimal, odata.metadata=full
public ETagHelper createETagHelper() { if (contentType.isCompatible(ContentType.JSON)) {
return new ETagHelperImpl(); deserializer = new ODataJsonDeserializer(contentType);
} // } else if(contentType.isCompatible(ContentType.APPLICATION_XML))
// We do not support XML deserialization right now so this must lead
// to an error.
// {
} else {
throw new DeserializerException("Unsupported format: "
+ contentType.toContentTypeString(),
DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
contentType.toContentTypeString());
}
@Override return deserializer;
public Preferences createPreferences(final Collection<String> preferHeaders) { }
return new PreferencesImpl(preferHeaders);
} @Override
public EdmPrimitiveType createPrimitiveTypeInstance(
final EdmPrimitiveTypeKind kind) {
return EdmPrimitiveTypeFactory.getInstance(kind);
}
@Override
public ETagHelper createETagHelper() {
return new ETagHelperImpl();
}
@Override
public Preferences createPreferences(final Collection<String> preferHeaders) {
return new PreferencesImpl(preferHeaders);
}
} }

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);
}
}