Example usage for javax.servlet ServletOutputStream ServletOutputStream

List of usage examples for javax.servlet ServletOutputStream ServletOutputStream

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream ServletOutputStream.

Prototype


protected ServletOutputStream() 

Source Link

Document

Does nothing, because this is an abstract class.

Usage

From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java

@Test
public void testDownloadMetadata() throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));

    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {
        @Override//  w w w.j  av  a  2s .  c  o  m
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String result = null;
            if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
                        + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n"
                        + "  <versioning>\n" + "    <latest>2.14.0</latest>\n"
                        + "    <release>2.14.0</release>\n" + "    <versions>\n"
                        + "      <version>1.6.1</version>\n" + "      <version>1.6.2</version>\n"
                        + "      <version>1.6.3</version>\n" + "      <version>1.6.4</version>\n"
                        + "      <version>2.0-M2</version>\n" + "      <version>2.0-M3</version>\n"
                        + "      <version>2.0.0</version>\n" + "      <version>2.1.0</version>\n"
                        + "      <version>2.2.0</version>\n" + "      <version>2.3.0</version>\n"
                        + "      <version>2.4.0</version>\n" + "      <version>2.5.0</version>\n"
                        + "      <version>2.6.0</version>\n" + "      <version>2.7.0</version>\n"
                        + "      <version>2.7.1</version>\n" + "      <version>2.7.2</version>\n"
                        + "      <version>2.7.3</version>\n" + "      <version>2.7.4</version>\n"
                        + "      <version>2.7.5</version>\n" + "      <version>2.8.0</version>\n"
                        + "      <version>2.8.1</version>\n" + "      <version>2.8.2</version>\n"
                        + "      <version>2.8.3</version>\n" + "      <version>2.8.4</version>\n"
                        + "      <version>2.8.5</version>\n" + "      <version>2.8.6</version>\n"
                        + "      <version>2.9.0-RC1</version>\n" + "      <version>2.9.0</version>\n"
                        + "      <version>2.9.1</version>\n" + "      <version>2.9.2</version>\n"
                        + "      <version>2.9.3</version>\n" + "      <version>2.9.4</version>\n"
                        + "      <version>2.9.5</version>\n" + "      <version>2.9.6</version>\n"
                        + "      <version>2.9.7</version>\n" + "      <version>2.9.8</version>\n"
                        + "      <version>2.10.0</version>\n" + "      <version>2.10.1</version>\n"
                        + "      <version>2.10.2</version>\n" + "      <version>2.10.3</version>\n"
                        + "      <version>2.10.4</version>\n" + "      <version>2.10.5</version>\n"
                        + "      <version>2.10.6</version>\n" + "      <version>2.10.7</version>\n"
                        + "      <version>2.11.0</version>\n" + "      <version>2.11.1</version>\n"
                        + "      <version>2.11.2</version>\n" + "      <version>2.11.3</version>\n"
                        + "      <version>2.11.4</version>\n" + "      <version>2.12.0</version>\n"
                        + "      <version>2.12.1</version>\n" + "      <version>2.12.2</version>\n"
                        + "      <version>2.12.3</version>\n" + "      <version>2.12.4</version>\n"
                        + "      <version>2.13.0</version>\n" + "      <version>2.13.1</version>\n"
                        + "      <version>2.13.2</version>\n" + "      <version>2.14.0</version>\n"
                        + "    </versions>\n" + "    <lastUpdated>20140918132816</lastUpdated>\n"
                        + "  </versioning>\n" + "</metadata>\n" + "\n";
            } else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n"
                        + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n"
                        + "  <versioning>\n" + "    <latest>2.14.0.redhat-620034</latest>\n"
                        + "    <release>2.14.0.redhat-620034</release>\n" + "    <versions>\n"
                        + "      <version>2.10.0.redhat-60074</version>\n"
                        + "      <version>2.12.0.redhat-610312</version>\n"
                        + "      <version>2.12.0.redhat-610328</version>\n"
                        + "      <version>2.12.0.redhat-610355</version>\n"
                        + "      <version>2.12.0.redhat-610378</version>\n"
                        + "      <version>2.12.0.redhat-610396</version>\n"
                        + "      <version>2.12.0.redhat-610399</version>\n"
                        + "      <version>2.12.0.redhat-610401</version>\n"
                        + "      <version>2.12.0.redhat-610402</version>\n"
                        + "      <version>2.12.0.redhat-611403</version>\n"
                        + "      <version>2.12.0.redhat-611405</version>\n"
                        + "      <version>2.12.0.redhat-611406</version>\n"
                        + "      <version>2.12.0.redhat-611408</version>\n"
                        + "      <version>2.12.0.redhat-611409</version>\n"
                        + "      <version>2.12.0.redhat-611410</version>\n"
                        + "      <version>2.12.0.redhat-611411</version>\n"
                        + "      <version>2.12.0.redhat-611412</version>\n"
                        + "      <version>2.14.0.redhat-620031</version>\n"
                        + "      <version>2.14.0.redhat-620033</version>\n"
                        + "      <version>2.14.0.redhat-620034</version>\n" + "    </versions>\n"
                        + "    <lastUpdated>20141019130841</lastUpdated>\n" + "  </versioning>\n"
                        + "</metadata>\n" + "\n";
            }
            if (result == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                baseRequest.setHandled(true);
                response.getOutputStream().close();
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(result.getBytes());
                response.getOutputStream().close();
            }
        }
    });
    server.start();

    try {
        int localPort = server.getConnectors()[0].getLocalPort();
        List<String> remoteRepos = Arrays
                .asList("http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2");
        RuntimeProperties props = new MockRuntimeProperties();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort,
                "fuse", "fuse", null);
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5);

        AsyncContext context = EasyMock.createMock(AsyncContext.class);

        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        //            EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/LATEST/camel-core-LATEST.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();

        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }
        }).anyTimes();
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();

        final CountDownLatch latch = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
            @Override
            public Object answer() throws Throwable {
                latch.countDown();
                return null;
            }
        });

        EasyMock.makeThreadSafe(context, true);
        EasyMock.replay(request, response, context);

        servlet.start();
        servlet.doGet(request, response);

        latch.await();

        org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader()
                .read(new ByteArrayInputStream(baos.toByteArray()), false);
        assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
        assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
        assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));

        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}

From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java

private void testDownload(Handler serverHandler) throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));

    Server server = new Server(0);
    server.setHandler(serverHandler);//ww  w .ja  va  2  s .  com
    server.start();

    try {
        int localPort = server.getConnectors()[0].getLocalPort();
        List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central");
        RuntimeProperties props = new MockRuntimeProperties();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort,
                "fuse", "fuse", null);
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5);

        AsyncContext context = EasyMock.createMock(AsyncContext.class);

        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getPathInfo())
                .andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();

        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }
        }).anyTimes();
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();

        final CountDownLatch latch = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
            @Override
            public Object answer() throws Throwable {
                latch.countDown();
                return null;
            }
        });

        EasyMock.makeThreadSafe(context, true);
        EasyMock.replay(request, response, context);

        servlet.start();
        servlet.doGet(request, response);

        latch.await();

        Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());

        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}

From source file:net.ymate.framework.core.util.WebUtils.java

/**
 * @param request         HttpServletRequest
 * @param response        HttpServletResponse
 * @param jspFile         JSP/*from   www. j  av  a 2 s.com*/
 * @param charsetEncoding ?
 * @return JSPHTML??
 * @throws ServletException ?
 * @throws IOException      ?
 */
public static String includeJSP(HttpServletRequest request, HttpServletResponse response, String jspFile,
        String charsetEncoding) throws ServletException, IOException {
    final OutputStream _output = new ByteArrayOutputStream();
    final PrintWriter _writer = new PrintWriter(new OutputStreamWriter(_output));
    final ServletOutputStream _servletOutput = new ServletOutputStream() {
        @Override
        public void write(int b) throws IOException {
            _output.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            _output.write(b, off, len);
        }
    };
    HttpServletResponse _response = new HttpServletResponseWrapper(response) {
        @Override
        public ServletOutputStream getOutputStream() {
            return _servletOutput;
        }

        @Override
        public PrintWriter getWriter() {
            return _writer;
        }
    };
    charsetEncoding = StringUtils.defaultIfEmpty(charsetEncoding, response.getCharacterEncoding());
    _response.setCharacterEncoding(StringUtils.defaultIfEmpty(charsetEncoding,
            WebMVC.get().getModuleCfg().getDefaultCharsetEncoding()));
    request.getRequestDispatcher(jspFile).include(request, _response);
    _writer.flush();
    return _output.toString();
}

From source file:net.ymate.platform.mvc.web.view.impl.JspView.java

public void render(final OutputStream output) throws Exception {
    // JSP?????UTF-8
    this.processPath();
    final ServletOutputStream _oStream = new ServletOutputStream() {
        @Override//  w  w w  .  j a v a2 s  .co m
        public void write(int b) throws IOException {
            output.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            output.write(b, off, len);
        }
    };
    final PrintWriter _printWriter = new PrintWriter(new OutputStreamWriter(output));
    HttpServletResponse _newResponse = new HttpServletResponseWrapper(WebContext.getResponse()) {
        @Override
        public ServletOutputStream getOutputStream() {
            return _oStream;
        }

        @Override
        public PrintWriter getWriter() {
            return _printWriter;
        }
    };
    _newResponse.setCharacterEncoding(WebMVC.getConfig().getCharsetEncoding());
    WebContext.getRequest().getRequestDispatcher(this.path).include(WebContext.getRequest(), _newResponse);
    _printWriter.flush();
}

From source file:net.ymate.platform.webmvc.view.impl.JspView.java

public void render(final OutputStream output) throws Exception {
    __doProcessPath();/*from   w  w w .  j av  a  2  s . c  o m*/
    // JSP?(???), ?UTF-8
    final ServletOutputStream _oStream = new ServletOutputStream() {
        public void write(int b) throws IOException {
            output.write(b);
        }

        public void write(byte[] b, int off, int len) throws IOException {
            output.write(b, off, len);
        }
    };
    final PrintWriter _printWriter = new PrintWriter(new OutputStreamWriter(output));
    //
    HttpServletRequest _request = WebContext.getRequest();
    HttpServletResponse _response = WebContext.getResponse();
    HttpServletResponse _newResponse = new HttpServletResponseWrapper(_response) {
        public ServletOutputStream getOutputStream() {
            return _oStream;
        }

        public PrintWriter getWriter() {
            return _printWriter;
        }
    };
    _request.getRequestDispatcher(__path).include(_request, _newResponse);
    _printWriter.flush();
}

From source file:nl.strohalm.cyclos.http.ResettableHttpServletResponse.java

@Override
public ServletOutputStream getOutputStream() throws IOException {
    if (writer != null) {
        throw new IllegalStateException("getWriter() was already invoked in this response");
    }/*  www.j a v  a  2s .  c om*/
    if (servletOutputStream == null) {
        contents = File.createTempFile("cyclos_", "_response");
        outputStream = new FileOutputStream(contents);
        servletOutputStream = new ServletOutputStream() {
            @Override
            public void close() throws IOException {
                outputStream.close();
            }

            @Override
            public void flush() throws IOException {
                outputStream.flush();
            }

            @Override
            public void write(final byte[] b) throws IOException {
                outputStream.write(b);
            }

            @Override
            public void write(final byte[] b, final int off, final int len) throws IOException {
                outputStream.write(b, off, len);
            }

            @Override
            public void write(final int b) throws IOException {
                outputStream.write(b);
            }
        };
    }
    return servletOutputStream;
}

From source file:org.ajax4jsf.webapp.CacheContent.java

/**
 * Create UNIX command 'tee' like stream - send all data to servlet
 * //ww w .  j  av a  2  s  .c  o  m
 * @param responseStream
 * @return
 */
public OutputStream getOutputStream() {
    if (null == servletStream) {
        outputStream = new FastBufferOutputStream(1024);
        servletStream = new ServletOutputStream() {

            /*
             * (non-Javadoc)
             * 
             * @see java.io.OutputStream#close()
             */
            public void close() throws IOException {
                filledOutputStream = true;
                // / content = outputStream.toByteArray();
                content = null;
            }

            /*
             * (non-Javadoc)
             * 
             * @see java.io.OutputStream#flush()
             */
            public void flush() throws IOException {
            }

            /*
             * (non-Javadoc)
             * 
             * @see java.io.OutputStream#write(byte[], int, int)
             */
            public void write(byte[] b, int off, int len) throws IOException {
                outputStream.write(b, off, len);
            }

            /*
             * (non-Javadoc)
             * 
             * @see java.io.OutputStream#write(byte[])
             */
            public void write(byte[] b) throws IOException {
                outputStream.write(b);
            }

            public void write(int b) throws IOException {
                outputStream.write(b);
            }

        };
    }
    return servletStream;
}

From source file:org.apache.karaf.cave.server.maven.MavenProxyServletTest.java

@Test
public void testDownloadMetadata() throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));

    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {
        @Override/* ww  w  . j a va2  s. c  o  m*/
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String result = null;
            if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
                        + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n"
                        + "  <versioning>\n" + "    <latest>2.14.0</latest>\n"
                        + "    <release>2.14.0</release>\n" + "    <versions>\n"
                        + "      <version>1.6.1</version>\n" + "      <version>1.6.2</version>\n"
                        + "      <version>1.6.3</version>\n" + "      <version>1.6.4</version>\n"
                        + "      <version>2.0-M2</version>\n" + "      <version>2.0-M3</version>\n"
                        + "      <version>2.0.0</version>\n" + "      <version>2.1.0</version>\n"
                        + "      <version>2.2.0</version>\n" + "      <version>2.3.0</version>\n"
                        + "      <version>2.4.0</version>\n" + "      <version>2.5.0</version>\n"
                        + "      <version>2.6.0</version>\n" + "      <version>2.7.0</version>\n"
                        + "      <version>2.7.1</version>\n" + "      <version>2.7.2</version>\n"
                        + "      <version>2.7.3</version>\n" + "      <version>2.7.4</version>\n"
                        + "      <version>2.7.5</version>\n" + "      <version>2.8.0</version>\n"
                        + "      <version>2.8.1</version>\n" + "      <version>2.8.2</version>\n"
                        + "      <version>2.8.3</version>\n" + "      <version>2.8.4</version>\n"
                        + "      <version>2.8.5</version>\n" + "      <version>2.8.6</version>\n"
                        + "      <version>2.9.0-RC1</version>\n" + "      <version>2.9.0</version>\n"
                        + "      <version>2.9.1</version>\n" + "      <version>2.9.2</version>\n"
                        + "      <version>2.9.3</version>\n" + "      <version>2.9.4</version>\n"
                        + "      <version>2.9.5</version>\n" + "      <version>2.9.6</version>\n"
                        + "      <version>2.9.7</version>\n" + "      <version>2.9.8</version>\n"
                        + "      <version>2.10.0</version>\n" + "      <version>2.10.1</version>\n"
                        + "      <version>2.10.2</version>\n" + "      <version>2.10.3</version>\n"
                        + "      <version>2.10.4</version>\n" + "      <version>2.10.5</version>\n"
                        + "      <version>2.10.6</version>\n" + "      <version>2.10.7</version>\n"
                        + "      <version>2.11.0</version>\n" + "      <version>2.11.1</version>\n"
                        + "      <version>2.11.2</version>\n" + "      <version>2.11.3</version>\n"
                        + "      <version>2.11.4</version>\n" + "      <version>2.12.0</version>\n"
                        + "      <version>2.12.1</version>\n" + "      <version>2.12.2</version>\n"
                        + "      <version>2.12.3</version>\n" + "      <version>2.12.4</version>\n"
                        + "      <version>2.13.0</version>\n" + "      <version>2.13.1</version>\n"
                        + "      <version>2.13.2</version>\n" + "      <version>2.14.0</version>\n"
                        + "    </versions>\n" + "    <lastUpdated>20140918132816</lastUpdated>\n"
                        + "  </versioning>\n" + "</metadata>\n" + "\n";
            } else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n"
                        + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n"
                        + "  <versioning>\n" + "    <latest>2.14.0.redhat-620034</latest>\n"
                        + "    <release>2.14.0.redhat-620034</release>\n" + "    <versions>\n"
                        + "      <version>2.10.0.redhat-60074</version>\n"
                        + "      <version>2.12.0.redhat-610312</version>\n"
                        + "      <version>2.12.0.redhat-610328</version>\n"
                        + "      <version>2.12.0.redhat-610355</version>\n"
                        + "      <version>2.12.0.redhat-610378</version>\n"
                        + "      <version>2.12.0.redhat-610396</version>\n"
                        + "      <version>2.12.0.redhat-610399</version>\n"
                        + "      <version>2.12.0.redhat-610401</version>\n"
                        + "      <version>2.12.0.redhat-610402</version>\n"
                        + "      <version>2.12.0.redhat-611403</version>\n"
                        + "      <version>2.12.0.redhat-611405</version>\n"
                        + "      <version>2.12.0.redhat-611406</version>\n"
                        + "      <version>2.12.0.redhat-611408</version>\n"
                        + "      <version>2.12.0.redhat-611409</version>\n"
                        + "      <version>2.12.0.redhat-611410</version>\n"
                        + "      <version>2.12.0.redhat-611411</version>\n"
                        + "      <version>2.12.0.redhat-611412</version>\n"
                        + "      <version>2.14.0.redhat-620031</version>\n"
                        + "      <version>2.14.0.redhat-620033</version>\n"
                        + "      <version>2.14.0.redhat-620034</version>\n" + "    </versions>\n"
                        + "    <lastUpdated>20141019130841</lastUpdated>\n" + "  </versioning>\n"
                        + "</metadata>\n" + "\n";
            }
            if (result == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                baseRequest.setHandled(true);
                response.getOutputStream().close();
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(result.getBytes());
                response.getOutputStream().close();
            }
        }
    });
    server.start();

    try {
        int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp",
                "http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2", "http", "localhost",
                localPort, "fuse", "fuse", null);
        CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null);

        AsyncContext context = EasyMock.createMock(AsyncContext.class);

        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        //            EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/LATEST/camel-core-LATEST.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();

        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }

            @Override
            public boolean isReady() {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
                // TODO Auto-generated method stub

            }
        }).anyTimes();
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();

        final CountDownLatch latch = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
            @Override
            public Object answer() throws Throwable {
                latch.countDown();
                return null;
            }
        });

        EasyMock.makeThreadSafe(context, true);
        EasyMock.replay(request, response, context);

        servlet.init();
        servlet.doGet(request, response);

        latch.await();

        shaded.org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader()
                .read(new ByteArrayInputStream(baos.toByteArray()), false);
        assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
        assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
        assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));

        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}

From source file:org.apache.karaf.cave.server.maven.MavenProxyServletTest.java

private void testDownload(Handler serverHandler) throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));

    Server server = new Server(0);
    server.setHandler(serverHandler);/*from   w  w w.j  av a  2s. c  o  m*/
    server.start();

    try {
        int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", "http://relevant.not/maven2@id=central", "http",
                "localhost", localPort, "fuse", "fuse", null);
        CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null);

        AsyncContext context = EasyMock.createMock(AsyncContext.class);

        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getPathInfo())
                .andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();

        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }

            @Override
            public boolean isReady() {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
                // TODO Auto-generated method stub

            }
        }).anyTimes();
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();

        final CountDownLatch latch = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
            @Override
            public Object answer() throws Throwable {
                latch.countDown();
                return null;
            }
        });

        EasyMock.makeThreadSafe(context, true);
        EasyMock.replay(request, response, context);

        servlet.init();
        servlet.doGet(request, response);

        latch.await();

        Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());

        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}

From source file:org.apache.karaf.services.mavenproxy.internal.MavenProxyServletTest.java

@Test
public void testDownloadMetadata() throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));

    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {
        @Override//w w w .j  av  a  2  s.  com
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String result = null;
            if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
                        + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n"
                        + "  <versioning>\n" + "    <latest>2.14.0</latest>\n"
                        + "    <release>2.14.0</release>\n" + "    <versions>\n"
                        + "      <version>1.6.1</version>\n" + "      <version>1.6.2</version>\n"
                        + "      <version>1.6.3</version>\n" + "      <version>1.6.4</version>\n"
                        + "      <version>2.0-M2</version>\n" + "      <version>2.0-M3</version>\n"
                        + "      <version>2.0.0</version>\n" + "      <version>2.1.0</version>\n"
                        + "      <version>2.2.0</version>\n" + "      <version>2.3.0</version>\n"
                        + "      <version>2.4.0</version>\n" + "      <version>2.5.0</version>\n"
                        + "      <version>2.6.0</version>\n" + "      <version>2.7.0</version>\n"
                        + "      <version>2.7.1</version>\n" + "      <version>2.7.2</version>\n"
                        + "      <version>2.7.3</version>\n" + "      <version>2.7.4</version>\n"
                        + "      <version>2.7.5</version>\n" + "      <version>2.8.0</version>\n"
                        + "      <version>2.8.1</version>\n" + "      <version>2.8.2</version>\n"
                        + "      <version>2.8.3</version>\n" + "      <version>2.8.4</version>\n"
                        + "      <version>2.8.5</version>\n" + "      <version>2.8.6</version>\n"
                        + "      <version>2.9.0-RC1</version>\n" + "      <version>2.9.0</version>\n"
                        + "      <version>2.9.1</version>\n" + "      <version>2.9.2</version>\n"
                        + "      <version>2.9.3</version>\n" + "      <version>2.9.4</version>\n"
                        + "      <version>2.9.5</version>\n" + "      <version>2.9.6</version>\n"
                        + "      <version>2.9.7</version>\n" + "      <version>2.9.8</version>\n"
                        + "      <version>2.10.0</version>\n" + "      <version>2.10.1</version>\n"
                        + "      <version>2.10.2</version>\n" + "      <version>2.10.3</version>\n"
                        + "      <version>2.10.4</version>\n" + "      <version>2.10.5</version>\n"
                        + "      <version>2.10.6</version>\n" + "      <version>2.10.7</version>\n"
                        + "      <version>2.11.0</version>\n" + "      <version>2.11.1</version>\n"
                        + "      <version>2.11.2</version>\n" + "      <version>2.11.3</version>\n"
                        + "      <version>2.11.4</version>\n" + "      <version>2.12.0</version>\n"
                        + "      <version>2.12.1</version>\n" + "      <version>2.12.2</version>\n"
                        + "      <version>2.12.3</version>\n" + "      <version>2.12.4</version>\n"
                        + "      <version>2.13.0</version>\n" + "      <version>2.13.1</version>\n"
                        + "      <version>2.13.2</version>\n" + "      <version>2.14.0</version>\n"
                        + "    </versions>\n" + "    <lastUpdated>20140918132816</lastUpdated>\n"
                        + "  </versioning>\n" + "</metadata>\n" + "\n";
            } else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n"
                        + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n"
                        + "  <versioning>\n" + "    <latest>2.14.0.redhat-620034</latest>\n"
                        + "    <release>2.14.0.redhat-620034</release>\n" + "    <versions>\n"
                        + "      <version>2.10.0.redhat-60074</version>\n"
                        + "      <version>2.12.0.redhat-610312</version>\n"
                        + "      <version>2.12.0.redhat-610328</version>\n"
                        + "      <version>2.12.0.redhat-610355</version>\n"
                        + "      <version>2.12.0.redhat-610378</version>\n"
                        + "      <version>2.12.0.redhat-610396</version>\n"
                        + "      <version>2.12.0.redhat-610399</version>\n"
                        + "      <version>2.12.0.redhat-610401</version>\n"
                        + "      <version>2.12.0.redhat-610402</version>\n"
                        + "      <version>2.12.0.redhat-611403</version>\n"
                        + "      <version>2.12.0.redhat-611405</version>\n"
                        + "      <version>2.12.0.redhat-611406</version>\n"
                        + "      <version>2.12.0.redhat-611408</version>\n"
                        + "      <version>2.12.0.redhat-611409</version>\n"
                        + "      <version>2.12.0.redhat-611410</version>\n"
                        + "      <version>2.12.0.redhat-611411</version>\n"
                        + "      <version>2.12.0.redhat-611412</version>\n"
                        + "      <version>2.14.0.redhat-620031</version>\n"
                        + "      <version>2.14.0.redhat-620033</version>\n"
                        + "      <version>2.14.0.redhat-620034</version>\n" + "    </versions>\n"
                        + "    <lastUpdated>20141019130841</lastUpdated>\n" + "  </versioning>\n"
                        + "</metadata>\n" + "\n";
            }
            if (result == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                baseRequest.setHandled(true);
                response.getOutputStream().close();
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(result.getBytes());
                response.getOutputStream().close();
            }
        }
    });
    server.start();

    try {
        int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp",
                "http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2", "http", "localhost",
                localPort, "fuse", "fuse", null);
        MavenProxyServlet servlet = new MavenProxyServlet(resolver, 5, null, null, null);

        AsyncContext context = EasyMock.createMock(AsyncContext.class);

        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        //            EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/LATEST/camel-core-LATEST.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();

        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }
        }).anyTimes();
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();

        final CountDownLatch latch = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
            @Override
            public Object answer() throws Throwable {
                latch.countDown();
                return null;
            }
        });

        EasyMock.makeThreadSafe(context, true);
        EasyMock.replay(request, response, context);

        servlet.init();
        servlet.doGet(request, response);

        latch.await();

        shaded.org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader()
                .read(new ByteArrayInputStream(baos.toByteArray()), false);
        assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
        assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
        assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));

        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}