protected void doGet()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/maven/MavenServlet.java [271:374]


    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        if (!authorize(req, resp, downloadRole)) {
            return;
        }
        String tpath = req.getPathInfo();
        if (tpath == null) {
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        if (tpath.startsWith("/")) {
            tpath = tpath.substring(1);
        }
        final String path = tpath;

        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(TimeUnit.MINUTES.toMillis(5));
        final ArtifactDownloadFuture future = new ArtifactDownloadFuture(path);
        ArtifactDownloadFuture masterFuture = requestMap.putIfAbsent(path, future);
        if (masterFuture == null) {
            masterFuture = future;
            masterFuture.lock();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        File file = download(path);
                        future.setValue(file);
                    } catch (Throwable t) {
                        future.setValue(t);
                    }
                }
            });
        } else {
            masterFuture.lock();
        }
        masterFuture.addListener(new FutureListener<ArtifactDownloadFuture>() {
            @Override
            public void operationComplete(ArtifactDownloadFuture future) {
                Object value = future.getValue();
                if (value instanceof Throwable) {
                    LOGGER.warn("Error while downloading artifact: {}", ((Throwable) value).getMessage(), value);
                    resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                } else if (value instanceof File) {
                    File artifactFile = (File) value;
                    try (InputStream is = new FileInputStream(artifactFile)) {
                        LOGGER.info("Writing response for file : {}", path);
                        resp.setStatus(HttpServletResponse.SC_OK);
                        resp.setContentType("application/octet-stream");
                        resp.setDateHeader("Date", System.currentTimeMillis());
                        resp.setHeader("Connection", "close");
                        resp.setContentLength(is.available());
                        Bundle bundle = FrameworkUtil.getBundle(getClass());
                        if (bundle != null) {
                            resp.setHeader("Server", bundle.getSymbolicName() + "/" + bundle.getVersion());
                        } else {
                            resp.setHeader("Server", "Karaf Maven Proxy");
                        }
                        StreamUtils.copy(is, resp.getOutputStream());
                    } catch (Exception e) {
                        LOGGER.warn("Error while sending artifact: {}", e.getMessage(), e);
                        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    }
                } else {
                    // browsing
                    try {
                        File requested = new File(location, path);
                        if (requested.exists()) {
                            if (requested.isDirectory()) {
                                Writer writer = new OutputStreamWriter(resp.getOutputStream());
                                writer.write("<html>");
                                writer.write("<head><title>Cave Repository " + name + ": " + path + "</title></head>");
                                writer.write("<body>");
                                writer.write("<header><h1>" + path + "</h1></header>");
                                writer.write("<hr/>");
                                writer.write("<main><pre id=\"contents\">");
                                if (!path.isEmpty()) {
                                    writer.write("<a href=\"" + req.getRequestURI() + "../\">..</a><br/>");
                                }
                                for (File child : requested.listFiles()) {
                                    writer.write("<a href=\"" + req.getRequestURI() + child.getName() + "/\" title=\"" + child.getName() + "\">" + child.getName() + "</a><br/>");
                                }
                                writer.write("</pre><hr/></main>");
                                writer.write("</body></html>");
                                writer.flush();
                            } else {
                                StreamUtils.copy(new FileInputStream(requested), resp.getOutputStream());
                            }
                        } else {
                            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                        }
                    } catch (Exception e) {
                        LOGGER.warn("", e);
                        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    }
                }
                future.release();
                try {
                    asyncContext.complete();
                } catch (IllegalStateException e) {
                    // Ignore, the response must have already been sent with an error
                }
            }
        });
    }