Search in sources :

Example 11 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class RebuildConfiguration method getFileOfData.

/**
 * 获取数据目录下的文件(或目录)
 *
 * @param filepath
 * @return
 */
public static File getFileOfData(String filepath) {
    if (filepath != null && filepath.contains("../")) {
        throw new SecurityException("Attack path detected : " + filepath);
    }
    String d = get(ConfigurationItem.DataDirectory);
    File datadir = null;
    if (StringUtils.isNotBlank(d)) {
        datadir = new File(d);
        if (!datadir.exists() && !datadir.mkdirs()) {
            log.error("Cannot mkdir for data directory : {}", datadir);
        }
    }
    if (datadir == null || !datadir.exists()) {
        datadir = FileUtils.getUserDirectory();
        datadir = new File(datadir, ".rebuild");
        if (!datadir.exists() && !datadir.mkdirs()) {
            log.error("Cannot mkdir for data directory : {}", datadir);
        }
    }
    if (!datadir.exists()) {
        throw new RebuildException("No data directory exists!");
    }
    return filepath == null ? datadir : new File(datadir, filepath);
}
Also used : RebuildException(com.rebuild.core.RebuildException) File(java.io.File)

Example 12 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class BarCodeSupport method createCode.

/**
 * @param content
 * @param format
 * @param height
 * @return
 */
public static BitMatrix createCode(String content, BarcodeFormat format, int height) {
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.MARGIN, 0);
    // 条形码宽度为自适应
    int width = format == BarcodeFormat.QR_CODE ? height : 0;
    try {
        return new MultiFormatWriter().encode(content, format, width, height, hints);
    } catch (WriterException ex) {
        throw new RebuildException("Encode BarCode error : " + content, ex);
    }
}
Also used : EncodeHintType(com.google.zxing.EncodeHintType) HashMap(java.util.HashMap) MultiFormatWriter(com.google.zxing.MultiFormatWriter) RebuildException(com.rebuild.core.RebuildException) WriterException(com.google.zxing.WriterException)

Example 13 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class BarCodeSupport method saveCode.

/**
 * @param content
 * @param format
 * @param height
 * @return
 */
public static File saveCode(String content, BarcodeFormat format, int height) {
    BitMatrix bitMatrix = createCode(content, format, height);
    String fileName = String.format("BarCode-%d.png", System.currentTimeMillis());
    File dest = RebuildConfiguration.getFileOfTemp(fileName);
    try {
        MatrixToImageWriter.writeToPath(bitMatrix, "png", dest.toPath());
        return dest;
    } catch (IOException ex) {
        throw new RebuildException("Write BarCode error : " + content, ex);
    }
}
Also used : RebuildException(com.rebuild.core.RebuildException) BitMatrix(com.google.zxing.common.BitMatrix) IOException(java.io.IOException) File(java.io.File)

Example 14 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class AES method decrypt.

/**
 * @param input
 * @param key
 * @return
 * @throws RebuildException
 */
public static String decrypt(String input, String key) throws RebuildException {
    key = StringUtils.leftPad(key, 16, "0").substring(0, 16);
    byte[] output;
    try {
        SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skey);
        output = cipher.doFinal(Base64.decodeBase64(input));
    } catch (Exception ex) {
        throw new RebuildException("Decrypting error : " + input, ex);
    }
    return new String(output, StandardCharsets.UTF_8);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) RebuildException(com.rebuild.core.RebuildException) Cipher(javax.crypto.Cipher) RebuildException(com.rebuild.core.RebuildException)

Example 15 with RebuildException

use of com.rebuild.core.RebuildException in project rebuild by getrebuild.

the class AES method encrypt.

/**
 * @param input
 * @param key
 * @return
 * @throws RebuildException
 */
public static String encrypt(String input, String key) throws RebuildException {
    key = StringUtils.leftPad(key, 16, "0").substring(0, 16);
    byte[] crypted;
    try {
        SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skey);
        crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
    } catch (Exception ex) {
        throw new RebuildException("Encrypting error : " + input, ex);
    }
    return new String(Base64.encodeBase64(crypted), StandardCharsets.UTF_8);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) RebuildException(com.rebuild.core.RebuildException) Cipher(javax.crypto.Cipher) RebuildException(com.rebuild.core.RebuildException)

Aggregations

RebuildException (com.rebuild.core.RebuildException)16 ID (cn.devezhao.persist4j.engine.ID)4 Entity (cn.devezhao.persist4j.Entity)2 JSONObject (com.alibaba.fastjson.JSONObject)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Cipher (javax.crypto.Cipher)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 Cell (cn.devezhao.commons.excel.Cell)1 Field (cn.devezhao.persist4j.Field)1 Record (cn.devezhao.persist4j.Record)1 PersistManagerImpl (cn.devezhao.persist4j.engine.PersistManagerImpl)1 MetadataException (cn.devezhao.persist4j.metadata.MetadataException)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 AnalysisEventListener (com.alibaba.excel.event.AnalysisEventListener)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 EncodeHintType (com.google.zxing.EncodeHintType)1