Search in sources :

Example 21 with Reader

use of java.io.Reader in project hive by apache.

the class TestReusableStringReader method testSkip.

@Test
public void testSkip() throws IOException {
    Reader reader = new ReusableStringReader();
    ((ReusableStringReader) reader).set(fox);
    // skip entire the data:
    long skipped = reader.skip(fox.length() + 1);
    assertEquals(fox.length(), skipped);
    assertEquals(-1, reader.read());
    // reset the data
    ((ReusableStringReader) reader).set(fox);
    char[] cc = new char[6];
    int read;
    read = reader.read(cc);
    assertEquals(6, read);
    assertEquals("Quick ", new String(cc));
    // skip some piece of data:
    skipped = reader.skip(30);
    assertEquals(30, skipped);
    read = reader.read(cc);
    assertEquals(4, read);
    assertEquals("dog.", new String(cc, 0, read));
    // skip when already at EOF:
    skipped = reader.skip(300);
    assertEquals(0, skipped);
    assertEquals(-1, reader.read());
    reader.close();
}
Also used : ReusableStringReader(org.apache.hadoop.hive.ql.udf.xml.UDFXPathUtil.ReusableStringReader) ReusableStringReader(org.apache.hadoop.hive.ql.udf.xml.UDFXPathUtil.ReusableStringReader) Reader(java.io.Reader) Test(org.junit.Test)

Example 22 with Reader

use of java.io.Reader in project tomcat by apache.

the class TestUpgrade method doTestCheckClosed.

private void doTestCheckClosed(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) throws Exception {
    UpgradeConnection conn = doUpgrade(upgradeHandlerClass);
    Reader r = conn.getReader();
    int c = r.read();
    Assert.assertEquals(-1, c);
}
Also used : Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 23 with Reader

use of java.io.Reader in project tomcat by apache.

the class TestHttp11Processor method testRequestBodySwallowing.

/*
     * Tests what happens if a request is completed during a dispatch but the
     * request body has not been fully read.
     */
@Test
public void testRequestBodySwallowing() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet servlet = new DispatchingServlet();
    Wrapper w = Tomcat.addServlet(ctx, "Test", servlet);
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/test", "Test");
    tomcat.start();
    // Hand-craft the client so we have complete control over the timing
    SocketAddress addr = new InetSocketAddress("localhost", getPort());
    Socket socket = new Socket();
    socket.setSoTimeout(300000);
    socket.connect(addr, 300000);
    OutputStream os = socket.getOutputStream();
    Writer writer = new OutputStreamWriter(os, "ISO-8859-1");
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is, "ISO-8859-1");
    BufferedReader reader = new BufferedReader(r);
    // Write the headers
    writer.write("POST /test HTTP/1.1\r\n");
    writer.write("Host: localhost:8080\r\n");
    writer.write("Transfer-Encoding: chunked\r\n");
    writer.write("\r\n");
    writer.flush();
    validateResponse(reader);
    // Write the request body
    writer.write("2\r\n");
    writer.write("AB\r\n");
    writer.write("0\r\n");
    writer.write("\r\n");
    writer.flush();
    // Write the 2nd request
    writer.write("POST /test HTTP/1.1\r\n");
    writer.write("Host: localhost:8080\r\n");
    writer.write("Transfer-Encoding: chunked\r\n");
    writer.write("\r\n");
    writer.flush();
    // Read the 2nd response
    validateResponse(reader);
    // Write the 2nd request body
    writer.write("2\r\n");
    writer.write("AB\r\n");
    writer.write("0\r\n");
    writer.write("\r\n");
    writer.flush();
    // Done
    socket.close();
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 24 with Reader

use of java.io.Reader in project tomcat by apache.

the class TestSsl method testRenegotiateWorks.

@Test
public void testRenegotiateWorks() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Assume.assumeTrue("SSL renegotiation has to be supported for this test", TesterSupport.isClientRenegotiationSupported(getTomcatInstance()));
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");
    TesterSupport.initSsl(tomcat);
    tomcat.start();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(null, TesterSupport.getTrustManagers(), null);
    SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", getPort());
    OutputStream os = socket.getOutputStream();
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is);
    doRequest(os, r);
    TesterHandshakeListener listener = new TesterHandshakeListener();
    socket.addHandshakeCompletedListener(listener);
    socket.startHandshake();
    // One request should be sufficient
    int requestCount = 0;
    int listenerComplete = 0;
    try {
        while (requestCount < 10) {
            requestCount++;
            doRequest(os, r);
            if (listener.isComplete() && listenerComplete == 0) {
                listenerComplete = requestCount;
            }
        }
    } catch (AssertionError | IOException e) {
        String message = "Failed on request number " + requestCount + " after startHandshake(). " + e.getMessage();
        log.error(message, e);
        Assert.fail(message);
    }
    Assert.assertTrue(listener.isComplete());
    System.out.println("Renegotiation completed after " + listenerComplete + " requests");
}
Also used : SSLContext(javax.net.ssl.SSLContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) TesterServlet(org.apache.catalina.startup.TesterServlet) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 25 with Reader

use of java.io.Reader in project tomcat by apache.

the class SSIFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // cast once
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    // indicate that we're in SSI processing
    req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
    // setup to capture output
    ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
    ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(getServletContext(), req, res, basos);
    // process remainder of filter chain
    chain.doFilter(req, responseIncludeWrapper);
    // we can't assume the chain flushed its output
    responseIncludeWrapper.flushOutputStreamOrWriter();
    byte[] bytes = basos.toByteArray();
    // get content type
    String contentType = responseIncludeWrapper.getContentType();
    // is this an allowed type for SSI processing?
    if (contentTypeRegEx.matcher(contentType).matches()) {
        String encoding = res.getCharacterEncoding();
        // set up SSI processing
        SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(getServletContext(), req, res, isVirtualWebappRelative, debug, encoding);
        SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec);
        // prepare readers/writers
        Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding);
        ByteArrayOutputStream ssiout = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding));
        // do SSI processing
        long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer);
        // set output bytes
        writer.flush();
        bytes = ssiout.toByteArray();
        // override headers
        if (expires != null) {
            res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000);
        }
        if (lastModified > 0) {
            res.setDateHeader("last-modified", lastModified);
        }
        res.setContentLength(bytes.length);
        Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType());
        if (shtmlMatcher.matches()) {
            // Convert shtml mime type to ordinary html mime type but preserve
            // encoding, if any.
            String enc = shtmlMatcher.group(1);
            res.setContentType("text/html" + ((enc != null) ? enc : ""));
        }
    }
    // write output
    OutputStream out = null;
    try {
        out = res.getOutputStream();
    } catch (IllegalStateException e) {
    // Ignore, will try to use a writer
    }
    if (out == null) {
        res.getWriter().write(new String(bytes));
    } else {
        out.write(bytes);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

Reader (java.io.Reader)1498 InputStreamReader (java.io.InputStreamReader)526 StringReader (java.io.StringReader)498 IOException (java.io.IOException)348 BufferedReader (java.io.BufferedReader)242 InputStream (java.io.InputStream)219 TokenStream (org.apache.lucene.analysis.TokenStream)171 Test (org.junit.Test)170 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)159 Connection (java.sql.Connection)137 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)126 FileReader (java.io.FileReader)108 FileInputStream (java.io.FileInputStream)107 File (java.io.File)105 BeforeClass (org.junit.BeforeClass)99 Tokenizer (org.apache.lucene.analysis.Tokenizer)91 SqlSession (org.apache.ibatis.session.SqlSession)83 StringWriter (java.io.StringWriter)81 ArrayList (java.util.ArrayList)77 Writer (java.io.Writer)63