Example usage for org.apache.commons.codec Charsets toCharset

List of usage examples for org.apache.commons.codec Charsets toCharset

Introduction

In this page you can find the example usage for org.apache.commons.codec Charsets toCharset.

Prototype

public static Charset toCharset(final String charset) 

Source Link

Document

Returns a Charset for the named charset.

Usage

From source file:org.apache.ignite.console.agent.handlers.RestHandler.java

/**
 * @param uri Url.// w ww .j  a v a2s. c o  m
 * @param params Params.
 * @param demo Use demo node.
 * @param mtd Method.
 * @param headers Headers.
 * @param body Body.
 */
protected RestResult executeRest(String uri, Map<String, Object> params, boolean demo, String mtd,
        Map<String, Object> headers, String body) throws IOException, URISyntaxException {
    if (log.isDebugEnabled())
        log.debug("Start execute REST command [method=" + mtd + ", uri=/" + (uri == null ? "" : uri)
                + ", parameters=" + params + "]");

    final URIBuilder builder;

    if (demo) {
        // try start demo if needed.
        AgentClusterDemo.testDrive(cfg);

        // null if demo node not started yet.
        if (cfg.demoNodeUri() == null)
            return RestResult.fail("Demo node is not started yet.", 404);

        builder = new URIBuilder(cfg.demoNodeUri());
    } else
        builder = new URIBuilder(cfg.nodeUri());

    if (builder.getPort() == -1)
        builder.setPort(DFLT_NODE_PORT);

    if (uri != null) {
        if (!uri.startsWith("/") && !cfg.nodeUri().endsWith("/"))
            uri = '/' + uri;

        builder.setPath(uri);
    }

    if (params != null) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getValue() != null)
                builder.addParameter(entry.getKey(), entry.getValue().toString());
        }
    }

    HttpRequestBase httpReq = null;

    try {
        if ("GET".equalsIgnoreCase(mtd))
            httpReq = new HttpGet(builder.build());
        else if ("POST".equalsIgnoreCase(mtd)) {
            HttpPost post;

            if (body == null) {
                List<NameValuePair> nvps = builder.getQueryParams();

                builder.clearParameters();

                post = new HttpPost(builder.build());

                if (!nvps.isEmpty())
                    post.setEntity(new UrlEncodedFormEntity(nvps));
            } else {
                post = new HttpPost(builder.build());

                post.setEntity(new StringEntity(body));
            }

            httpReq = post;
        } else
            throw new IOException("Unknown HTTP-method: " + mtd);

        if (headers != null) {
            for (Map.Entry<String, Object> entry : headers.entrySet())
                httpReq.addHeader(entry.getKey(),
                        entry.getValue() == null ? null : entry.getValue().toString());
        }

        try (CloseableHttpResponse resp = httpClient.execute(httpReq)) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            resp.getEntity().writeTo(out);

            Charset charset = Charsets.UTF_8;

            Header encodingHdr = resp.getEntity().getContentEncoding();

            if (encodingHdr != null) {
                String encoding = encodingHdr.getValue();

                charset = Charsets.toCharset(encoding);
            }

            return RestResult.success(resp.getStatusLine().getStatusCode(),
                    new String(out.toByteArray(), charset));
        } catch (ConnectException e) {
            log.info("Failed connect to node and execute REST command [uri=" + builder.build() + "]");

            return RestResult.fail("Failed connect to node and execute REST command.", 404);
        }
    } finally {
        if (httpReq != null)
            httpReq.reset();
    }
}

From source file:org.apache.rocketmq.mysql.schema.column.StringColumnParser.java

@Override
public Object getValue(Object value) {

    if (value == null) {
        return null;
    }/*ww  w  . j  a va  2s.c o m*/

    if (value instanceof String) {
        return value;
    }

    byte[] bytes = (byte[]) value;

    switch (charset) {
    case "utf8":
    case "utf8mb4":
        return new String(bytes, Charsets.UTF_8);
    case "latin1":
    case "ascii":
        return new String(bytes, Charsets.ISO_8859_1);
    case "ucs2":
        return new String(bytes, Charsets.UTF_16);
    default:
        return new String(bytes, Charsets.toCharset(charset));

    }
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
 * using the specified character encoding.
 * <p>/*  ww w.  j av a  2  s. c  om*/
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 *
 * @param input    the <code>Reader</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested byte array
 * @throws NullPointerException        if the input is null
 * @throws IOException                 if an I/O error occurs
 * @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *                                     supported.
 * @since 1.1
 */
public static byte[] toByteArray(Reader input, String encoding) throws IOException {
    return toByteArray(input, Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the specified character encoding.
 * <p>/*from  w w  w  .j  a  v a2  s . co m*/
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param is       the <code>InputStream</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested character array
 * @throws NullPointerException        if the input is null
 * @throws IOException                 if an I/O error occurs
 * @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *                                     supported.
 * @since 1.1
 */
public static char[] toCharArray(InputStream is, String encoding) throws IOException {
    return toCharArray(is, Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a String
 * using the specified character encoding.
 * <p>//www. j a  va 2s. c  o m
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input    the <code>InputStream</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested String
 * @throws NullPointerException        if the input is null
 * @throws IOException                 if an I/O error occurs
 * @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *                                     supported.
 */
public static String toString(InputStream input, String encoding) throws IOException {
    return toString(input, Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Gets the contents at the given URI./*from ww  w .j  ava2  s  .co m*/
 *
 * @param uri      The URI source.
 * @param encoding The encoding name for the URL contents.
 * @return The contents of the URL as a String.
 * @throws IOException if an I/O exception occurs.
 * @since 2.3.
 */
public static String toString(URI uri, Charset encoding) throws IOException {
    return toString(uri.toURL(), Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Gets the contents at the given URI.//from w w  w.j  av  a  2  s.co m
 *
 * @param uri      The URI source.
 * @param encoding The encoding name for the URL contents.
 * @return The contents of the URL as a String.
 * @throws IOException                 if an I/O exception occurs.
 * @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *                                     supported.
 * @since 2.1
 */
public static String toString(URI uri, String encoding) throws IOException {
    return toString(uri, Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Gets the contents at the given URL./*w  ww . j  a v a 2 s  .co  m*/
 *
 * @param url      The URL source.
 * @param encoding The encoding name for the URL contents.
 * @return The contents of the URL as a String.
 * @throws IOException                 if an I/O exception occurs.
 * @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *                                     supported.
 * @since 2.1
 */
public static String toString(URL url, String encoding) throws IOException {
    return toString(url, Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Get the contents of a <code>byte[]</code> as a String
 * using the specified character encoding.
 * <p>//from   w ww. ja v  a  2 s.co m
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 *
 * @param input    the byte array to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested String
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs (never occurs)
 */
public static String toString(byte[] input, String encoding) throws IOException {
    return new String(input, Charsets.toCharset(encoding));
}

From source file:org.power.requests.util.IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a list of Strings,
 * one entry per line, using the specified character encoding.
 * <p>/*  w  ww. j a v a  2 s.  com*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input    the <code>InputStream</code> to read from, not null
 * @param encoding the encoding to use, null means platform default
 * @return the list of Strings, never null
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 * @since 2.3
 */
public static List<String> readLines(InputStream input, Charset encoding) throws IOException {
    InputStreamReader reader = new InputStreamReader(input, Charsets.toCharset(encoding));
    return readLines(reader);
}