Search in sources :

Example 1 with RException

use of org.sonatype.nexus.repository.r.internal.RException in project nexus-repository-r by sonatype-nexus-community.

the class RPackagesUtils method buildPackages.

public static Content buildPackages(final Collection<Map<String, String>> entries) throws IOException {
    CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try (CompressorOutputStream cos = compressorStreamFactory.createCompressorOutputStream(GZIP, os)) {
        try (OutputStreamWriter writer = new OutputStreamWriter(cos, UTF_8)) {
            for (Map<String, String> entry : entries) {
                InternetHeaders headers = new InternetHeaders();
                headers.addHeader(P_PACKAGE, entry.get(P_PACKAGE));
                headers.addHeader(P_VERSION, entry.get(P_VERSION));
                headers.addHeader(P_DEPENDS, entry.get(P_DEPENDS));
                headers.addHeader(P_IMPORTS, entry.get(P_IMPORTS));
                headers.addHeader(P_SUGGESTS, entry.get(P_SUGGESTS));
                headers.addHeader(P_LINKINGTO, entry.get(P_LINKINGTO));
                headers.addHeader(P_LICENSE, entry.get(P_LICENSE));
                headers.addHeader(P_NEEDS_COMPILATION, entry.get(P_NEEDS_COMPILATION));
                Enumeration<String> headerLines = headers.getAllHeaderLines();
                while (headerLines.hasMoreElements()) {
                    String line = headerLines.nextElement();
                    writer.write(line, 0, line.length());
                    writer.write('\n');
                }
                writer.write('\n');
            }
        }
    } catch (CompressorException e) {
        throw new RException(null, e);
    }
    return new Content(new BytesPayload(os.toByteArray(), "application/x-gzip"));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) BytesPayload(org.sonatype.nexus.repository.view.payloads.BytesPayload) RException(org.sonatype.nexus.repository.r.internal.RException) CompressorException(org.apache.commons.compress.compressors.CompressorException) Content(org.sonatype.nexus.repository.view.Content) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with RException

use of org.sonatype.nexus.repository.r.internal.RException in project nexus-repository-r by sonatype-nexus-community.

the class RMetadataUtils method parseDescriptionFile.

/**
 * Parses metadata stored in a Debian Control File-like format.
 *
 * @see <a href="https://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-DESCRIPTION-file">Description File</a>
 */
public static Map<String, String> parseDescriptionFile(final InputStream in) {
    checkNotNull(in);
    try {
        LinkedHashMap<String, String> results = new LinkedHashMap<>();
        InternetHeaders headers = new InternetHeaders(in);
        Enumeration headerEnumeration = headers.getAllHeaders();
        while (headerEnumeration.hasMoreElements()) {
            Header header = (Header) headerEnumeration.nextElement();
            String name = header.getName();
            String value = header.getValue().replace("\r\n", "\n").replace("\r", // TODO: "should" be ASCII only, otherwise need to know encoding?
            "\n");
            // TODO: Supposedly no duplicates, is this true?
            results.put(name, value);
        }
        return results;
    } catch (MessagingException e) {
        throw new RException(null, e);
    }
}
Also used : Enumeration(java.util.Enumeration) InternetHeaders(javax.mail.internet.InternetHeaders) Header(javax.mail.Header) MessagingException(javax.mail.MessagingException) RException(org.sonatype.nexus.repository.r.internal.RException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with RException

use of org.sonatype.nexus.repository.r.internal.RException in project nexus-repository-r by sonatype-nexus-community.

the class RPackagesUtils method parseMetadata.

public static List<Map<String, String>> parseMetadata(final InputStream in) {
    List<Map<String, String>> entries = new ArrayList<>();
    try (InputStreamReader inr = new InputStreamReader(in, UTF_8)) {
        try (BufferedReader br = new BufferedReader(inr)) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                if (line.trim().isEmpty()) {
                    String content = sb.toString();
                    if (!content.trim().isEmpty()) {
                        entries.add(parseDescriptionFile(new ByteArrayInputStream(content.getBytes(UTF_8))));
                    }
                    sb = new StringBuilder();
                } else {
                    sb.append(line);
                    sb.append('\n');
                }
            }
            String content = sb.toString();
            if (!content.trim().isEmpty()) {
                entries.add(parseDescriptionFile(new ByteArrayInputStream(content.getBytes(UTF_8))));
            }
        }
        return entries;
    } catch (IOException e) {
        throw new RException(null, e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) RException(org.sonatype.nexus.repository.r.internal.RException) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

RException (org.sonatype.nexus.repository.r.internal.RException)3 LinkedHashMap (java.util.LinkedHashMap)2 InternetHeaders (javax.mail.internet.InternetHeaders)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 Map (java.util.Map)1 Header (javax.mail.Header)1 MessagingException (javax.mail.MessagingException)1 CompressorException (org.apache.commons.compress.compressors.CompressorException)1 CompressorOutputStream (org.apache.commons.compress.compressors.CompressorOutputStream)1 CompressorStreamFactory (org.apache.commons.compress.compressors.CompressorStreamFactory)1 Content (org.sonatype.nexus.repository.view.Content)1 BytesPayload (org.sonatype.nexus.repository.view.payloads.BytesPayload)1