[OLINGO-234] Everything ready for V4 function / action import invoke, integration tests cannot be committed yet, need fit counterparts

This commit is contained in:
Francesco Chicchiriccò 2014-04-08 14:43:55 +02:00
parent 338ed70789
commit 4f59dba769
25 changed files with 397 additions and 159 deletions

View File

@ -51,16 +51,6 @@ public interface CommonODataBinder extends Serializable {
*/
Entry getEntry(CommonODataEntity entity, Class<? extends Entry> reference);
/**
* Gets an <tt>Entry</tt> from the given OData entity.
*
* @param entity OData entity.
* @param reference reference class.
* @param setType whether to explicitly output type information.
* @return <tt>Entry</tt> object.
*/
Entry getEntry(CommonODataEntity entity, Class<? extends Entry> reference, boolean setType);
/**
* Gets a <tt>Link</tt> from the given OData link.
*
@ -75,10 +65,9 @@ public interface CommonODataBinder extends Serializable {
*
* @param property OData property.
* @param reference reference class.
* @param setType whether to explicitly output type information.
* @return <tt>Property</tt> object.
*/
Property getProperty(CommonODataProperty property, Class<? extends Entry> reference, boolean setType);
Property getProperty(CommonODataProperty property, Class<? extends Entry> reference);
/**
* Adds the given property to the given entity.

View File

@ -45,16 +45,6 @@ public interface ODataWriter extends Serializable {
*/
InputStream writeEntities(Collection<CommonODataEntity> entities, ODataPubFormat format);
/**
* Writes a collection of OData entities.
*
* @param entities entities to be serialized.
* @param format serialization format.
* @param outputType whether to explicitly output type information.
* @return stream of serialized objects.
*/
InputStream writeEntities(Collection<CommonODataEntity> entities, ODataPubFormat format, boolean outputType);
/**
* Serializes a single OData entity.
*
@ -64,16 +54,6 @@ public interface ODataWriter extends Serializable {
*/
InputStream writeEntity(CommonODataEntity entity, ODataPubFormat format);
/**
* Serializes a single OData entity.
*
* @param entity entity to be serialized.
* @param format serialization format.
* @param outputType whether to explicitly output type information.
* @return stream of serialized object.
*/
InputStream writeEntity(CommonODataEntity entity, ODataPubFormat format, boolean outputType);
/**
* Writes a single OData entity property.
*

View File

@ -21,7 +21,6 @@ package org.apache.olingo.client.core.communication.request.invoke;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
@ -30,7 +29,6 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.client.api.communication.request.ODataBatchableRequest;
import org.apache.olingo.client.api.communication.request.invoke.ODataInvokeRequest;
@ -45,6 +43,7 @@ import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.commons.api.format.ODataPubFormat;
import org.apache.olingo.client.api.http.HttpClientException;
import org.apache.olingo.client.api.http.HttpMethod;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.client.core.uri.URIUtils;
import org.apache.olingo.client.core.communication.request.AbstractODataBasicRequest;
import org.apache.olingo.client.core.communication.response.AbstractODataResponse;
@ -52,7 +51,7 @@ import org.apache.olingo.client.core.communication.response.AbstractODataRespons
/**
* This class implements an OData invoke operation request.
*/
public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
public abstract class AbstractODataInvokeRequest<T extends ODataInvokeResult>
extends AbstractODataBasicRequest<ODataInvokeResponse<T>, ODataPubFormat>
implements ODataInvokeRequest<T>, ODataBatchableRequest {
@ -61,7 +60,7 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
/**
* Function parameters.
*/
private Map<String, ODataValue> parameters;
protected Map<String, ODataValue> parameters;
/**
* Constructor.
@ -71,7 +70,7 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
* @param method HTTP method of the request.
* @param uri URI that identifies the operation.
*/
public ODataInvokeRequestImpl(
public AbstractODataInvokeRequest(
final CommonODataClient odataClient,
final Class<T> reference,
final HttpMethod method,
@ -94,18 +93,24 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
}
}
private String getActualFormat(final ODataPubFormat format) {
return (CommonODataProperty.class.isAssignableFrom(reference) && format == ODataPubFormat.ATOM)
? ODataFormat.XML.toString(odataClient.getServiceVersion())
: format.toString(odataClient.getServiceVersion());
}
/**
* {@inheritDoc }
*/
@Override
public void setFormat(final ODataPubFormat format) {
final String _format = (reference.isAssignableFrom(CommonODataProperty.class) && format == ODataPubFormat.ATOM)
? ODataFormat.XML.toString(odataClient.getServiceVersion())
: format.toString(odataClient.getServiceVersion());
final String _format = getActualFormat(format);
setAccept(_format);
setContentType(_format);
}
protected abstract ODataPubFormat getPOSTParameterFormat();
@Override
protected InputStream getPayload() {
if (!this.parameters.isEmpty() && this.method == HttpMethod.POST) {
@ -123,6 +128,12 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
} else if (param.getValue().isCollection()) {
property = odataClient.getObjectFactory().
newCollectionProperty(param.getKey(), param.getValue().asCollection());
} else if (param.getValue() instanceof org.apache.olingo.commons.api.domain.v4.ODataValue
&& ((org.apache.olingo.commons.api.domain.v4.ODataValue) param.getValue()).isEnum()) {
property = ((ODataClient) odataClient).getObjectFactory().
newEnumProperty(param.getKey(),
((org.apache.olingo.commons.api.domain.v4.ODataValue) param.getValue()).asEnum());
}
if (property != null) {
@ -130,12 +141,14 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
}
}
return odataClient.getWriter().writeEntity(tmp, ODataPubFormat.JSON, false);
return odataClient.getWriter().writeEntity(tmp, getPOSTParameterFormat());
}
return null;
}
protected abstract URI buildGETURI();
/**
* {@inheritDoc }
*/
@ -145,23 +158,11 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
if (!this.parameters.isEmpty()) {
if (this.method == HttpMethod.GET) {
final URIBuilder uriBuilder = new URIBuilder(this.uri);
for (Map.Entry<String, ODataValue> param : parameters.entrySet()) {
if (!param.getValue().isPrimitive()) {
throw new IllegalArgumentException("Only primitive values can be passed via GET");
}
uriBuilder.addParameter(param.getKey(), URIUtils.escape(odataClient.getServiceVersion(), param.getValue()));
}
try {
((HttpRequestBase) this.request).setURI(uriBuilder.build());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("While adding GET parameters", e);
}
((HttpRequestBase) this.request).setURI(buildGETURI());
} else if (this.method == HttpMethod.POST) {
((HttpPost) request).setEntity(URIUtils.buildInputStreamEntity(odataClient, input));
setContentType(ODataPubFormat.JSON.toString(odataClient.getServiceVersion()));
setContentType(getActualFormat(getPOSTParameterFormat()));
}
}
@ -201,25 +202,24 @@ public class ODataInvokeRequestImpl<T extends ODataInvokeResult>
* {@inheritDoc }
*/
@Override
@SuppressWarnings("unchecked")
public T getBody() {
if (invokeResult == null) {
if (reference.isAssignableFrom(ODataNoContent.class)) {
invokeResult = (T) new ODataNoContent();
if (ODataNoContent.class.isAssignableFrom(reference)) {
invokeResult = reference.cast(new ODataNoContent());
}
try {
if (reference.isAssignableFrom(CommonODataEntitySet.class)) {
invokeResult = (T) odataClient.getReader().readEntitySet(res.getEntity().getContent(),
ODataPubFormat.fromString(getContentType()));
if (CommonODataEntitySet.class.isAssignableFrom(reference)) {
invokeResult = reference.cast(odataClient.getReader().readEntitySet(res.getEntity().getContent(),
ODataPubFormat.fromString(getContentType())));
}
if (reference.isAssignableFrom(CommonODataEntity.class)) {
invokeResult = (T) odataClient.getReader().readEntity(res.getEntity().getContent(),
ODataPubFormat.fromString(getContentType()));
if (CommonODataEntity.class.isAssignableFrom(reference)) {
invokeResult = reference.cast(odataClient.getReader().readEntity(res.getEntity().getContent(),
ODataPubFormat.fromString(getContentType())));
}
if (reference.isAssignableFrom(CommonODataProperty.class)) {
invokeResult = (T) odataClient.getReader().readProperty(res.getEntity().getContent(),
ODataFormat.fromString(getContentType()));
if (CommonODataProperty.class.isAssignableFrom(reference)) {
invokeResult = reference.cast(odataClient.getReader().readProperty(res.getEntity().getContent(),
ODataFormat.fromString(getContentType())));
}
} catch (IOException e) {
throw new HttpClientException(e);

View File

@ -24,14 +24,13 @@ import org.apache.olingo.client.api.v3.ODataClient;
import org.apache.olingo.client.api.communication.request.invoke.ODataInvokeRequest;
import org.apache.olingo.client.api.communication.request.invoke.ODataNoContent;
import org.apache.olingo.client.api.communication.request.invoke.v3.InvokeRequestFactory;
import org.apache.olingo.commons.api.domain.CommonODataEntity;
import org.apache.olingo.commons.api.domain.CommonODataEntitySet;
import org.apache.olingo.commons.api.domain.ODataInvokeResult;
import org.apache.olingo.commons.api.domain.CommonODataProperty;
import org.apache.olingo.commons.api.domain.ODataValue;
import org.apache.olingo.client.api.http.HttpMethod;
import org.apache.olingo.client.core.communication.request.invoke.AbstractInvokeRequestFactory;
import org.apache.olingo.client.core.communication.request.invoke.ODataInvokeRequestImpl;
import org.apache.olingo.commons.api.domain.v3.ODataEntity;
import org.apache.olingo.commons.api.domain.v3.ODataEntitySet;
import org.apache.olingo.commons.api.domain.v3.ODataProperty;
import org.apache.olingo.commons.api.edm.EdmAction;
import org.apache.olingo.commons.api.edm.EdmOperation;
import org.apache.olingo.commons.api.edm.EdmReturnType;
@ -61,14 +60,14 @@ public class InvokeRequestFactoryImpl extends AbstractInvokeRequestFactory imple
client, ODataNoContent.class, method, uri);
} else {
if (returnType.isCollection() && returnType.getType().getKind() == EdmTypeKind.ENTITY) {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<CommonODataEntitySet>(
client, CommonODataEntitySet.class, method, uri);
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataEntitySet>(
client, ODataEntitySet.class, method, uri);
} else if (!returnType.isCollection() && returnType.getType().getKind() == EdmTypeKind.ENTITY) {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<CommonODataEntity>(
client, CommonODataEntity.class, method, uri);
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataEntity>(
client, ODataEntity.class, method, uri);
} else {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<CommonODataProperty>(
client, CommonODataProperty.class, method, uri);
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataProperty>(
client, ODataProperty.class, method, uri);
}
}
if (parameters != null) {

View File

@ -0,0 +1,64 @@
/*
* 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.client.core.communication.request.invoke.v3;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.client.api.http.HttpMethod;
import org.apache.olingo.client.core.communication.request.invoke.AbstractODataInvokeRequest;
import org.apache.olingo.client.core.uri.URIUtils;
import org.apache.olingo.commons.api.domain.ODataInvokeResult;
import org.apache.olingo.commons.api.domain.ODataValue;
import org.apache.olingo.commons.api.format.ODataPubFormat;
public class ODataInvokeRequestImpl<T extends ODataInvokeResult> extends AbstractODataInvokeRequest<T> {
public ODataInvokeRequestImpl(final CommonODataClient odataClient, final Class<T> reference, final HttpMethod method,
final URI uri) {
super(odataClient, reference, method, uri);
}
@Override
protected ODataPubFormat getPOSTParameterFormat() {
return ODataPubFormat.JSON;
}
@Override
protected URI buildGETURI() {
final URIBuilder uriBuilder = new URIBuilder(this.uri);
for (Map.Entry<String, ODataValue> param : parameters.entrySet()) {
if (!param.getValue().isPrimitive()) {
throw new IllegalArgumentException("Only primitive values can be passed via GET");
}
uriBuilder.addParameter(param.getKey(), URIUtils.escape(odataClient.getServiceVersion(), param.getValue()));
}
try {
return uriBuilder.build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("While adding GET parameters", e);
}
}
}

View File

@ -20,14 +20,21 @@ package org.apache.olingo.client.core.communication.request.invoke.v4;
import java.net.URI;
import java.util.Map;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.client.api.communication.request.invoke.ODataInvokeRequest;
import org.apache.olingo.client.api.communication.request.invoke.ODataNoContent;
import org.apache.olingo.client.api.communication.request.invoke.v4.InvokeRequestFactory;
import org.apache.olingo.client.api.http.HttpMethod;
import org.apache.olingo.commons.api.domain.ODataInvokeResult;
import org.apache.olingo.commons.api.domain.ODataValue;
import org.apache.olingo.client.core.communication.request.invoke.AbstractInvokeRequestFactory;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataEntitySet;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.edm.EdmAction;
import org.apache.olingo.commons.api.edm.EdmOperation;
import org.apache.olingo.commons.api.edm.EdmReturnType;
import org.apache.olingo.commons.api.edm.constants.EdmTypeKind;
public class InvokeRequestFactoryImpl extends AbstractInvokeRequestFactory implements InvokeRequestFactory {
@ -37,10 +44,36 @@ public class InvokeRequestFactoryImpl extends AbstractInvokeRequestFactory imple
super(client);
}
@SuppressWarnings("unchecked")
@Override
public <RES extends ODataInvokeResult> ODataInvokeRequest<RES> getInvokeRequest(
final URI uri, final EdmOperation operation, final Map<String, ODataValue> parameters) {
throw new NotImplementedException("Not available yet.");
final HttpMethod method = operation instanceof EdmAction
? HttpMethod.POST
: HttpMethod.GET;
final EdmReturnType returnType = operation.getReturnType();
ODataInvokeRequest<RES> request;
if (returnType == null) {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataNoContent>(
client, ODataNoContent.class, method, uri);
} else {
if (returnType.isCollection() && returnType.getType().getKind() == EdmTypeKind.ENTITY) {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataEntitySet>(
client, ODataEntitySet.class, method, uri);
} else if (!returnType.isCollection() && returnType.getType().getKind() == EdmTypeKind.ENTITY) {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataEntity>(
client, ODataEntity.class, method, uri);
} else {
request = (ODataInvokeRequest<RES>) new ODataInvokeRequestImpl<ODataProperty>(
client, ODataProperty.class, method, uri);
}
}
if (parameters != null) {
request.setParameters(parameters);
}
return request;
}
}

View File

@ -0,0 +1,92 @@
/*
* 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.client.core.communication.request.invoke.v4;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Map;
import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.client.api.http.HttpMethod;
import org.apache.olingo.client.core.communication.request.invoke.AbstractODataInvokeRequest;
import org.apache.olingo.client.core.uri.URIUtils;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.domain.ODataInvokeResult;
import org.apache.olingo.commons.api.domain.ODataValue;
import org.apache.olingo.commons.api.format.ODataPubFormat;
public class ODataInvokeRequestImpl<T extends ODataInvokeResult> extends AbstractODataInvokeRequest<T> {
private ODataPubFormat format;
public ODataInvokeRequestImpl(final CommonODataClient odataClient, final Class<T> reference, final HttpMethod method,
final URI uri) {
super(odataClient, reference, method, uri);
}
@Override
public void setFormat(final ODataPubFormat format) {
super.setFormat(format);
this.format = format;
}
@Override
protected ODataPubFormat getPOSTParameterFormat() {
return format;
}
@Override
protected URI buildGETURI() {
String baseURI = this.uri.toASCIIString();
if (baseURI.endsWith("()")) {
baseURI = baseURI.substring(0, baseURI.length() - 2);
} else if (!baseURI.endsWith("(")) {
baseURI = baseURI.substring(0, baseURI.length() - 1);
}
final StringBuilder inlineParams = new StringBuilder();
for (Map.Entry<String, ODataValue> param : parameters.entrySet()) {
inlineParams.append(param.getKey()).append("=");
Object value = null;
if (param.getValue().isPrimitive()) {
value = param.getValue().asPrimitive().toValue();
} else if (param.getValue().isComplex()) {
value = param.getValue().asComplex().asJavaMap();
} else if (param.getValue().isCollection()) {
value = param.getValue().asCollection().asJavaCollection();
} else if (param.getValue() instanceof org.apache.olingo.commons.api.domain.v4.ODataValue
&& ((org.apache.olingo.commons.api.domain.v4.ODataValue) param.getValue()).isEnum()) {
value = ((org.apache.olingo.commons.api.domain.v4.ODataValue) param.getValue()).asEnum().toString();
}
inlineParams.append(URIUtils.escape(odataClient.getServiceVersion(), value)).append(',');
}
inlineParams.deleteCharAt(inlineParams.length() - 1);
try {
return URI.create(baseURI + "(" + URLEncoder.encode(inlineParams.toString(), Constants.UTF8) + ")");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("While adding GET parameters", e);
}
}
}

View File

@ -110,11 +110,6 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
return feed;
}
@Override
public Entry getEntry(final CommonODataEntity entity, final Class<? extends Entry> reference) {
return getEntry(entity, reference, true);
}
protected void links(final ODataLinked odataLinked, final Linked linked, final Class<? extends Entry> reference) {
// -------------------------------------------------------------
// Append navigation links (handling inline entry / feed as well)
@ -140,7 +135,7 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
}
@Override
public Entry getEntry(final CommonODataEntity entity, final Class<? extends Entry> reference, final boolean setType) {
public Entry getEntry(final CommonODataEntity entity, final Class<? extends Entry> reference) {
final Entry entry = ResourceFactory.newEntry(reference);
entry.setType(entity.getName());
@ -185,7 +180,7 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
}
for (CommonODataProperty property : entity.getProperties()) {
entry.getProperties().add(getProperty(property, reference, setType));
entry.getProperties().add(getProperty(property, reference));
}
return entry;
@ -217,7 +212,7 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
return linkResource;
}
protected Value getValue(final ODataValue value, final Class<? extends Entry> reference, final boolean setType) {
protected Value getValue(final ODataValue value, final Class<? extends Entry> reference) {
Value valueResource = null;
if (value == null) {
@ -231,14 +226,14 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
valueResource = new ComplexValueImpl();
for (final Iterator<? extends CommonODataProperty> itor = _value.iterator(); itor.hasNext();) {
valueResource.asComplex().get().add(getProperty(itor.next(), reference, setType));
valueResource.asComplex().get().add(getProperty(itor.next(), reference));
}
} else if (value.isCollection()) {
final ODataCollectionValue<? extends ODataValue> _value = value.asCollection();
valueResource = new CollectionValueImpl();
for (final Iterator<? extends ODataValue> itor = _value.iterator(); itor.hasNext();) {
valueResource.asCollection().get().add(getValue(itor.next(), reference, setType));
valueResource.asCollection().get().add(getValue(itor.next(), reference));
}
}

View File

@ -45,18 +45,11 @@ public class ODataWriterImpl implements ODataWriter {
@Override
public InputStream writeEntities(final Collection<CommonODataEntity> entities, final ODataPubFormat format) {
return writeEntities(entities, format, true);
}
@Override
public InputStream writeEntities(
final Collection<CommonODataEntity> entities, final ODataPubFormat format, final boolean outputType) {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
for (CommonODataEntity entity : entities) {
client.getSerializer().entry(client.getBinder().getEntry(
entity, ResourceFactory.entryClassForFormat(format == ODataPubFormat.ATOM), outputType), output);
entity, ResourceFactory.entryClassForFormat(format == ODataPubFormat.ATOM)), output);
}
return new ByteArrayInputStream(output.toByteArray());
@ -67,14 +60,7 @@ public class ODataWriterImpl implements ODataWriter {
@Override
public InputStream writeEntity(final CommonODataEntity entity, final ODataPubFormat format) {
return writeEntity(entity, format, true);
}
@Override
public InputStream writeEntity(final CommonODataEntity entity, final ODataPubFormat format,
final boolean outputType) {
return writeEntities(Collections.<CommonODataEntity>singleton(entity), format, outputType);
return writeEntities(Collections.<CommonODataEntity>singleton(entity), format);
}
@Override
@ -82,7 +68,7 @@ public class ODataWriterImpl implements ODataWriter {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
client.getSerializer().property(client.getBinder().getProperty(
property, ResourceFactory.entryClassForFormat(format == ODataFormat.XML), true), output);
property, ResourceFactory.entryClassForFormat(format == ODataFormat.XML)), output);
return new ByteArrayInputStream(output.toByteArray());
} finally {

View File

@ -56,21 +56,17 @@ public class ODataBinderImpl extends AbstractODataBinder implements ODataBinder
}
@Override
public Property getProperty(final CommonODataProperty property, final Class<? extends Entry> reference,
final boolean setType) {
public Property getProperty(final CommonODataProperty property, final Class<? extends Entry> reference) {
final Property propertyResource = ResourceFactory.newProperty(reference);
propertyResource.setName(property.getName());
propertyResource.setValue(getValue(property.getValue(), reference, setType));
propertyResource.setValue(getValue(property.getValue(), reference));
if (setType) {
if (property.hasPrimitiveValue()) {
propertyResource.setType(property.getPrimitiveValue().getTypeName());
} else if (property.hasComplexValue()) {
propertyResource.setType(((ODataProperty) property).getComplexValue().getTypeName());
} else if (property.hasCollectionValue()) {
propertyResource.setType(((ODataProperty) property).getCollectionValue().getTypeName());
}
if (property.hasPrimitiveValue()) {
propertyResource.setType(property.getPrimitiveValue().getTypeName());
} else if (property.hasComplexValue()) {
propertyResource.setType(((ODataProperty) property).getComplexValue().getTypeName());
} else if (property.hasCollectionValue()) {
propertyResource.setType(((ODataProperty) property).getCollectionValue().getTypeName());
}
return propertyResource;

View File

@ -88,39 +88,35 @@ public class ODataBinderImpl extends AbstractODataBinder implements ODataBinder
}
@Override
public Entry getEntry(final CommonODataEntity entity, final Class<? extends Entry> reference, final boolean setType) {
final Entry entry = super.getEntry(entity, reference, setType);
public Entry getEntry(final CommonODataEntity entity, final Class<? extends Entry> reference) {
final Entry entry = super.getEntry(entity, reference);
entry.setId(((ODataEntity) entity).getReference());
return entry;
}
@Override
public Property getProperty(final CommonODataProperty property, final Class<? extends Entry> reference,
final boolean setType) {
public Property getProperty(final CommonODataProperty property, final Class<? extends Entry> reference) {
final ODataProperty _property = (ODataProperty) property;
final Property propertyResource = ResourceFactory.newProperty(reference);
propertyResource.setName(_property.getName());
propertyResource.setValue(getValue(_property.getValue(), reference, setType));
propertyResource.setValue(getValue(_property.getValue(), reference));
if (setType) {
if (_property.hasPrimitiveValue()) {
propertyResource.setType(_property.getPrimitiveValue().getTypeName());
} else if (_property.hasEnumValue()) {
propertyResource.setType(_property.getEnumValue().getTypeName());
} else if (_property.hasComplexValue()) {
propertyResource.setType(_property.getComplexValue().getTypeName());
} else if (_property.hasCollectionValue()) {
propertyResource.setType(_property.getCollectionValue().getTypeName());
}
if (_property.hasPrimitiveValue()) {
propertyResource.setType(_property.getPrimitiveValue().getTypeName());
} else if (_property.hasEnumValue()) {
propertyResource.setType(_property.getEnumValue().getTypeName());
} else if (_property.hasComplexValue()) {
propertyResource.setType(_property.getComplexValue().getTypeName());
} else if (_property.hasCollectionValue()) {
propertyResource.setType(_property.getCollectionValue().getTypeName());
}
return propertyResource;
}
@Override
protected Value getValue(final ODataValue value, final Class<? extends Entry> reference, final boolean setType) {
protected Value getValue(final ODataValue value, final Class<? extends Entry> reference) {
Value valueResource;
if (value instanceof org.apache.olingo.commons.api.domain.v4.ODataValue
&& ((org.apache.olingo.commons.api.domain.v4.ODataValue) value).isEnum()) {
@ -128,7 +124,7 @@ public class ODataBinderImpl extends AbstractODataBinder implements ODataBinder
valueResource = new EnumValueImpl(
((org.apache.olingo.commons.api.domain.v4.ODataValue) value).asEnum().getValue());
} else {
valueResource = super.getValue(value, reference, setType);
valueResource = super.getValue(value, reference);
if (value instanceof org.apache.olingo.commons.api.domain.v4.ODataValue
&& ((org.apache.olingo.commons.api.domain.v4.ODataValue) value).isLinkedComplex()) {

View File

@ -41,7 +41,6 @@ import org.apache.http.entity.InputStreamEntity;
import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
import org.apache.olingo.commons.api.edm.EdmOperationImport;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
@ -143,17 +142,17 @@ public final class URIUtils {
* Gets operation import URI segment.
*
* @param entityContainer entity container.
* @param operationImport function import.
* @param operationImportName action / function import name.
* @return URI segment.
*/
public static String operationImportURISegment(
final EdmEntityContainer entityContainer, final EdmOperationImport operationImport) {
final EdmEntityContainer entityContainer, final String operationImportName) {
final StringBuilder result = new StringBuilder();
if (!entityContainer.isDefault()) {
result.append(entityContainer.getName()).append('.');
}
result.append(operationImport.getName());
result.append(operationImportName);
return result.toString();
}
@ -282,12 +281,11 @@ public final class URIUtils {
private static String quoteString(final String string, final boolean singleQuoteEscape)
throws UnsupportedEncodingException {
final String encoded = URLEncoder.encode(string, Constants.UTF8);
return ENUM_VALUE.matcher(string).matches()
? encoded
? string
: singleQuoteEscape
? "'" + encoded + "'"
: "\"" + encoded + "\"";
? "'" + string + "'"
: "\"" + string + "\"";
}
/**
@ -322,11 +320,11 @@ public final class URIUtils {
} else if (version.compareTo(ODataServiceVersion.V40) >= 0 && obj instanceof Map) {
final StringBuffer buffer = new StringBuffer("{");
for (@SuppressWarnings("unchecked")
final Iterator<Map.Entry<Object, Object>> itor =
((Map<Object, Object>) obj).entrySet().iterator(); itor.hasNext();) {
final Iterator<Map.Entry<String, Object>> itor =
((Map<String, Object>) obj).entrySet().iterator(); itor.hasNext();) {
final Map.Entry<Object, Object> entry = itor.next();
buffer.append("\"").append(URLEncoder.encode(entry.getKey().toString(), Constants.UTF8)).append("\"");
final Map.Entry<String, Object> entry = itor.next();
buffer.append("\"").append(entry.getKey()).append("\"");
buffer.append(':').append(escape(version, entry.getValue(), false));
if (itor.hasNext()) {

View File

@ -62,7 +62,7 @@ public class ActionOverloadingTestITCase extends AbstractTestITCase {
assertEquals(EdmInt32.getInstance(), unbound.getReturnType().getType());
final URIBuilder unboundBuilder = getClient().getURIBuilder(testActionOverloadingServiceRootURL).
appendOperationCallSegment(URIUtils.operationImportURISegment(container, actImp));
appendOperationCallSegment(URIUtils.operationImportURISegment(container, actImp.getName()));
final ODataInvokeResponse<ODataProperty> unboundRes = getClient().getInvokeRequestFactory().
<ODataProperty>getInvokeRequest(unboundBuilder.build(), unbound).execute();
assertNotNull(unboundRes);

View File

@ -153,7 +153,7 @@ public class ErrorTestITCase extends AbstractTestITCase {
final EdmEntityContainer container = metadata.getSchemas().get(0).getEntityContainer();
final EdmFunctionImport funcImp = container.getFunctionImport("InStreamErrorGetCustomer");
final URIBuilder builder = client.getURIBuilder(testStaticServiceRootURL).
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp));
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp.getName()));
final ODataInvokeRequest<ODataEntitySet> req =
client.getInvokeRequestFactory().getInvokeRequest(builder.build(), funcImp.getUnboundFunction(null));
req.setFormat(format);

View File

@ -73,7 +73,7 @@ public class InvokeTestITCase extends AbstractTestITCase {
EdmFunction func = funcImp.getUnboundFunction(null);
URIBuilder builder = getClient().getURIBuilder(testStaticServiceRootURL).
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp));
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp.getName()));
ODataInvokeRequest<ODataProperty> req = getClient().getInvokeRequestFactory().
getInvokeRequest(builder.build(), func);
@ -90,7 +90,7 @@ public class InvokeTestITCase extends AbstractTestITCase {
func = funcImp.getUnboundFunction(null);
builder = getClient().getURIBuilder(testStaticServiceRootURL).
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp));
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp.getName()));
req = getClient().getInvokeRequestFactory().getInvokeRequest(builder.build(), func);
req.setFormat(format);
@ -123,7 +123,7 @@ public class InvokeTestITCase extends AbstractTestITCase {
EdmFunctionImport funcImp = container.getFunctionImport("GetArgumentPlusOne");
URIBuilder builder = getClient().getURIBuilder(testStaticServiceRootURL).
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp));
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp.getName()));
EdmFunction function = funcImp.getUnboundFunction(Collections.singletonList("arg1"));
EdmParameter param = function.getParameter(function.getParameterNames().get(0));
@ -148,7 +148,7 @@ public class InvokeTestITCase extends AbstractTestITCase {
funcImp = container.getFunctionImport("GetSpecificCustomer");
builder = getClient().getURIBuilder(testStaticServiceRootURL).
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp));
appendOperationCallSegment(URIUtils.operationImportURISegment(container, funcImp.getName()));
function = funcImp.getUnboundFunction(Collections.singletonList("Name"));
param = function.getParameter(function.getParameterNames().get(0));

View File

@ -51,8 +51,7 @@ public class URIEscapeTest {
final EdmEnumType pattern = new EdmEnumTypeImpl(ODataServiceVersion.V40,
null, new FullQualifiedName("Sales", "Pattern"), new EnumTypeImpl());
assertEquals(URLEncoder.encode("Sales.Pattern'Yellow'", Constants.UTF8),
URIUtils.escape(ODataServiceVersion.V40, pattern.toUriLiteral("Yellow")));
assertEquals("Sales.Pattern'Yellow'", URIUtils.escape(ODataServiceVersion.V40, pattern.toUriLiteral("Yellow")));
}
@Test

View File

@ -18,6 +18,8 @@
*/
package org.apache.olingo.commons.api.domain;
import java.util.Collection;
/**
* OData collection property value.
*
@ -45,4 +47,11 @@ public interface ODataCollectionValue<OV extends ODataValue> extends ODataValue,
* @return collection size.
*/
int size();
/**
* Converts this instance as POJO collection.
*
* @return this instance as POJO collection
*/
Collection<Object> asJavaCollection();
}

View File

@ -18,6 +18,8 @@
*/
package org.apache.olingo.commons.api.domain;
import java.util.Map;
/**
* OData complex property value.
*
@ -47,4 +49,10 @@ public interface ODataComplexValue<OP extends CommonODataProperty> extends OData
*/
int size();
/**
* Converts this instance as POJO collection.
*
* @return this instance as POJO collection
*/
Map<String, Object> asJavaMap();
}

View File

@ -21,4 +21,7 @@ package org.apache.olingo.commons.api.domain.v4;
public interface ODataEnumValue extends ODataValue {
String getValue();
@Override
String toString();
}

View File

@ -19,6 +19,7 @@
package org.apache.olingo.commons.core.domain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.olingo.commons.api.domain.AbstractODataValue;
@ -38,7 +39,7 @@ public abstract class AbstractODataCollectionValue<OV extends ODataValue>
/**
* Values.
*/
private final List<OV> values = new ArrayList<OV>();
protected final List<OV> values = new ArrayList<OV>();
/**
* Constructor.
@ -89,4 +90,21 @@ public abstract class AbstractODataCollectionValue<OV extends ODataValue>
public boolean isEmpty() {
return values.isEmpty();
}
@Override
public Collection<Object> asJavaCollection() {
final List<Object> result = new ArrayList<Object>();
for (OV value : values) {
if (value.isPrimitive()) {
result.add(value.asPrimitive().toValue());
} else if (value.isComplex()) {
result.add(value.asComplex().asJavaMap());
} else if (value.isCollection()) {
result.add(value.asCollection().asJavaCollection());
}
}
return result;
}
}

View File

@ -38,7 +38,7 @@ public abstract class AbstractODataComplexValue<OP extends CommonODataProperty>
/**
* Complex type fields.
*/
private final Map<String, OP> fields = new LinkedHashMap<String, OP>();
protected final Map<String, OP> fields = new LinkedHashMap<String, OP>();
/**
* Constructor.
@ -90,4 +90,24 @@ public abstract class AbstractODataComplexValue<OP extends CommonODataProperty>
public int size() {
return fields.size();
}
@Override
public Map<String, Object> asJavaMap() {
final Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Map.Entry<String, OP> entry : fields.entrySet()) {
Object value = null;
if (entry.getValue().hasPrimitiveValue()) {
value = entry.getValue().getPrimitiveValue().toValue();
} else if (entry.getValue().hasComplexValue()) {
value = entry.getValue().getValue().asComplex().asJavaMap();
} else if (entry.getValue().hasCollectionValue()) {
value = entry.getValue().getValue().asCollection().asJavaCollection();
}
result.put(entry.getKey(), value);
}
return result;
}
}

View File

@ -18,6 +18,9 @@
*/
package org.apache.olingo.commons.core.domain.v4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.olingo.commons.api.domain.v4.ODataLinkedComplexValue;
import org.apache.olingo.commons.api.domain.v4.ODataEnumValue;
import org.apache.olingo.commons.api.domain.v4.ODataValue;
@ -50,4 +53,22 @@ public class ODataCollectionValueImpl extends AbstractODataCollectionValue<OData
public ODataLinkedComplexValue asLinkedComplex() {
return null;
}
@Override
public Collection<Object> asJavaCollection() {
final List<Object> result = new ArrayList<Object>();
for (ODataValue value : values) {
if (value.isPrimitive()) {
result.add(value.asPrimitive().toValue());
} else if (value.isComplex()) {
result.add(value.asComplex().asJavaMap());
} else if (value.isCollection()) {
result.add(value.asCollection().asJavaCollection());
} else if (value.isEnum()) {
result.add(value.asEnum().toString());
}
}
return result;
}
}

View File

@ -19,7 +19,9 @@
package org.apache.olingo.commons.core.domain.v4;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.olingo.commons.api.domain.ODataLink;
import org.apache.olingo.commons.api.domain.v4.ODataLinkedComplexValue;
import org.apache.olingo.commons.api.domain.v4.ODataEnumValue;
@ -123,4 +125,25 @@ public class ODataComplexValueImpl extends AbstractODataComplexValue<ODataProper
return associationLinks;
}
@Override
public Map<String, Object> asJavaMap() {
final Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Map.Entry<String, ODataProperty> entry : fields.entrySet()) {
Object value = null;
if (entry.getValue().hasPrimitiveValue()) {
value = entry.getValue().getPrimitiveValue().toValue();
} else if (entry.getValue().hasComplexValue()) {
value = entry.getValue().getComplexValue().asJavaMap();
} else if (entry.getValue().hasCollectionValue()) {
value = entry.getValue().getCollectionValue().asJavaCollection();
} else if (entry.getValue().hasEnumValue()) {
value = entry.getValue().getEnumValue().toString();
}
result.put(entry.getKey(), value);
}
return result;
}
}

View File

@ -57,4 +57,11 @@ public class ODataEnumValueImpl extends AbstractODataValue implements ODataEnumV
public ODataLinkedComplexValue asLinkedComplex() {
return null;
}
@Override
public String toString() {
return getTypeName() + "'" + getValue() + "'";
}
}

View File

@ -49,7 +49,9 @@ public class ODataPropertyImpl extends AbstractODataProperty implements ODataPro
@Override
public ODataComplexValue<ODataProperty> getComplexValue() {
return hasComplexValue() ? getValue().<ODataProperty>asComplex() : null;
return hasComplexValue()
? getValue().<ODataProperty>asComplex()
: null;
}
@Override