private ResourceImpl createResource()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/RepositoryServiceImpl.java [686:737]


    private ResourceImpl createResource(URLConnection urlConnection, String uri, String location, boolean readFully) throws BundleException, IOException, NoSuchAlgorithmException {
        Map<String, String> headers = null;
        String digest = null;
        long size = -1;
        // find headers, estimate length and checksum
        try (ContentInputStream is = new ContentInputStream(urlConnection.getInputStream())) {
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (MANIFEST_NAME.equals(entry.getName())) {
                    Attributes attributes = new Manifest(zis).getMainAttributes();
                    headers = new HashMap<>();
                    for (Map.Entry attr : attributes.entrySet()) {
                        headers.put(attr.getKey().toString(), attr.getValue().toString());
                    }
                    if (!readFully) {
                        break;
                    }
                }
            }
            if (readFully) {
                digest = is.getDigest();
                size = is.getSize();
            }
        }
        if (headers == null) {
            throw new BundleException("Resource " + urlConnection.getURL() + " does not contain a manifest");
        }
        // fix the content directive
        try {
            ResourceImpl resource = ResourceBuilder.build(uri, headers);
            for (Capability cap : resource.getCapabilities(null)) {
                if (cap.getNamespace().equals(CONTENT_NAMESPACE)) {
                    String resourceURI = cap.getAttributes().get(CAPABILITY_URL_ATTRIBUTE).toString();
                    String locationURI = "file:" + location;
                    if (resourceURI.startsWith(locationURI)) {
                        resourceURI = resourceURI.substring(locationURI.length() + 1);
                        cap.getAttributes().put(CAPABILITY_URL_ATTRIBUTE, resourceURI);
                    }
                    if (readFully) {
                        cap.getAttributes().put(CONTENT_NAMESPACE, digest);
                        cap.getAttributes().put(CAPABILITY_SIZE_ATTRIBUTE, size);
                    }
                    cap.getAttributes().put(CAPABILITY_MIME_ATTRIBUTE, "application/vnd.osgi.bundle");
                    break;
                }
            }
            return resource;
        } catch (BundleException e) {
            throw new BundleException("Unable to create resource from " + uri + ": " + e.getMessage(), e);
        }
    }