Search in sources :

Example 1 with StreamingOutputMarshaller

use of org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller in project eclipselink by eclipse-ee4j.

the class AbstractEntityResource method findInternal.

protected Response findInternal(String version, String persistenceUnit, String type, String id, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "findInternal", new Object[] { "GET", version, persistenceUnit, type, id, uriInfo.getRequestUri().toASCIIString() });
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
        final Map<String, String> discriminators = getMatrixParameters(uriInfo, persistenceUnit);
        final Object entityId = IdHelper.buildId(context, type, id);
        final Object entity = context.find(discriminators, type, entityId, getQueryParameters(uriInfo));
        if (entity == null) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_for_key", new Object[] { type, id, persistenceUnit });
            throw JPARSException.entityNotFound(type, id, persistenceUnit);
        }
        // Fields filtering
        if (context.getSupportedFeatureSet().isSupported(Feature.FIELDS_FILTERING)) {
            final FieldsFilteringValidator fieldsFilteringValidator = new FieldsFilteringValidator(uriInfo);
            if (fieldsFilteringValidator.isFeatureApplicable()) {
                final StreamingOutputMarshaller marshaller = new StreamingOutputMarshaller(context, singleEntityResponse(context, entity, uriInfo), headers.getAcceptableMediaTypes(), fieldsFilteringValidator.getFilter());
                return Response.ok(marshaller).build();
            }
        }
        return Response.ok(new StreamingOutputMarshaller(context, singleEntityResponse(context, entity, uriInfo), headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) FieldsFilteringValidator(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 2 with StreamingOutputMarshaller

use of org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller in project eclipselink by eclipse-ee4j.

the class AbstractEntityResource method setOrAddAttributeInternal.

protected Response setOrAddAttributeInternal(String version, String persistenceUnit, String type, String id, String attribute, HttpHeaders headers, UriInfo uriInfo, InputStream in) {
    JPARSLogger.entering(CLASS_NAME, "setOrAddAttributeInternal", new Object[] { "POST", headers.getMediaType(), version, persistenceUnit, type, id, attribute, uriInfo.getRequestUri().toASCIIString() });
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
        Object entityId = IdHelper.buildId(context, type, id);
        String partner = getRelationshipPartner(getMatrixParameters(uriInfo, attribute), getQueryParameters(uriInfo));
        ClassDescriptor descriptor = context.getDescriptor(type);
        DatabaseMapping mapping = descriptor.getMappingForAttributeName(attribute);
        if (!mapping.isForeignReferenceMapping()) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_appropriate_mapping_for_update", new Object[] { attribute, type, id, persistenceUnit });
            throw JPARSException.databaseMappingCouldNotBeFoundForEntityAttribute(attribute, type, id, persistenceUnit);
        }
        Object entity = context.unmarshalEntity(mapping.getReferenceDescriptor().getAlias(), mediaType(headers.getAcceptableMediaTypes()), in);
        Object result = context.updateOrAddAttribute(getMatrixParameters(uriInfo, persistenceUnit), type, entityId, getQueryParameters(uriInfo), attribute, entity, partner);
        if (result == null) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_update_attribute", new Object[] { attribute, type, id, persistenceUnit });
            throw JPARSException.attributeCouldNotBeUpdated(attribute, type, id, persistenceUnit);
        }
        return Response.ok(new StreamingOutputMarshaller(context, singleEntityResponse(context, result, uriInfo), headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 3 with StreamingOutputMarshaller

use of org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller in project eclipselink by eclipse-ee4j.

the class AbstractEntityResource method createInternal.

protected Response createInternal(String version, String persistenceUnit, String type, HttpHeaders headers, UriInfo uriInfo, InputStream in) {
    JPARSLogger.entering(CLASS_NAME, "createInternal", new Object[] { "PUT", headers.getMediaType(), version, persistenceUnit, type, uriInfo.getRequestUri().toASCIIString() });
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
        final ClassDescriptor descriptor = context.getDescriptor(type);
        if (descriptor == null) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_class_in_persistence_unit", new Object[] { type, persistenceUnit });
            throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);
        }
        final Object entity = context.unmarshalEntity(type, mediaType(headers.getAcceptableMediaTypes()), in);
        // Check idempotence of the entity
        if (!checkIdempotence(descriptor, entity)) {
            JPARSLogger.error(context.getSessionLog(), "jpars_put_not_idempotent", new Object[] { type, persistenceUnit });
            throw JPARSException.entityIsNotIdempotent(type, persistenceUnit);
        }
        // Check idempotence of the entity's relationships
        if (!checkIdempotenceOnRelationships(descriptor, entity)) {
            JPARSLogger.error(context.getSessionLog(), "jpars_put_not_idempotent", new Object[] { type, persistenceUnit });
            throw JPARSException.entityIsNotIdempotent(type, persistenceUnit);
        }
        // Cascade persist. Sets references to the parent object in collections with objects passed by value.
        if (context.getServiceVersion().compareTo(ServiceVersion.VERSION_2_0) >= 0) {
            processBidirectionalRelationships(context, descriptor, entity);
        }
        // No sequencing in relationships, we can create the object now...
        context.create(getMatrixParameters(uriInfo, persistenceUnit), entity);
        final ResponseBuilder rb = Response.status(Status.CREATED);
        return rb.entity(new StreamingOutputMarshaller(context, singleEntityResponse(context, entity, uriInfo), headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) FeatureResponseBuilder(org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 4 with StreamingOutputMarshaller

use of org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller in project eclipselink by eclipse-ee4j.

the class AbstractPersistenceResource method callSessionBeanInternal.

@SuppressWarnings("rawtypes")
protected Response callSessionBeanInternal(String version, HttpHeaders headers, UriInfo uriInfo, InputStream is) {
    JPARSLogger.entering(CLASS_NAME, "callSessionBeanInternal", new Object[] { "POST", headers.getMediaType(), version, uriInfo.getRequestUri().toASCIIString() });
    try {
        if (!isValidVersion(version)) {
            JPARSLogger.error("unsupported_service_version_in_the_request", new Object[] { version });
            throw JPARSException.invalidServiceVersion(version);
        }
        SessionBeanCall call = unmarshallSessionBeanCall(is);
        String jndiName = call.getJndiName();
        if (!isValid(jndiName)) {
            JPARSLogger.error("jpars_invalid_jndi_name", new Object[] { jndiName });
            throw JPARSException.jndiNamePassedIsInvalid(jndiName);
        }
        javax.naming.Context ctx = new InitialContext();
        Object ans = ctx.lookup(jndiName);
        if (ans == null) {
            JPARSLogger.error("jpars_could_not_find_session_bean", new Object[] { jndiName });
            throw JPARSException.sessionBeanCouldNotBeFound(jndiName);
        }
        PersistenceContext context = null;
        if (call.getContext() != null) {
            context = getPersistenceFactory().get(call.getContext(), uriInfo.getBaseUri(), version, null);
            if (context == null) {
                JPARSLogger.error("jpars_could_not_find_persistence_context", new Object[] { call.getContext() });
                throw JPARSException.persistenceContextCouldNotBeBootstrapped(call.getContext());
            }
        }
        Class[] parameters = new Class[call.getParameters().size()];
        Object[] args = new Object[call.getParameters().size()];
        int i = 0;
        for (Parameter param : call.getParameters()) {
            Class<?> parameterClass = null;
            Object parameterValue;
            if (context != null) {
                parameterClass = context.getClass(param.getTypeName());
            }
            if (parameterClass != null) {
                parameterValue = context.unmarshalEntity(param.getTypeName(), headers.getMediaType(), is);
            } else {
                parameterClass = Thread.currentThread().getContextClassLoader().loadClass(param.getTypeName());
                parameterValue = ConversionManager.getDefaultManager().convertObject(param.getValue(), parameterClass);
            }
            parameters[i] = parameterClass;
            args[i] = parameterValue;
            i++;
        }
        Method method = ans.getClass().getMethod(call.getMethodName(), parameters);
        Object returnValue = method.invoke(ans, args);
        return Response.ok(new StreamingOutputMarshaller(null, returnValue, headers.getAcceptableMediaTypes())).build();
    } catch (JAXBException | NamingException | ReflectiveOperationException | RuntimeException e) {
        JPARSLogger.exception("exception_in_callSessionBeanInternal", new Object[] { version, headers.getMediaType(), uriInfo.getRequestUri().toASCIIString() }, e);
        throw JPARSException.exceptionOccurred(e);
    }
}
Also used : JAXBException(jakarta.xml.bind.JAXBException) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) Method(java.lang.reflect.Method) InitialContext(javax.naming.InitialContext) SessionBeanCall(org.eclipse.persistence.internal.jpa.rs.metadata.model.SessionBeanCall) Parameter(org.eclipse.persistence.internal.jpa.rs.metadata.model.Parameter) NamingException(javax.naming.NamingException) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)

Example 5 with StreamingOutputMarshaller

use of org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller in project eclipselink by eclipse-ee4j.

the class AbstractPersistenceUnitResource method getQueryMetadataInternal.

protected Response getQueryMetadataInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "getQueryMetadataInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        List<Query> returnQueries = new ArrayList<>();
        Map<String, List<DatabaseQuery>> queries = context.getServerSession().getQueries();
        if (queries.get(queryName) != null) {
            for (DatabaseQuery query : queries.get(queryName)) {
                returnQueries.add(getQuery(query, context));
            }
        }
        String mediaType = StreamingOutputMarshaller.mediaType(headers.getAcceptableMediaTypes()).toString();
        QueryList queryList = new QueryList();
        queryList.setList(returnQueries);
        String result = null;
        if (mediaType.equals(MediaType.APPLICATION_JSON)) {
            result = marshallMetadata(queryList.getList(), mediaType);
        } else {
            result = marshallMetadata(queryList, mediaType);
        }
        return Response.ok(new StreamingOutputMarshaller(null, result, headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ArrayList(java.util.ArrayList) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) ArrayList(java.util.ArrayList) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) List(java.util.List) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Aggregations

StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)20 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)17 JPARSException (org.eclipse.persistence.jpa.rs.exceptions.JPARSException)11 JAXBException (jakarta.xml.bind.JAXBException)8 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)7 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Resource (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Resource)3 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)3 JAXBElement (jakarta.xml.bind.JAXBElement)2 Link (org.eclipse.persistence.internal.jpa.rs.metadata.model.Link)2 Query (org.eclipse.persistence.internal.jpa.rs.metadata.model.Query)2 ResourceSchema (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema)2 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)2 FeatureResponseBuilder (org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder)2 ItemLinksBuilder (org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder)2 FieldsFilteringValidator (org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator)2 AbstractResource (org.eclipse.persistence.jpa.rs.resources.common.AbstractResource)2