Example usage for org.apache.commons.io IOUtils readLines

List of usage examples for org.apache.commons.io IOUtils readLines

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils readLines.

Prototype

public static List readLines(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a list of Strings, one entry per line.

Usage

From source file:org.apache.hadoop.security.TestKDiag.java

/**
 * Dump any file to standard out.//  ww w .  j a va 2  s . co  m
 * @param file file to dump
 * @throws IOException IO problems
 */
private void dump(File file) throws IOException {
    try (FileInputStream in = new FileInputStream(file)) {
        for (String line : IOUtils.readLines(in)) {
            LOG.info(line);
        }
    }
}

From source file:org.apache.hadoop.security.token.delegation.web.TestWebDelegationToken.java

private void testDelegationTokenAuthenticatedURLWithNoDT(Class<? extends Filter> filterClass) throws Exception {
    final Server jetty = createJettyServer();
    Context context = new Context();
    context.setContextPath("/foo");
    jetty.setHandler(context);// w w  w  . j  a  va 2s . c om
    context.addFilter(new FilterHolder(filterClass), "/*", 0);
    context.addServlet(new ServletHolder(UserServlet.class), "/bar");

    try {
        jetty.start();
        final URL url = new URL(getJettyURL() + "/foo/bar");

        UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
                DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();
                HttpURLConnection conn = aUrl.openConnection(url, token);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                List<String> ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals(FOO_USER, ret.get(0));

                try {
                    aUrl.getDelegationToken(url, token, FOO_USER);
                    Assert.fail();
                } catch (AuthenticationException ex) {
                    Assert.assertTrue(ex.getMessage().contains("delegation token operation"));
                }
                return null;
            }
        });
    } finally {
        jetty.stop();
    }
}

From source file:org.apache.hadoop.security.token.delegation.web.TestWebDelegationToken.java

@Test
public void testFallbackToPseudoDelegationTokenAuthenticator() throws Exception {
    final Server jetty = createJettyServer();
    Context context = new Context();
    context.setContextPath("/foo");
    jetty.setHandler(context);/* w  w w .j a  va  2  s  .  c  o m*/
    context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", 0);
    context.addServlet(new ServletHolder(UserServlet.class), "/bar");

    try {
        jetty.start();
        final URL url = new URL(getJettyURL() + "/foo/bar");

        UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
                DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();
                HttpURLConnection conn = aUrl.openConnection(url, token);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                List<String> ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals(FOO_USER, ret.get(0));

                aUrl.getDelegationToken(url, token, FOO_USER);
                Assert.assertNotNull(token.getDelegationToken());
                Assert.assertEquals(new Text("token-kind"), token.getDelegationToken().getKind());
                return null;
            }
        });
    } finally {
        jetty.stop();
    }
}

From source file:org.apache.hadoop.security.token.delegation.web.TestWebDelegationToken.java

@Test
public void testProxyUser() throws Exception {
    final Server jetty = createJettyServer();
    Context context = new Context();
    context.setContextPath("/foo");
    jetty.setHandler(context);//from   w  w w  .  java  2  s .  co  m
    context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", 0);
    context.addServlet(new ServletHolder(UserServlet.class), "/bar");

    try {
        jetty.start();
        final URL url = new URL(getJettyURL() + "/foo/bar");

        // proxyuser using raw HTTP, verifying doAs is case insensitive
        String strUrl = String.format("%s?user.name=%s&doas=%s", url.toExternalForm(), FOO_USER, OK_USER);
        HttpURLConnection conn = (HttpURLConnection) new URL(strUrl).openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        List<String> ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals(OK_USER, ret.get(0));
        strUrl = String.format("%s?user.name=%s&DOAS=%s", url.toExternalForm(), FOO_USER, OK_USER);
        conn = (HttpURLConnection) new URL(strUrl).openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals(OK_USER, ret.get(0));

        UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
                DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();

                // proxyuser using authentication handler authentication
                HttpURLConnection conn = aUrl.openConnection(url, token, OK_USER);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                List<String> ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals(OK_USER, ret.get(0));

                // unauthorized proxy user using authentication handler authentication
                conn = aUrl.openConnection(url, token, FAIL_USER);
                Assert.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode());

                // proxy using delegation token authentication
                aUrl.getDelegationToken(url, token, FOO_USER);

                UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
                ugi.addToken(token.getDelegationToken());
                token = new DelegationTokenAuthenticatedURL.Token();

                // requests using delegation token as auth do not honor doAs
                conn = aUrl.openConnection(url, token, OK_USER);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals(FOO_USER, ret.get(0));

                return null;
            }
        });
    } finally {
        jetty.stop();
    }
}

From source file:org.apache.hadoop.security.token.delegation.web.TestWebDelegationToken.java

@Test
public void testHttpUGI() throws Exception {
    final Server jetty = createJettyServer();
    Context context = new Context();
    context.setContextPath("/foo");
    jetty.setHandler(context);/*from  ww w.  j  a v  a 2s .co m*/
    context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", 0);
    context.addServlet(new ServletHolder(UGIServlet.class), "/bar");

    try {
        jetty.start();
        final URL url = new URL(getJettyURL() + "/foo/bar");

        UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
                DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();

                // user foo
                HttpURLConnection conn = aUrl.openConnection(url, token);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                List<String> ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals("remoteuser=" + FOO_USER + ":ugi=" + FOO_USER, ret.get(0));

                // user ok-user via proxyuser foo
                conn = aUrl.openConnection(url, token, OK_USER);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals("realugi=" + FOO_USER + ":remoteuser=" + OK_USER + ":ugi=" + OK_USER,
                        ret.get(0));

                return null;
            }
        });
    } finally {
        jetty.stop();
    }
}

From source file:org.apache.hadoop.security.token.delegation.web.TestWebDelegationToken.java

@Test
public void testIpaddressCheck() throws Exception {
    final Server jetty = createJettyServer();
    ((AbstractConnector) jetty.getConnectors()[0]).setResolveNames(true);
    Context context = new Context();
    context.setContextPath("/foo");
    jetty.setHandler(context);/*from   ww  w  .j a va  2 s  .c  o  m*/

    context.addFilter(new FilterHolder(IpAddressBasedPseudoDTAFilter.class), "/*", 0);
    context.addServlet(new ServletHolder(UGIServlet.class), "/bar");

    try {
        jetty.start();
        final URL url = new URL(getJettyURL() + "/foo/bar");

        UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
                DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();

                // user ok-user via proxyuser foo
                HttpURLConnection conn = aUrl.openConnection(url, token, OK_USER);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                List<String> ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals("realugi=" + FOO_USER + ":remoteuser=" + OK_USER + ":ugi=" + OK_USER,
                        ret.get(0));

                return null;
            }
        });
    } finally {
        jetty.stop();
    }
}

From source file:org.apache.hadoop.yarn.util.SmapsBasedProcessTree.java

/**
 * Update memory related information/*from  w w w .jav  a 2s.  co  m*/
 * 
 * @param pinfo
 * @param procfsDir
 */
private static void constructProcessSMAPInfo(ProcessMemInfo pInfo, String procfsDir) {
    BufferedReader in = null;
    FileReader fReader = null;
    try {
        File pidDir = new File(procfsDir, pInfo.getPid());
        File file = new File(pidDir, SMAPS);
        fReader = new FileReader(file);
        in = new BufferedReader(fReader);
        MemoryMappingInfo memoryMappingInfo = null;
        List<String> lines = IOUtils.readLines(new FileInputStream(file));
        for (String line : lines) {
            line = line.trim();
            try {
                Matcher address = ADDRESS_PATTERN.matcher(line);
                if (address.find()) {
                    memoryMappingInfo = new MemoryMappingInfo(line);
                    memoryMappingInfo.setPermission(address.group(4));
                    pInfo.getModuleMemList().add(memoryMappingInfo);
                    continue;
                }
                Matcher memInfo = MEM_INFO_PATTERN.matcher(line);
                if (memInfo.find()) {
                    String key = memInfo.group(1).trim();
                    String value = memInfo.group(2).replace(KB, "").trim();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("MemInfo : " + key + " : Value  : " + value);
                    }
                    memoryMappingInfo.updateModuleMemInfo(key, value);
                }
            } catch (Throwable t) {
                LOG.warn("Error parsing smaps line : " + line + "; " + t.getMessage());
            }
        }
    } catch (FileNotFoundException f) {
        LOG.error(f.getMessage());
    } catch (IOException e) {
        LOG.error(e.getMessage());
    } catch (Throwable t) {
        LOG.error(t.getMessage());
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.apache.jackrabbit.demo.mu.servlets.SourceFileServlet.java

protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {
    String fileName = httpServletRequest.getParameter("file");
    InputStream is = getServletContext().getResourceAsStream("/src" + fileName);
    List linesOfCode = IOUtils.readLines(is);
    httpServletRequest.setAttribute("lines", linesOfCode);
    httpServletRequest.getRequestDispatcher("/pages/SourceFile.jsp").forward(httpServletRequest,
            httpServletResponse);// w w  w.  ja va2s. co  m
}

From source file:org.apache.james.mime4j.dom.MessageTest.java

public void testAddHeaderWriteTo() throws Exception {
    String headerName = "testheader";
    String headerValue = "testvalue";
    String testheader = headerName + ": " + headerValue;

    byte[] inputByte = getRawMessageAsByteArray();

    DefaultMessageBuilder builder = new DefaultMessageBuilder();
    DefaultMessageWriter writer = new DefaultMessageWriter();

    Message m = builder.parseMessage(new ByteArrayInputStream(inputByte));
    m.getHeader().addField(DefaultFieldParser.parse(testheader));

    assertEquals("header added", m.getHeader().getField(headerName).getBody(), headerValue);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.writeMessage(m, out);/*  w w  w. j  a  va 2s  .  com*/
    List<?> lines = IOUtils.readLines(
            (new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray())))));

    assertTrue("header added", lines.contains(testheader));
}

From source file:org.apache.james.mime4j.message.MessageImplTest.java

@Test
public void testAddHeaderWriteTo() throws Exception {
    String headerName = "testheader";
    String headerValue = "testvalue";
    String testheader = headerName + ": " + headerValue;

    byte[] inputByte = getRawMessageAsByteArray();

    DefaultMessageBuilder builder = new DefaultMessageBuilder();
    DefaultMessageWriter writer = new DefaultMessageWriter();

    Message m = builder.parseMessage(new ByteArrayInputStream(inputByte));
    m.getHeader().addField(DefaultFieldParser.parse(testheader));

    Assert.assertEquals("header added", m.getHeader().getField(headerName).getBody(), headerValue);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.writeMessage(m, out);/*from w w w .  j  a va2 s .  co m*/
    List<?> lines = IOUtils.readLines(
            (new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray())))));

    Assert.assertTrue("header added", lines.contains(testheader));
}