/** @since 4.2 */ public NByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = b.length; this.buf = ByteBuffer.wrap(b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
/** @since 4.2 */ public NByteArrayEntity( final byte[] b, final int off, final int len, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) < 0) || ((off + len) > b.length)) { throw new IndexOutOfBoundsException( "off: " + off + " len: " + len + " b.length: " + b.length); } this.b = b; this.off = off; this.len = len; this.buf = ByteBuffer.wrap(b, off, len); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
/** public for unit test, not to be used otherwise */ public void execute( HttpUriRequest httpUriRequest, ContentType contentType, FutureCallback<HttpResponse> callback) { // add accept header when its not a form or multipart final String contentTypeString = contentType.toString(); if (!ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType.getMimeType()) && !contentType.getMimeType().startsWith(MULTIPART_MIME_TYPE)) { // otherwise accept what is being sent httpUriRequest.addHeader(HttpHeaders.ACCEPT, contentTypeString); } // is something being sent? if (httpUriRequest instanceof HttpEntityEnclosingRequestBase && httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) { httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeString); } // set user specified custom headers if (httpHeaders != null && !httpHeaders.isEmpty()) { for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpUriRequest.setHeader(entry.getKey(), entry.getValue()); } } // add client protocol version if not specified if (!httpUriRequest.containsHeader(ODataHttpHeaders.DATASERVICEVERSION)) { httpUriRequest.addHeader(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V20); } if (!httpUriRequest.containsHeader(MAX_DATA_SERVICE_VERSION)) { httpUriRequest.addHeader(MAX_DATA_SERVICE_VERSION, ODataServiceVersion.V30); } // execute request client.execute(httpUriRequest, callback); }
private ODataResponse writeContent(Edm edm, UriInfoWithType uriInfo, Object content) throws ODataApplicationException, EdmException, EntityProviderException, URISyntaxException, IOException { String responseContentType = getContentType(); ODataResponse response; switch (uriInfo.getUriType()) { case URI4: case URI5: // simple property final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath(); final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1); responseContentType = simpleProperty.getMimeType(); if (uriInfo.isValue()) { response = EntityProvider.writePropertyValue(simpleProperty, content); responseContentType = TEXT_PLAIN_WITH_CS_UTF_8.toString(); } else { response = EntityProvider.writeProperty(getContentType(), simpleProperty, content); } break; case URI3: // complex property final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath(); final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1); response = EntityProvider.writeProperty(responseContentType, complexProperty, content); break; case URI7A: // $links with 0..1 cardinality property final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet(); EntityProviderWriteProperties linkProperties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build(); @SuppressWarnings("unchecked") final Map<String, Object> linkMap = (Map<String, Object>) content; response = EntityProvider.writeLink( responseContentType, targetLinkEntitySet, linkMap, linkProperties); break; case URI7B: // $links with * cardinality property final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet(); EntityProviderWriteProperties linksProperties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build(); @SuppressWarnings("unchecked") final List<Map<String, Object>> linksMap = (List<Map<String, Object>>) content; response = EntityProvider.writeLinks( responseContentType, targetLinksEntitySet, linksMap, linksProperties); break; case URI1: case URI2: case URI6A: case URI6B: // Entity final EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet(); EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build(); @SuppressWarnings("unchecked") final Map<String, Object> objectMap = (Map<String, Object>) content; response = EntityProvider.writeEntry(responseContentType, targetEntitySet, objectMap, properties); break; case URI9: // $batch @SuppressWarnings("unchecked") final List<Olingo2BatchRequest> batchParts = (List<Olingo2BatchRequest>) content; response = parseBatchRequest(edm, batchParts); break; default: // notify exception and return!!! throw new ODataApplicationException( "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH); } return response.getContentHeader() != null ? response : ODataResponse.fromResponse(response).contentHeader(responseContentType).build(); }
@SuppressWarnings("unchecked") private <T> T readContent(UriInfoWithType uriInfo, InputStream content) throws EntityProviderException, ODataApplicationException { T response; switch (uriInfo.getUriType()) { case URI0: // service document response = (T) EntityProvider.readServiceDocument( content, SERVICE_DOCUMENT_CONTENT_TYPE.toString()); break; case URI8: // $metadata response = (T) EntityProvider.readMetadata(content, false); break; case URI7A: // link response = (T) EntityProvider.readLink(getContentType(), uriInfo.getTargetEntitySet(), content); break; case URI7B: // links response = (T) EntityProvider.readLinks(getContentType(), uriInfo.getTargetEntitySet(), content); break; case URI3: // complex property final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath(); final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1); response = (T) EntityProvider.readProperty( getContentType(), complexProperty, content, EntityProviderReadProperties.init().build()); break; case URI4: case URI5: // simple property final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath(); final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1); if (uriInfo.isValue()) { response = (T) EntityProvider.readPropertyValue(simpleProperty, content); } else { response = (T) EntityProvider.readProperty( getContentType(), simpleProperty, content, EntityProviderReadProperties.init().build()); } break; case URI15: case URI16: case URI50A: case URI50B: // $count final String stringCount = new String(EntityProvider.readBinary(content), Consts.UTF_8); response = (T) Long.valueOf(stringCount); break; case URI1: case URI6B: if (uriInfo.getCustomQueryOptions().containsKey("!deltatoken")) { // ODataDeltaFeed response = (T) EntityProvider.readDeltaFeed( getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build()); } else { // ODataFeed response = (T) EntityProvider.readFeed( getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build()); } break; case URI2: case URI6A: response = (T) EntityProvider.readEntry( getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build()); break; default: throw new ODataApplicationException( "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH); } return response; }
@Override public String getContentType() { return contentType.toString(); }