feat(制品库功能开发): 通过异步请求同步数据库中Maven类型制品文件和Nexus制品文件属性

将Nexus制品文件对象标识填充到数据库制品文件标识中(制品文件表示通过Base64解码后存储标识字段)
This commit is contained in:
OTTO 2024-07-08 11:46:54 +08:00
parent 3737d639ae
commit 59f81032fc
2 changed files with 67 additions and 95 deletions

View File

@ -19,73 +19,61 @@ import java.nio.charset.StandardCharsets;
*
* @author microservices
*/
public final class Base64
{
public final class Base64 {
private static final Logger logger = LoggerFactory.getLogger(Base64.class);
static private final int BASELENGTH = 128;
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
static private final int SIXTEENBIT = 16;
static private final int FOURBYTE = 4;
static private final int SIGN = -128;
static private final char PAD = '=';
static final private byte[] base64Alphabet = new byte[BASELENGTH];
static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
static private final int BASELENGTH = 128;
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
static private final int SIXTEENBIT = 16;
static private final int FOURBYTE = 4;
static private final int SIGN = -128;
static private final char PAD = '=';
static final private byte[] base64Alphabet = new byte[BASELENGTH];
static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
static
{
for (int i = 0; i < BASELENGTH; ++i)
{
static {
for (int i = 0; i < BASELENGTH; ++i) {
base64Alphabet[i] = -1;
}
for (int i = 'Z'; i >= 'A'; i--)
{
for (int i = 'Z'; i >= 'A'; i--) {
base64Alphabet[i] = (byte) (i - 'A');
}
for (int i = 'z'; i >= 'a'; i--)
{
for (int i = 'z'; i >= 'a'; i--) {
base64Alphabet[i] = (byte) (i - 'a' + 26);
}
for (int i = '9'; i >= '0'; i--)
{
for (int i = '9'; i >= '0'; i--) {
base64Alphabet[i] = (byte) (i - '0' + 52);
}
base64Alphabet['+'] = 62;
base64Alphabet['/'] = 63;
for (int i = 0; i <= 25; i++)
{
for (int i = 0; i <= 25; i++) {
lookUpBase64Alphabet[i] = (char) ('A' + i);
}
for (int i = 26, j = 0; i <= 51; i++, j++)
{
for (int i = 26, j = 0; i <= 51; i++, j++) {
lookUpBase64Alphabet[i] = (char) ('a' + j);
}
for (int i = 52, j = 0; i <= 61; i++, j++)
{
for (int i = 52, j = 0; i <= 61; i++, j++) {
lookUpBase64Alphabet[i] = (char) ('0' + j);
}
lookUpBase64Alphabet[62] = (char) '+';
lookUpBase64Alphabet[63] = (char) '/';
}
private static boolean isWhiteSpace(char octect)
{
private static boolean isWhiteSpace(char octect) {
return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
}
private static boolean isPad(char octect)
{
private static boolean isPad(char octect) {
return (octect == PAD);
}
private static boolean isData(char octect)
{
private static boolean isData(char octect) {
return (octect < BASELENGTH && base64Alphabet[octect] != -1);
}
@ -95,16 +83,13 @@ public final class Base64
* @param binaryData Array containing binaryData
* @return Encoded Base64 array
*/
public static String encode(byte[] binaryData)
{
if (binaryData == null)
{
public static String encode(byte[] binaryData) {
if (binaryData == null) {
return null;
}
int lengthDataBits = binaryData.length * EIGHTBIT;
if (lengthDataBits == 0)
{
if (lengthDataBits == 0) {
return "";
}
@ -120,8 +105,7 @@ public final class Base64
int encodedIndex = 0;
int dataIndex = 0;
for (int i = 0; i < numberTriplets; i++)
{
for (int i = 0; i < numberTriplets; i++) {
b1 = binaryData[dataIndex++];
b2 = binaryData[dataIndex++];
b3 = binaryData[dataIndex++];
@ -140,8 +124,7 @@ public final class Base64
}
// form integral number of 6-bit groups
if (fewerThan24bits == EIGHTBIT)
{
if (fewerThan24bits == EIGHTBIT) {
b1 = binaryData[dataIndex];
k = (byte) (b1 & 0x03);
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
@ -149,9 +132,7 @@ public final class Base64
encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
encodedData[encodedIndex++] = PAD;
encodedData[encodedIndex++] = PAD;
}
else if (fewerThan24bits == SIXTEENBIT)
{
} else if (fewerThan24bits == SIXTEENBIT) {
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
l = (byte) (b2 & 0x0f);
@ -174,10 +155,8 @@ public final class Base64
* @param encoded string containing Base64 data
* @return Array containind decoded data.
*/
public static byte[] decode(String encoded)
{
if (encoded == null)
{
public static byte[] decode(String encoded) {
if (encoded == null) {
return null;
}
@ -185,15 +164,13 @@ public final class Base64
// remove white spaces
int len = removeWhiteSpace(base64Data);
if (len % FOURBYTE != 0)
{
if (len % FOURBYTE != 0) {
return null;// should be divisible by four
}
int numberQuadruple = (len / FOURBYTE);
if (numberQuadruple == 0)
{
if (numberQuadruple == 0) {
return new byte[0];
}
@ -206,12 +183,10 @@ public final class Base64
int dataIndex = 0;
decodedData = new byte[(numberQuadruple) * 3];
for (; i < numberQuadruple - 1; i++)
{
for (; i < numberQuadruple - 1; i++) {
if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))
|| !isData((d3 = base64Data[dataIndex++])) || !isData((d4 = base64Data[dataIndex++])))
{
|| !isData((d3 = base64Data[dataIndex++])) || !isData((d4 = base64Data[dataIndex++]))) {
return null;
} // if found "no data" just return null
@ -225,8 +200,7 @@ public final class Base64
decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
}
if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++])))
{
if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) {
return null;// if found "no data" just return null
}
@ -235,10 +209,8 @@ public final class Base64
d3 = base64Data[dataIndex++];
d4 = base64Data[dataIndex++];
if (!isData((d3)) || !isData((d4)))
{// Check if they are PAD characters
if (isPad(d3) && isPad(d4))
{
if (!isData((d3)) || !isData((d4))) {// Check if they are PAD characters
if (isPad(d3) && isPad(d4)) {
if ((b2 & 0xf) != 0)// last 4 bits should be zero
{
return null;
@ -247,9 +219,7 @@ public final class Base64
System.arraycopy(decodedData, 0, tmp, 0, i * 3);
tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
return tmp;
}
else if (!isPad(d3) && isPad(d4))
{
} else if (!isPad(d3) && isPad(d4)) {
b3 = base64Alphabet[d3];
if ((b3 & 0x3) != 0)// last 2 bits should be zero
{
@ -260,14 +230,10 @@ public final class Base64
tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
return tmp;
}
else
{
} else {
return null;
}
}
else
{ // No PAD e.g 3cQl
} else { // No PAD e.g 3cQl
b3 = base64Alphabet[d3];
b4 = base64Alphabet[d4];
decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
@ -284,20 +250,16 @@ public final class Base64
* @param data the byte array of base64 data (with WS)
* @return the new length
*/
private static int removeWhiteSpace(char[] data)
{
if (data == null)
{
private static int removeWhiteSpace(char[] data) {
if (data == null) {
return 0;
}
// count characters that's not whitespace
int newSize = 0;
int len = data.length;
for (int i = 0; i < len; i++)
{
if (!isWhiteSpace(data[i]))
{
for (int i = 0; i < len; i++) {
if (!isWhiteSpace(data[i])) {
data[newSize++] = data[i];
}
}
@ -392,18 +354,18 @@ public final class Base64
return base64Content;
}
public static String base64ToDecoderString(String base64Str){
if (StringUtils.isEmpty(base64Str)) {
return base64Str;
public static String base64ToDecoderString(String base64Str) {
if (StringUtils.isEmpty(base64Str)) {
return base64Str;
}
String base64DecoderString;
try {
byte[] decoderBytes = java.util.Base64.getDecoder().decode(base64Str);
base64DecoderString = new String(decoderBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new ServiceException("Base64编码格式不正确");
}
return base64DecoderString;
}
String base64DecoderString;
try {
byte[] decoderBytes = java.util.Base64.getDecoder().decode(base64Str);
base64DecoderString = new String(decoderBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new ServiceException("Base64编码格式不正确");
}
return base64DecoderString;
}
}

View File

@ -2,11 +2,15 @@ package com.microservices.pms.productLibrary.domain;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.sign.Base64;
import com.microservices.common.core.web.domain.BaseEntity;
import com.microservices.pms.productLibrary.domain.vo.NexusMavenAssetVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 制品库-制品对象 pms_product_library_component
@ -18,6 +22,7 @@ import lombok.Data;
@ApiModel("制品对象")
public class PmsProductLibraryComponent extends BaseEntity {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(PmsProductLibraryComponent.class);
/**
* 制品ID
@ -118,7 +123,12 @@ public class PmsProductLibraryComponent extends BaseEntity {
private String reservedField3;
public void setNexusMavenAssetVo(NexusMavenAssetVo nexusMavenAssetVo) {
this.setIdentifier(nexusMavenAssetVo.getId());
String composeId = Base64.base64ToDecoderString(nexusMavenAssetVo.getId());
if (StringUtils.isNotEmpty(composeId)) {
String[] composeIds = composeId.split(":");
this.setIdentifier(composeIds[1]);
}
this.setAttributes(JSONObject.toJSONString(nexusMavenAssetVo));
}
}