Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #1488

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Cleanup #1488

Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<project name="play! framework" default="jar" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">

<property name="baseversion" value="1.7.x" />
<property name="baseversion" value="1.8.x" />

<path id="project.classpath">
<fileset dir=".">
Expand Down
Binary file added framework/lib-test/assertj-core-3.25.1.jar
Binary file not shown.
Binary file removed framework/lib-test/fest-assert-1.4.jar
Binary file not shown.
Binary file removed framework/lib-test/fest-util-1.1.6.jar
Binary file not shown.
16 changes: 0 additions & 16 deletions framework/patches/pgjdbc.9.0.patch

This file was deleted.

65 changes: 34 additions & 31 deletions framework/src/play/libs/Codec.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package play.libs;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import play.exceptions.UnexpectedException;
Expand Down Expand Up @@ -33,18 +33,18 @@
* @return The base64 encoded String
*/
public static String encodeBASE64(String value) {
return new String(Base64.encodeBase64(value.getBytes(UTF_8)));
return java.util.Base64.getEncoder().encodeToString(value.getBytes(UTF_8));
}

/**
* Encode binary data to base64
*
*
* @param value
* The binary data
* @return The base64 encoded String
*/
public static String encodeBASE64(byte[] value) {
return new String(Base64.encodeBase64(value));
return java.util.Base64.getEncoder().encodeToString(value);
}

/**
Expand All @@ -55,50 +55,34 @@
* @return decoded binary data
*/
public static byte[] decodeBASE64(String value) {
return Base64.decodeBase64(value.getBytes(UTF_8));
return java.util.Base64.getDecoder().decode(value);
}

/**
* Build an hexadecimal MD5 hash for a String
* Build a hexadecimal MD5 hash for a String
*
* @param value
* The String to hash
* @return An hexadecimal Hash
* @return A hexadecimal Hash
*/
public static String hexMD5(String value) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(value.getBytes(UTF_8));
byte[] digest = messageDigest.digest();
return byteToHexString(digest);
} catch (Exception ex) {
throw new UnexpectedException(ex);
}
return hashToHexString("MD5", value);
}

/**
* Build an hexadecimal SHA1 hash for a String
*
* Build a hexadecimal SHA1 hash for a String
*
* @param value
* The String to hash
* @return An hexadecimal Hash
* @return A hexadecimal Hash
*/
public static String hexSHA1(String value) {
try {
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
md.update(value.getBytes(UTF_8));
byte[] digest = md.digest();
return byteToHexString(digest);
} catch (Exception ex) {
throw new UnexpectedException(ex);
}
return hashToHexString("SHA-1", value);
}

/**
* Write a byte array as hexadecimal String.
*
*
* @param bytes
* byte array
* @return The hexadecimal String
Expand All @@ -108,8 +92,8 @@
}

/**
* Transform an hexadecimal String to a byte array.
*
* Transform a hexadecimal String to a byte array.
*
* @param hexString
* Hexadecimal string to transform
* @return The byte array
Expand All @@ -122,4 +106,23 @@
}
}

/**
* Build a hexadecimal hash specified by {@code algorithm} for a given {@code value}.
*
* @param algorithm The hash name
* @param value The String to hash
*
* @return A hexadecimal hash
*/
private static String hashToHexString(String algorithm, String value) {
try {
return byteToHexString(
MessageDigest.getInstance(algorithm)
aleksandy marked this conversation as resolved.
Show resolved Hide resolved
.digest(value.getBytes(UTF_8))
);
} catch (NoSuchAlgorithmException ex) {
throw new UnexpectedException(ex);
}
}

}
4 changes: 2 additions & 2 deletions framework/src/play/mvc/ActionInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static void invoke(Http.Request request, Http.Response response) {
// Generate a cache key for this request
cacheKey = cacheFor.generator().newInstance().generate(request);
}
if(cacheKey != null && !"".equals(cacheKey)) {
if(cacheKey != null && !cacheKey.isEmpty()) {
actionResult = (Result) Cache.get(cacheKey);
}
}
Expand All @@ -169,7 +169,7 @@ public static void invoke(Http.Request request, Http.Response response) {
} catch (Result result) {
actionResult = result;
// Cache it if needed
if (cacheKey != null && !"".equals(cacheKey)) {
if (cacheKey != null && !cacheKey.isEmpty()) {
Cache.set(cacheKey, actionResult, actionMethod.getAnnotation(CacheFor.class).value());
}
} catch (JavaExecutionException e) {
Expand Down
87 changes: 49 additions & 38 deletions framework/src/play/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringJoiner;
import java.util.TimeZone;
import java.util.function.BiFunction;

import play.Play;
import play.mvc.Scope;
import play.vfs.VirtualFile;

import static java.util.Objects.requireNonNull;

/**
* Generic utils
*/
Expand All @@ -29,19 +33,17 @@ public static <T> String join(Iterable<T> values, String separator) {
if (values == null) {
return "";
}
Iterator<T> iter = values.iterator();
if (!iter.hasNext()) {
return "";
}
StringBuilder toReturn = new StringBuilder(String.valueOf(iter.next()));
while (iter.hasNext()) {
toReturn.append(separator).append(iter.next());

StringJoiner joiner = new StringJoiner(separator);
for (T value : values) {
joiner.add(String.valueOf(value));
}
return toReturn.toString();

return joiner.toString();
}

public static String join(String[] values, String separator) {
return (values == null) ? "" : join(Arrays.asList(values), separator);
return (values == null) ? "" : String.join(separator, values);
}

public static String join(Annotation[] values, String separator) {
Expand Down Expand Up @@ -92,24 +94,20 @@ public static String open(String file, Integer line) {
*/
public static class Maps {

private static final BiFunction<String[], String[], String[]> MERGE_MAP_VALUES = (oldValues, newValues) -> {
String[] merged = new String[oldValues.length + newValues.length];
System.arraycopy(oldValues, 0, merged, 0, oldValues.length);
System.arraycopy(newValues, 0, merged, oldValues.length, newValues.length);

return merged;
};

public static void mergeValueInMap(Map<String, String[]> map, String name, String value) {
String[] newValues = null;
String[] oldValues = map.get(name);
if (oldValues == null) {
newValues = new String[1];
newValues[0] = value;
} else {
newValues = new String[oldValues.length + 1];
System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
newValues[oldValues.length] = value;
}
map.put(name, newValues);
map.merge(name, new String[]{ value }, MERGE_MAP_VALUES);
}

public static void mergeValueInMap(Map<String, String[]> map, String name, String[] values) {
for (String value : values) {
mergeValueInMap(map, name, value);
}
map.merge(name, requireNonNull(values), MERGE_MAP_VALUES);
}

public static <K, V> Map<K, V> filterMap(Map<K, V> map, String keypattern) {
Expand All @@ -129,20 +127,22 @@ public static <K, V> Map<K, V> filterMap(Map<K, V> map, String keypattern) {
}
}

private static final ThreadLocal<SimpleDateFormat> httpFormatter = new ThreadLocal<>();
private static final ThreadLocal<SimpleDateFormat> httpFormatter = ThreadLocal.withInitial(() -> {
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));

return format;
});

public static SimpleDateFormat getHttpDateFormatter() {
if (httpFormatter.get() == null) {
httpFormatter.set(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US));
httpFormatter.get().setTimeZone(TimeZone.getTimeZone("GMT"));
}
return httpFormatter.get();
}

public static Map<String, String[]> filterMap(Map<String, String[]> map, String prefix) {
Map<String, String[]> newMap = new HashMap<>();
prefix += '.';
Map<String, String[]> newMap = new HashMap<>(map.size());
for (String key : map.keySet()) {
if (!key.startsWith(prefix + ".")) {
if (!key.startsWith(prefix)) {
newMap.put(key, map.get(key));
}
}
Expand All @@ -155,7 +155,7 @@ public static Map<String, String> filterParams(Scope.Params params, String prefi

public static Map<String, String> filterParams(Map<String, String[]> params, String prefix, String separator) {
Map<String, String> filteredMap = new LinkedHashMap<>();
prefix += ".";
prefix += '.';
for (Map.Entry<String, String[]> e : params.entrySet()) {
if (e.getKey().startsWith(prefix)) {
filteredMap.put(e.getKey().substring(prefix.length()), Utils.join(e.getValue(), separator));
Expand Down Expand Up @@ -201,15 +201,26 @@ public Date parse(String source) throws ParseException {
throw new ParseException("Date format not understood", 0);
}

static final ThreadLocal<AlternativeDateFormat> dateformat = new ThreadLocal<>();
static final ThreadLocal<AlternativeDateFormat> dateformat = ThreadLocal.withInitial(() ->
new AlternativeDateFormat(
Locale.US,
"yyyy-MM-dd'T'HH:mm:ss'Z'", // ISO8601 + timezone
"yyyy-MM-dd'T'HH:mm:ss", // ISO8601
"yyyy-MM-dd HH:mm:ss",
"yyyyMMdd HHmmss",
"yyyy-MM-dd",
"yyyyMMdd'T'HHmmss",
"yyyyMMddHHmmss",
"dd'/'MM'/'yyyy",
"dd-MM-yyyy",
"dd'/'MM'/'yyyy HH:mm:ss",
"dd-MM-yyyy HH:mm:ss",
"ddMMyyyy HHmmss",
"ddMMyyyy"
)
);

public static AlternativeDateFormat getDefaultFormatter() {
if (dateformat.get() == null) {
dateformat.set(new AlternativeDateFormat(Locale.US, "yyyy-MM-dd'T'HH:mm:ss'Z'", // ISO8601 + timezone
"yyyy-MM-dd'T'HH:mm:ss", // ISO8601
"yyyy-MM-dd HH:mm:ss", "yyyyMMdd HHmmss", "yyyy-MM-dd", "yyyyMMdd'T'HHmmss", "yyyyMMddHHmmss", "dd'/'MM'/'yyyy",
"dd-MM-yyyy", "dd'/'MM'/'yyyy HH:mm:ss", "dd-MM-yyyy HH:mm:ss", "ddMMyyyy HHmmss", "ddMMyyyy"));
}
return dateformat.get();
}
}
Expand Down
2 changes: 1 addition & 1 deletion framework/test-src/play/cache/EhCacheImplTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package play.cache;

import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class EhCacheImplTest {

Expand Down
2 changes: 1 addition & 1 deletion framework/test-src/play/data/binding/BeanWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.HashMap;
import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class BeanWrapperTest {

Expand Down
2 changes: 1 addition & 1 deletion framework/test-src/play/data/binding/BinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.*;

import static java.math.BigDecimal.TEN;
import static org.fest.assertions.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.Properties;

import static org.fest.assertions.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

public class ValidationTest {
Expand Down
2 changes: 1 addition & 1 deletion framework/test-src/play/i18n/LangTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package play.i18n;

import static org.fest.assertions.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.Locale;
Expand Down
Loading