Example usage for org.apache.commons.codec.binary Base64 isBase64

List of usage examples for org.apache.commons.codec.binary Base64 isBase64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 isBase64.

Prototype

public static boolean isBase64(final byte[] arrayOctet) 

Source Link

Document

Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.

Usage

From source file:org.apache.nifi.processors.standard.util.ValidatingBase64InputStream.java

@Override
public int read() throws IOException {
    int data = super.read();
    if (!Base64.isBase64((byte) data)) {
        throw new IOException("Data is not base64 encoded.");
    }//from ww  w.j a v a  2s.c  o  m
    return super.read();
}

From source file:org.apache.niolex.notify.Notify.java

/**
 * Fired when the children of this notify changed.
 *
 * @param list the new children list//from  ww w . j  ava 2s .co m
 */
public void onChildrenChange(List<String> list) {
    Pair<List<String>, List<String>> pair = CollectionUtil.intersection(this.children, list);
    for (String s : pair.a) {
        // All the deleted item.
        if (Base64.isBase64(s)) {
            // We try to decode it.
            try {
                Pair<byte[], byte[]> kv = KVBase64Util.base64toKV(s);
                properties.remove(new ByteArray(kv.a));
            } catch (Exception e) {
            }
        }
    }
    this.children = list;
    for (String s : pair.b) {
        // All the added item.
        if (Base64.isBase64(s)) {
            // We try to decode it.
            try {
                Pair<byte[], byte[]> kv = KVBase64Util.base64toKV(s);
                properties.put(new ByteArray(kv.a), kv.b);
                firePropertyChange(kv.a, kv.b);
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.apache.olingo.client.core.AbstractPrimitiveTest.java

protected void binary(final String entity, final String propertyName) throws EdmPrimitiveTypeException {
    final ODataPrimitiveValue opv = readPrimitiveValue(entity, propertyName);
    assertEquals(EdmPrimitiveTypeKind.Binary, opv.getTypeKind());

    final byte[] value = opv.toCastValue(byte[].class);
    assertNotNull(value);/*from  w  w  w.  j  a v  a 2  s .c  o  m*/
    assertTrue(value.length > 0);
    assertTrue(Base64.isBase64(opv.toString()));
}

From source file:org.apache.openaz.xacml.std.json.JSONRequest.java

/**
 * Helper to parse all components of one Category or default Category
 *
 * @param categoryMap/* w  w  w  .j  a  va 2  s  .co m*/
 * @param request
 */
private static void parseCategory(Map<?, ?> categoryMap, String categoryName, Identifier defaultCategoryId,
        StdMutableRequest stdMutableRequest) throws JSONStructureException {

    Identifier categoryId = defaultCategoryId;
    Object categoryIDString = ((Map<?, ?>) categoryMap).remove("CategoryId");
    if (categoryIDString == null && defaultCategoryId == null) {
        throw new JSONStructureException("Category is missing CategoryId");
    }
    if (categoryIDString != null) {
        if (!(categoryIDString instanceof String)) {
            throw new JSONStructureException(
                    "Expect '" + categoryName + "' CategoryId to be String got " + categoryIDString.getClass());
        } else {
            // TODO Spec says CategoryId may be shorthand, but none have been specified
            categoryId = new IdentifierImpl(categoryIDString.toString());
        }
    }
    // if we know the category, make sure user gave correct Id
    if (defaultCategoryId != null && !defaultCategoryId.equals(categoryId)) {
        throw new JSONStructureException(categoryName + " given CategoryId '" + categoryId
                + "' which does not match default id '" + defaultCategoryId + "'");
    }

    // get the Id, a.k.a xmlId
    String xmlId = (String) ((Map<?, ?>) categoryMap).remove("Id");

    // get the Attributes for this Category, if any
    List<Attribute> attributeList = new ArrayList<Attribute>();
    Object attributesMap = ((Map<?, ?>) categoryMap).remove("Attribute");
    if (attributesMap != null) {
        if (attributesMap instanceof ArrayList) {
            attributeList = parseAttribute(categoryId, (ArrayList<?>) attributesMap);
        } else if (attributesMap instanceof Map) {
            // underlying code expects only collections of Attributes, so create a collection of one to
            // pass this single value
            ArrayList<Map<?, ?>> listForOne = new ArrayList<Map<?, ?>>();
            listForOne.add((Map<?, ?>) attributesMap);
            attributeList = parseAttribute(categoryId, listForOne);
        } else {
            throw new JSONStructureException("Category '" + categoryName + "' saw unexpected Attribute class "
                    + attributesMap.getClass());
        }
    }

    // Get the Content node for this Category, if any
    Node contentRootNode = null;
    Object content = categoryMap.remove("Content");
    if (content != null) {
        if (content instanceof String) {
            //
            // Is it Base64 Encoded?
            //
            if (Base64.isBase64(((String) content).getBytes())) {
                //
                // Attempt to decode it
                //
                byte[] realContent = Base64.decodeBase64((String) content);
                //
                // Now what is it? JSON or XML? Should be XML.
                //
                try {
                    contentRootNode = parseXML(new String(realContent, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    throw new JSONStructureException(
                            "Category '" + categoryName + "' Unsupported encoding in Content");
                }
            } else {
                //
                // No, so what is it? Should be XML escaped
                //
                contentRootNode = parseXML((String) content);
            }
        } else if (content instanceof byte[]) {
            //
            // Should be Base64
            //
            if (Base64.isBase64(((String) content).getBytes())) {
                //
                // Attempt to decode it
                //
                byte[] realContent = Base64.decodeBase64((String) content);
                //
                // Now what is it? JSON or XML? Should be XML.
                //
                try {
                    contentRootNode = parseXML(new String(realContent, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    throw new JSONStructureException(
                            "Category '" + categoryName + "' Unsupported encoding in Content");
                }
            } else {
                throw new JSONStructureException(
                        "Category '" + categoryName + "' Content expected Base64 value");
            }
        } else {
            throw new JSONStructureException("Category '" + categoryName
                    + "' Unable to determine what Content is " + content.getClass());
        }
    }

    checkUnknown(categoryName, categoryMap);

    StdMutableRequestAttributes attributeCategory = new StdMutableRequestAttributes(categoryId, attributeList,
            contentRootNode, xmlId);

    stdMutableRequest.add(attributeCategory);

}

From source file:org.apache.syncope.core.misc.serialization.SyncTokenDeserializer.java

@Override
public SyncToken deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException {

    ObjectNode tree = jp.readValueAsTree();

    Object value = null;//w w  w. ja va2 s .c  om
    if (tree.has("value")) {
        JsonNode node = tree.get("value");
        value = node.isNull() ? null
                : node.isBoolean() ? node.asBoolean()
                        : node.isDouble() ? node.asDouble()
                                : node.isLong() ? node.asLong() : node.isInt() ? node.asInt() : node.asText();

        if (value instanceof String) {
            String base64 = (String) value;
            if (Base64.isBase64(base64)) {
                value = Base64.decodeBase64(base64);
            }
        }
    }

    return new SyncToken(value);
}

From source file:org.artifactory.security.crypto.ArtifactoryBase64.java

public static byte[] extractBytes(String encrypted) {
    String stripped;//from  w  w w  . j  a  va2 s.c o m
    if (encrypted.startsWith(ESCAPED_DEFAULT_ENCRYPTION_PREFIX)) {
        stripped = StringUtils.removeStart(encrypted, ESCAPED_DEFAULT_ENCRYPTION_PREFIX);
    } else if (encrypted.startsWith(getEncryptionPrefix())) {
        stripped = StringUtils.removeStart(encrypted, getEncryptionPrefix());
    } else if (encrypted.length() > 125) {
        // The private and public key are big and have no {DESede} in the front but are full base64
        stripped = encrypted;
    } else {
        return null;
    }
    if (Base64.isBase64(stripped)) {
        return fromBase64(stripped);
    }
    return null;
}

From source file:org.callimachusproject.auth.CookieAuthenticationManager.java

public CookieAuthenticationManager(String identifier, String redirect_uri, String fullname_prefix, String path,
        List<String> domains, RealmManager realms, ParameterAuthReader reader)
        throws OpenRDFException, IOException {
    assert domains != null;
    assert domains.size() > 0;
    this.domains = domains;
    Set<Integer> ports = new HashSet<Integer>();
    for (String domain : domains) {
        int port = java.net.URI.create(domain).getPort();
        ports.add(port);//from  ww  w . j  a v a 2s. c  o  m
        StringBuilder suffix = new StringBuilder();
        if (port > 0) {
            suffix.append(port);
        }
        if (domain.startsWith("https")) {
            suffix.append('s');
        }
        suffix.append('=');
        userCookies.add("username" + suffix);
    }
    assert reader != null;
    assert identifier != null;
    this.reader = reader;
    this.identifier = identifier;
    this.redirect_prefix = redirect_uri + "&return_to=";
    this.fullname_prefix = fullname_prefix;
    assert redirect_uri.contains("?");
    boolean secureOnly = identifier.startsWith("https");
    this.protectedPath = path;
    this.secureCookie = secureOnly ? ";Secure" : "";
    String hex = Integer.toHexString(Math.abs(identifier.hashCode()));
    this.sid = "sid" + hex + "=";
    String string = realms.getRealm(identifier).getOriginSecret();
    if (Base64.isBase64(string)) {
        this.secret = Base64.decodeBase64(string);
    } else {
        this.secret = string.getBytes(Charset.forName("UTF-8"));
    }
}

From source file:org.callimachusproject.auth.DigestPasswordAccessor.java

private byte[] readBytes(String string) {
    if (Base64.isBase64(string))
        return Base64.decodeBase64(string);
    return string.getBytes(Charset.forName("UTF-8"));
}

From source file:org.ctoolkit.restapi.client.pubsub.PubsubMessageListener.java

/**
 * Encode Base64 based string. If not encoded, value will be returned as been provided.
 *
 * @param data optionally encoded string value
 * @return the decoded data//from   w w w  .j  ava  2s .com
 */
default String decode(String data) {
    String decoded;
    if (Base64.isBase64(data.getBytes())) {
        decoded = new String(new PubsubMessage().setData(data).decodeData(), Charsets.UTF_8);
    } else {
        decoded = data;
    }
    return decoded;
}

From source file:org.dd4t.core.util.CompressionUtils.java

/**
 * Decodes the given string using Base64 algorithm.
 *
 * @param message String representing the message to decode
 * @return byte[] representing the decoded array
 *//*from ww  w  .  j a v a 2  s. co  m*/
public static byte[] decodeBase64(String message) {
    if (Base64.isBase64(message)) {
        return Base64.decodeBase64(message);
    }

    byte[] result;
    try {
        result = message.getBytes("UTF-8");
    } catch (UnsupportedEncodingException uee) {
        LOG.warn("Unsupported Encoding Exception: " + uee.getMessage());
        result = message.getBytes();
    }
    return result;
}