View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.config.plugins.convert;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  
22  import org.apache.logging.log4j.Logger;
23  import org.apache.logging.log4j.status.StatusLogger;
24  import org.apache.logging.log4j.util.LoaderUtil;
25  
26  /**
27   * @Since 2.9
28   */
29  public class Base64Converter {
30  
31      private static final Logger LOGGER = StatusLogger.getLogger();
32      private static Method method = null;
33      private static Object decoder = null;
34  
35      static {
36          try {
37              // Base64 is available in Java 8 and up.
38              Class<?> clazz = LoaderUtil.loadClass("java.util.Base64");
39              final Method getDecoder = clazz.getMethod("getDecoder", (Class[]) null);
40              decoder = getDecoder.invoke(null, (Object[]) null);
41              clazz = decoder.getClass();
42              method = clazz.getMethod("decode", String.class);
43          } catch (final ClassNotFoundException ex) {
44  
45          } catch (final NoSuchMethodException ex) {
46  
47          } catch (final IllegalAccessException ex) {
48  
49          } catch (final InvocationTargetException ex) {
50  
51          }
52          if (method == null) {
53              try {
54                  // DatatypeConverter is not in the default module in Java 9.
55                  final Class<?> clazz = LoaderUtil.loadClass("javax.xml.bind.DatatypeConverter");
56                  method = clazz.getMethod("parseBase64Binary", String.class);
57              } catch (final ClassNotFoundException ex) {
58                  LOGGER.error("No Base64 Converter is available");
59              } catch (final NoSuchMethodException ex) {
60  
61              }
62          }
63      }
64  
65      public static byte[] parseBase64Binary(final String encoded) {
66          if (method == null) {
67              LOGGER.error("No base64 converter");
68          } else {
69              try {
70                  return (byte[]) method.invoke(decoder, encoded);
71              } catch (final IllegalAccessException ex) {
72                  LOGGER.error("Error decoding string - " + ex.getMessage());
73              } catch (final InvocationTargetException ex) {
74                  LOGGER.error("Error decoding string - " + ex.getMessage());
75              }
76          }
77          return new byte[0];
78      }
79  }