use of org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2 in project eclipselink by eclipse-ee4j.
the class ReferenceAdapterV2 method marshal.
/**
* Marshal just passes through.
*/
@Override
public T marshal(T o) throws Exception {
if (o == null) {
return null;
}
// Add canonical link
final String href = HrefHelper.buildEntityHref(context, o.getClass().getSimpleName(), IdHelper.stringifyId(o, o.getClass().getSimpleName(), context));
if (o._persistence_getLinks() == null) {
final ItemLinks itemLinks = (new ItemLinksBuilder()).addCanonical(href).build();
o._persistence_setLinks(itemLinks);
} else {
final ItemLinks itemLinks = o._persistence_getLinks();
final LinkV2 canonicalLink = itemLinks.getCanonicalLink();
if (canonicalLink == null) {
o._persistence_getLinks().addLink(new LinkV2(ReservedWords.JPARS_REL_CANONICAL, href));
}
}
return o;
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2 in project eclipselink by eclipse-ee4j.
the class AbstractResource method marshallMetadata.
/**
* Marshall metadata.
*
* @param metadata the metadata
* @param mediaType the media type
* @return the string
* @throws JAXBException the jAXB exception
*/
protected String marshallMetadata(Object metadata, String mediaType) throws JAXBException {
final Class<?>[] jaxbClasses = new Class<?>[] { Link.class, Attribute.class, Descriptor.class, LinkTemplate.class, PersistenceUnit.class, Query.class, LinkList.class, QueryList.class, ResourceSchema.class, Property.class, Reference.class, LinkV2.class, MetadataCatalog.class, Resource.class, ItemLinks.class, ContextsCatalog.class };
final JAXBContext context = (JAXBContext) JAXBContextFactory.createContext(jaxbClasses, null);
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, Boolean.FALSE);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType);
marshaller.setProperty(MarshallerProperties.JSON_REDUCE_ANY_ARRAYS, true);
final StringWriter writer = new StringWriter();
marshaller.marshal(metadata, writer);
return writer.toString();
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2 in project eclipselink by eclipse-ee4j.
the class CollectionWrapperBuilder method wrapCollectionsForEntity.
private void wrapCollectionsForEntity(Object entity) {
if (!PersistenceWeavedRest.class.isAssignableFrom(entity.getClass())) {
return;
}
for (final Field field : entity.getClass().getDeclaredFields()) {
if (Collection.class.isAssignableFrom(field.getType())) {
// Get entity id
final String id = IdHelper.stringifyId(entity, entity.getClass().getSimpleName(), context);
// Generate links
final List<LinkV2> links = new ArrayList<>(2);
final String href = HrefHelper.buildEntityFieldHref(context, entity.getClass().getSimpleName(), id, field.getName());
links.add(new LinkV2(ReservedWords.JPARS_REL_SELF, href));
links.add(new LinkV2(ReservedWords.JPARS_REL_CANONICAL, href));
// Get accessibility
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
// Make proxy
try {
// No proxy for weaved fields and null fields
if (!field.getName().startsWith("_") && field.get(entity) != null) {
CollectionProxy proxy = getRestCollectionProxy((Collection<?>) field.get(entity), entity.getClass().getName(), field.getName());
proxy.setLinks(links);
field.set(entity, field.getType().cast(proxy));
}
} catch (IllegalAccessException e) {
throw JPARSException.exceptionOccurred(e);
}
// Restore accessibility
if (!accessible) {
field.setAccessible(false);
}
}
}
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2 in project eclipselink by eclipse-ee4j.
the class SelfLinksResponseBuilder method buildReportQueryResponse.
@Override
public Object buildReportQueryResponse(PersistenceContext context, Map<String, Object> queryParams, List<Object[]> results, List<ReportItem> items, UriInfo uriInfo) {
ReportQueryResultCollection response = new ReportQueryResultCollection();
for (Object result : results) {
ReportQueryResultListItem queryResultListItem = new ReportQueryResultListItem();
List<JAXBElement<?>> jaxbFields = createShellJAXBElementList(items, result);
generateLinksInElementsList(context, jaxbFields);
queryResultListItem.setFields(jaxbFields);
response.addItem(queryResultListItem);
}
response.addLink(new LinkV2(ReservedWords.JPARS_REL_SELF, uriInfo.getRequestUri().toString()));
return response;
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2 in project eclipselink by eclipse-ee4j.
the class SelfLinksResponseBuilder method collectionResponse.
private Object collectionResponse(PersistenceContext context, List<Object> results, UriInfo uriInfo) {
if ((results != null) && (!results.isEmpty())) {
final ReadAllQueryResultCollection response = new ReadAllQueryResultCollection();
for (Object item : results) {
if (item instanceof PersistenceWeavedRest) {
final PersistenceWeavedRest entity = (PersistenceWeavedRest) item;
final ClassDescriptor classDescriptor = context.getServerSession().getProject().getDescriptor(item.getClass());
final String entityClassName = classDescriptor.getAlias();
final String entityId = IdHelper.stringifyId(entity, entityClassName, context);
final String href = HrefHelper.buildEntityHref(context, entityClassName, entityId);
final ItemLinksBuilder itemLinksBuilder = (new ItemLinksBuilder()).addCanonical(href);
entity._persistence_setLinks(itemLinksBuilder.build());
generateLinksForRelationships(context, entity);
response.addItem(entity);
} else {
response.addItem(item);
}
}
response.addLink(new LinkV2(ReservedWords.JPARS_REL_SELF, uriInfo.getRequestUri().toString()));
return response;
}
return results;
}
Aggregations