diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java index c3b50fdf7e1..fe54f85f077 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.client; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.List; @@ -92,13 +93,20 @@ public class HttpContentResponse implements ContentResponse public String getContentAsString() { String encoding = this.encoding; - try + if (encoding == null) { - return new String(getContent(), encoding == null ? "UTF-8" : encoding); + return new String(getContent(), StandardCharsets.UTF_8); } - catch (UnsupportedEncodingException e) + else { - throw new UnsupportedCharsetException(encoding); + try + { + return new String(getContent(), encoding); + } + catch (UnsupportedEncodingException e) + { + throw new UnsupportedCharsetException(encoding); + } } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java index 7bf9fc29abd..d50d6d9c4e6 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.client.util; import java.net.URI; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.Authentication; @@ -73,8 +74,7 @@ public class BasicAuthentication implements Authentication @Override public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context) { - String encoding = StringUtil.__ISO_8859_1; - String value = "Basic " + B64Code.encode(user + ":" + password, encoding); + String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1); return new BasicResult(headerInfo.getHeader(), uri, value); } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java index 6a7969df0a9..50ddbd1ae51 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java @@ -20,6 +20,8 @@ package org.eclipse.jetty.client.util; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.Locale; @@ -130,7 +132,7 @@ public abstract class BufferingResponseListener extends Response.Listener.Empty { String encoding = this.encoding; if (encoding == null) - encoding = "UTF-8"; + return getContentAsString(StandardCharsets.UTF_8); return getContentAsString(encoding); } @@ -150,4 +152,14 @@ public abstract class BufferingResponseListener extends Response.Listener.Empty throw new UnsupportedCharsetException(encoding); } } + + /** + * @param encoding the encoding of the content bytes + * @return the content as a string, with the specified encoding + * @see #getContentAsString() + */ + public String getContentAsString(Charset encoding) + { + return new String(getContent(), encoding); + } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java index 55ac0ec5f51..d80db07f880 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.client.util; import java.net.URI; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -217,14 +218,13 @@ public class DigestAuthentication implements Authentication if (digester == null) return; - Charset charset = Charset.forName("ISO-8859-1"); String A1 = user + ":" + realm + ":" + password; - String hashA1 = toHexString(digester.digest(A1.getBytes(charset))); + String hashA1 = toHexString(digester.digest(A1.getBytes(StandardCharsets.ISO_8859_1))); String A2 = request.method() + ":" + request.getURI(); if ("auth-int".equals(qop)) A2 += ":" + toHexString(digester.digest(content)); - String hashA2 = toHexString(digester.digest(A2.getBytes(charset))); + String hashA2 = toHexString(digester.digest(A2.getBytes(StandardCharsets.ISO_8859_1))); String nonceCount; String clientNonce; @@ -241,7 +241,7 @@ public class DigestAuthentication implements Authentication clientNonce = null; A3 = hashA1 + ":" + nonce + ":" + hashA2; } - String hashA3 = toHexString(digester.digest(A3.getBytes(charset))); + String hashA3 = toHexString(digester.digest(A3.getBytes(StandardCharsets.ISO_8859_1))); StringBuilder value = new StringBuilder("Digest"); value.append(" username=\"").append(user).append("\""); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java index 95036b77c24..49fa4421726 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.client.util; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.client.api.ContentProvider; @@ -32,7 +33,7 @@ public class StringContentProvider extends BytesContentProvider { public StringContentProvider(String content) { - this(content, "UTF-8"); + this(content, StandardCharsets.UTF_8); } public StringContentProvider(String content, String encoding) diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java index 436d68455cb..8c0a44ddbf9 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java @@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -59,7 +60,7 @@ public class GZIPContentDecoderTest data += data; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -68,7 +69,7 @@ public class GZIPContentDecoderTest int read; while ((read = input.read()) >= 0) baos.write(read); - assertEquals(data, new String(baos.toByteArray(), "UTF-8")); + assertEquals(data, new String(baos.toByteArray(), StandardCharsets.UTF_8)); } @Test @@ -91,13 +92,13 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); GZIPContentDecoder decoder = new GZIPContentDecoder(); ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); } @Test @@ -107,7 +108,7 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -121,7 +122,7 @@ public class GZIPContentDecoderTest ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); assertEquals(0, decoded.capacity()); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); } @Test @@ -131,7 +132,7 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -143,7 +144,7 @@ public class GZIPContentDecoderTest GZIPContentDecoder decoder = new GZIPContentDecoder(); ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); assertFalse(decoder.isFinished()); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); assertEquals(0, decoded.remaining()); @@ -157,7 +158,7 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -171,7 +172,7 @@ public class GZIPContentDecoderTest ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); assertEquals(0, decoded.capacity()); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); } @Test @@ -180,14 +181,14 @@ public class GZIPContentDecoderTest String data1 = "0"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data1.getBytes("UTF-8")); + output.write(data1.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes1 = baos.toByteArray(); String data2 = "1"; baos = new ByteArrayOutputStream(); output = new GZIPOutputStream(baos); - output.write(data2.getBytes("UTF-8")); + output.write(data2.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes2 = baos.toByteArray(); @@ -198,11 +199,11 @@ public class GZIPContentDecoderTest GZIPContentDecoder decoder = new GZIPContentDecoder(); ByteBuffer buffer = ByteBuffer.wrap(bytes); ByteBuffer decoded = decoder.decode(buffer); - assertEquals(data1, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data1, StandardCharsets.UTF_8.decode(decoded).toString()); assertTrue(decoder.isFinished()); assertTrue(buffer.hasRemaining()); decoded = decoder.decode(buffer); - assertEquals(data2, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data2, StandardCharsets.UTF_8.decode(decoded).toString()); assertTrue(decoder.isFinished()); assertFalse(buffer.hasRemaining()); } @@ -215,7 +216,7 @@ public class GZIPContentDecoderTest data += data; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -225,7 +226,7 @@ public class GZIPContentDecoderTest while (buffer.hasRemaining()) { ByteBuffer decoded = decoder.decode(buffer); - result += Charset.forName("UTF-8").decode(decoded).toString(); + result += StandardCharsets.UTF_8.decode(decoded).toString(); } assertEquals(data, result); } @@ -238,7 +239,7 @@ public class GZIPContentDecoderTest data += data; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -249,7 +250,7 @@ public class GZIPContentDecoderTest { ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[]{buffer.get()})); if (decoded.hasRemaining()) - result += Charset.forName("UTF-8").decode(decoded).toString(); + result += StandardCharsets.UTF_8.decode(decoded).toString(); } assertEquals(data, result); assertTrue(decoder.isFinished()); @@ -263,12 +264,12 @@ public class GZIPContentDecoderTest data1 += data1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data1.getBytes("UTF-8")); + output.write(data1.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes1 = baos.toByteArray(); String data2 = "HELLO"; - byte[] bytes2 = data2.getBytes("UTF-8"); + byte[] bytes2 = data2.getBytes(StandardCharsets.UTF_8); byte[] bytes = new byte[bytes1.length + bytes2.length]; System.arraycopy(bytes1, 0, bytes, 0, bytes1.length); @@ -281,12 +282,12 @@ public class GZIPContentDecoderTest { ByteBuffer decoded = decoder.decode(buffer); if (decoded.hasRemaining()) - result += Charset.forName("UTF-8").decode(decoded).toString(); + result += StandardCharsets.UTF_8.decode(decoded).toString(); if (decoder.isFinished()) break; } assertEquals(data1, result); assertTrue(buffer.hasRemaining()); - assertEquals(data2, Charset.forName("UTF-8").decode(buffer).toString()); + assertEquals(data2, StandardCharsets.UTF_8.decode(buffer).toString()); } } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java index e281ac43ff8..50b7a9e87a4 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.client; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -60,13 +61,13 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest @Test public void test_Expect100Continue_WithOneContent_Respond100Continue() throws Exception { - test_Expect100Continue_Respond100Continue("data1".getBytes("UTF-8")); + test_Expect100Continue_Respond100Continue("data1".getBytes(StandardCharsets.UTF_8)); } @Test public void test_Expect100Continue_WithMultipleContents_Respond100Continue() throws Exception { - test_Expect100Continue_Respond100Continue("data1".getBytes("UTF-8"), "data2".getBytes("UTF-8"), "data3".getBytes("UTF-8")); + test_Expect100Continue_Respond100Continue("data1".getBytes(StandardCharsets.UTF_8), "data2".getBytes(StandardCharsets.UTF_8), "data3".getBytes(StandardCharsets.UTF_8)); } private void test_Expect100Continue_Respond100Continue(byte[]... contents) throws Exception diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java index cb5f7d9b86a..c44acc573c4 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.client; import java.io.IOException; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import javax.servlet.ServletException; @@ -83,7 +84,7 @@ public class HttpClientProxyTest extends AbstractHttpClientServerTest { final String user = "foo"; final String password = "bar"; - final String credentials = B64Code.encode(user + ":" + password, "ISO-8859-1"); + final String credentials = B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1); final String serverHost = "server"; final String realm = "test_realm"; final int status = HttpStatus.NO_CONTENT_204; diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java index 9fc6a81630f..ce3a19fec4d 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java @@ -25,6 +25,7 @@ import java.net.URI; import java.net.URLEncoder; import java.nio.ByteBuffer; import java.nio.channels.UnresolvedAddressException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -171,10 +172,10 @@ public class HttpClientTest extends AbstractHttpClientServerTest response.setCharacterEncoding("UTF-8"); ServletOutputStream output = response.getOutputStream(); String paramValue1 = request.getParameter(paramName1); - output.write(paramValue1.getBytes("UTF-8")); + output.write(paramValue1.getBytes(StandardCharsets.UTF_8)); String paramValue2 = request.getParameter(paramName2); Assert.assertEquals("", paramValue2); - output.write("empty".getBytes("UTF-8")); + output.write("empty".getBytes(StandardCharsets.UTF_8)); baseRequest.setHandled(true); } }); @@ -186,7 +187,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - String content = new String(response.getContent(), "UTF-8"); + String content = new String(response.getContent(), StandardCharsets.UTF_8); Assert.assertEquals(value1 + "empty", content); } @@ -204,9 +205,9 @@ public class HttpClientTest extends AbstractHttpClientServerTest ServletOutputStream output = response.getOutputStream(); String[] paramValues1 = request.getParameterValues(paramName1); for (String paramValue : paramValues1) - output.write(paramValue.getBytes("UTF-8")); + output.write(paramValue.getBytes(StandardCharsets.UTF_8)); String paramValue2 = request.getParameter(paramName2); - output.write(paramValue2.getBytes("UTF-8")); + output.write(paramValue2.getBytes(StandardCharsets.UTF_8)); baseRequest.setHandled(true); } }); @@ -222,7 +223,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - String content = new String(response.getContent(), "UTF-8"); + String content = new String(response.getContent(), StandardCharsets.UTF_8); Assert.assertEquals(value11 + value12 + value2, content); } @@ -254,7 +255,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8")); + Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8)); } @Test @@ -286,7 +287,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8")); + Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8)); } @Test diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java index ad78b1ed968..e7a55a5ada3 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java @@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -126,7 +127,7 @@ public class HttpReceiverTest Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH)); - String received = listener.getContentAsString("UTF-8"); + String received = listener.getContentAsString(StandardCharsets.UTF_8); Assert.assertEquals(content, received); } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java index e4cb6a003b4..f63b5324527 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.client; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Locale; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -191,7 +192,7 @@ public class HttpSenderTest HttpConnection connection = new HttpConnection(client, endPoint, destination); Request request = client.newRequest(URI.create("http://localhost/")); String content = "abcdef"; - request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes("UTF-8")))); + request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)))); final CountDownLatch headersLatch = new CountDownLatch(1); final CountDownLatch successLatch = new CountDownLatch(1); request.listener(new Request.Listener.Empty() @@ -226,7 +227,7 @@ public class HttpSenderTest Request request = client.newRequest(URI.create("http://localhost/")); String content1 = "0123456789"; String content2 = "abcdef"; - request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes("UTF-8")), ByteBuffer.wrap(content2.getBytes("UTF-8")))); + request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8)))); final CountDownLatch headersLatch = new CountDownLatch(1); final CountDownLatch successLatch = new CountDownLatch(1); request.listener(new Request.Listener.Empty() @@ -261,7 +262,7 @@ public class HttpSenderTest Request request = client.newRequest(URI.create("http://localhost/")); String content1 = "0123456789"; String content2 = "ABCDEF"; - request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes("UTF-8")), ByteBuffer.wrap(content2.getBytes("UTF-8"))) + request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))) { @Override public long getLength() diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java b/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java index cd7d09e0989..15d15e212c5 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java @@ -24,6 +24,7 @@ import java.io.OutputStream; import java.net.HttpCookie; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -269,7 +270,7 @@ public class Usage HttpClient client = new HttpClient(); client.start(); - InputStream input = new ByteArrayInputStream("content".getBytes("UTF-8")); + InputStream input = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8)); ContentResponse response = client.newRequest("localhost", 8080) // Provide the content as InputStream diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java index ad75b485338..b2b860dafbe 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -151,7 +152,7 @@ public class SslBytesClientTest extends SslBytesTest SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow(); // Read request - BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.startsWith("GET")); while (line.length() > 0) @@ -161,7 +162,7 @@ public class SslBytesClientTest extends SslBytesTest OutputStream output = server.getOutputStream(); output.write(("HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); output.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -197,7 +198,7 @@ public class SslBytesClientTest extends SslBytesTest // Read request InputStream serverInput = server.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.startsWith("GET")); while (line.length() > 0) @@ -206,16 +207,16 @@ public class SslBytesClientTest extends SslBytesTest OutputStream serverOutput = server.getOutputStream(); byte[] data1 = new byte[1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write first part of the response serverOutput.write(("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); serverOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -319,7 +320,7 @@ public class SslBytesClientTest extends SslBytesTest // Read request InputStream serverInput = server.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.startsWith("GET")); while (line.length() > 0) @@ -328,16 +329,16 @@ public class SslBytesClientTest extends SslBytesTest OutputStream serverOutput = server.getOutputStream(); byte[] data1 = new byte[1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write first part of the response serverOutput.write(("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); serverOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java index 4802db7338a..9f585ac8802 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java @@ -28,6 +28,7 @@ import java.net.SocketTimeoutException; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; @@ -575,7 +576,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -617,7 +618,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -633,7 +634,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -710,7 +711,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -727,7 +728,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -782,7 +783,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -850,7 +851,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -913,7 +914,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -964,7 +965,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1007,7 +1008,7 @@ public class SslBytesServerTest extends SslBytesTest byte[] data = new byte[128 * 1024]; Arrays.fill(data, (byte)'X'); - final String content = new String(data, "UTF-8"); + final String content = new String(data, StandardCharsets.UTF_8); Future
*/ -public interface Connection extends AutoCloseable +public interface Connection extends Closeable { public void addListener(Listener listener); diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java b/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java index 96a4f17519d..4e0a93264ee 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java @@ -25,8 +25,6 @@ import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.nio.channels.CancelledKeyException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.ClosedSelectorException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; @@ -229,7 +227,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa { connection.onOpen(); } - catch (Exception x) + catch (Throwable x) { if (isRunning()) LOG.warn("Exception while notifying connection " + connection, x); @@ -249,9 +247,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa { connection.onClose(); } - catch (Exception x) + catch (Throwable x) { - LOG.info("Exception while notifying connection " + connection, x); + LOG.debug("Exception while notifying connection " + connection, x); } } @@ -406,8 +404,15 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa protected void runChange(Runnable change) { - LOG.debug("Running change {}", change); - change.run(); + try + { + LOG.debug("Running change {}", change); + change.run(); + } + catch (Throwable x) + { + LOG.debug("Could not run change " + change, x); + } } @Override @@ -468,7 +473,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa } selectedKeys.clear(); } - catch (Exception x) + catch (Throwable x) { if (isRunning()) LOG.warn(x); @@ -515,7 +520,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa if (attachment instanceof EndPoint) ((EndPoint)attachment).close(); } - catch (Exception x) + catch (Throwable x) { LOG.warn("Could not process key for channel " + key.channel(), x); if (attachment instanceof EndPoint) @@ -525,10 +530,10 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa private void processConnect(SelectionKey key, Connect connect) { - key.attach(connect.attachment); SocketChannel channel = (SocketChannel)key.channel(); try { + key.attach(connect.attachment); boolean connected = finishConnect(channel); if (connected) { @@ -542,10 +547,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa throw new ConnectException(); } } - catch (Exception x) + catch (Throwable x) { connect.failed(x); - closeNoExceptions(channel); } } @@ -555,7 +559,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa { closeable.close(); } - catch (IOException x) + catch (Throwable x) { LOG.ignore(x); } @@ -702,8 +706,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa EndPoint endpoint = createEndPoint(_channel, key); key.attach(endpoint); } - catch (IOException x) + catch (Throwable x) { + closeNoExceptions(_channel); LOG.debug(x); } } @@ -730,16 +735,20 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa { channel.register(_selector, SelectionKey.OP_CONNECT, this); } - catch (ClosedSelectorException | ClosedChannelException x) + catch (Throwable x) { - LOG.debug(x); + failed(x); } } protected void failed(Throwable failure) { if (failed.compareAndSet(false, true)) + { + timeout.cancel(); + closeNoExceptions(channel); connectionFailed(channel, failure, attachment); + } } } @@ -759,19 +768,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa if (channel.isConnectionPending()) { LOG.debug("Channel {} timed out while connecting, closing it", channel); - try - { - // This will unregister the channel from the selector - channel.close(); - } - catch (IOException x) - { - LOG.ignore(x); - } - finally - { - connect.failed(new SocketTimeoutException()); - } + connect.failed(new SocketTimeoutException()); } } } @@ -836,7 +833,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa { try { - endPoint.getConnection().close(); + closeNoExceptions(endPoint.getConnection()); } finally { diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java b/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java index d94c270013d..6ea17ffbd5f 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.io; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; +import java.nio.charset.Charset; /* ------------------------------------------------------------ */ @@ -33,14 +34,14 @@ import java.io.Writer; public class WriterOutputStream extends OutputStream { protected final Writer _writer; - protected final String _encoding; + protected final Charset _encoding; private final byte[] _buf=new byte[1]; /* ------------------------------------------------------------ */ public WriterOutputStream(Writer writer, String encoding) { _writer=writer; - _encoding=encoding; + _encoding=encoding==null?null:Charset.forName(encoding); } /* ------------------------------------------------------------ */ diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java index cfd518664d9..2d77b9321cf 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java @@ -26,14 +26,17 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.SocketAddress; import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -389,42 +392,40 @@ public class IOTest @Test public void testReset() throws Exception { - ServerSocket connector; - Socket client; - Socket server; + try (ServerSocket connector = new ServerSocket(0); + Socket client = new Socket("127.0.0.1", connector.getLocalPort()); + Socket server = connector.accept();) + { + client.setTcpNoDelay(true); + client.setSoLinger(true, 0); + server.setTcpNoDelay(true); + server.setSoLinger(true, 0); - connector = new ServerSocket(0); - client = new Socket("127.0.0.1", connector.getLocalPort()); - server = connector.accept(); - client.setTcpNoDelay(true); - client.setSoLinger(true, 0); - server.setTcpNoDelay(true); - server.setSoLinger(true, 0); + client.getOutputStream().write(1); + assertEquals(1, server.getInputStream().read()); + server.getOutputStream().write(1); + assertEquals(1, client.getInputStream().read()); - client.getOutputStream().write(1); - assertEquals(1, server.getInputStream().read()); - server.getOutputStream().write(1); - assertEquals(1, client.getInputStream().read()); + // Server generator shutdowns output after non persistent sending response. + server.shutdownOutput(); - // Server generator shutdowns output after non persistent sending response. - server.shutdownOutput(); + // client endpoint reads EOF and shutdown input as result + assertEquals(-1, client.getInputStream().read()); + client.shutdownInput(); - // client endpoint reads EOF and shutdown input as result - assertEquals(-1, client.getInputStream().read()); - client.shutdownInput(); + // client connection see's EOF and shutsdown output as no more requests to be sent. + client.shutdownOutput(); - // client connection see's EOF and shutsdown output as no more requests to be sent. - client.shutdownOutput(); + // Since input already shutdown, client also closes socket. + client.close(); - // Since input already shutdown, client also closes socket. - client.close(); + // Server reads the EOF from client oshut and shut's down it's input + assertEquals(-1, server.getInputStream().read()); + server.shutdownInput(); - // Server reads the EOF from client oshut and shut's down it's input - assertEquals(-1, server.getInputStream().read()); - server.shutdownInput(); - - // Since output was already shutdown, server closes - server.close(); + // Since output was already shutdown, server closes + server.close(); + } } @Test @@ -432,17 +433,19 @@ public class IOTest { AsynchronousServerSocketChannel connector = AsynchronousServerSocketChannel.open(); connector.bind(null); + InetSocketAddress addr=(InetSocketAddress)connector.getLocalAddress(); FutureA {@link Connector} for TCP/IP network connectors
*/ -public interface NetworkConnector extends Connector, AutoCloseable +public interface NetworkConnector extends Connector, Closeable { /** *Performs the activities needed to open the network communication @@ -39,7 +40,6 @@ public interface NetworkConnector extends Connector, AutoCloseable * (for example, to stop accepting network connections).
* Once a connector has been closed, it cannot be opened again without first * calling {@link #stop()} and it will not be active again until a subsequent call to {@link #start()} - * @throws IOException if this connector cannot be closed */ @Override void close(); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java index f2cfe17d9cb..5283fbf80c1 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java @@ -28,6 +28,7 @@ import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.security.Principal; import java.util.ArrayList; @@ -1853,7 +1854,7 @@ public class Request implements HttpServletRequest /* ------------------------------------------------------------ */ /** * Set the character encoding used for the query string. This call will effect the return of getQueryString and getParamaters. It must be called before any - * geParameter methods. + * getParameter methods. * * The request attribute "org.eclipse.jetty.server.server.Request.queryEncoding" may be set as an alternate method of calling setQueryEncoding. * @@ -2080,6 +2081,7 @@ public class Request implements HttpServletRequest setAttribute(__MULTIPART_INPUT_STREAM, _multiPartInputStream); setAttribute(__MULTIPART_CONTEXT, _context); Collection\npathInfo="+request.getPathInfo()+"\n\n"); writer.write("
\ncontentType="+request.getContentType()+"\n\n"); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ExtendedServerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ExtendedServerTest.java new file mode 100644 index 00000000000..fd0c1cd2c60 --- /dev/null +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ExtendedServerTest.java @@ -0,0 +1,160 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.server; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpVersion; +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.SelectChannelEndPoint; +import org.eclipse.jetty.io.SelectorManager.ManagedSelector; +import org.eclipse.jetty.server.HttpServerTestFixture.HelloWorldHandler; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.util.thread.Scheduler; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Extended Server Tester. + */ +public class ExtendedServerTest extends HttpServerTestBase +{ + @Before + public void init() throws Exception + { + startServer(new ServerConnector(_server,new HttpConnectionFactory() + { + @Override + public Connection newConnection(Connector connector, EndPoint endPoint) + { + return configure(new ExtendedHttpConnection(getHttpConfiguration(), connector, endPoint), connector, endPoint); + } + }) + { + + @Override + protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException + { + return new ExtendedEndPoint(channel,selectSet,key, getScheduler(), getIdleTimeout()); + } + + }); + } + + private static class ExtendedEndPoint extends SelectChannelEndPoint + { + private volatile long _lastSelected; + + public ExtendedEndPoint(SocketChannel channel, ManagedSelector selector, SelectionKey key, Scheduler scheduler, long idleTimeout) + { + super(channel,selector,key,scheduler,idleTimeout); + } + + @Override + public void onSelected() + { + _lastSelected=System.currentTimeMillis(); + super.onSelected(); + } + + long getLastSelected() + { + return _lastSelected; + } + } + + private static class ExtendedHttpConnection extends HttpConnection + { + public ExtendedHttpConnection(HttpConfiguration config, Connector connector, EndPoint endPoint) + { + super(config,connector,endPoint); + } + + @Override + protected HttpChannelOverHttp newHttpChannel(HttpInput
Does not insert whitespace as described in RFC 1521. + * @param s String to encode. + * @param charEncoding The character encoding of the provided input String. + * @return String containing the encoded form of the input. + */ + public static String encode(String s, Charset charEncoding) + { + byte[] bytes=s.getBytes(charEncoding==null ? StandardCharsets.ISO_8859_1 : charEncoding); + return new String(encode(bytes)); + } + /** * Fast Base 64 encode as described in RFC 1421. *
Does not insert whitespace as described in RFC 1521. @@ -236,6 +250,24 @@ public class B64Code return new String(decoded,Charset.forName(charEncoding)); } + /** + * Base 64 decode as described in RFC 2045. + *
Unlike {@link #decode(char[])}, extra whitespace is ignored.
+ * @param encoded String to decode.
+ * @param charEncoding Character encoding
+ * used to map the decoded bytes into a String.
+ * @return String decoded byte array.
+ * @throws IllegalArgumentException if the input is not a valid
+ * B64 encoding.
+ */
+ public static String decode(String encoded, Charset charEncoding)
+ {
+ byte[] decoded=decode(encoded);
+ if (charEncoding==null)
+ return new String(decoded);
+ return new String(decoded, charEncoding);
+ }
+
/**
* Fast Base 64 decode as described in RFC 1421.
*
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java
index 198e9e747a0..95e4f508fdf 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java
@@ -29,6 +29,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
/* ------------------------------------------------------------------------------- */
@@ -454,7 +455,7 @@ public class BufferUtil
*/
public static String toString(ByteBuffer buffer)
{
- return toString(buffer, StringUtil.__ISO_8859_1_CHARSET);
+ return toString(buffer, StandardCharsets.ISO_8859_1);
}
/* ------------------------------------------------------------ */
@@ -464,7 +465,7 @@ public class BufferUtil
*/
public static String toUTF8String(ByteBuffer buffer)
{
- return toString(buffer, StringUtil.__UTF8_CHARSET);
+ return toString(buffer, StandardCharsets.UTF_8);
}
/* ------------------------------------------------------------ */
@@ -733,12 +734,12 @@ public class BufferUtil
public static ByteBuffer toBuffer(String s)
{
- return ByteBuffer.wrap(s.getBytes(StringUtil.__ISO_8859_1_CHARSET));
+ return ByteBuffer.wrap(s.getBytes(StandardCharsets.ISO_8859_1));
}
public static ByteBuffer toDirectBuffer(String s)
{
- byte[] bytes = s.getBytes(StringUtil.__ISO_8859_1_CHARSET);
+ byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length);
buf.put(bytes);
buf.flip();
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java
index 3787a801c79..c4ed7f4725e 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.nio.charset.StandardCharsets;
/* ------------------------------------------------------------ */
@@ -211,7 +212,7 @@ public class ByteArrayISO8859Writer extends Writer
if (_bout==null)
{
_bout = new ByteArrayOutputStream2(2*length);
- _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1);
+ _writer = new OutputStreamWriter(_bout,StandardCharsets.ISO_8859_1);
}
else
_bout.reset();
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java b/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
index 0194edd8499..c1e501be330 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
@@ -29,6 +29,7 @@ import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
+import java.nio.charset.Charset;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@@ -311,7 +312,7 @@ public class IO
public static String toString(InputStream in)
throws IOException
{
- return toString(in,null);
+ return toString(in,(Charset)null);
}
/* ------------------------------------------------------------ */
@@ -319,14 +320,22 @@ public class IO
*/
public static String toString(InputStream in,String encoding)
throws IOException
+ {
+ return toString(in, encoding==null?null:Charset.forName(encoding));
+ }
+
+ /** Read input stream to string.
+ */
+ public static String toString(InputStream in, Charset encoding)
+ throws IOException
{
StringWriter writer=new StringWriter();
InputStreamReader reader = encoding==null?new InputStreamReader(in):new InputStreamReader(in,encoding);
-
+
copy(reader,writer);
return writer.toString();
}
-
+
/* ------------------------------------------------------------ */
/** Read input stream to string.
*/
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java
index f47a19726ff..b09edfb8e04 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java
@@ -29,6 +29,7 @@ import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -475,7 +476,7 @@ public class MultiPartInputStreamParser
}
String boundary="--"+contentTypeBoundary;
- byte[] byteBoundary=(boundary+"--").getBytes(StringUtil.__ISO_8859_1);
+ byte[] byteBoundary=(boundary+"--").getBytes(StandardCharsets.ISO_8859_1);
// Get first boundary
String line = null;
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java
index 85cc700254b..047a1c012ec 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.util;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
/* ================================================================ */
@@ -53,7 +54,7 @@ public class MultiPartOutputStream extends FilterOutputStream
boundary = "jetty"+System.identityHashCode(this)+
Long.toString(System.currentTimeMillis(),36);
- boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1);
+ boundaryBytes=boundary.getBytes(StandardCharsets.ISO_8859_1);
}
public MultiPartOutputStream(OutputStream out, String boundary)
@@ -62,7 +63,7 @@ public class MultiPartOutputStream extends FilterOutputStream
super(out);
this.boundary = boundary;
- boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1);
+ boundaryBytes=boundary.getBytes(StandardCharsets.ISO_8859_1);
}
/* ------------------------------------------------------------ */
@@ -110,7 +111,7 @@ public class MultiPartOutputStream extends FilterOutputStream
out.write(boundaryBytes);
out.write(__CRLF);
if (contentType != null)
- out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
+ out.write(("Content-Type: "+contentType).getBytes(StandardCharsets.ISO_8859_1));
out.write(__CRLF);
out.write(__CRLF);
}
@@ -128,11 +129,11 @@ public class MultiPartOutputStream extends FilterOutputStream
out.write(boundaryBytes);
out.write(__CRLF);
if (contentType != null)
- out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
+ out.write(("Content-Type: "+contentType).getBytes(StandardCharsets.ISO_8859_1));
out.write(__CRLF);
for (int i=0;headers!=null && i
* See Opening Handshake (Section 1.3)
*/
- private final static byte[] MAGIC;
-
- static
- {
- try
- {
- MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(StringUtil.__ISO_8859_1);
- }
- catch (UnsupportedEncodingException e)
- {
- throw new RuntimeException(e);
- }
- }
+ private final static byte[] MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(StandardCharsets.ISO_8859_1);
/**
* Concatenate the provided key with the Magic GUID and return the Base64 encoded form.
@@ -62,7 +51,7 @@ public class AcceptHash
try
{
MessageDigest md = MessageDigest.getInstance("SHA1");
- md.update(key.getBytes("UTF-8"));
+ md.update(key.getBytes(StandardCharsets.UTF_8));
md.update(MAGIC);
return new String(B64Code.encode(md.digest()));
}
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java
index 750e07e7b20..869a122cef6 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java
@@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.common;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.eclipse.jetty.util.BufferUtil;
@@ -661,7 +662,7 @@ public class WebSocketFrame implements Frame
public WebSocketFrame setPayload(String str)
{
- setPayload(BufferUtil.toBuffer(str,StringUtil.__UTF8_CHARSET));
+ setPayload(BufferUtil.toBuffer(str, StandardCharsets.UTF_8));
return this;
}
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java
index fefff4e4488..1b79f3ba27f 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -89,7 +90,7 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Inc
String query = requestURI.getQuery();
if (StringUtil.isNotBlank(query))
{
- UrlEncoded.decodeTo(query,params,StringUtil.__UTF8_CHARSET,-1);
+ UrlEncoded.decodeTo(query,params, StandardCharsets.UTF_8,-1);
}
for (String name : params.keySet())
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelRequest.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelRequest.java
index 64fb9282802..62e1fc04739 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelRequest.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelRequest.java
@@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.common.extensions.mux.op;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
@@ -104,7 +105,7 @@ public class MuxAddChannelRequest implements MuxControlBlock
public void setHandshake(String rawstring)
{
- setHandshake(BufferUtil.toBuffer(rawstring,StringUtil.__UTF8_CHARSET));
+ setHandshake(BufferUtil.toBuffer(rawstring, StandardCharsets.UTF_8));
}
public void setRsv(byte rsv)
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelResponse.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelResponse.java
index a30b6ecf7ba..6273a7c4ace 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelResponse.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/op/MuxAddChannelResponse.java
@@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.common.extensions.mux.op;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
@@ -115,7 +116,7 @@ public class MuxAddChannelResponse implements MuxControlBlock
public void setHandshake(String responseHandshake)
{
- setHandshake(BufferUtil.toBuffer(responseHandshake,StringUtil.__UTF8_CHARSET));
+ setHandshake(BufferUtil.toBuffer(responseHandshake, StandardCharsets.UTF_8));
}
public void setRsv(byte rsv)
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java
index 4fe7cf28fe1..defae5b633b 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.StatusCode;
@@ -36,7 +37,7 @@ public class ClosePayloadParserTest
{
String expectedReason = "Game Over";
- byte utf[] = expectedReason.getBytes(StringUtil.__UTF8_CHARSET);
+ byte utf[] = expectedReason.getBytes(StandardCharsets.UTF_8);
ByteBuffer payload = ByteBuffer.allocate(utf.length + 2);
payload.putChar((char)StatusCode.NORMAL);
payload.put(utf,0,utf.length);
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java
index ccfb53f0682..9dae24d0cc6 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.eclipse.jetty.util.StringUtil;
@@ -75,7 +76,7 @@ public class TextPayloadParserTest
sb.append(". The end.");
String expectedText = sb.toString();
- byte utf[] = expectedText.getBytes(StringUtil.__UTF8);
+ byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a long length payload",utf.length,greaterThan(0xFFFF));
@@ -111,7 +112,7 @@ public class TextPayloadParserTest
sb.append(". The end.");
String expectedText = sb.toString();
- byte utf[] = expectedText.getBytes(StringUtil.__UTF8);
+ byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a medium length payload",utf.length,allOf(greaterThan(0x7E),lessThan(0xFFFF)));
@@ -141,8 +142,8 @@ public class TextPayloadParserTest
String part1 = "Hello ";
String part2 = "World";
- byte b1[] = part1.getBytes(StringUtil.__UTF8_CHARSET);
- byte b2[] = part2.getBytes(StringUtil.__UTF8_CHARSET);
+ byte b1[] = part1.getBytes(StandardCharsets.UTF_8);
+ byte b2[] = part2.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(32);
@@ -178,7 +179,7 @@ public class TextPayloadParserTest
public void testShortMaskedText() throws Exception
{
String expectedText = "Hello World";
- byte utf[] = expectedText.getBytes(StringUtil.__UTF8_CHARSET);
+ byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(24);
buf.put((byte)0x81);
@@ -204,7 +205,7 @@ public class TextPayloadParserTest
{
String expectedText = "Hell\uFF4f W\uFF4Frld";
- byte utf[] = expectedText.getBytes(StringUtil.__UTF8);
+ byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(24);
buf.put((byte)0x81);
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java
index 4a3bd826ed1..117e8f891fb 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.ab;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.eclipse.jetty.util.BufferUtil;
@@ -234,7 +235,7 @@ public class TestABCase7_3
ByteBuffer actual = generator.generate(close.asFrame());
ByteBuffer expected = ByteBuffer.allocate(132);
- byte messageBytes[] = message.toString().getBytes(StringUtil.__UTF8_CHARSET);
+ byte messageBytes[] = message.toString().getBytes(StandardCharsets.UTF_8);
expected.put(new byte[]
{ (byte)0x88 });
@@ -260,7 +261,7 @@ public class TestABCase7_3
message.append("*");
}
- byte[] messageBytes = message.toString().getBytes(StringUtil.__UTF8_CHARSET);
+ byte[] messageBytes = message.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer expected = ByteBuffer.allocate(132);
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java
index c7b67cd57c0..ce740e0144c 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@@ -89,7 +90,7 @@ public class FragmentExtensionTest
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StandardCharsets.UTF_8);
Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
}
@@ -125,7 +126,7 @@ public class FragmentExtensionTest
Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false));
Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8);
Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice());
}
@@ -295,7 +296,7 @@ public class FragmentExtensionTest
Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false));
Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8);
Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice());
}
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java
index b1a4528f723..217c2eb6eca 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
@@ -62,7 +63,7 @@ public class IdentityExtensionTest
Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false));
Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer("hello",StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer("hello",StandardCharsets.UTF_8);
Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice());
}
@@ -92,7 +93,7 @@ public class IdentityExtensionTest
Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false));
Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer("hello",StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer("hello",StandardCharsets.UTF_8);
Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice());
}
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateCompressionMethodTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateCompressionMethodTest.java
index 32f741ee2c4..92d3f554f6a 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateCompressionMethodTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateCompressionMethodTest.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.extensions.compress;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -43,7 +44,7 @@ public class DeflateCompressionMethodTest
{
String expected = msg.toString();
- ByteBuffer orig = BufferUtil.toBuffer(expected,StringUtil.__UTF8_CHARSET);
+ ByteBuffer orig = BufferUtil.toBuffer(expected,StandardCharsets.UTF_8);
LOG.debug("orig: {}",BufferUtil.toDetailString(orig));
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/FrameCompressionExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/FrameCompressionExtensionTest.java
index 68f47308ebc..84f87b9cdf8 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/FrameCompressionExtensionTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/FrameCompressionExtensionTest.java
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
@@ -84,7 +85,7 @@ public class FrameCompressionExtensionTest
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i],StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i],StandardCharsets.UTF_8);
Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
}
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/MessageCompressionExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/MessageCompressionExtensionTest.java
index 3f8c96d3a6f..546261d5679 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/MessageCompressionExtensionTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/MessageCompressionExtensionTest.java
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@@ -84,7 +85,7 @@ public class MessageCompressionExtensionTest
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(expectedStr,StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(expectedStr, StandardCharsets.UTF_8);
Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
}
@@ -188,7 +189,7 @@ public class MessageCompressionExtensionTest
Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false));
Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8);
Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice());
}
@@ -243,7 +244,7 @@ public class MessageCompressionExtensionTest
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StandardCharsets.UTF_8);
Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
}
@@ -279,7 +280,7 @@ public class MessageCompressionExtensionTest
for(String part: quote) {
Process process = method.compress();
process.begin();
- process.input(BufferUtil.toBuffer(part,StringUtil.__UTF8_CHARSET));
+ process.input(BufferUtil.toBuffer(part,StandardCharsets.UTF_8));
expectedBuffers.add(process.process());
process.end();
}
@@ -353,7 +354,7 @@ public class MessageCompressionExtensionTest
Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false));
Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false));
- ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET);
+ ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8);
Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining()));
ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice());
}
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxParserRFCTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxParserRFCTest.java
index 332fa4eb864..5eceb200180 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxParserRFCTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxParserRFCTest.java
@@ -23,6 +23,7 @@ import static org.hamcrest.Matchers.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -236,7 +237,7 @@ public class MuxParserRFCTest
}
else
{
- out.write(part.getBytes(StringUtil.__UTF8_CHARSET));
+ out.write(part.getBytes(StandardCharsets.UTF_8));
}
}
return out.toByteArray();
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java
index 837cf10bcbe..7532b0da410 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -39,7 +40,7 @@ public class HttpResponseHeaderParserTest
private void appendUtf8(ByteBuffer buf, String line)
{
- buf.put(ByteBuffer.wrap(StringUtil.getBytes(line,StringUtil.__UTF8)));
+ buf.put(ByteBuffer.wrap(StringUtil.getUtf8Bytes(line)));
}
@Test
@@ -56,7 +57,7 @@ public class HttpResponseHeaderParserTest
// and some body content
resp.append("What you are looking for is not here\r\n");
- ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StringUtil.__UTF8_CHARSET);
+ ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StandardCharsets.UTF_8);
HttpResponseParseCapture capture = new HttpResponseParseCapture();
HttpResponseHeaderParser parser = new HttpResponseHeaderParser(capture);
@@ -178,7 +179,7 @@ public class HttpResponseHeaderParserTest
resp.append("Sec-WebSocket-Protocol: chat\r\n");
resp.append("\r\n");
- ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StringUtil.__UTF8_CHARSET);
+ ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StandardCharsets.UTF_8);
HttpResponseParseCapture capture = new HttpResponseParseCapture();
HttpResponseHeaderParser parser = new HttpResponseHeaderParser(capture);
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java
index 8573c931334..601919abb1e 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java
@@ -33,6 +33,7 @@ import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@@ -718,7 +719,7 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames, Connecti
public void writeRaw(String str) throws IOException
{
LOG.debug("write((String)[{}]){}{})",str.length(),'\n',str);
- out.write(StringUtil.getBytes(str,StringUtil.__ISO_8859_1));
+ out.write(str.getBytes(StandardCharsets.ISO_8859_1));
}
public void writeRawSlowly(ByteBuffer buf, int segmentSize) throws IOException
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java
index d7206349196..faced9edcc6 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java
@@ -29,6 +29,7 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.TypeUtil;
@@ -91,7 +92,7 @@ public class SafariD00
// System.out.printf("--- Request ---%n%s",req);
- byte reqBytes[] = req.toString().getBytes("UTF-8");
+ byte reqBytes[] = req.toString().getBytes(StandardCharsets.UTF_8);
byte hixieBytes[] = TypeUtil.fromHexString("e739617916c9daf3");
byte buf[] = new byte[reqBytes.length + hixieBytes.length];
System.arraycopy(reqBytes,0,buf,0,reqBytes.length);
@@ -140,7 +141,7 @@ public class SafariD00
for (String msg : msgs)
{
buf.put((byte)0x00);
- buf.put(msg.getBytes("UTF-8"));
+ buf.put(msg.getBytes(StandardCharsets.UTF_8));
buf.put((byte)0xFF);
}
diff --git a/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java b/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java
index e93f0baf29d..f40991de2d6 100644
--- a/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java
+++ b/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
+import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;
@@ -239,11 +240,10 @@ public abstract class ContinuationBase
int port=_port;
String response=null;
- try
+ try (Socket socket = new Socket("localhost",port);)
{
- Socket socket = new Socket("localhost",port);
socket.setSoTimeout(10000);
- socket.getOutputStream().write(request.getBytes("UTF-8"));
+ socket.getOutputStream().write(request.getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().flush();
response = toString(socket.getInputStream());
@@ -269,6 +269,7 @@ public abstract class ContinuationBase
{}
/* ------------------------------------------------------------ */
+ @Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
{
final Continuation continuation = ContinuationSupport.getContinuation(request);
@@ -306,7 +307,7 @@ public abstract class ContinuationBase
if (continuation.isInitial())
{
- ((HttpServletResponse)response).addHeader("history","initial");
+ response.addHeader("history","initial");
if (read_before>0)
{
byte[] buf=new byte[read_before];
@@ -325,7 +326,7 @@ public abstract class ContinuationBase
if (suspend_for>0)
continuation.setTimeout(suspend_for);
continuation.addContinuationListener(__listener);
- ((HttpServletResponse)response).addHeader("history","suspend");
+ response.addHeader("history","suspend");
continuation.suspend(response);
if (complete_after>0)
@@ -404,7 +405,7 @@ public abstract class ContinuationBase
}
else
{
- ((HttpServletResponse)response).addHeader("history","!initial");
+ response.addHeader("history","!initial");
if (suspend2_for>=0 && request.getAttribute("2nd")==null)
{
request.setAttribute("2nd","cycle");
@@ -412,7 +413,7 @@ public abstract class ContinuationBase
if (suspend2_for>0)
continuation.setTimeout(suspend2_for);
// continuation.addContinuationListener(__listener);
- ((HttpServletResponse)response).addHeader("history","suspend");
+ response.addHeader("history","suspend");
continuation.suspend(response);
if (complete2_after>0)
@@ -452,7 +453,7 @@ public abstract class ContinuationBase
@Override
public void run()
{
- ((HttpServletResponse)response).addHeader("history","resume");
+ response.addHeader("history","resume");
continuation.resume();
}
};
@@ -463,7 +464,7 @@ public abstract class ContinuationBase
}
else if (resume2_after==0)
{
- ((HttpServletResponse)response).addHeader("history","resume");
+ response.addHeader("history","resume");
continuation.resume();
}
if (undispatch)
diff --git a/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationTest.java b/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationTest.java
index 2ca10a0a944..8b1b3f57f01 100644
--- a/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationTest.java
+++ b/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationTest.java
@@ -23,7 +23,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
-import junit.framework.Assert;
+import org.junit.Assert;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
@@ -194,6 +194,7 @@ public class ContinuationTest extends ContinuationBase
class Log extends AbstractLifeCycle implements RequestLog
{
+ @Override
public void log(Request request, Response response)
{
_log.add(response.getStatus()+" "+response.getContentCount()+" "+request.getRequestURI());
diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java
index 4911cd246c9..d9928b50836 100644
--- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java
+++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URI;
+import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
@@ -129,14 +130,14 @@ public class DigestPostTest
public void testServerDirectlyHTTP10() throws Exception
{
Socket socket = new Socket("127.0.0.1",((NetworkConnector)_server.getConnectors()[0]).getLocalPort());
- byte[] bytes = __message.getBytes("UTF-8");
+ byte[] bytes = __message.getBytes(StandardCharsets.UTF_8);
_received=null;
socket.getOutputStream().write(
("POST /test/ HTTP/1.0\r\n"+
"Host: 127.0.0.1:"+((NetworkConnector)_server.getConnectors()[0]).getLocalPort()+"\r\n"+
"Content-Length: "+bytes.length+"\r\n"+
- "\r\n").getBytes("UTF-8"));
+ "\r\n").getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().write(bytes);
socket.getOutputStream().flush();
@@ -163,7 +164,7 @@ public class DigestPostTest
"Host: 127.0.0.1:"+((NetworkConnector)_server.getConnectors()[0]).getLocalPort()+"\r\n"+
"Content-Length: "+bytes.length+"\r\n"+
"Authorization: "+digest+"\r\n"+
- "\r\n").getBytes("UTF-8"));
+ "\r\n").getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().write(bytes);
socket.getOutputStream().flush();
@@ -177,7 +178,7 @@ public class DigestPostTest
public void testServerDirectlyHTTP11() throws Exception
{
Socket socket = new Socket("127.0.0.1",((NetworkConnector)_server.getConnectors()[0]).getLocalPort());
- byte[] bytes = __message.getBytes("UTF-8");
+ byte[] bytes = __message.getBytes(StandardCharsets.UTF_8);
_received=null;
socket.getOutputStream().write(
@@ -192,7 +193,7 @@ public class DigestPostTest
byte[] buf=new byte[4096];
int len=socket.getInputStream().read(buf);
- String result=new String(buf,0,len,"UTF-8");
+ String result=new String(buf,0,len,StandardCharsets.UTF_8);
Assert.assertTrue(result.startsWith("HTTP/1.1 401 Unauthorized"));
Assert.assertEquals(null,_received);