Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,56 +100,61 @@ public Triple<List<T>, URI, List<ClientAnnotation>> fetchPartial(
final URI next;
final List<ClientAnnotation> anns = new ArrayList<ClientAnnotation>();

if (isSingleton) {
final ODataRetrieveResponse<ClientSingleton> res =
((ODataClient) getClient()).getRetrieveRequestFactory().getSingletonRequest(uri).execute();

entities.add(res.getBody());
next = null;
} else {
final ODataEntitySetRequest<ClientEntitySet> req =
getClient().getRetrieveRequestFactory().getEntitySetRequest(uri);
req.setPrefer(getClient().newPreferences().includeAnnotations("*"));

final ODataRetrieveResponse<ClientEntitySet> res = req.execute();

final ClientEntitySet entitySet = res.getBody();
entities.addAll(entitySet.getEntities());
next = entitySet.getNext();
anns.addAll(entitySet.getAnnotations());
}

final List<T> res = new ArrayList<T>(entities.size());

for (ClientEntity entity : entities) {
Class<?> actualRef = null;
if (entity.getTypeName() != null) {
actualRef = service.getEntityTypeClass(entity.getTypeName().toString());
try {
if (isSingleton) {
final ODataRetrieveResponse<ClientSingleton> res =
((ODataClient) getClient()).getRetrieveRequestFactory().getSingletonRequest(uri).execute();

entities.add(res.getBody());
next = null;
} else {
final ODataEntitySetRequest<ClientEntitySet> req =
getClient().getRetrieveRequestFactory().getEntitySetRequest(uri);
req.setPrefer(getClient().newPreferences().includeAnnotations("*"));

final ODataRetrieveResponse<ClientEntitySet> res = req.execute();

final ClientEntitySet entitySet = res.getBody();
entities.addAll(entitySet.getEntities());
next = entitySet.getNext();
anns.addAll(entitySet.getAnnotations());
}
if (actualRef == null) {
actualRef = typeRef;

final List<T> res = new ArrayList<T>(entities.size());

for (ClientEntity entity : entities) {
Class<?> actualRef = null;
if (entity.getTypeName() != null) {
actualRef = service.getEntityTypeClass(entity.getTypeName().toString());
}
if (actualRef == null) {
actualRef = typeRef;
}

final EntityInvocationHandler handler =
this instanceof EntitySetInvocationHandler
? EntityInvocationHandler.getInstance(
entity,
EntitySetInvocationHandler.class.cast(this),
actualRef)
: EntityInvocationHandler.getInstance(
entity,
targetEntitySetURI,
actualRef,
service);

final EntityInvocationHandler handlerInTheContext = getContext().entityContext().getEntity(handler.getUUID());

res.add((T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class<?>[] { actualRef },
handlerInTheContext == null ? handler : handlerInTheContext));
}

final EntityInvocationHandler handler =
this instanceof EntitySetInvocationHandler
? EntityInvocationHandler.getInstance(
entity,
EntitySetInvocationHandler.class.cast(this),
actualRef)
: EntityInvocationHandler.getInstance(
entity,
targetEntitySetURI,
actualRef,
service);

final EntityInvocationHandler handlerInTheContext = getContext().entityContext().getEntity(handler.getUUID());

res.add((T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class<?>[] { actualRef },
handlerInTheContext == null ? handler : handlerInTheContext));
return new ImmutableTriple<List<T>, URI, List<ClientAnnotation>>(res, next, anns);
} catch (Exception e) {
LOG.warn("Error fetching entity set partial '" + uri + "'", e);
throw new IllegalArgumentException("Error fetching entity set", e);
}

return new ImmutableTriple<List<T>, URI, List<ClientAnnotation>>(res, next, anns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,41 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
@SuppressWarnings("unchecked")
@Override
public Triple<List<T>, URI, List<ClientAnnotation>> fetchPartial(final URI uri, final Class<T> typeRef) {
final ODataPropertyRequest<ClientProperty> req =
getClient().getRetrieveRequestFactory().getPropertyRequest(uri);
req.setPrefer(getClient().newPreferences().includeAnnotations("*"));

final ODataRetrieveResponse<ClientProperty> res = req.execute();

final List<T> resItems = new ArrayList<T>();

final ClientProperty property = res.getBody();
if (property != null && property.hasCollectionValue()) {
for (ClientValue item : (ClientCollectionValue<ClientValue>) property.getValue()) {
Class<?> actualRef = null;
if (StringUtils.isNotBlank(item.getTypeName())) {
actualRef = service.getComplexTypeClass(item.getTypeName());
}
if (actualRef == null) {
actualRef = typeRef;
try {
final ODataPropertyRequest<ClientProperty> req =
getClient().getRetrieveRequestFactory().getPropertyRequest(uri);
req.setPrefer(getClient().newPreferences().includeAnnotations("*"));

final ODataRetrieveResponse<ClientProperty> res = req.execute();

final List<T> resItems = new ArrayList<T>();

final ClientProperty property = res.getBody();
if (property != null && property.hasCollectionValue()) {
for (ClientValue item : (ClientCollectionValue<ClientValue>) property.getValue()) {
Class<?> actualRef = null;
if (StringUtils.isNotBlank(item.getTypeName())) {
actualRef = service.getComplexTypeClass(item.getTypeName());
}
if (actualRef == null) {
actualRef = typeRef;
}

resItems.add((T) getComplex(property.getName(), item, actualRef, null, null, true));
}

resItems.add((T) getComplex(property.getName(), item, actualRef, null, null, true));
}
}

return new ImmutableTriple<List<T>, URI, List<ClientAnnotation>>(
resItems, null, Collections.<ClientAnnotation> emptyList());
return new ImmutableTriple<List<T>, URI, List<ClientAnnotation>>(
resItems, null, Collections.<ClientAnnotation> emptyList());
} catch (java.net.URISyntaxException e) {
LOG.warn("Invalid URI for fetching complex collection '" + uri + "'", e);
throw new IllegalArgumentException("Invalid URI while fetching complex collection", e);
} catch (java.io.IOException e) {
LOG.warn("I/O error while fetching complex collection '" + uri + "'", e);
throw new IllegalArgumentException("I/O error while fetching complex collection", e);
} catch (Exception e) {
LOG.warn("Error fetching complex collection '" + uri + "'", e);
throw new IllegalArgumentException("Error fetching complex collection", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg

public void load() {
if (this.uri != null) {
final ODataRetrieveResponse<InputStream> res =
getClient().getRetrieveRequestFactory().getMediaRequest(this.uri).execute();
contentType = res.getContentType();
stream = res.getBody();
try {
final ODataRetrieveResponse<InputStream> res =
getClient().getRetrieveRequestFactory().getMediaRequest(this.uri).execute();
contentType = res.getContentType();
stream = res.getBody();
} catch (java.net.URISyntaxException e) {
LOG.warn("Invalid media URI '" + uri + "'", e);
throw new IllegalArgumentException("Invalid media URI: " + uri, e);
} catch (java.io.IOException e) {
LOG.warn("I/O error while retrieving media from '" + uri + "'", e);
throw new IllegalArgumentException("I/O error while retrieving media: " + uri, e);
} catch (Exception e) {
LOG.warn("Error while retrieving media from '" + uri + "'", e);
throw new IllegalArgumentException("Error retrieving media: " + uri, e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,29 +321,37 @@ public EdmStreamValue getStreamChanges() {
}

public EdmStreamValue loadStream() {
final URI contentSource = getEntity().getMediaContentSource() == null
? getClient().newURIBuilder(baseURI.toASCIIString()).appendValueSegment().build()
: getEntity().getMediaContentSource();
try {
final URI contentSource = getEntity().getMediaContentSource() == null
? getClient().newURIBuilder(baseURI.toASCIIString()).appendValueSegment().build()
: getEntity().getMediaContentSource();

if (this.stream == null
&& typeRef.getAnnotation(EntityType.class).hasStream()
&& contentSource != null) {

if (this.stream == null
&& typeRef.getAnnotation(EntityType.class).hasStream()
&& contentSource != null) {
final ODataMediaRequest retrieveReq =
getClient().getRetrieveRequestFactory().getMediaEntityRequest(contentSource);

final ODataMediaRequest retrieveReq =
getClient().getRetrieveRequestFactory().getMediaEntityRequest(contentSource);
if (StringUtils.isNotBlank(getEntity().getMediaContentType())) {
retrieveReq.setFormat(ContentType.parse(getEntity().getMediaContentType()));
}

if (StringUtils.isNotBlank(getEntity().getMediaContentType())) {
retrieveReq.setFormat(ContentType.parse(getEntity().getMediaContentType()));
final ODataRetrieveResponse<InputStream> res = retrieveReq.execute();
this.stream = EdmStreamValue.class.cast(Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class<?>[] { EdmStreamValue.class },
new EdmStreamValueHandler(res.getContentType(), res.getBody(), contentSource, service)));
}

final ODataRetrieveResponse<InputStream> res = retrieveReq.execute();
this.stream = EdmStreamValue.class.cast(Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class<?>[] { EdmStreamValue.class },
new EdmStreamValueHandler(res.getContentType(), res.getBody(), contentSource, service)));
return this.stream;
} catch (java.net.URISyntaxException e) {
LOG.warn("Invalid media content source URI for entity '" + uuid + "'", e);
throw new IllegalArgumentException("Invalid media content source URI for " + typeRef.getSimpleName(), e);
} catch (Exception e) {
LOG.warn("Error retrieving media stream for entity '" + uuid + "'", e);
throw new IllegalArgumentException("Error retrieving media stream for " + typeRef.getSimpleName(), e);
}

return this.stream;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,21 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg

@Override
public Long count() {
final ODataValueRequest req = getClient().getRetrieveRequestFactory().
getValueRequest(getClient().newURIBuilder(this.uri.build().toASCIIString()).count().build());
req.setFormat(ContentType.TEXT_PLAIN);
return Long.valueOf(req.execute().getBody().asPrimitive().toString());
try {
final ODataValueRequest req = getClient().getRetrieveRequestFactory().
getValueRequest(getClient().newURIBuilder(this.uri.build().toASCIIString()).count().build());
req.setFormat(ContentType.TEXT_PLAIN);
return Long.valueOf(req.execute().getBody().asPrimitive().toString());
} catch (java.net.URISyntaxException e) {
LOG.warn("Invalid URI building for count on entity set '" + uri + "'", e);
throw new IllegalArgumentException("Invalid URI for count on " + this.uri, e);
} catch (java.io.IOException e) {
LOG.warn("I/O error while executing count request for entity set '" + uri + "'", e);
throw new IllegalArgumentException("I/O error while executing count for " + this.uri, e);
} catch (Exception e) {
LOG.warn("Error executing count for entity set '" + uri + "'", e);
throw new IllegalArgumentException("Error executing count for " + this.uri, e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ protected void doFlush(final PersistenceChanges changes, final TransactionItems
LOG.error("While performing {}", entry.getKey().getURI(), e);

throw new ODataFlushException(0, Collections.singletonList(new ODataResponseError(e, index, request)));
} catch (Exception e) {
// handle checked exceptions (e.g., URISyntaxException, IOException) by wrapping
LOG.error("While performing {}", entry.getKey().getURI(), e);
throw new ODataFlushException(0, Collections.singletonList(
new ODataResponseError(new org.apache.olingo.commons.api.ex.ODataRuntimeException(e), index, request)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
@Override
@SuppressWarnings("unchecked")
public Triple<List<T>, URI, List<ClientAnnotation>> fetchPartial(final URI uri, final Class<T> typeRef) {
final ODataPropertyRequest<ClientProperty> req =
try {
final ODataPropertyRequest<ClientProperty> req =
getClient().getRetrieveRequestFactory().getPropertyRequest(uri);
req.setPrefer(getClient().newPreferences().includeAnnotations("*"));

Expand All @@ -107,14 +108,23 @@ public Triple<List<T>, URI, List<ClientAnnotation>> fetchPartial(final URI uri,
}
}

return new ImmutableTriple<List<T>, URI, List<ClientAnnotation>>(
resItems, null, Collections.<ClientAnnotation>emptyList());
return new ImmutableTriple<List<T>, URI, List<ClientAnnotation>>(
resItems, null, Collections.<ClientAnnotation>emptyList());
} catch (Exception e) {
LOG.warn("Error fetching primitive collection '" + uri + "'", e);
throw new IllegalArgumentException("Error fetching primitive collection", e);
}
}

public void delete() {
if (baseURI != null) {
getContext().entityContext().addFurtherDeletes(
getClient().newURIBuilder(baseURI.toASCIIString()).appendValueSegment().build());
try {
getContext().entityContext().addFurtherDeletes(
getClient().newURIBuilder(baseURI.toASCIIString()).appendValueSegment().build());
} catch (Exception e) {
LOG.warn("Error scheduling delete for primitive collection '" + baseURI + "'", e);
throw new IllegalArgumentException("Error deleting primitive collection", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,28 @@
*/
package org.apache.olingo.ext.proxy.commons;

import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.olingo.client.api.communication.response.ODataResponse;

class ResponseStatusLine implements StatusLine {
final class ResponseStatusLine{

private final ODataResponse response;
private final int statusCode;
private final String reasonPhrase;

public ResponseStatusLine(final ODataResponse response) {
this.response = response;
this.statusCode = response == null ? 0 : response.getStatusCode();
this.reasonPhrase = response == null ? "" : response.getStatusMessage();
}

@Override
public ProtocolVersion getProtocolVersion() {
return null;
}

@Override
public int getStatusCode() {
return response.getStatusCode();
return statusCode;
}

@Override
public String getReasonPhrase() {
return response.getStatusMessage();
return reasonPhrase;
}

@Override
public String toString() {
return statusCode + " " + reasonPhrase;
}
}
Loading