Many small updates from code analysis; remove unused files; tune server params (#1311)

This commit is contained in:
Sean Owen 2020-09-06 18:21:05 -05:00 committed by GitHub
parent da049f21d9
commit 40c2e2e9af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 87 additions and 111 deletions

View file

@ -75,7 +75,7 @@ import android.util.Log;
* user was prompted to download the application. This lets the calling app potentially manage the dialog.
* In particular, ideally, the app dismisses the dialog if it's still active in its {@link Activity#onPause()}
* method.</p>
*
*
* <p>You can use {@link #setTitle(String)} to customize the title of this download prompt dialog (or, use
* {@link #setTitleByID(int)} to set the title by string resource ID.) Likewise, the prompt message, and
* yes/no button labels can be changed.</p>
@ -83,7 +83,7 @@ import android.util.Log;
* <p>Finally, you can use {@link #addExtra(String, Object)} to add more parameters to the Intent used
* to invoke the scanner. This can be used to set additional options not directly exposed by this
* simplified API.</p>
*
*
* <p>By default, this will only allow applications that are known to respond to this intent correctly
* do so. The apps that are allowed to response can be set with {@link #setTargetApplications(List)}.
* For example, set to {@link #TARGET_BARCODE_SCANNER_ONLY} to only target the Barcode Scanner app itself.</p>
@ -130,19 +130,20 @@ public class IntentIntegrator {
public static final Collection<String> DATA_MATRIX_TYPES = Collections.singleton("DATA_MATRIX");
public static final Collection<String> ALL_CODE_TYPES = null;
public static final List<String> TARGET_BARCODE_SCANNER_ONLY = Collections.singletonList(BS_PACKAGE);
public static final List<String> TARGET_ALL_KNOWN = list(
BSPLUS_PACKAGE, // Barcode Scanner+
BSPLUS_PACKAGE + ".simple", // Barcode Scanner+ Simple
BS_PACKAGE // Barcode Scanner
BS_PACKAGE // Barcode Scanner
// What else supports this intent?
);
// Should be FLAG_ACTIVITY_NEW_DOCUMENT in API 21+.
// Defined once here because the current value is deprecated, so generates just one warning
@SuppressWarnings("deprecation")
private static final int FLAG_NEW_DOC = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
private final Activity activity;
private final Fragment fragment;
@ -180,11 +181,11 @@ public class IntentIntegrator {
buttonNo = DEFAULT_NO;
targetApplications = TARGET_ALL_KNOWN;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ -228,18 +229,18 @@ public class IntentIntegrator {
public void setButtonNoByID(int buttonNoID) {
buttonNo = activity.getString(buttonNoID);
}
public Collection<String> getTargetApplications() {
return targetApplications;
}
public final void setTargetApplications(List<String> targetApplications) {
if (targetApplications.isEmpty()) {
throw new IllegalArgumentException("No target applications");
}
this.targetApplications = targetApplications;
}
public void setSingleTargetApplication(String targetApplication) {
this.targetApplications = Collections.singletonList(targetApplication);
}
@ -261,7 +262,7 @@ public class IntentIntegrator {
public final AlertDialog initiateScan() {
return initiateScan(ALL_CODE_TYPES, -1);
}
/**
* Initiates a scan for all known barcode types with the specified camera.
*
@ -285,7 +286,7 @@ public class IntentIntegrator {
public final AlertDialog initiateScan(Collection<String> desiredBarcodeFormats) {
return initiateScan(desiredBarcodeFormats, -1);
}
/**
* Initiates a scan, using the specified camera, only for a certain set of barcode types, given as strings corresponding
* to their names in ZXing's {@code BarcodeFormat} class like "UPC_A". You can supply constants
@ -346,7 +347,7 @@ public class IntentIntegrator {
fragment.startActivityForResult(intent, code);
}
}
private String findTargetAppPackage(Intent intent) {
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
@ -359,7 +360,7 @@ public class IntentIntegrator {
}
return null;
}
private static boolean contains(Iterable<ResolveInfo> availableApps, String targetApp) {
for (ResolveInfo availableApp : availableApps) {
String packageName = availableApp.activityInfo.packageName;
@ -479,7 +480,7 @@ public class IntentIntegrator {
}
return null;
}
private static List<String> list(String... values) {
return Collections.unmodifiableList(Arrays.asList(values));
}

View file

@ -124,7 +124,7 @@ public final class Intents {
* of the app which requested the scan via
* {@link android.app.Activity#startActivityForResult(android.content.Intent, int)}
* The barcodes contents can be retrieved with
* {@link android.content.Intent#getStringExtra(String)}.
* {@link android.content.Intent#getStringExtra(String)}.
* If the user presses Back, the result code will be {@link android.app.Activity#RESULT_CANCELED}.
*/
public static final String RESULT = "SCAN_RESULT";
@ -202,7 +202,7 @@ public final class Intents {
/**
* The data to encode. Use {@link android.content.Intent#putExtra(String, String)} or
* {@link android.content.Intent#putExtra(String, android.os.Bundle)},
* {@link android.content.Intent#putExtra(String, android.os.Bundle)},
* depending on the type and format specified. Non-QR Code formats should
* just use a String here. For QR Code, see Contents for details.
*/
@ -299,5 +299,6 @@ public final class Intents {
// Not the best place for this, but, better than a new class
// Should be FLAG_ACTIVITY_NEW_DOCUMENT in API 21+.
// Defined once here because the current value is deprecated, so generates just one warning
@SuppressWarnings("deprecation")
public static final int FLAG_NEW_DOC = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
}

View file

@ -95,6 +95,7 @@ abstract class ContactEncoder {
}
}
@SuppressWarnings("deprecation")
static String formatPhone(String phoneData) {
// Just collect the call to a deprecated method in one place
return PhoneNumberUtils.formatNumber(phoneData);

View file

@ -504,7 +504,7 @@ public abstract class ResultHandler {
try {
text = URLEncoder.encode(text, "UTF-8");
} catch (UnsupportedEncodingException e) {
// can't happen; UTF-8 is always supported. Continue, I guess, without encoding
// can't happen; UTF-8 is always supported. Continue, I guess, without encoding
}
String url = customProductSearch;
if (rawResult != null) {
@ -520,6 +520,7 @@ public abstract class ResultHandler {
return url.replace("%s", text);
}
@SuppressWarnings("deprecation")
static String formatPhone(String phoneData) {
// Just collect the call to a deprecated method in one place
return PhoneNumberUtils.formatNumber(phoneData);

View file

@ -125,9 +125,9 @@ public final class HighLevelEncoder {
}
*/
private static char randomize253State(char ch, int codewordPosition) {
private static char randomize253State(int codewordPosition) {
int pseudoRandom = ((149 * codewordPosition) % 253) + 1;
int tempVariable = ch + pseudoRandom;
int tempVariable = PAD + pseudoRandom;
return (char) (tempVariable <= 254 ? tempVariable : tempVariable - 254);
}
@ -200,7 +200,7 @@ public final class HighLevelEncoder {
codewords.append(PAD);
}
while (codewords.length() < capacity) {
codewords.append(randomize253State(PAD, codewords.length() + 1));
codewords.append(randomize253State(codewords.length() + 1));
}
return context.getCodewords().toString();

View file

@ -19,7 +19,6 @@ package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
@ -34,8 +33,7 @@ public final class UPCAWriter implements Writer {
private final EAN13Writer subWriter = new EAN13Writer();
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
throws WriterException {
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) {
return encode(contents, format, width, height, null);
}
@ -44,7 +42,7 @@ public final class UPCAWriter implements Writer {
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
Map<EncodeHintType,?> hints) {
if (format != BarcodeFormat.UPC_A) {
throw new IllegalArgumentException("Can only encode UPC-A, but got " + format);
}

View file

@ -166,7 +166,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
}
boolean tryStackedDecode = !this.rows.isEmpty();
storeRow(rowNumber, false); // TODO: deal with reversed rows
storeRow(rowNumber); // TODO: deal with reversed rows
if (tryStackedDecode) {
// When the image is 180-rotated, then rows are sorted in wrong direction.
// Try twice with both the directions.
@ -263,7 +263,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
return false;
}
private void storeRow(int rowNumber, boolean wasReversed) {
private void storeRow(int rowNumber) {
// Discard if duplicate above or below; otherwise insert in order by row number.
int insertPos = 0;
boolean prevIsSame = false;
@ -290,7 +290,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
return;
}
this.rows.add(insertPos, new ExpandedRow(this.pairs, rowNumber, wasReversed));
this.rows.add(insertPos, new ExpandedRow(this.pairs, rowNumber, false));
removePartialRows(this.pairs, this.rows);
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -30,7 +29,7 @@ import org.junit.Test;
public final class CodaBarWriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
doTest("B515-3/B",
"00000" +
"1001001011" + "0110101001" + "0101011001" + "0110101001" + "0101001101" +
@ -39,7 +38,7 @@ public final class CodaBarWriterTestCase extends Assert {
}
@Test
public void testEncode2() throws WriterException {
public void testEncode2() {
doTest("T123T",
"00000" +
"1011001001" + "0101011001" + "0101001011" + "0110010101" + "01011001001" +
@ -47,16 +46,16 @@ public final class CodaBarWriterTestCase extends Assert {
}
@Test
public void testAltStartEnd() throws WriterException {
public void testAltStartEnd() {
assertEquals(encode("T123456789-$T"), encode("A123456789-$A"));
}
private static void doTest(String input, CharSequence expected) throws WriterException {
private static void doTest(String input, CharSequence expected) {
BitMatrix result = encode(input);
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
}
private static BitMatrix encode(String input) throws WriterException {
private static BitMatrix encode(String input) {
return new CodaBarWriter().encode(input, BarcodeFormat.CODABAR, 0, 0);
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -29,7 +28,7 @@ import org.junit.Test;
public final class Code39WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
doTest("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"000001001011011010110101001011010110100101101101101001010101011001011011010110010101" +
"011011001010101010011011011010100110101011010011010101011001101011010101001101011010" +
@ -40,7 +39,7 @@ public final class Code39WriterTestCase extends Assert {
// extended mode blocks
doTest("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f",
"000001001011011010101001001001011001010101101001001001010110101001011010010010010101" +
"000001001011011010101001001001011001010101101001001001010110101001011010010010010101" +
"011010010110100100100101011011010010101001001001010101011001011010010010010101101011" +
"001010100100100101010110110010101001001001010101010011011010010010010101101010011010" +
"100100100101010110100110101001001001010101011001101010010010010101101010100110100100" +
@ -86,7 +85,7 @@ public final class Code39WriterTestCase extends Assert {
"011011001010010110110100000");
}
private static void doTest(String input, CharSequence expected) throws WriterException {
private static void doTest(String input, CharSequence expected) {
BitMatrix result = new Code39Writer().encode(input, BarcodeFormat.CODE_39, 0, 0);
assertEquals(input, expected, BitMatrixTestCase.matrixToString(result));
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -29,7 +28,7 @@ import org.junit.Test;
public final class Code93WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
doTest("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"000001010111101101010001101001001101000101100101001100100101100010101011010001011001" +
"001011000101001101001000110101010110001010011001010001101001011001000101101101101001" +
@ -54,7 +53,7 @@ public final class Code93WriterTestCase extends Assert {
"101011110" + "100000");
}
private static void doTest(String input, CharSequence expected) throws WriterException {
private static void doTest(String input, CharSequence expected) {
BitMatrix result = new Code93Writer().encode(input, BarcodeFormat.CODE_93, 0, 0);
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -29,21 +28,21 @@ import org.junit.Test;
public final class EAN13WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
String testStr = "00001010001011010011101100110010011011110100111010101011001101101100100001010111001001110100010010100000";
BitMatrix result = new EAN13Writer().encode("5901234123457", BarcodeFormat.EAN_13, testStr.length(), 0);
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
@Test
public void testAddChecksumAndEncode() throws WriterException {
public void testAddChecksumAndEncode() {
String testStr = "00001010001011010011101100110010011011110100111010101011001101101100100001010111001001110100010010100000";
BitMatrix result = new EAN13Writer().encode("590123412345", BarcodeFormat.EAN_13, testStr.length(), 0);
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
@Test(expected = IllegalArgumentException.class)
public void testEncodeIllegalCharacters() throws WriterException {
public void testEncodeIllegalCharacters() {
new EAN13Writer().encode("5901234123abc", BarcodeFormat.EAN_13, 0, 0);
}
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -29,21 +28,21 @@ import org.junit.Test;
public final class EAN8WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
String testStr = "0000001010001011010111101111010110111010101001110111001010001001011100101000000";
BitMatrix result = new EAN8Writer().encode("96385074", BarcodeFormat.EAN_8, testStr.length(), 0);
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
@Test
public void testAddChecksumAndEncode() throws WriterException {
public void testAddChecksumAndEncode() {
String testStr = "0000001010001011010111101111010110111010101001110111001010001001011100101000000";
BitMatrix result = new EAN8Writer().encode("9638507", BarcodeFormat.EAN_8, testStr.length(), 0);
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
@Test(expected = IllegalArgumentException.class)
public void testEncodeIllegalCharacters() throws WriterException {
public void testEncodeIllegalCharacters() {
new EAN8Writer().encode("96385abc", BarcodeFormat.EAN_8, 0, 0);
}
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -29,19 +28,19 @@ import org.junit.Test;
public final class ITFWriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
doTest("00123456789012",
"0000010101010111000111000101110100010101110001110111010001010001110100011" +
"100010101000101011100011101011101000111000101110100010101110001110100000");
}
private static void doTest(String input, CharSequence expected) throws WriterException {
private static void doTest(String input, CharSequence expected) {
BitMatrix result = new ITFWriter().encode(input, BarcodeFormat.ITF, 0, 0);
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
}
@Test(expected = IllegalArgumentException.class)
public void testEncodeIllegalCharacters() throws WriterException {
public void testEncodeIllegalCharacters() {
new ITFWriter().encode("00123456789abc", BarcodeFormat.ITF, 0, 0);
}

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
@ -30,14 +29,14 @@ import org.junit.Test;
public final class UPCAWriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
String testStr = "00001010100011011011101100010001011010111101111010101011100101110100100111011001101101100101110010100000";
BitMatrix result = new UPCAWriter().encode("485963095124", BarcodeFormat.UPC_A, testStr.length(), 0);
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
@Test
public void testAddChecksumAndEncode() throws WriterException {
public void testAddChecksumAndEncode() {
String testStr = "00001010011001001001101111010100011011000101011110101010001001001000111010011100101100110110110010100000";
BitMatrix result = new UPCAWriter().encode("12345678901", BarcodeFormat.UPC_A, testStr.length(), 0);
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));

View file

@ -17,7 +17,6 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
@ -29,30 +28,30 @@ import org.junit.Test;
public final class UPCEWriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
public void testEncode() {
doTest("05096893",
"0000000000010101110010100111000101101011110110111001011101010100000000000");
}
@Test
public void testEncodeSystem1() throws WriterException {
public void testEncodeSystem1() {
doTest("12345670",
"0000000000010100100110111101010001101110010000101001000101010100000000000");
}
@Test
public void testAddChecksumAndEncode() throws WriterException {
public void testAddChecksumAndEncode() {
doTest("0509689",
"0000000000010101110010100111000101101011110110111001011101010100000000000");
}
private static void doTest(String content, String encoding) throws WriterException {
private static void doTest(String content, String encoding) {
BitMatrix result = new UPCEWriter().encode(content, BarcodeFormat.UPC_E, encoding.length(), 0);
assertEquals(encoding, BitMatrixTestCase.matrixToString(result));
}
@Test(expected = IllegalArgumentException.class)
public void testEncodeIllegalCharacters() throws WriterException {
public void testEncodeIllegalCharacters() {
new UPCEWriter().encode("05096abc", BarcodeFormat.UPC_E, 0, 0);
}
}

View file

@ -26,9 +26,9 @@ import org.junit.Test;
*/
public final class BitVectorTestCase extends Assert {
private static long getUnsignedInt(BitArray v, int index) {
private static long getUnsignedInt(BitArray v) {
long result = 0L;
for (int i = 0, offset = index * 8; i < 32; i++) {
for (int i = 0, offset = 0; i < 32; i++) {
if (v.get(offset + i)) {
result |= 1L << (31 - i);
}
@ -43,43 +43,43 @@ public final class BitVectorTestCase extends Assert {
// 1
v.appendBit(true);
assertEquals(1, v.getSize());
assertEquals(0x80000000L, getUnsignedInt(v, 0));
assertEquals(0x80000000L, getUnsignedInt(v));
// 10
v.appendBit(false);
assertEquals(2, v.getSize());
assertEquals(0x80000000L, getUnsignedInt(v, 0));
assertEquals(0x80000000L, getUnsignedInt(v));
// 101
v.appendBit(true);
assertEquals(3, v.getSize());
assertEquals(0xa0000000L, getUnsignedInt(v, 0));
assertEquals(0xa0000000L, getUnsignedInt(v));
// 1010
v.appendBit(false);
assertEquals(4, v.getSize());
assertEquals(0xa0000000L, getUnsignedInt(v, 0));
assertEquals(0xa0000000L, getUnsignedInt(v));
// 10101
v.appendBit(true);
assertEquals(5, v.getSize());
assertEquals(0xa8000000L, getUnsignedInt(v, 0));
assertEquals(0xa8000000L, getUnsignedInt(v));
// 101010
v.appendBit(false);
assertEquals(6, v.getSize());
assertEquals(0xa8000000L, getUnsignedInt(v, 0));
assertEquals(0xa8000000L, getUnsignedInt(v));
// 1010101
v.appendBit(true);
assertEquals(7, v.getSize());
assertEquals(0xaa000000L, getUnsignedInt(v, 0));
assertEquals(0xaa000000L, getUnsignedInt(v));
// 10101010
v.appendBit(false);
assertEquals(8, v.getSize());
assertEquals(0xaa000000L, getUnsignedInt(v, 0));
assertEquals(0xaa000000L, getUnsignedInt(v));
// 10101010 1
v.appendBit(true);
assertEquals(9, v.getSize());
assertEquals(0xaa800000L, getUnsignedInt(v, 0));
assertEquals(0xaa800000L, getUnsignedInt(v));
// 10101010 10
v.appendBit(false);
assertEquals(10, v.getSize());
assertEquals(0xaa800000L, getUnsignedInt(v, 0));
assertEquals(0xaa800000L, getUnsignedInt(v));
}
@Test
@ -87,15 +87,15 @@ public final class BitVectorTestCase extends Assert {
BitArray v = new BitArray();
v.appendBits(0x1, 1);
assertEquals(1, v.getSize());
assertEquals(0x80000000L, getUnsignedInt(v, 0));
assertEquals(0x80000000L, getUnsignedInt(v));
v = new BitArray();
v.appendBits(0xff, 8);
assertEquals(8, v.getSize());
assertEquals(0xff000000L, getUnsignedInt(v, 0));
assertEquals(0xff000000L, getUnsignedInt(v));
v = new BitArray();
v.appendBits(0xff7, 12);
assertEquals(12, v.getSize());
assertEquals(0xff700000L, getUnsignedInt(v, 0));
assertEquals(0xff700000L, getUnsignedInt(v));
}
@Test
@ -132,7 +132,7 @@ public final class BitVectorTestCase extends Assert {
BitArray v2 = new BitArray();
v2.appendBits(0xaaaa5555, 32);
v1.xor(v2);
assertEquals(0xffffffffL, getUnsignedInt(v1, 0));
assertEquals(0xffffffffL, getUnsignedInt(v1));
}
@Test
@ -142,7 +142,7 @@ public final class BitVectorTestCase extends Assert {
BitArray v2 = new BitArray();
v2.appendBits(0x55, 7); // 101 0101
v1.xor(v2);
assertEquals(0xfe000000L, getUnsignedInt(v1, 0)); // 1111 1110
assertEquals(0xfe000000L, getUnsignedInt(v1)); // 1111 1110
}
@Test

View file

@ -23,8 +23,8 @@ import javax.servlet.annotation.WebInitParam;
* Protect the /chart endpoint from too many requests.
*/
@WebFilter(urlPatterns = {"/w/chart"}, initParams = {
@WebInitParam(name = "maxAccessPerTime", value = "250"),
@WebInitParam(name = "accessTimeSec", value = "500"),
@WebInitParam(name = "maxAccessPerTime", value = "120"),
@WebInitParam(name = "accessTimeSec", value = "60"),
@WebInitParam(name = "maxEntries", value = "10000"),
@WebInitParam(name = "maxLoad", value = "0.9")
})

View file

@ -24,7 +24,7 @@ import javax.servlet.annotation.WebInitParam;
*/
@WebFilter(urlPatterns = {"/w/decode"}, initParams = {
@WebInitParam(name = "maxAccessPerTime", value = "60"),
@WebInitParam(name = "accessTimeSec", value = "180"),
@WebInitParam(name = "accessTimeSec", value = "60"),
@WebInitParam(name = "maxEntries", value = "10000"),
@WebInitParam(name = "maxLoad", value = "0.9")
})

View file

@ -59,7 +59,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -144,14 +143,6 @@ public final class DecodeServlet extends HttpServlet {
String name = getClass().getSimpleName();
timer = new Timer(name);
destHostTracker = new DoSTracker(timer, name, maxAccessPerTime, accessTimeMS, maxEntries, null);
// Hack to try to avoid odd OOM due to memory leak in JAI?
timer.scheduleAtFixedRate(
new TimerTask() {
@Override
public void run() {
System.gc();
}
}, 0L, TimeUnit.MILLISECONDS.convert(10, TimeUnit.MINUTES));
}
@Override

View file

@ -76,7 +76,7 @@ public abstract class DoSFilter implements Filter {
if (isBanned((HttpServletRequest) request)) {
HttpServletResponse servletResponse = (HttpServletResponse) response;
// Send very short response as requests may be very frequent
servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
servletResponse.setStatus(429); // 429 = Too Many Requests from RFC 6585
servletResponse.getWriter().write("Forbidden");
} else {
chain.doFilter(request, response);

View file

@ -118,11 +118,9 @@ final class DoSTracker {
int cores = mxBean.getAvailableProcessors();
double loadRatio = loadAvg / cores;
log.info(name + ": Load ratio: " + loadRatio + " (" + loadAvg + '/' + cores + ") vs " + maxLoad);
if (loadRatio > maxLoad) {
maxAccessesPerTime = Math.min(maxAllowedCount, maxAccessesPerTime);
} else {
maxAccessesPerTime = Math.max(minDisallowedCount, maxAccessesPerTime);
}
maxAccessesPerTime = loadRatio > maxLoad ?
Math.min(maxAllowedCount, maxAccessesPerTime) :
Math.max(minDisallowedCount, maxAccessesPerTime);
log.info(name + ": New maxAccessesPerTime: " + maxAccessesPerTime);
}
}

View file

@ -1,4 +0,0 @@
commands:
yum_update:
command: yum -y update
ignoreErrors: true

View file

@ -1,3 +0,0 @@
container_commands:
replace-config:
command: cp .ebextensions/server.xml /etc/tomcat8/server.xml

View file

@ -17,7 +17,7 @@
-->
<!-- Additional config:
JAVA_OPTS="-Djava.security.egd=file:/dev/urandom -Djava.awt.headless=true -Xmx320m -Xmx320m -XX:+UseG1GC"
JAVA_OPTS="-Djava.security.egd=file:/dev/urandom -Djava.awt.headless=true -Xmx2560m"
-->
<Server port="-1" shutdown="">
@ -30,10 +30,10 @@ JAVA_OPTS="-Djava.security.egd=file:/dev/urandom -Djava.awt.headless=true -Xmx32
<Service name="Catalina">
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="4" minSpareThreads="1"/>
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="8" minSpareThreads="1"/>
<Connector port="80" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
redirectPort="443"
redirectPort="443"
server="Apache" compression="8192"
executor="tomcatThreadPool"
URIEncoding="UTF-8"/>

View file

@ -44,7 +44,7 @@ public final class DoSFilterTestCase extends Assert {
for (int i = 0; i < MAX_ACCESS_PER_TIME; i++) {
testRequest(filter, "1.2.3.4", null, HttpServletResponse.SC_OK);
}
testRequest(filter, "1.2.3.4", null, HttpServletResponse.SC_FORBIDDEN);
testRequest(filter, "1.2.3.4", null, 429);
} finally {
filter.destroy();
}
@ -56,8 +56,8 @@ public final class DoSFilterTestCase extends Assert {
Filter filter = new DecodeDoSFilter();
initFilter(filter);
try {
testRequest(filter, null, null, HttpServletResponse.SC_FORBIDDEN);
testRequest(filter, null, "1.1.1.1", HttpServletResponse.SC_FORBIDDEN);
testRequest(filter, null, null, 429);
testRequest(filter, null, "1.1.1.1", 429);
} finally {
filter.destroy();
}
@ -71,7 +71,7 @@ public final class DoSFilterTestCase extends Assert {
for (int i = 0; i < MAX_ACCESS_PER_TIME; i++) {
testRequest(filter, "1.2.3.4", "1.1.1." + i + ", proxy1", HttpServletResponse.SC_OK);
}
testRequest(filter, "1.2.3.4", "1.1.1.0", HttpServletResponse.SC_FORBIDDEN);
testRequest(filter, "1.2.3.4", "1.1.1.0", 429);
} finally {
filter.destroy();
}