Example usage for javax.servlet AsyncContext setTimeout

List of usage examples for javax.servlet AsyncContext setTimeout

Introduction

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

Prototype

public void setTimeout(long timeout);

Source Link

Document

Sets the timeout (in milliseconds) for this AsyncContext.

Usage

From source file:framework.GlobalHelpers.java

public static AsyncForward asyncForward() {
    AsyncContext ctx = req().startAsync();
    ctx.setTimeout(30000);
    AsyncForward forward = new AsyncForward(ctx);
    forward.action = "/" + req().getController() + "/" + req().getAction() + "Async.html";
    return forward;
}

From source file:com.googlesource.gerrit.plugins.lfs.fs.LfsFsContentServlet.java

private void getObject(HttpServletRequest req, HttpServletResponse rsp, Optional<AnyLongObjectId> obj)
        throws IOException {
    if (obj.isPresent()) {
        AsyncContext context = req.startAsync();
        context.setTimeout(timeout);
        rsp.getOutputStream().setWriteListener(new ObjectDownloadListener(repository, context, rsp, obj.get()));
    }/* ww w .jav a  2  s.  co  m*/
}

From source file:com.googlesource.gerrit.plugins.lfs.fs.LfsFsContentServlet.java

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    AnyLongObjectId id = getObjectToTransfer(req, rsp);
    if (id == null) {
        return;// www .  ja  v a2  s.c o  m
    }

    if (!authorizer.verifyAuthInfo(req.getHeader(HDR_AUTHORIZATION), UPLOAD, id)) {
        sendError(rsp, HttpStatus.SC_UNAUTHORIZED,
                MessageFormat.format(LfsServerText.get().failedToCalcSignature, "Invalid authorization token"));
        return;
    }

    AsyncContext context = req.startAsync();
    context.setTimeout(timeout);
    req.getInputStream().setReadListener(new ObjectUploadListener(repository, context, req, rsp, id));
}

From source file:org.eclipse.jgit.lfs.server.fs.FileLfsServlet.java

/**
 * Handle object uploads// ww w.  j  a  va  2  s . c  om
 *
 * @param req
 *            servlet request
 * @param rsp
 *            servlet response
 * @throws ServletException
 *             if a servlet-specific error occurs
 * @throws IOException
 *             if an I/O error occurs
 */
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    AnyLongObjectId id = getObjectToTransfer(req, rsp);
    if (id != null) {
        AsyncContext context = req.startAsync();
        context.setTimeout(timeout);
        req.getInputStream().setReadListener(new ObjectUploadListener(repository, context, req, rsp, id));
    }
}

From source file:org.eclipse.jgit.lfs.server.fs.FileLfsServlet.java

/**
 * Handles object downloads// w  w  w  . ja v a 2  s .  c  om
 *
 * @param req
 *            servlet request
 * @param rsp
 *            servlet response
 * @throws ServletException
 *             if a servlet-specific error occurs
 * @throws IOException
 *             if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    AnyLongObjectId obj = getObjectToTransfer(req, rsp);
    if (obj != null) {
        if (repository.getSize(obj) == -1) {
            sendError(rsp, HttpStatus.SC_NOT_FOUND,
                    MessageFormat.format(LfsServerText.get().objectNotFound, obj.getName()));
            return;
        }
        AsyncContext context = req.startAsync();
        context.setTimeout(timeout);
        rsp.getOutputStream().setWriteListener(new ObjectDownloadListener(repository, context, rsp, obj));
    }
}

From source file:com.ibm.amc.feedback.FeedbackHandler.java

public void handleRequest(final HttpServletRequest request, final HttpServletResponse response) {
    if (logger.isEntryEnabled())
        logger.entry("handleRequest", request, response);

    final String user = request.getRemoteUser();
    final Queue<ActionStatusResponse> statuses = getActionStatuses(user);
    if (statuses.isEmpty()) {
        // No updates pending - register listener
        final AsyncContext asyncContext = request.startAsync(request, response);
        asyncContext.setTimeout(900000000L);
        logger.debug("handleRequest", "Registering new listener for user " + user);
        synchronized (feedbackListeners) {
            Set<AsyncContext> contexts = feedbackListeners.get(user);
            if (contexts == null) {
                contexts = new HashSet<AsyncContext>();
                feedbackListeners.put(user, contexts);
            }/*from  w  w  w. j av  a2 s  .  com*/
            contexts.add(asyncContext);
        }

        // Timeout listener
        executor.schedule(new Runnable() {

            @Override
            public void run() {
                synchronized (feedbackListeners) {
                    final Set<AsyncContext> contexts = feedbackListeners.get(user);
                    if (contexts.remove(asyncContext)) {
                        if (logger.isDebugEnabled())
                            logger.debug("handleRequest$Runnable.run", "Timing out listener for user " + user);
                        writeResponse(asyncContext.getResponse(), new LinkedList<ActionStatusResponse>());
                        asyncContext.complete();
                        if (contexts.isEmpty())
                            feedbackListeners.remove(user);
                    }
                }
            }

        }, POLLING_TIMEOUT, TimeUnit.SECONDS);
    } else {
        // Update pending - send response immediately
        writeResponse(response, statuses);
    }

    if (logger.isEntryEnabled())
        logger.exit("handleRequest");
}

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);//from ww w  .  ja  v  a 2 s  . c o m
    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: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  a v a  2 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 = 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: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//from   ww  w.j  a  va2s .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);//w  w  w.j  av a  2s.  co  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);
        }
    }
}