Search in sources :

Example 1 with StreamHostObject

use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.

the class WebSocketHostObject method processBinary.

public void processBinary(ByteBuffer byteBuffer) {
    if (binaryCallback == null) {
        return;
    }
    Context cx = RhinoEngine.enterContext(this.contextFactory);
    RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, this.asyncContext);
    ByteArrayInputStream bis = new ByteArrayInputStream(byteBuffer.array());
    StreamHostObject sho = (StreamHostObject) cx.newObject(this, "Stream", new Object[] { bis });
    binaryCallback.call(cx, this, this, new Object[] { sho });
    RhinoEngine.exitContext();
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject)

Example 2 with StreamHostObject

use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.

the class FileHostObject method jsFunction_write.

public static // NOPMD
void jsFunction_write(// NOPMD
Context cx, // NOPMD
Scriptable thisObj, // NOPMD
Object[] args, // NOPMD
Function funObj) throws ScriptException {
    String functionName = "write";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    Object data = args[0];
    FileHostObject fho = (FileHostObject) thisObj;
    if (data instanceof InputStream) {
        fho.file.write((InputStream) data);
    } else if (data instanceof StreamHostObject) {
        fho.file.write(((StreamHostObject) data).getStream());
    } else {
        fho.file.write(HostObjectUtil.serializeObject(args[0]));
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 3 with StreamHostObject

use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.

the class JaggeryParserHostObject method jsFunction_parse.

/**
 * Parses a Jaggery string | stream passed into the parse method and returns a parsed stream
 */
public static Scriptable jsFunction_parse(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "parse";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(HOSTOBJECT_NAME, functionName, argsCount, false);
    }
    InputStream source = null;
    if (args[0] instanceof String) {
        source = new ByteArrayInputStream(((String) args[0]).getBytes());
    } else if (args[0] instanceof StreamHostObject) {
        source = ((StreamHostObject) args[0]).getStream();
    } else {
        HostObjectUtil.invalidArgsError(HOSTOBJECT_NAME, functionName, "1", "string|stream", args[0], false);
    }
    return cx.newObject(thisObj, "Stream", new Object[] { JaggeryParser.parse(source) });
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject)

Example 4 with StreamHostObject

use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.

the class CommonManager method print.

/**
 * JaggeryMethod responsible of writing to the output stream
 */
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "print";
    JaggeryContext jaggeryContext = getJaggeryContext();
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs("RhinoTopLevel", functionName, argsCount, false);
    }
    OutputStream out = (OutputStream) jaggeryContext.getProperty(CommonManager.JAGGERY_OUTPUT_STREAM);
    if (args[0] instanceof StreamHostObject) {
        InputStream in = ((StreamHostObject) args[0]).getStream();
        if (in instanceof FileInputStream) {
            // if form file we will use channel since it's faster
            ReadableByteChannel inputChannel = null;
            WritableByteChannel outputChannel = null;
            FileChannel fc = null;
            try {
                inputChannel = Channels.newChannel(in);
                outputChannel = Channels.newChannel(out);
                fc = (FileChannel) inputChannel;
                fc.transferTo(0, fc.size(), outputChannel);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new ScriptException(e);
            } finally {
                clearResources(fc, inputChannel, outputChannel, in, out);
            }
        } else {
            try {
                IOUtils.copy(in, out);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new ScriptException(e);
            } finally {
                clearResources(in, out);
            }
        }
    } else {
        try {
            out.write(HostObjectUtil.serializeObject(args[0]).getBytes());
        } catch (IOException e) {
            if (log.isDebugEnabled()) {
                log.debug(e.getMessage(), e);
            }
            throw new ScriptException(e);
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) FileChannel(java.nio.channels.FileChannel) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 5 with StreamHostObject

use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.

the class WebSocketHostObject method jsFunction_send.

public static void jsFunction_send(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "send";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String) && !(args[1] instanceof StreamHostObject)) {
        HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string | Stream", args[0], false);
    }
    WebSocketHostObject who = (WebSocketHostObject) thisObj;
    if (args[0] instanceof String) {
        try {
            who.inbound.getWsOutbound().writeTextMessage(CharBuffer.wrap((String) args[0]));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
    } else {
        StreamHostObject sho = (StreamHostObject) args[0];
        InputStream is = sho.getStream();
        try {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) != -1) {
                who.inbound.getWsOutbound().writeBinaryMessage(ByteBuffer.wrap(buffer, 0, length));
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) IOException(java.io.IOException)

Aggregations

StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)2 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 ZipInputStream (java.util.zip.ZipInputStream)1 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 LogHostObject (org.jaggeryjs.hostobjects.log.LogHostObject)1 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1