Example usage for io.netty.util CharsetUtil US_ASCII

List of usage examples for io.netty.util CharsetUtil US_ASCII

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil US_ASCII.

Prototype

Charset US_ASCII

To view the source code for io.netty.util CharsetUtil US_ASCII.

Click Source Link

Document

7-bit ASCII, as known as ISO646-US or the Basic Latin block of the Unicode character set

Usage

From source file:divconq.net.ssl.PemReader.java

License:Apache License

static ByteBuf[] readCertificates(File file) throws CertificateException {
    String content;//from  w w  w .  j a v  a2s. com
    try {
        content = readContent(file);
    } catch (IOException e) {
        throw new CertificateException("failed to read a file: " + file, e);
    }

    List<ByteBuf> certs = new ArrayList<ByteBuf>();
    Matcher m = CERT_PATTERN.matcher(content);
    int start = 0;
    for (;;) {
        if (!m.find(start)) {
            break;
        }

        ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
        ByteBuf der = Base64.decode(base64);
        base64.release();
        certs.add(der);

        start = m.end();
    }

    if (certs.isEmpty()) {
        throw new CertificateException("found no certificates: " + file);
    }

    return certs.toArray(new ByteBuf[certs.size()]);
}

From source file:divconq.net.ssl.PemReader.java

License:Apache License

static ByteBuf readPrivateKey(File file) throws KeyException {
    String content;//from   w w  w .  j  a  v a  2s  .com
    try {
        content = readContent(file);
    } catch (IOException e) {
        throw new KeyException("failed to read a file: " + file, e);
    }

    Matcher m = KEY_PATTERN.matcher(content);
    if (!m.find()) {
        throw new KeyException("found no private key: " + file);
    }

    ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
    ByteBuf der = Base64.decode(base64);
    base64.release();
    return der;
}

From source file:divconq.net.ssl.PemReader.java

License:Apache License

private static String readContent(File file) throws IOException {
    InputStream in = new FileInputStream(file);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//from   ww  w . j a  v a  2  s.co m
        byte[] buf = new byte[8192];
        for (;;) {
            int ret = in.read(buf);
            if (ret < 0) {
                break;
            }
            out.write(buf, 0, ret);
        }
        return out.toString(CharsetUtil.US_ASCII.name());
    } finally {
        safeClose(in);
        safeClose(out);
    }
}

From source file:io.netty.example.ocsp.OcspServerExample.java

License:Apache License

private static X509Certificate[] parseCertificates(Class<?> clazz, String name) throws Exception {
    InputStream in = clazz.getResourceAsStream(name);
    if (in == null) {
        throw new FileNotFoundException("clazz=" + clazz + ", name=" + name);
    }/*from   w  w w  .j a v  a 2s  .  c  o  m*/

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, CharsetUtil.US_ASCII));
        try {
            return parseCertificates(reader);
        } finally {
            reader.close();
        }
    } finally {
        in.close();
    }
}

From source file:io.riox.springxd.sinks.websocket.NettyWebSocketServerIndexPage.java

License:Apache License

public static ByteBuf getContent(String webSocketLocation) {
    return Unpooled.copiedBuffer(CONTENT.replace("%WEBSOCKET_URL%", webSocketLocation), CharsetUtil.US_ASCII);
}

From source file:io.undertow.websockets.core.protocol.AbstractWebSocketServerTest.java

License:Open Source License

@Test
public void testText() throws Exception {
    if (getVersion() == WebSocketVersion.V00) {
        // ignore 00 tests for now
        return;//from  w  ww.j  a v  a  2 s. c o m
    }
    final AtomicBoolean connected = new AtomicBoolean(false);
    DefaultServer.setRootHandler(new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {
        @Override
        public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
            connected.set(true);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {
                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message)
                        throws IOException {
                    String string = message.getData();

                    if (string.equals("hello")) {
                        WebSockets.sendText("world", channel, null);
                    } else {
                        WebSockets.sendText(string, channel, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    }));

    final FutureResult<?> latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(getVersion(),
            new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default"))
                    + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", CharsetUtil.US_ASCII)),
            new FrameChecker(TextWebSocketFrame.class, "world".getBytes(CharsetUtil.US_ASCII), latch));
    latch.getIoFuture().get();
    client.destroy();
}

From source file:me.davehummel.tredserver.web.VoxelExplorerIndex.java

License:Apache License

public static ByteBuf getContent(String webSocketLocation) {
    return Unpooled.copiedBuffer("<html><head><title>Voxel Explorer</title></head>" + NEWLINE
            + "<body onload=\"start()\">" + NEWLINE + "<canvas id=\"glcanvas\" width=\"640\" height=\"480\">\n"
            + "    Your browser doesn't appear to support the \n" + "    <code>&lt;canvas&gt;</code> element.\n"
            + "</canvas> <p>" + "<script type=\"text/javascript\">" + NEWLINE + "var socket;" + NEWLINE
            + "if (!window.WebSocket) {" + NEWLINE + "  window.WebSocket = window.MozWebSocket;" + NEWLINE + '}'
            + NEWLINE + "if (window.WebSocket) {" + NEWLINE + "  socket = new WebSocket(\"" + webSocketLocation
            + "\");" + NEWLINE + "  socket.onmessage = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + '\\n' + event.data;" + NEWLINE + "   ta.scrollTop = ta.scrollHeight;"
            + "  };" + NEWLINE + "  socket.onopen = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = \"Web Socket opened!\";" + NEWLINE + "  };" + NEWLINE
            + "  socket.onclose = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + \"Web Socket closed\"; " + NEWLINE + "  };" + NEWLINE + "} else {"
            + NEWLINE + "  alert(\"Your browser does not support Web Socket.\");" + NEWLINE + '}' + NEWLINE
            + NEWLINE + "function send(message) {" + NEWLINE + "  if (!window.WebSocket) { return; }" + NEWLINE
            + "  if (socket.readyState == WebSocket.OPEN) {" + NEWLINE + "    socket.send(message);" + NEWLINE
            + "  } else {" + NEWLINE + "    alert(\"The socket is not open.\");" + NEWLINE + "  }" + NEWLINE
            + '}' + NEWLINE + "var gl; // A global variable for the WebGL context\n" + "\n"
            + "function start() {\n" + "  var canvas = document.getElementById(\"glcanvas\");\n" + "\n"
            + "  // Initialize the GL context\n" + "  gl = initWebGL(canvas);\n" + "  \n"
            + "  // Only continue if WebGL is available and working\n" + "  \n" + "  if (gl) {\n"
            + "    // Set clear color to black, fully opaque\n" + "    gl.clearColor(0.0, 0.0, 0.0, 1.0);\n"
            + "    // Enable depth testing\n" + "    gl.enable(gl.DEPTH_TEST);\n"
            + "    // Near things obscure far things\n" + "    gl.depthFunc(gl.LEQUAL);\n"
            + "    // Clear the color as well as the depth buffer.\n"
            + "    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);\n" + "  }\n" + "}"
            + "function initWebGL(canvas) {\n" + "  gl = null;\n" + "  \n" + "  try {\n"
            + "    // Try to grab the standard context. If it fails, fallback to experimental.\n"
            + "    gl = canvas.getContext(\"webgl\") || canvas.getContext(\"experimental-webgl\");\n" + "  }\n"
            + "  catch(e) {}\n" + "  \n" + "  // If we don't have a GL context, give up now\n"
            + "  if (!gl) {\n"
            + "    alert(\"Unable to initialize WebGL. Your browser may not support it.\");\n"
            + "    gl = null;\n" + "  }\n" + "  \n" + "  return gl;\n" + "}" + "</script>" + NEWLINE
            + "<form onsubmit=\"return false;\">" + NEWLINE
            + "<input type=\"text\" name=\"message\" style=\"width:400px\" value=\"C G 10 HEADING\"/>"
            + "<input type=\"button\" value=\"Send\"" + NEWLINE
            + "       onclick=\"send(this.form.message.value)\" />" + NEWLINE + "<h3>Output</h3>" + NEWLINE
            + "<textarea id=\"responseText\" style=\"width:500px;height:300px;\"></textarea>" + NEWLINE
            + "</form>" + NEWLINE + "</body>" + NEWLINE + "</html>" + NEWLINE, CharsetUtil.US_ASCII);
}

From source file:me.davehummel.tredserver.web.WebSocketServerIndexPage.java

License:Apache License

public static ByteBuf getContent(String webSocketLocation) {
    return Unpooled.copiedBuffer("<html><head><title>Robot</title></head>" + NEWLINE + "<body>" + NEWLINE
            + "<script type=\"text/javascript\">" + NEWLINE + "var socket;" + NEWLINE
            + "if (!window.WebSocket) {" + NEWLINE + "  window.WebSocket = window.MozWebSocket;" + NEWLINE + '}'
            + NEWLINE + "if (window.WebSocket) {" + NEWLINE + "  socket = new WebSocket(\"" + webSocketLocation
            + "\");" + NEWLINE + "  socket.onmessage = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + '\\n' + event.data;" + NEWLINE + "   ta.scrollTop = ta.scrollHeight;"
            + "  };" + NEWLINE + "  socket.onopen = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = \"Web Socket opened!\";" + NEWLINE + "  };" + NEWLINE
            + "  socket.onclose = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + \"Web Socket closed\"; " + NEWLINE + "  };" + NEWLINE + "} else {"
            + NEWLINE + "  alert(\"Your browser does not support Web Socket.\");" + NEWLINE + '}' + NEWLINE
            + NEWLINE + "function send(message) {" + NEWLINE + "  if (!window.WebSocket) { return; }" + NEWLINE
            + "  if (socket.readyState == WebSocket.OPEN) {" + NEWLINE + "    socket.send(message);" + NEWLINE
            + "  } else {" + NEWLINE + "    alert(\"The socket is not open.\");" + NEWLINE + "  }" + NEWLINE
            + '}' + NEWLINE + "</script>" + NEWLINE + "<form onsubmit=\"return false;\">" + NEWLINE
            + "<input type=\"text\" name=\"message\" style=\"width:400px\" value=\"C G 10 HEADING\"/>"
            + "<input type=\"button\" value=\"Send\"" + NEWLINE
            + "       onclick=\"send(this.form.message.value)\" />" + NEWLINE + "<h3>Output</h3>" + NEWLINE
            + "<textarea id=\"responseText\" style=\"width:500px;height:300px;\"></textarea>" + NEWLINE
            + "</form>" + NEWLINE + "</body>" + NEWLINE + "</html>" + NEWLINE, CharsetUtil.US_ASCII);
}

From source file:me.hrps.rp.preview.chat.service.WebSocketServerIndexPage.java

License:Apache License

public static ByteBuf getContent2(String webSocketLocation) {
    return Unpooled.copiedBuffer("<!DOCTYPE html>" + NEWLINE + "<html>" + NEWLINE + "    <head>" + NEWLINE
            + "        <title>Pusher Chat  - live demo</title>" + NEWLINE
            + "        <link href=\"css/chat-style.css\" rel=\"stylesheet\">" + NEWLINE
            + "        <script src=\"http://js.pusher.com/1.12/pusher.min.js\" type=\"text/javascript\"></script> "
            + NEWLINE//ww  w .  ja  va 2 s .c o m
            + "        <script src=\"http://code.jquery.com/jquery-1.8.2.min.js\" type=\"text/javascript\"></script>"
            + NEWLINE + "        <script src=\"js/jquery.pusherchat.js\" type=\"text/javascript\"></script>"
            + NEWLINE + "        <script src=\"js/jquery.playSound.js\" type=\"text/javascript\"></script>"
            + NEWLINE + "        " + NEWLINE + "        <style>" + NEWLINE + "            /* demo style */"
            + NEWLINE + "            body{color: #333;font-family:arial, verdana, sans-serif;}" + NEWLINE
            + "            small{color:#ccc ; font-size: 14px}" + NEWLINE
            + "            .account a{ color :#333; background: #eee; border: 1px solid #ccc;padding: 5px; border-radius: 5px;display: inline-block;}"
            + NEWLINE
            + "            pre{line-height: 11px;font-size: 11px;background: #fafafa;border: 1px solid #ccc; padding: 10px}"
            + NEWLINE + "            .hide{font-size: 19px ;color:red ; font-weight: bold;display: none}"
            + NEWLINE + "            .connexion {font-size: 19px ;color:green ; font-weight: bold;}" + NEWLINE
            + "        </style>" + NEWLINE + "    </head>" + NEWLINE + "    <body>" + NEWLINE
            + "       <!--  -->"
            + "        <!--***************************************************** pusher chat html *******************************************************-->"
            + NEWLINE + "        <div id=\"pusherChat\">" + NEWLINE
            + "            <div id=\"membersContent\">                " + NEWLINE
            + "                <span id=\"expand\"><span class=\"close\">&#x25BC;</span><span class=\"open\">&#x25B2;</span></span>"
            + NEWLINE + "                <h2><span id=\"count\">0</span> online</h2>" + NEWLINE
            + "                <div class=\"scroll\">" + NEWLINE
            + "                    <div id=\"members-list\"></div>" + NEWLINE + "                </div>"
            + NEWLINE + "            </div>" + NEWLINE + "            <!-- chat box template -->" + NEWLINE
            + "            <div id=\"templateChatBox\">" + NEWLINE
            + "                <div class=\"pusherChatBox\">" + NEWLINE
            + "                    <span class=\"state\">" + NEWLINE
            + "                        <span class=\"pencil\">" + NEWLINE
            + "                            <img src=\"assets/pencil.gif\" />" + NEWLINE
            + "                        </span>" + NEWLINE + "                        <span class=\"quote\">"
            + NEWLINE + "                            <img src=\"assets/quote.gif\" />" + NEWLINE
            + "                        </span>" + NEWLINE + "                    </span>" + NEWLINE
            + "                    <span class=\"expand\"><span class=\"close\">&#x25BC;</span><span class=\"open\">&#x25B2;</span></span>"
            + NEWLINE + "                    <span class=\"closeBox\">x</span>" + NEWLINE
            + "                    <h2><a href=\"#\" title=\"go to profile\"><img src=\"\" class=\"imgFriend\" /></a> <span class=\"userName\"></span></h2>"
            + "                    <div class=\"slider\">" + NEWLINE
            + "                        <div class=\"logMsg\">" + NEWLINE
            + "                            <div class=\"msgTxt\">" + NEWLINE
            + "                            </div>" + NEWLINE + "                        </div>" + NEWLINE
            + "                        <form method=\"post\" name=\"#123\">" + NEWLINE
            + "                            <textarea  name=\"msg\" rows=\"3\" ></textarea>" + NEWLINE
            + "                            <input type=\"hidden\" name=\"from\" class=\"from\" />" + NEWLINE
            + "                            <input type=\"hidden\" name=\"to\"  class=\"to\"/>" + NEWLINE
            + "                            <input type=\"hidden\" name=\"typing\"  class=\"typing\" value=\"false\"/>"
            + "                        </form>" + NEWLINE + "                    </div>" + NEWLINE
            + "                </div>" + NEWLINE + "            </div>" + NEWLINE
            + "            <!-- chat box template end -->" + "            <div class=\"chatBoxWrap\">"
            + "                <div class=\"chatBoxslide\"></div>"
            + "                <span id=\"slideLeft\"> <img src=\"assets/quote.gif\" />&#x25C0;</span> "
            + "                <span id=\"slideRight\">&#x25B6; <img src=\"assets/quote.gif\" /></span>"
            + "            </div>" + "        </div>"
            + "        <!--***************************************************** end pusher chat html *******************************************************-->"
            + "        <hr/>" + "       " + "        <script type=\"text/javascript\">" + "            /*"
            + "             * this part is only for demo you don\'t need this" + "             */"
            + "            function getUrlVars() {" + "                var vars = {};"
            + "                var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {"
            + "                    vars[key] = value;" + "                });" + "                return vars;"
            + "            }" + "            var id = getUrlVars()[\'user_id\'];"
            + "            var name = getUrlVars()[\'name\'];" + "            if (id==\"undefined\") {"
            + "                id=\"\"; " + "            } else $(\'#user_\'+id).hide();"
            + "            if (name==\"undefined\") name=\"\";"
            + "            if (!id) $(\'#pusherChat\').remove();" + "            if (name)"
            + "                $(\'.connexion\').html(\'You are connected as \'+name.replace(\'%20\',\' \'));"
            + "            /*" + "             * this part is only for demo you don\'t need this"
            + "             */" + "        </script>" + "        <script>"
            + "        $(document).ready(function(){" + "            function showWindow(){"
            + "                var htm=\"<div id=\'register\' style=\'position:fixed; height:200px; width:400px; background:#CCC;\'><em id=\'close\' style=\'font-size:30px; float:right; margin-right:20px;\'></em><h3 style=\'color:#FFF; text-align:center;\'></h3><p style=\'text-align:center;\'><input type=\'text\' id=\'username\' placeholder=\'\'  /></p><p style=\'text-align:center;\'><input type=\'password\' id=\'userpass\' placeholder=\'\' /><em id=\'strength\'></em></p><p id=\'regist\' style=\'margin:40px auto 0 auto;height:30px;width:180px;background:#09C;color:#FFF;font-size:18px;text-align:center;line-height:30px;border-radius: 6px;cursor:pointer\'></p></div>\";"
            + "                $(\"body\").append(htm);" + "                return false;" + "                }"
            + "            function removeWindow(){" + "                $(\"#register\").fadeOut(function(){"
            + "                    $(\"#register\").remove();" + "                    });"
            + "                return false;" + "                }" + "            function center(id){"
            + "                var h=$(window).height();" + "                var w=$(window).width();"
            + "                var fh=$(\"#\"+id).height();" + "                var fw=$(\"#\"+id).width();"
            + "                $(\"#\"+id).css({" + "                    \"top\":(h-fh)/2,"
            + "                    \"left\":(w-fw)/2" + "                    });" + "            }"
            + "            $(window).resize(function(){" + "            center(\"register\");"
            + "            });" + "      " + "            showWindow();" + "            center(\"register\");"
            + "            $(\"#close\").click(function(){" + "               // removeWindow();"
            + "               alert(\"\");" + "            });"
            + "            $(\"#regist\").click(function(){"
            + "               var name=$(\"#username\").val(),pwd=$(\"#userpass\").val();"
            + "               if(name==\"\"||pwd==\"\"){"
            + "                  alert(\"!\")"
            + "               }else{"
            + "                  //ajax?  http://stackoverflow.com/questions/9310112/why-am-i-seeing-an-origin-is-not-allowed-by-access-control-allow-origin-error"
            + "                  $.post(\"http://localhost:9080\",{name:name,pwd:pwd},function(data){"
            + "                     console.log(1)" + "                  })" + "               }"
            + "            })" + "            " + "        });" + "            $.fn.pusherChat({"
            + "                \'pusherKey\':\'GET YOUR KEY FROM http://pusher.com\',"
            + "                \'authPath\':\'server/pusher_auth.php?user_id=\'+id+\'&name=\'+name,"
            + "                \'friendsList\' : \'ajax/friends-list.json\',"
            + "                \'serverPath\' : \'server/server.php\',"
            + "                \'profilePage\':true," + "                \'onFriendConnect\': function(member){"
            + "                    if (member.id) $(\'#user_\'+member.id).hide();  "
            + "                    if (!$(\'.account a:visible\').html()) $(\'.hide\').show();"
            + "                }," + "                \'onFriendLogOut\': function(member){"
            + "                    if (member.id) $(\'#user_\'+member.id).show();  "
            + "                    if ($(\'.account a:visible\').html()) $(\'.hide\').hide();"
            + "                }," + "                \'onSubscription\':function(members){"
            + "                    if ($(\'.account a:visible\').html()) $(\'.hide\').hide();"
            + "                    $.each(members._members_map, function(val){"
            + "                        $(\'#user_\'+val).hide();" + "                    });            "
            + "                }" + "            });" + "        </script>" + NEWLINE + "    </body>" + NEWLINE
            + "</html>", CharsetUtil.US_ASCII);

}

From source file:net.demo.netty.http.websockets.WebSocketServerIndexPage.java

License:Apache License

public static ByteBuf getContent(String webSocketLocation) {
    String s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
            + "<html><head><title>Web Socket Test</title></head>" + NEWLINE + "<body>" + NEWLINE
            + "<script type=\"text/javascript\">" + NEWLINE + "var socket;" + NEWLINE
            + "if (!window.WebSocket) {" + NEWLINE + "  window.WebSocket = window.MozWebSocket;" + NEWLINE + '}'
            + NEWLINE + "if (window.WebSocket) {" + NEWLINE + "  socket = new WebSocket(\"" + webSocketLocation
            + "\");" + NEWLINE + "  socket.onmessage = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + '\\n' + event.data" + NEWLINE + "  };" + NEWLINE
            + "  socket.onopen = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = \"Web Socket opened!\";" + NEWLINE + "  };" + NEWLINE
            + "  socket.onclose = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + \"Web Socket closed\"; " + NEWLINE + "  };" + NEWLINE + "} else {"
            + NEWLINE + "  alert(\"Your browser does not support Web Socket.\");" + NEWLINE + '}' + NEWLINE
            + NEWLINE + "function send(message) {" + NEWLINE + "  if (!window.WebSocket) { return; }" + NEWLINE
            + "  if (socket.readyState == WebSocket.OPEN) {" + NEWLINE + "    socket.send(message);" + NEWLINE
            + "  } else {" + NEWLINE + "    alert(\"The socket is not open.\");" + NEWLINE + "  }" + NEWLINE
            + '}' + NEWLINE + "</script>" + NEWLINE + "<form onsubmit=\"return false;\">" + NEWLINE
            + "<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>"
            + "<input type=\"button\" value=\"Send Web Socket Data\"" + NEWLINE
            + "       onclick=\"send(this.form.message.value)\" />" + NEWLINE + "<h3>Output</h3>" + NEWLINE
            + "<textarea id=\"responseText\" style=\"width:500px;height:300px;\"></textarea>" + NEWLINE
            + "</form>" + NEWLINE + "</body>" + NEWLINE + "</html>" + NEWLINE;
    //      System.out.println(s);
    return Unpooled.copiedBuffer(s, CharsetUtil.US_ASCII);
}