Search in sources :

Example 1 with PythonException

use of org.xdi.exception.PythonException in project oxCore by GluuFederation.

the class CustomScriptManager method createExternalType.

private BaseExternalType createExternalType(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) {
    String customScriptInum = customScript.getInum();
    BaseExternalType externalType;
    try {
        externalType = createExternalTypeFromStringWithPythonException(customScript, configurationAttributes);
    } catch (PythonException ex) {
        log.error("Failed to prepare external type '{0}'", ex, customScriptInum);
        return null;
    }
    if (externalType == null) {
        log.debug("Using default external type class");
        externalType = customScript.getScriptType().getDefaultImplementation();
    }
    return externalType;
}
Also used : BaseExternalType(org.xdi.model.custom.script.type.BaseExternalType) PythonException(org.xdi.exception.PythonException)

Example 2 with PythonException

use of org.xdi.exception.PythonException in project oxTrust by GluuFederation.

the class RegistrationInterceptionService method createRegistrationScriptFromStringWithPythonException.

private RegistrationScript createRegistrationScriptFromStringWithPythonException(RegistrationInterceptorScript script) {
    String pythonScriptText = script.getCustomScript();
    if (pythonScriptText == null) {
        return null;
    }
    RegistrationScript pythonScript = null;
    InputStream bis = null;
    try {
        bis = new ByteArrayInputStream(pythonScriptText.getBytes(Util.UTF8_STRING_ENCODING));
        pythonScript = pythonService.loadPythonScript(bis, "RegistrationScriptClass", RegistrationScript.class, new PyObject[] { new PyLong(System.currentTimeMillis()) });
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    } catch (PythonException e) {
        log.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    return pythonScript;
}
Also used : RegistrationScript(org.gluu.oxtrust.service.python.interfaces.RegistrationScript) PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PythonException(org.xdi.exception.PythonException) PyObject(org.python.core.PyObject)

Example 3 with PythonException

use of org.xdi.exception.PythonException in project oxCore by GluuFederation.

the class CustomScriptManager method createExternalTypeFromStringWithPythonException.

public BaseExternalType createExternalTypeFromStringWithPythonException(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) throws PythonException {
    String script = customScript.getScript();
    if (script == null) {
        return null;
    }
    CustomScriptType customScriptType = customScript.getScriptType();
    BaseExternalType externalType = null;
    InputStream bis = null;
    try {
        bis = new ByteArrayInputStream(script.getBytes("UTF-8"));
        externalType = pythonService.loadPythonScript(bis, customScriptType.getPythonClass(), customScriptType.getCustomScriptType(), new PyObject[] { new PyLong(System.currentTimeMillis()) });
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    if (externalType == null) {
        return null;
    }
    boolean initialized = false;
    try {
        initialized = externalType.init(configurationAttributes);
    } catch (Exception ex) {
        log.error("Failed to initialize custom script: '{0}'", ex, customScript.getName());
    }
    if (initialized) {
        return externalType;
    }
    return null;
}
Also used : PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomScriptType(org.xdi.model.custom.script.CustomScriptType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BaseExternalType(org.xdi.model.custom.script.type.BaseExternalType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PyObject(org.python.core.PyObject) PythonException(org.xdi.exception.PythonException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with PythonException

use of org.xdi.exception.PythonException in project oxCore by GluuFederation.

the class PythonService method loadPythonScript.

@SuppressWarnings("unchecked")
private <T> T loadPythonScript(String scriptPythonType, Class<T> scriptJavaType, PyObject[] constructorArgs, PythonInterpreter interpreter) throws PythonException {
    PyObject scriptPythonTypeObject = interpreter.get(scriptPythonType);
    if (scriptPythonTypeObject == null) {
        return null;
    }
    PyObject scriptPythonTypeClass;
    try {
        scriptPythonTypeClass = scriptPythonTypeObject.__call__(constructorArgs);
    } catch (Exception ex) {
        log.error("Failed to initialize python class", ex.getMessage());
        throw new PythonException(String.format("Failed to initialize python class '%s'", scriptPythonType), ex);
    }
    Object scriptJavaClass = scriptPythonTypeClass.__tojava__(scriptJavaType);
    if (!ReflectHelper.assignableFrom(scriptJavaClass.getClass(), scriptJavaType)) {
        return null;
    }
    return (T) scriptJavaClass;
}
Also used : PythonException(org.xdi.exception.PythonException) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject) PythonException(org.xdi.exception.PythonException) IOException(java.io.IOException) PyException(org.python.core.PyException)

Aggregations

PythonException (org.xdi.exception.PythonException)4 PyObject (org.python.core.PyObject)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 PyLong (org.python.core.PyLong)2 BaseExternalType (org.xdi.model.custom.script.type.BaseExternalType)2 RegistrationScript (org.gluu.oxtrust.service.python.interfaces.RegistrationScript)1 PyException (org.python.core.PyException)1 CustomScriptType (org.xdi.model.custom.script.CustomScriptType)1