Search in sources :

Example 1 with LinkV2

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;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ItemLinks(org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2)

Example 2 with LinkV2

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();
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) ResourceSchema(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) Attribute(org.eclipse.persistence.internal.jpa.rs.metadata.model.Attribute) Reference(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference) ItemLinks(org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks) Resource(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Resource) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) MetadataCatalog(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.MetadataCatalog) PersistenceUnit(org.eclipse.persistence.internal.jpa.rs.metadata.model.PersistenceUnit) StringWriter(java.io.StringWriter) Descriptor(org.eclipse.persistence.internal.jpa.rs.metadata.model.Descriptor) LinkTemplate(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate) LinkList(org.eclipse.persistence.jpa.rs.util.list.LinkList) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) Property(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Property) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2) Link(org.eclipse.persistence.internal.jpa.rs.metadata.model.Link) ContextsCatalog(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ContextsCatalog)

Example 3 with LinkV2

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);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) ArrayList(java.util.ArrayList) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2)

Example 4 with LinkV2

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;
}
Also used : ReportQueryResultListItem(org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultListItem) JAXBElement(jakarta.xml.bind.JAXBElement) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2) ReportQueryResultCollection(org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultCollection)

Example 5 with LinkV2

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;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) ReadAllQueryResultCollection(org.eclipse.persistence.jpa.rs.util.list.ReadAllQueryResultCollection) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2)

Aggregations

LinkV2 (org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2)10 ItemLinksBuilder (org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder)5 ItemLinks (org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks)3 Resource (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Resource)3 JAXBElement (jakarta.xml.bind.JAXBElement)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 MetadataCatalog (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.MetadataCatalog)2 PersistenceWeavedRest (org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest)2 AbstractResource (org.eclipse.persistence.jpa.rs.resources.common.AbstractResource)2 Marshaller (jakarta.xml.bind.Marshaller)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Attribute (org.eclipse.persistence.internal.jpa.rs.metadata.model.Attribute)1 Descriptor (org.eclipse.persistence.internal.jpa.rs.metadata.model.Descriptor)1 Link (org.eclipse.persistence.internal.jpa.rs.metadata.model.Link)1 LinkTemplate (org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate)1 PersistenceUnit (org.eclipse.persistence.internal.jpa.rs.metadata.model.PersistenceUnit)1 Query (org.eclipse.persistence.internal.jpa.rs.metadata.model.Query)1