001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.config.plugins.convert;
018
019import java.lang.reflect.InvocationTargetException;
020import java.lang.reflect.Method;
021
022import org.apache.logging.log4j.Logger;
023import org.apache.logging.log4j.status.StatusLogger;
024import org.apache.logging.log4j.util.Constants;
025import org.apache.logging.log4j.util.LoaderUtil;
026
027/**
028 * @Since 2.9
029 */
030public class Base64Converter {
031
032    private static final Logger LOGGER = StatusLogger.getLogger();
033    private static Method method = null;
034    private static Object decoder = null;
035
036    static {
037        try {
038            // Base64 is available in Java 8 and up.
039            Class<?> clazz = LoaderUtil.loadClass("java.util.Base64");
040            final Method getDecoder = clazz.getMethod("getDecoder", (Class[]) null);
041            decoder = getDecoder.invoke(null, (Object[]) null);
042            clazz = decoder.getClass();
043            method = clazz.getMethod("decode", String.class);
044        } catch (final ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
045            InvocationTargetException ex) {
046            // ignore
047        }
048        if (method == null) {
049            try {
050                // DatatypeConverter is not in the default module in Java 9.
051                final Class<?> clazz = LoaderUtil.loadClass("javax.xml.bind.DatatypeConverter");
052                method = clazz.getMethod("parseBase64Binary", String.class);
053            } catch (final ClassNotFoundException ex) {
054                LOGGER.error("No Base64 Converter is available");
055            } catch (final NoSuchMethodException ex) {
056
057            }
058        }
059    }
060
061    public static byte[] parseBase64Binary(final String encoded) {
062        if (method == null) {
063            LOGGER.error("No base64 converter");
064        } else {
065            try {
066                return (byte[]) method.invoke(decoder, encoded);
067            } catch (final IllegalAccessException | InvocationTargetException ex) {
068                LOGGER.error("Error decoding string - " + ex.getMessage());
069            }
070        }
071        return Constants.EMPTY_BYTE_ARRAY;
072    }
073}