Skip to content
Permalink
Browse files

Improved: Use ‘java.util.Base64’

(OFBIZ-11142)

Since Java 8 a standard Base64 encoder/decoder is available. As a
consequence we can remove our embedded implementation and use it
instead.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863504 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 20, 2019
1 parent 54f210a commit 2c2340db0aa004343f3e5cf34ca097ab6acd6852
@@ -22,6 +22,7 @@
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -30,7 +31,6 @@

import javax.xml.parsers.ParserConfigurationException;

import org.apache.ofbiz.base.util.Base64;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.HttpClient;
@@ -850,7 +850,7 @@ public static String sendDhlRequest(String xmlString, Delegator delegator, Strin
continue;
sb.append(encodedImageString.charAt(i));
}
byte[] labelBytes = Base64.base64Decode(sb.toString().getBytes(UtilIO.getUtf8()));
byte[] labelBytes = Base64.getMimeDecoder().decode(sb.toString().getBytes(UtilIO.getUtf8()));

if (labelBytes != null) {
// store in db blob
@@ -24,6 +24,7 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -32,7 +33,6 @@

import javax.xml.parsers.ParserConfigurationException;

import org.apache.ofbiz.base.util.Base64;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.HttpClient;
@@ -1021,7 +1021,7 @@ public static String sendFedexRequest(String xmlString, Delegator delegator, Str
"fDXShipReplyString", fDXShipReplyString), locale));
}

byte[] labelBytes = Base64.base64Decode(encodedImageString.getBytes(UtilIO.getUtf8()));
byte[] labelBytes = Base64.getMimeDecoder().decode(encodedImageString.getBytes(UtilIO.getUtf8()));

if (labelBytes != null) {

@@ -24,7 +24,10 @@
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
@@ -37,7 +40,6 @@

import javax.xml.parsers.ParserConfigurationException;

import org.apache.ofbiz.base.util.Base64;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.HttpClient;
@@ -1103,15 +1105,15 @@
}
byte[] labelImageBytes = null;
if (packageLabelGraphicImageString != null) {
labelImageBytes = Base64.base64Decode(packageLabelGraphicImageString.getBytes(UtilIO.getUtf8()));
labelImageBytes = Base64.getMimeDecoder().decode(packageLabelGraphicImageString.getBytes(UtilIO.getUtf8()));
shipmentPackageRouteSeg.setBytes("labelImage", labelImageBytes);
}
byte[] labelInternationalSignatureGraphicImageBytes = null;
if (packageLabelInternationalSignatureGraphicImageString != null) {
labelInternationalSignatureGraphicImageBytes = Base64.base64Decode(packageLabelInternationalSignatureGraphicImageString.getBytes(UtilIO.getUtf8()));
labelInternationalSignatureGraphicImageBytes = Base64.getMimeDecoder().decode(packageLabelInternationalSignatureGraphicImageString.getBytes(UtilIO.getUtf8()));
shipmentPackageRouteSeg.set("labelIntlSignImage", labelInternationalSignatureGraphicImageBytes);
}
String packageLabelHTMLImageStringDecoded = Base64.base64Decode(packageLabelHTMLImageString);
String packageLabelHTMLImageStringDecoded = Arrays.toString(Base64.getMimeDecoder().decode(packageLabelHTMLImageString.getBytes(StandardCharsets.UTF_8)));
shipmentPackageRouteSeg.set("labelHtml", packageLabelHTMLImageStringDecoded);

if (shipmentUpsSaveCertificationInfo) {
@@ -1161,7 +1163,7 @@
Element controlLogReceiptElement = UtilXml.firstChildElement(shipmentResultsElement, "ControlLogReceipt");
if (controlLogReceiptElement != null) {
String fileString = UtilXml.childElementValue(controlLogReceiptElement, "GraphicImage");
String fileStringDecoded = Base64.base64Decode(fileString);
String fileStringDecoded = Arrays.toString(Base64.getMimeDecoder().decode(fileString.getBytes(StandardCharsets.UTF_8)));
if (fileStringDecoded != null) {
shipmentRouteSegment.set("upsHighValueReport", fileStringDecoded);
shipmentRouteSegment.store();
@@ -25,6 +25,7 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Base64;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
@@ -36,7 +37,6 @@
import javax.xml.transform.TransformerException;

import org.apache.commons.lang.StringUtils;
import org.apache.ofbiz.base.util.Base64;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.HttpClient;
@@ -1410,7 +1410,7 @@
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"FacilityShipmentUspsDeliveryConfirmationResponseIncompleteElementDeliveryConfirmationLabel", locale));
}
shipmentPackageRouteSeg.setBytes("labelImage", Base64.base64Decode(labelImageString.getBytes(UtilIO.getUtf8())));
shipmentPackageRouteSeg.setBytes("labelImage", Base64.getMimeDecoder().decode(labelImageString.getBytes(UtilIO.getUtf8())));
String trackingCode = UtilXml.childElementValue(responseElement, "DeliveryConfirmationNumber");
if (UtilValidate.isEmpty(trackingCode)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
@@ -1650,7 +1650,7 @@
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"FacilityShipmentUspsPriorityMailLabelResponseIncompleteElementLabelImage", locale));
}
shipmentPackageRouteSeg.setBytes("labelImage", Base64.base64Decode(labelImageString.getBytes(UtilIO.getUtf8())));
shipmentPackageRouteSeg.setBytes("labelImage", Base64.getMimeDecoder().decode(labelImageString.getBytes(UtilIO.getUtf8())));
String trackingCode = UtilXml.childElementValue(responseElement, "BarcodeNumber");
if (UtilValidate.isEmpty(trackingCode)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
@@ -19,6 +19,7 @@
package org.apache.ofbiz.base.crypto;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
@@ -191,7 +192,7 @@ public static String pbkdf2HashCrypt(String hashType, String salt, String value)
StringBuilder sb = new StringBuilder();
sb.append("{").append(pbkdf2Type).append("}");
sb.append(PBKDF2_ITERATIONS).append("$");
sb.append(org.apache.ofbiz.base.util.Base64.base64Encode(salt)).append("$");
sb.append(java.util.Base64.getMimeEncoder().encodeToString(salt.getBytes(StandardCharsets.UTF_8))).append("$");
sb.append(new String(hash));
return sb.toString();
} catch (InvalidKeySpecException e) {
@@ -207,7 +208,7 @@ public static boolean doComparePbkdf2(String crypted, String password){
String hashType = crypted.substring(1, typeEnd);
String[] parts = crypted.split("\\$");
int iterations = Integer.parseInt(parts[0].substring(typeEnd+1));
byte[] salt = org.apache.ofbiz.base.util.Base64.base64Decode(parts[1]).getBytes(UtilIO.getUtf8());
byte[] salt = Arrays.toString(java.util.Base64.getMimeDecoder().decode(parts[1].getBytes(StandardCharsets.UTF_8))).getBytes(UtilIO.getUtf8());
byte[] hash = Base64.decodeBase64(parts[2].getBytes(UtilIO.getUtf8()));

PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, hash.length * 8);

This file was deleted.

@@ -26,7 +26,9 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.cert.CertificateException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -487,7 +489,7 @@ private InputStream sendHttpRequestStream(String method, boolean overrideTrust)

// if there is basicAuth info set the request property for it
if (basicAuthUsername != null) {
String basicAuthString = "Basic " + Base64.base64Encode(basicAuthUsername + ":" + (basicAuthPassword == null ? "" : basicAuthPassword));
String basicAuthString = "Basic " + Base64.getMimeEncoder().encodeToString((basicAuthUsername + ":" + (basicAuthPassword == null ? "" : basicAuthPassword)).getBytes(StandardCharsets.UTF_8));
con.setRequestProperty("Authorization", basicAuthString);
if (Debug.verboseOn() || debug) {
Debug.logVerbose("Header - Authorization: " + basicAuthString, module);

0 comments on commit 2c2340d

Please sign in to comment.
You can’t perform that action at this time.