Search in sources :

Example 1 with SessionBeanCall

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.SessionBeanCall 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 2 with SessionBeanCall

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.SessionBeanCall in project eclipselink by eclipse-ee4j.

the class AbstractPersistenceResource method unmarshallSessionBeanCall.

private SessionBeanCall unmarshallSessionBeanCall(InputStream data) throws JAXBException {
    Class<?>[] jaxbClasses = new Class<?>[] { SessionBeanCall.class };
    JAXBContext context = (JAXBContext) JAXBContextFactory.createContext(jaxbClasses, null);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, Boolean.FALSE);
    unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
    StreamSource ss = new StreamSource(data);
    return unmarshaller.unmarshal(ss, SessionBeanCall.class).getValue();
}
Also used : SessionBeanCall(org.eclipse.persistence.internal.jpa.rs.metadata.model.SessionBeanCall) StreamSource(javax.xml.transform.stream.StreamSource) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Aggregations

SessionBeanCall (org.eclipse.persistence.internal.jpa.rs.metadata.model.SessionBeanCall)2 JAXBException (jakarta.xml.bind.JAXBException)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 Method (java.lang.reflect.Method)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Parameter (org.eclipse.persistence.internal.jpa.rs.metadata.model.Parameter)1 JAXBContext (org.eclipse.persistence.jaxb.JAXBContext)1 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)1 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)1