ignite-6734 sun.misc.BASE64Encoder usages are removed. Signed-off-by: Andrey Gura <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/d36c1719 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/d36c1719 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/d36c1719 Branch: refs/heads/ignite-zk Commit: d36c1719d64b5639f54fa93fdd2faa21522363a9 Parents: 4f739a3 Author: Andrey Kuznetsov <[email protected]> Authored: Tue Dec 26 18:52:43 2017 +0300 Committer: Andrey Gura <[email protected]> Committed: Tue Dec 26 18:52:43 2017 +0300 ---------------------------------------------------------------------- dev-tools/src/main/groovy/jiraslurp.groovy | 2 +- .../rest/JettyRestProcessorSignedSelfTest.java | 7 ++- .../rest/protocols/GridRestProtocolAdapter.java | 7 ++- .../ignite/internal/util/Base64Encoder.java | 33 ++++++++++++++ .../ignite/internal/util/Base64EncoderImpl.java | 45 ++++++++++++++++++++ .../apache/ignite/internal/util/GridUnsafe.java | 19 ++++++++- .../internal/util/LegacyBase64Encoder.java | 42 ++++++++++++++++++ 7 files changed, 145 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/dev-tools/src/main/groovy/jiraslurp.groovy ---------------------------------------------------------------------- diff --git a/dev-tools/src/main/groovy/jiraslurp.groovy b/dev-tools/src/main/groovy/jiraslurp.groovy index e5f9129..566b9d0 100644 --- a/dev-tools/src/main/groovy/jiraslurp.groovy +++ b/dev-tools/src/main/groovy/jiraslurp.groovy @@ -81,7 +81,7 @@ def sendHttpRequest = { requestMethod, urlString, user, pwd, postData, contentTy HttpURLConnection conn = (HttpURLConnection)url.openConnection(); - String encoded = new sun.misc.BASE64Encoder().encode("$user:$pwd".getBytes()); + String encoded = "$user:$pwd".getBytes().encodeBase64().toString(); conn.setRequestProperty("Authorization", "Basic " + encoded); http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorSignedSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorSignedSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorSignedSelfTest.java index 4dc8eeb..205154e 100644 --- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorSignedSelfTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorSignedSelfTest.java @@ -24,7 +24,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.internal.util.typedef.internal.U; -import sun.misc.BASE64Encoder; + +import static org.apache.ignite.internal.util.GridUnsafe.encodeBase64; /** * @@ -87,11 +88,9 @@ public class JettyRestProcessorSignedSelfTest extends JettyRestProcessorAbstract try { MessageDigest md = MessageDigest.getInstance("SHA-1"); - BASE64Encoder enc = new BASE64Encoder(); - md.update(s.getBytes()); - String hash = enc.encode(md.digest()); + String hash = encodeBase64(md.digest()); return ts + ":" + hash; } http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/GridRestProtocolAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/GridRestProtocolAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/GridRestProtocolAdapter.java index 3eb594f..e78edef 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/GridRestProtocolAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/GridRestProtocolAdapter.java @@ -35,7 +35,8 @@ import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.lang.IgniteBiTuple; import org.jetbrains.annotations.Nullable; -import sun.misc.BASE64Encoder; + +import static org.apache.ignite.internal.util.GridUnsafe.encodeBase64; /** * Abstract protocol adapter. @@ -106,11 +107,9 @@ public abstract class GridRestProtocolAdapter implements GridRestProtocol { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); - BASE64Encoder enc = new BASE64Encoder(); - md.update(s.getBytes(UTF_8)); - String compHash = enc.encode(md.digest()); + String compHash = encodeBase64(md.digest()); return hash.equalsIgnoreCase(compHash); } http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/modules/core/src/main/java/org/apache/ignite/internal/util/Base64Encoder.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/Base64Encoder.java b/modules/core/src/main/java/org/apache/ignite/internal/util/Base64Encoder.java new file mode 100644 index 0000000..f5e4689 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/Base64Encoder.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.util; + +/** + * BASE64 encoder interface. + * + * @deprecated Temporary interface. Use {@code java.util.Base64} directly instead. + */ +@Deprecated +public interface Base64Encoder { + /** + * Encodes given byte array. + * + * @return Encoded string. + */ + public String encode(byte[] msg); +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/modules/core/src/main/java/org/apache/ignite/internal/util/Base64EncoderImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/Base64EncoderImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/util/Base64EncoderImpl.java new file mode 100644 index 0000000..fa29b5e --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/Base64EncoderImpl.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Implementation of {@link Base64Encoder} interface for Java 8 and later. + * + * @deprecated Use {@code java.util.Base64} directly instead. + */ +@Deprecated +public class Base64EncoderImpl implements Base64Encoder { + /** {@inheritDoc} */ + @Override public String encode(byte[] msg) { + try { + Object encoder = Class.forName("java.util.Base64").getDeclaredMethod("getEncoder").invoke(null); + + Class<?> encCls = Class.forName("java.util.Base64$Encoder"); + + Method encMtd = encCls.getDeclaredMethod("encodeToString", byte[].class); + + return (String)encMtd.invoke(encoder, (Object)msg); + } + catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new RuntimeException("Failed to encode Base64 message", e); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java index c4583a2..f4c5e80 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java @@ -32,6 +32,7 @@ import sun.misc.JavaNioAccess; import sun.misc.SharedSecrets; import sun.misc.Unsafe; +import static org.apache.ignite.internal.util.IgniteUtils.jdkVersion; import static org.apache.ignite.internal.util.IgniteUtils.majorJavaVersion; /** @@ -107,10 +108,16 @@ public abstract class GridUnsafe { /** Cleaner code for direct {@code java.nio.ByteBuffer}. */ private static final DirectBufferCleaner DIRECT_BUF_CLEANER = - majorJavaVersion(System.getProperty("java.specification.version")) < 9 + majorJavaVersion(jdkVersion()) < 9 ? new ReflectiveDirectBufferCleaner() : new UnsafeDirectBufferCleaner(); + /** */ + private static final Base64Encoder BASE64_ENC = + majorJavaVersion(jdkVersion()) < 8 + ? new LegacyBase64Encoder() + : new Base64EncoderImpl(); + /** * Ensure singleton. */ @@ -1746,4 +1753,14 @@ public abstract class GridUnsafe { UNSAFE.putByte(addr, (byte)(val)); } } + + /** + * Encodes bytes into Base64 string. + * + * @param msg Message to encode. + * @return Encoded message. + */ + public static String encodeBase64(byte[] msg) { + return BASE64_ENC.encode(msg); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/d36c1719/modules/core/src/main/java/org/apache/ignite/internal/util/LegacyBase64Encoder.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/LegacyBase64Encoder.java b/modules/core/src/main/java/org/apache/ignite/internal/util/LegacyBase64Encoder.java new file mode 100644 index 0000000..e6e9cb5 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/LegacyBase64Encoder.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.util; + +import java.lang.reflect.Method; + +/** + * Implementation of {@link Base64Encoder} interface for Java 7 and earlier. + * + * @deprecated Use {@code jaba.util.Base64} instead. + */ +@Deprecated +public class LegacyBase64Encoder implements Base64Encoder { + /** {@inheritDoc} */ + @Override public String encode(byte[] msg) { + try { + Class<?> encoderCls = Class.forName("sun.misc.BASE64Encoder"); + + Method mtd = encoderCls.getMethod("encode", byte[].class); + + return (String)mtd.invoke(encoderCls.newInstance(), (Object)msg); + } + catch (ReflectiveOperationException e) { + throw new RuntimeException("Failed to encode message to BASE64", e); + } + } +}
