Search in sources :

Example 41 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class zip method decompress.

public static final void decompress(IData pipeline) throws ServiceException {
    // --- <<IS-START(decompress)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $contents.zip
    // [i] field:0:optional $encoding
    // [i] field:0:optional $mode {"stream","bytes","string","base64"}
    // [o] record:1:optional $contents
    // [o] - field:0:required name
    // [o] - object:0:required content
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object input = IDataHelper.get(cursor, "$contents.zip");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        ObjectConvertMode mode = IDataHelper.get(cursor, "$mode", ObjectConvertMode.class);
        ZipEntryWithData[] entries = ZipHelper.decompress(InputStreamHelper.normalize(input, charset));
        IDataHelper.put(cursor, "$contents", ZipEntryWithData.toIDataArray(entries, charset, mode), false);
    } catch (IOException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : ZipEntryWithData(permafrost.tundra.zip.ZipEntryWithData) ObjectConvertMode(permafrost.tundra.lang.ObjectConvertMode) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Example 42 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class json method emit.

// ---( server methods )---
public static final void emit(IData pipeline) throws ServiceException {
    // --- <<IS-START(emit)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] record:0:optional $document
    // [i] - object:1:optional recordWithNoID
    // [i] field:0:optional $encoding
    // [i] field:0:optional $mode {"stream","bytes","string"}
    // [i] field:0:optional $minify? {"false","true"}
    // [o] object:0:optional $content
    IDataCursor cursor = pipeline.getCursor();
    try {
        IData document = IDataHelper.get(cursor, "$document", IData.class);
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        ObjectConvertMode mode = IDataHelper.get(cursor, "$mode", ObjectConvertMode.class);
        boolean minify = IDataHelper.getOrDefault(cursor, "$minify?", Boolean.class, false);
        if (document != null) {
            IDataJSONParser parser = minify ? new IDataJSONParser(!minify) : IDataJSONParser.getInstance();
            IDataHelper.put(cursor, "$content", ObjectHelper.convert(parser.emit(document, charset), charset, mode));
        }
    } catch (IOException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : ObjectConvertMode(permafrost.tundra.lang.ObjectConvertMode) Charset(java.nio.charset.Charset) IOException(java.io.IOException) IDataJSONParser(permafrost.tundra.data.IDataJSONParser)

Example 43 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class service method respond.

public static final void respond(IData pipeline) throws ServiceException {
    // --- <<IS-START(respond)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] field:0:required $code
    // [i] field:0:optional $message
    // [i] record:0:optional $headers
    // [i] object:0:optional $content
    // [i] field:0:optional $content.type
    // [i] field:0:optional $encoding
    IDataCursor cursor = pipeline.getCursor();
    try {
        int code = IDataHelper.get(cursor, "$code", Integer.class);
        String message = IDataHelper.get(cursor, "$message", String.class);
        IData headers = IDataHelper.get(cursor, "$headers", IData.class);
        Object content = IDataHelper.get(cursor, "$content");
        String contentType = IDataHelper.get(cursor, "$content.type", String.class);
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        ServiceHelper.respond(code, message, headers, InputStreamHelper.normalize(content, charset), contentType, charset);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : Charset(java.nio.charset.Charset)

Example 44 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class xpath method get.

public static final void get(IData pipeline) throws ServiceException {
    // --- <<IS-START(get)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] field:0:required $expression
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [i] field:0:optional $recurse? {"false","true"}
    // [o] record:1:optional $nodes
    // [o] - object:0:required node
    // [o] - field:0:required name.qualified
    // [o] - field:0:optional name.local
    // [o] - field:0:optional name.prefix
    // [o] - field:0:optional name.uri
    // [o] - field:0:required type
    // [o] - field:0:optional value
    // [o] - record:1:optional attributes
    // [o] -- object:0:required node
    // [o] -- field:0:required name.qualified
    // [o] -- field:0:optional name.local
    // [o] -- field:0:optional name.prefix
    // [o] -- field:0:optional name.uri
    // [o] -- field:0:required type
    // [o] -- field:0:optional value
    // [o] - record:1:optional elements
    // [o] -- object:0:required node
    // [o] -- field:0:required name.qualified
    // [o] -- field:0:optional name.local
    // [o] -- field:0:optional name.prefix
    // [o] -- field:0:optional name.uri
    // [o] -- field:0:required type
    // [o] -- field:0:optional value
    // [o] field:0:required $nodes.length
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        String expression = IDataHelper.get(cursor, "$expression", String.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        boolean recurse = IDataHelper.getOrDefault(cursor, "$recurse?", Boolean.class, false);
        XPathExpression compiledExpression = XPathHelper.compile(expression, namespace);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        Nodes nodes = XPathHelper.get(node, compiledExpression);
        if (nodes != null) {
            IDataHelper.put(cursor, "$nodes", nodes.reflect(namespace, recurse));
            IDataHelper.put(cursor, "$nodes.length", nodes.size(), String.class);
        } else {
            IDataHelper.put(cursor, "$nodes.length", "0");
        }
    } catch (XPathExpressionException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset) Nodes(permafrost.tundra.xml.dom.Nodes)

Example 45 with Charset

use of java.nio.charset.Charset in project Tundra by Permafrost.

the class gzip method decompress.

public static final void decompress(IData pipeline) throws ServiceException {
    // --- <<IS-START(decompress)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content.gzip
    // [i] field:0:optional $encoding
    // [i] field:0:optional $mode {"stream","bytes","string","base64"}
    // [o] object:0:optional $content
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object input = IDataHelper.get(cursor, "$content.gzip");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        ObjectConvertMode mode = IDataHelper.get(cursor, "$mode", ObjectConvertMode.class);
        Object output = ObjectHelper.convert(GzipHelper.decompress(InputStreamHelper.normalize(input, charset)), charset, mode);
        IDataHelper.put(cursor, "$content", output, false);
    } catch (IOException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : ObjectConvertMode(permafrost.tundra.lang.ObjectConvertMode) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Aggregations

Charset (java.nio.charset.Charset)1400 IOException (java.io.IOException)259 Test (org.junit.Test)186 InputStream (java.io.InputStream)114 ByteBuffer (java.nio.ByteBuffer)110 File (java.io.File)104 ArrayList (java.util.ArrayList)102 InputStreamReader (java.io.InputStreamReader)99 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)64 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 CharsetDecoder (java.nio.charset.CharsetDecoder)55 ByteArrayInputStream (java.io.ByteArrayInputStream)54 List (java.util.List)54 Path (java.nio.file.Path)50 CharsetEncoder (java.nio.charset.CharsetEncoder)49 FileInputStream (java.io.FileInputStream)48 OutputStream (java.io.OutputStream)47