5 changed files with 98 additions and 8 deletions
@ -0,0 +1,87 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed 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.thingsboard.script.api.mvel; |
||||
|
|
||||
|
import org.mvel2.ExecutionContext; |
||||
|
import org.mvel2.ParserConfiguration; |
||||
|
import org.mvel2.execution.ExecutionArrayList; |
||||
|
import org.mvel2.util.MethodStub; |
||||
|
|
||||
|
import java.io.UnsupportedEncodingException; |
||||
|
import java.util.Base64; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class TbUtils { |
||||
|
|
||||
|
public static void register(ParserConfiguration parserConfig) throws Exception { |
||||
|
parserConfig.addImport("btoa", new MethodStub(TbUtils.class.getMethod("btoa", |
||||
|
String.class))); |
||||
|
parserConfig.addImport("atob", new MethodStub(TbUtils.class.getMethod("atob", |
||||
|
String.class))); |
||||
|
parserConfig.addImport("bytesToString", new MethodStub(TbUtils.class.getMethod("bytesToString", |
||||
|
List.class))); |
||||
|
parserConfig.addImport("bytesToString", new MethodStub(TbUtils.class.getMethod("bytesToString", |
||||
|
List.class, String.class))); |
||||
|
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes", |
||||
|
ExecutionContext.class, String.class))); |
||||
|
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes", |
||||
|
ExecutionContext.class, String.class, String.class))); |
||||
|
} |
||||
|
|
||||
|
public static String btoa(String input) { |
||||
|
return new String(Base64.getEncoder().encode(input.getBytes())); |
||||
|
} |
||||
|
|
||||
|
public static String atob(String encoded) { |
||||
|
return new String(Base64.getDecoder().decode(encoded)); |
||||
|
} |
||||
|
|
||||
|
public static String bytesToString(List<Byte> bytesList) { |
||||
|
byte[] bytes = bytesFromList(bytesList); |
||||
|
return new String(bytes); |
||||
|
} |
||||
|
|
||||
|
public static String bytesToString(List<Byte> bytesList, String charsetName) throws UnsupportedEncodingException { |
||||
|
byte[] bytes = bytesFromList(bytesList); |
||||
|
return new String(bytes, charsetName); |
||||
|
} |
||||
|
|
||||
|
public static List<Byte> stringToBytes(ExecutionContext ctx, String str) { |
||||
|
byte[] bytes = str.getBytes(); |
||||
|
return bytesToList(ctx, bytes); |
||||
|
} |
||||
|
|
||||
|
public static List<Byte> stringToBytes(ExecutionContext ctx, String str, String charsetName) throws UnsupportedEncodingException { |
||||
|
byte[] bytes = str.getBytes(charsetName); |
||||
|
return bytesToList(ctx, bytes); |
||||
|
} |
||||
|
|
||||
|
private static byte[] bytesFromList(List<Byte> bytesList) { |
||||
|
byte[] bytes = new byte[bytesList.size()]; |
||||
|
for (int i = 0; i < bytesList.size(); i++) { |
||||
|
bytes[i] = bytesList.get(i); |
||||
|
} |
||||
|
return bytes; |
||||
|
} |
||||
|
|
||||
|
private static List<Byte> bytesToList(ExecutionContext ctx, byte[] bytes) { |
||||
|
List<Byte> list = new ExecutionArrayList<>(ctx); |
||||
|
for (int i = 0; i < bytes.length; i++) { |
||||
|
list.add(bytes[i]); |
||||
|
} |
||||
|
return list; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue