diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractPersistenceManager.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractPersistenceManager.java index 917ea7b97..a45df6fb0 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractPersistenceManager.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractPersistenceManager.java @@ -479,10 +479,12 @@ abstract class AbstractPersistenceManager implements PersistenceManager { final PersistenceChanges changeset) { LOG.debug("Update '{}'", targetRef); + URI sericeRoot = handler.getClient().newURIBuilder(handler.getClient().getServiceRoot()).build(); + if (service.getClient().getServiceVersion().compareTo(ODataServiceVersion.V30) >= 1) { final ODataReferenceAddingRequest req = ((org.apache.olingo.client.api.v4.EdmEnabledODataClient) service.getClient()).getCUDRequestFactory(). - getReferenceAddingRequest(source, targetRef); + getReferenceAddingRequest(sericeRoot, source, targetRef); req.setPrefer(new ODataPreferences(service.getClient().getServiceVersion()).returnContent()); diff --git a/fit/src/main/java/org/apache/olingo/fit/V4Services.java b/fit/src/main/java/org/apache/olingo/fit/V4Services.java index b370cb83c..6af5e2e79 100644 --- a/fit/src/main/java/org/apache/olingo/fit/V4Services.java +++ b/fit/src/main/java/org/apache/olingo/fit/V4Services.java @@ -61,6 +61,7 @@ import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.NotFoundException; import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -70,6 +71,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -85,6 +87,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; import java.util.regex.Pattern; + import javax.ws.rs.BadRequestException; @Service @@ -175,7 +178,29 @@ public class V4Services extends AbstractServices { return xml.createFaultResponse(Accept.JSON_FULLMETA.toString(), e); } } + + @PUT + @Path("/People(1)/Parent") + @SuppressWarnings("unused") + public Response changeSingleValuedNavigationPropertyReference( + @Context final UriInfo uriInfo, + @HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept, + @HeaderParam("Content-Type") @DefaultValue(StringUtils.EMPTY) final String contentType, + final String content) { + try { + final Accept contentTypeValue = Accept.parse(contentType, version); + assert contentTypeValue == Accept.JSON; + + ResWrap entity = jsonDeserializer.toEntity(IOUtils.toInputStream(content, Constants.ENCODING)); + + return Response.noContent().type(MediaType.APPLICATION_JSON).build(); + }catch (Exception e) { + LOG.error("While update single property reference", e); + return xml.createFaultResponse(accept, e); + } + } + @POST @Path("/async/$batch") public Response async( diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/APIBasicDesignTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/APIBasicDesignTestITCase.java index 2f9191657..2aa5fbda3 100644 --- a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/APIBasicDesignTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/APIBasicDesignTestITCase.java @@ -31,6 +31,7 @@ import java.math.BigDecimal; import java.sql.Timestamp; import java.util.Calendar; import java.util.TimeZone; + import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.olingo.client.api.v4.EdmEnabledODataClient; @@ -106,7 +107,23 @@ public class APIBasicDesignTestITCase extends AbstractTestITCase { assertEquals("http://localhost:9080/stub/StaticService/V40/Static.svc/Orders(7)", orders.iterator().next().readEntityReferenceID()); } - + + @Test + public void changeSingleNavigationProperty() { + /* + * See OData Spec 11.4.6.3 + * Alternatively, a relationship MAY be updated as part of an update to the source entity by including + * the required binding information for the new target entity. + * + * => use PATCH instead of PUT + */ + final Person person1 = container.getPeople().getByKey(1).load(); + final Person person5 = container.getPeople().getByKey(5).load(); + + person1.setParent(person5); + container.flush(); + } + @Test public void addViaReference() { final Order order = container.getOrders().getByKey(8).load(); diff --git a/fit/src/test/java/org/apache/olingo/fit/v4/EntityUpdateTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/v4/EntityUpdateTestITCase.java index 267036815..3f6ab926d 100644 --- a/fit/src/test/java/org/apache/olingo/fit/v4/EntityUpdateTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/v4/EntityUpdateTestITCase.java @@ -18,16 +18,8 @@ */ package org.apache.olingo.fit.v4; -import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest; -import org.apache.olingo.client.api.communication.request.cud.v4.UpdateType; -import org.apache.olingo.client.api.communication.response.ODataEntityUpdateResponse; -import org.apache.olingo.commons.api.domain.ODataLink; -import org.apache.olingo.commons.api.domain.v4.ODataEntity; -import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; -import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; -import org.apache.olingo.commons.api.edm.FullQualifiedName; -import org.apache.olingo.commons.api.format.ODataFormat; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.math.BigDecimal; import java.net.URI; @@ -35,8 +27,19 @@ import java.util.Calendar; import java.util.TimeZone; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest; +import org.apache.olingo.client.api.communication.request.cud.v4.ODataReferenceAddingRequest; +import org.apache.olingo.client.api.communication.request.cud.v4.UpdateType; +import org.apache.olingo.client.api.communication.response.ODataEntityUpdateResponse; +import org.apache.olingo.client.api.communication.response.v4.ODataReferenceAddingResponse; +import org.apache.olingo.commons.api.domain.ODataLink; +import org.apache.olingo.commons.api.domain.v4.ODataEntity; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.format.ODataFormat; +import org.junit.Ignore; +import org.junit.Test; public class EntityUpdateTestITCase extends AbstractTestITCase { @@ -74,6 +77,30 @@ public class EntityUpdateTestITCase extends AbstractTestITCase { } } + @Test + @Ignore + public void testUpateSingleValuedNavtiogationReference() throws Exception { + URI targetURI = + getClient().newURIBuilder(testStaticServiceRootURL) + .appendEntitySetSegment("People") + .appendKeySegment(1) + .appendNavigationSegment("Parent") + .build(); + + URI reference = getClient().newURIBuilder(testStaticServiceRootURL) + .appendEntitySetSegment("People") + .appendKeySegment(0) + .build(); + + final ODataReferenceAddingRequest request = + getClient().getCUDRequestFactory().getReferenceSingleChangeRequest(new URI(testStaticServiceRootURL), + targetURI, reference); + + final ODataReferenceAddingResponse response = request.execute(); + + assertEquals(204, response.getStatusCode()); + } + @Test public void atomUpsert() { upsert(UpdateType.PATCH, ODataFormat.ATOM); diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/CUDRequestFactory.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/CUDRequestFactory.java index 54884fe7f..0e4900d20 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/CUDRequestFactory.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/CUDRequestFactory.java @@ -1,18 +1,18 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * 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 - * + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -27,20 +27,32 @@ import org.apache.olingo.commons.api.domain.v4.ODataSingleton; public interface CUDRequestFactory extends CommonCUDRequestFactory { ODataEntityUpdateRequest getSingletonUpdateRequest( - URI targetURI, UpdateType type, ODataSingleton changes); + URI targetURI, UpdateType type, ODataSingleton changes); ODataEntityUpdateRequest getSingletonUpdateRequest( - UpdateType type, ODataSingleton entity); + UpdateType type, ODataSingleton entity); /** * A successful POST request to a navigation property's references collection adds a relationship to an existing - * entity. The request body MUST contain a single entity reference that identifies the entity to be added. See the - * appropriate format document for details. On successful completion, the response MUST be 204 No Content and contain - * an empty body. - * - * @param targetURI entity set URI + * entity. The request body MUST contain a single entity reference that identifies the entity to be added. + * [OData-Protocol 4.0 - 11.4.6.1] + * + * @param serviceRoot serviceRoot URI + * @param targetURI navigation property reference collection URI * @param reference entity reference - * @return new ODataEntityCreateRequest instance. + * @return new ODataReferenceAddingRequest instance. */ - ODataReferenceAddingRequest getReferenceAddingRequest(URI targetURI, URI reference); + ODataReferenceAddingRequest getReferenceAddingRequest(URI serviceRoot, URI targetURI, URI reference); + + /** + * A successful PUT request to a single-valued navigation property�s reference resource changes the related entity. + * The request body MUST contain a single entity reference that identifies the existing entity to be related. + * [OData-Protocol 4.0 - 11.4.6.3] + * + * @param serviceRoot serviceRoot URI + * @param targetURI single-valued navigation property URI + * @param reference reference + * @return new ODataReferenceAddingRequest instance + */ + ODataReferenceAddingRequest getReferenceSingleChangeRequest(URI serviceRoot, URI targetURI, URI reference); } diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/ODataReferenceAddingRequest.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/ODataReferenceAddingRequest.java index d107eb32e..73a6ef43e 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/ODataReferenceAddingRequest.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/cud/v4/ODataReferenceAddingRequest.java @@ -23,7 +23,15 @@ import org.apache.olingo.client.api.communication.request.ODataBatchableRequest; import org.apache.olingo.client.api.communication.response.v4.ODataReferenceAddingResponse; /** - * This class implements an OData delete request. + * This class implements an OData reference adding request. + * + * ODataReferenceAdding requests eighter add or change the reference of navigation properties. + * + * If the navigation property is a collection of navigation references, the request adds a new reference to the + * collection. [OData Protocol 4.0 - 11.4.6.1] + * + * If the request addresses an navigation property, which references a single entity, the reference will + * be changed to the value provided by the request. [OData-Protocol 4.0 - 11.4.6.3] */ public interface ODataReferenceAddingRequest extends ODataBasicRequest, ODataBatchableRequest { diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/response/v4/ODataReferenceAddingResponse.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/response/v4/ODataReferenceAddingResponse.java index 8c854612b..9527d38f2 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/response/v4/ODataReferenceAddingResponse.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/response/v4/ODataReferenceAddingResponse.java @@ -21,9 +21,12 @@ package org.apache.olingo.client.api.communication.response.v4; import org.apache.olingo.client.api.communication.response.ODataResponse; /** - * This class implements the response to an OData delete request. + * This class implements the response to an OData Reference Adding request. * - * @see org.apache.olingo.client.api.communication.request.cud.ODataDeleteRequest + * If the request was successful, the service response has status code 204 and + * the body has to be empty. + * + * @see org.apache.olingo.api.request.cud.v4.ODataReferenceAddingRequest */ public interface ODataReferenceAddingResponse extends ODataResponse { } diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/serialization/ODataWriter.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/serialization/ODataWriter.java index 87114870e..0e9298974 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/serialization/ODataWriter.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/serialization/ODataWriter.java @@ -19,8 +19,10 @@ package org.apache.olingo.client.api.serialization; import java.io.InputStream; +import java.net.URI; import java.util.Collection; +import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.domain.CommonODataEntity; import org.apache.olingo.commons.api.domain.CommonODataProperty; import org.apache.olingo.commons.api.domain.ODataLink; @@ -79,4 +81,15 @@ public interface ODataWriter { */ InputStream writeLink(ODataLink link, ODataFormat format) throws ODataSerializerException; + + /** + * Writes a entity reference + * + * @param reference reference to be serialized + * @param format serialization format + * @return stream of serialized objects + * @throws ODataSerializerException + */ + InputStream writeReference(ResWrap reference, ODataFormat format) + throws ODataSerializerException; } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v3/ODataLinkUpdateRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v3/ODataLinkUpdateRequestImpl.java index b182d8928..b39f424f4 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v3/ODataLinkUpdateRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v3/ODataLinkUpdateRequestImpl.java @@ -55,7 +55,7 @@ public class ODataLinkUpdateRequestImpl extends AbstractODataBasicRequest odataClient, + public ODataLinkUpdateRequestImpl(final CommonODataClient odataClient, final HttpMethod method, final URI targetURI, final ODataLink link) { super(odataClient, method, targetURI); // set request body diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/CUDRequestFactoryImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/CUDRequestFactoryImpl.java index f53e98587..fdf0cd7d9 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/CUDRequestFactoryImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/CUDRequestFactoryImpl.java @@ -1,18 +1,18 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * 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 - * + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -26,6 +26,7 @@ import org.apache.olingo.client.api.communication.request.cud.v4.ODataReferenceA import org.apache.olingo.client.api.communication.request.cud.v4.UpdateType; import org.apache.olingo.client.api.v4.ODataClient; import org.apache.olingo.client.core.communication.request.cud.AbstractCUDRequestFactory; +import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.domain.v4.ODataSingleton; import org.apache.olingo.commons.api.http.HttpMethod; @@ -50,7 +51,20 @@ public class CUDRequestFactoryImpl extends AbstractCUDRequestFactory } @Override - public ODataReferenceAddingRequest getReferenceAddingRequest(final URI targetURI, final URI reference) { - return new ODataReferenceAddingRequestImpl(client, HttpMethod.POST, targetURI, reference); - } + public ODataReferenceAddingRequest getReferenceAddingRequest(final URI serviceRoot, final URI targetURI, + final URI reference) { + final URI contextURI = client.newURIBuilder(serviceRoot.toASCIIString()).appendMetadataSegment().build(); + ResWrap wrappedPayload = new ResWrap(contextURI, null, reference); + + return new ODataReferenceAddingRequestImpl(client, HttpMethod.POST, targetURI, wrappedPayload); + } + + public ODataReferenceAddingRequest getReferenceSingleChangeRequest(final URI serviceRoot, final URI targetURI, + final URI reference) { + // See OData Protocol 11.4.6.3 + final URI contextURI = client.newURIBuilder(serviceRoot.toASCIIString()).appendMetadataSegment().build(); + ResWrap wrappedPayload = new ResWrap(contextURI, null, reference); + + return new ODataReferenceAddingRequestImpl(client, HttpMethod.PUT, targetURI, wrappedPayload); + } } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/ODataReferenceAddingRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/ODataReferenceAddingRequestImpl.java index 885e53d31..df4fb6d97 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/ODataReferenceAddingRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/v4/ODataReferenceAddingRequestImpl.java @@ -1,53 +1,56 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * 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 - * + * 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 + * 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.cud.v4; -import java.io.IOException; import java.io.InputStream; import java.net.URI; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.olingo.client.api.CommonODataClient; import org.apache.olingo.client.api.communication.request.cud.v4.ODataReferenceAddingRequest; import org.apache.olingo.client.api.communication.response.v4.ODataReferenceAddingResponse; +import org.apache.olingo.client.api.serialization.ODataWriter; import org.apache.olingo.client.core.communication.request.AbstractODataBasicRequest; import org.apache.olingo.client.core.communication.response.AbstractODataResponse; -import org.apache.olingo.commons.api.Constants; +import org.apache.olingo.client.core.uri.URIUtils; +import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.format.ODataFormat; import org.apache.olingo.commons.api.http.HttpMethod; +import org.apache.olingo.commons.api.serialization.ODataSerializerException; /** - * This class implements an OData delete request. + * See {@link ODataReferenceAddingRequest} + * + * Will be used, for single-valued navigation properties as was well as collection navigation properties */ public class ODataReferenceAddingRequestImpl extends AbstractODataBasicRequest - implements ODataReferenceAddingRequest { + implements ODataReferenceAddingRequest { - final URI reference; + final ResWrap reference; ODataReferenceAddingRequestImpl( - final CommonODataClient odataClient, final HttpMethod method, final URI uri, final URI reference) { + final CommonODataClient odataClient, final HttpMethod method, final URI uri, final ResWrap reference) { super(odataClient, method, uri); this.reference = reference; - - } @Override @@ -63,9 +66,10 @@ public class ODataReferenceAddingRequestImpl extends AbstractODataBasicRequest odataClient, final HttpClient httpClient, final HttpResponse res) { + final CommonODataClient odataClient, final HttpClient httpClient, final HttpResponse res) { super(odataClient, httpClient, res); this.close(); diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataWriterImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataWriterImpl.java index 94cec0be0..9bc5f3446 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataWriterImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataWriterImpl.java @@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; +import java.net.URI; import java.util.Collection; import java.util.Collections; @@ -30,6 +31,7 @@ import org.apache.commons.io.IOUtils; import org.apache.olingo.client.api.CommonODataClient; import org.apache.olingo.client.api.serialization.ODataWriter; import org.apache.olingo.commons.api.Constants; +import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.domain.CommonODataEntity; import org.apache.olingo.commons.api.domain.CommonODataProperty; import org.apache.olingo.commons.api.domain.ODataLink; @@ -107,4 +109,23 @@ public class ODataWriterImpl implements ODataWriter { IOUtils.closeQuietly(output); } } + + @Override + public InputStream writeReference(ResWrap reference, ODataFormat format) throws ODataSerializerException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + OutputStreamWriter writer = null; + + try { + writer = new OutputStreamWriter(output, Constants.UTF8); + } catch (final UnsupportedEncodingException e) { + } + + try { + client.getSerializer(format).write(writer, reference); + + return new ByteArrayInputStream(output.toByteArray()); + } finally { + IOUtils.closeQuietly(output); + } + } } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java index 5475064e8..68b116aea 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java @@ -53,6 +53,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.io.Writer; +import java.net.URI; import java.util.Collections; import java.util.List; @@ -526,6 +527,20 @@ public class AtomSerializer extends AbstractAtomDealer implements ODataSerialize } } + private void reference(final Writer outWriter, final ResWrap container) throws XMLStreamException { + final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter); + + writer.writeStartDocument(); + + writer.writeStartElement(Constants.ATTR_METADATA, Constants.ATTR_REF); + writer.writeNamespace(Constants.ATTR_METADATA, version.getNamespace(NamespaceKey.METADATA)); + writer.writeAttribute(Constants.ATTR_METADATA, Constants.CONTEXT, container.getContextURL().toASCIIString()); + writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().toASCIIString()); + writer.writeEndElement(); + + writer.writeEndDocument(); + } + @Override @SuppressWarnings("unchecked") public void write(final Writer writer, final ResWrap container) throws ODataSerializerException { @@ -540,6 +555,8 @@ public class AtomSerializer extends AbstractAtomDealer implements ODataSerialize property(writer, (Property) obj); } else if (obj instanceof Link) { link(writer, (Link) obj); + } else if(obj instanceof URI) { + reference(writer,(ResWrap) container); } } catch (final XMLStreamException e) { throw new ODataSerializerException(e); diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java index 6955b11a8..3db0ea904 100755 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java @@ -20,6 +20,7 @@ package org.apache.olingo.commons.core.serialization; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; + import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.olingo.commons.api.Constants; @@ -47,6 +48,7 @@ import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; import java.io.IOException; import java.io.Writer; +import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -100,6 +102,15 @@ public class JsonSerializer implements ODataSerializer { } } + private void reference(ResWrapcontainer, JsonGenerator json) throws IOException { + json.writeStartObject(); + + json.writeStringField(Constants.JSON_CONTEXT, container.getContextURL().toASCIIString()); + json.writeStringField(Constants.JSON_ID, container.getPayload().toASCIIString()); + + json.writeEndObject(); + } + @SuppressWarnings("unchecked") @Override public void write(final Writer writer, final ResWrap container) throws ODataSerializerException { @@ -114,6 +125,8 @@ public class JsonSerializer implements ODataSerializer { new JsonPropertySerializer(version, serverMode).doContainerSerialize((ResWrap) container, json); } else if (obj instanceof Link) { link((Link) obj, json); + } else if(obj instanceof URI) { + reference((ResWrap) container, json); } json.flush(); } catch (final IOException e) {