Search in sources :

Example 1 with PyLong

use of org.python.core.PyLong 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 2 with PyLong

use of org.python.core.PyLong in project freemarker by apache.

the class JythonModelCache method create.

@Override
protected TemplateModel create(Object obj) {
    boolean asHash = false;
    boolean asSequence = false;
    JythonVersionAdapter versionAdapter = JythonVersionAdapterHolder.INSTANCE;
    if (versionAdapter.isPyInstance(obj)) {
        Object jobj = versionAdapter.pyInstanceToJava(obj);
        // FreeMarker-aware, Jython-wrapped Java objects are left intact
        if (jobj instanceof TemplateModel) {
            return (TemplateModel) jobj;
        }
        if (jobj instanceof Map) {
            asHash = true;
        }
        if (jobj instanceof Date) {
            return new DateModel((Date) jobj, BeansWrapper.getDefaultInstance());
        } else if (jobj instanceof Collection) {
            asSequence = true;
            // doesn't support sets.
            if (!(jobj instanceof List)) {
                obj = new ArrayList((Collection) jobj);
            }
        }
    }
    // If it's not a PyObject, first make a PyObject out of it.
    if (!(obj instanceof PyObject)) {
        obj = Py.java2py(obj);
    }
    if (asHash || obj instanceof PyDictionary || obj instanceof PyStringMap) {
        return JythonHashModel.FACTORY.create(obj, wrapper);
    }
    if (asSequence || obj instanceof PySequence) {
        return JythonSequenceModel.FACTORY.create(obj, wrapper);
    }
    if (obj instanceof PyInteger || obj instanceof PyLong || obj instanceof PyFloat) {
        return JythonNumberModel.FACTORY.create(obj, wrapper);
    }
    if (obj instanceof PyNone) {
        return null;
    }
    return JythonModel.FACTORY.create(obj, wrapper);
}
Also used : DateModel(freemarker.ext.beans.DateModel) ArrayList(java.util.ArrayList) PyFloat(org.python.core.PyFloat) TemplateModel(freemarker.template.TemplateModel) PyInteger(org.python.core.PyInteger) PyDictionary(org.python.core.PyDictionary) Date(java.util.Date) PyNone(org.python.core.PyNone) PyLong(org.python.core.PyLong) PyStringMap(org.python.core.PyStringMap) Collection(java.util.Collection) PyObject(org.python.core.PyObject) ArrayList(java.util.ArrayList) List(java.util.List) PySequence(org.python.core.PySequence) PyStringMap(org.python.core.PyStringMap) Map(java.util.Map) PyObject(org.python.core.PyObject)

Example 3 with PyLong

use of org.python.core.PyLong in project oxCore by GluuFederation.

the class CustomScriptManager method createExternalTypeFromStringWithPythonException.

public BaseExternalType createExternalTypeFromStringWithPythonException(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) throws Exception {
    String script = customScript.getScript();
    String scriptName = StringHelper.toLowerCase(customScript.getName()) + ".py";
    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, scriptName, customScriptType.getPythonClass(), customScriptType.getCustomScriptType(), new PyObject[] { new PyLong(System.currentTimeMillis()) });
    } catch (UnsupportedEncodingException e) {
        log.error(String.format("%s. Script inum: %s", e.getMessage(), customScript.getInum()), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    if (externalType == null) {
        return null;
    }
    boolean initialized = false;
    try {
        if (externalType.getApiVersion() > 10) {
            initialized = externalType.init(customScript, configurationAttributes);
        } else {
            initialized = externalType.init(configurationAttributes);
            log.warn(" Update the script's init method to init(self, customScript, configurationAttributes)", customScript.getName());
        }
    } catch (Exception ex) {
        log.error("Failed to initialize custom script: '{}'", ex, customScript.getName());
    }
    if (initialized) {
        return externalType;
    }
    return null;
}
Also used : PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomScriptType(org.gluu.model.custom.script.CustomScriptType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PyObject(org.python.core.PyObject) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with PyLong

use of org.python.core.PyLong 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();
    String scriptName = StringHelper.toLowerCase(customScript.getName()) + ".py";
    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, scriptName, 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: '{}'", 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)

Aggregations

PyLong (org.python.core.PyLong)4 PyObject (org.python.core.PyObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 IOException (java.io.IOException)2 PythonException (org.xdi.exception.PythonException)2 DateModel (freemarker.ext.beans.DateModel)1 TemplateModel (freemarker.template.TemplateModel)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 CustomScriptType (org.gluu.model.custom.script.CustomScriptType)1 BaseExternalType (org.gluu.model.custom.script.type.BaseExternalType)1 RegistrationScript (org.gluu.oxtrust.service.python.interfaces.RegistrationScript)1 PyDictionary (org.python.core.PyDictionary)1 PyFloat (org.python.core.PyFloat)1 PyInteger (org.python.core.PyInteger)1