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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations