Skip to content

Commit

Permalink
Improved: Use ‘java.util.Base64’
Browse files Browse the repository at this point in the history
(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 2c2340d
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -850,7 +850,7 @@ public static Map<String, Object> handleDhlShipmentConfirmResponse(String rateRe
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1021,7 +1021,7 @@ public static Map<String, Object> handleFedexShipReply(String fDXShipReplyString
"fDXShipReplyString", fDXShipReplyString), locale));
}

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

if (labelBytes != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1103,15 +1105,15 @@ public static Map<String, Object> handleUpsShipmentAcceptResponse(Document shipm
}
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) {
Expand Down Expand Up @@ -1161,7 +1163,7 @@ public static Map<String, Object> handleUpsShipmentAcceptResponse(Document shipm
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1410,7 +1410,7 @@ public static Map<String, Object> uspsDeliveryConfirmation(DispatchContext dctx,
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,
Expand Down Expand Up @@ -1650,7 +1650,7 @@ public static Map<String, Object> uspsPriorityMailInternationalLabel(DispatchCon
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
213 changes: 0 additions & 213 deletions framework/base/src/main/java/org/apache/ofbiz/base/util/Base64.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 2c2340d

Please sign in to comment.