Search in sources :

Example 21 with ByteSource

use of com.google.common.io.ByteSource in project GeoGig by boundlessgeo.

the class Logging method getOrCreateLoggingConfigFile.

@Nullable
private static URL getOrCreateLoggingConfigFile(final File geogigdir) {
    final File logsDir = new File(geogigdir, "log");
    if (!logsDir.exists() && !logsDir.mkdir()) {
        return null;
    }
    final File configFile = new File(logsDir, "logback.xml");
    if (configFile.exists()) {
        try {
            return configFile.toURI().toURL();
        } catch (MalformedURLException e) {
            throw Throwables.propagate(e);
        }
    }
    ByteSource from;
    final URL resource = GeogigCLI.class.getResource("logback_default.xml");
    try {
        from = Resources.asByteSource(resource);
    } catch (NullPointerException npe) {
        LOGGER.warn("Couldn't obtain default logging configuration file");
        return null;
    }
    try {
        from.copyTo(Files.asByteSink(configFile));
        return configFile.toURI().toURL();
    } catch (Exception e) {
        LOGGER.warn("Error copying logback_default.xml to {}. Using default configuration.", configFile, e);
        return resource;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ByteSource(com.google.common.io.ByteSource) File(java.io.File) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) JoranException(ch.qos.logback.core.joran.spi.JoranException) Nullable(javax.annotation.Nullable)

Example 22 with ByteSource

use of com.google.common.io.ByteSource in project mica2 by obiba.

the class StudyPackageImportServiceImpl method importNetwork.

private void importNetwork(Network network, boolean publish, StudyPackage studyPackage) throws IOException {
    Network updated;
    try {
        Network existing = networkService.findById(network.getId());
        network.getStudyIds().stream().filter(sid -> !existing.getStudyIds().contains(sid)).forEach(sid -> existing.getStudyIds().add(sid));
        updated = existing;
    } catch (NoSuchNetworkException e) {
        updated = network;
    }
    for (Map.Entry<String, ByteSource> e : studyPackage.attachments.entrySet()) {
        Attachment attachment = network.getLogo();
        if (attachment != null && attachment.getId().equals(e.getKey())) {
            saveTempFile(attachment, e.getValue());
            updated.setLogo(attachment);
        }
    }
    networkService.save(updated);
    if (publish)
        networkService.publish(updated.getId(), true, PublishCascadingScope.ALL);
}
Also used : LocalizedString(org.obiba.mica.core.domain.LocalizedString) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) AbstractProtobufProvider(org.obiba.jersey.protobuf.AbstractProtobufProvider) Inject(javax.inject.Inject) TempFile(org.obiba.mica.file.TempFile) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) PublishCascadingScope(org.obiba.mica.core.domain.PublishCascadingScope) Service(org.springframework.stereotype.Service) Map(java.util.Map) Mica(org.obiba.mica.web.model.Mica) NoSuchDatasetException(org.obiba.mica.dataset.NoSuchDatasetException) TempFileService(org.obiba.mica.file.service.TempFileService) Network(org.obiba.mica.network.domain.Network) ByteSource(com.google.common.io.ByteSource) ZipEntry(java.util.zip.ZipEntry) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dtos(org.obiba.mica.web.model.Dtos) Charsets(com.google.common.base.Charsets) Attachment(org.obiba.mica.file.Attachment) Logger(org.slf4j.Logger) Pair(org.apache.commons.math3.util.Pair) BaseStudy(org.obiba.mica.study.domain.BaseStudy) Set(java.util.Set) CollectedDatasetService(org.obiba.mica.dataset.service.CollectedDatasetService) FileSystemService(org.obiba.mica.file.service.FileSystemService) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Dataset(org.obiba.mica.dataset.domain.Dataset) NoSuchNetworkException(org.obiba.mica.network.NoSuchNetworkException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) HarmonizedDatasetService(org.obiba.mica.dataset.service.HarmonizedDatasetService) Sets(com.google.common.collect.Sets) JsonFormat(com.googlecode.protobuf.format.JsonFormat) List(java.util.List) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) NetworkService(org.obiba.mica.network.service.NetworkService) InputStream(java.io.InputStream) Network(org.obiba.mica.network.domain.Network) NoSuchNetworkException(org.obiba.mica.network.NoSuchNetworkException) ByteSource(com.google.common.io.ByteSource) Attachment(org.obiba.mica.file.Attachment) LocalizedString(org.obiba.mica.core.domain.LocalizedString) Map(java.util.Map)

Example 23 with ByteSource

use of com.google.common.io.ByteSource in project wombat by PLOS.

the class PowerPointController method getImageFile.

private ByteSource getImageFile(AssetPointer assetId) throws IOException {
    Map<String, ?> files = articleService.getItemFiles(assetId);
    Map<String, ?> file = (Map<String, ?>) files.get(IMAGE_SIZE);
    ContentKey key = ContentKey.createForUuid((String) file.get("crepoKey"), UUID.fromString((String) file.get("crepoUuid")));
    return new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return corpusContentApi.request(key, ImmutableList.of()).getEntity().getContent();
        }
    };
}
Also used : ContentKey(org.ambraproject.wombat.service.remote.ContentKey) ByteSource(com.google.common.io.ByteSource) Map(java.util.Map)

Example 24 with ByteSource

use of com.google.common.io.ByteSource in project grafikon by jub77.

the class FileLoadSaveImages method saveTimetableImage.

/**
 * saves image for timetable.
 *
 * @param image image
 * @param os zip output stream
 * @throws java.io.IOException
 */
public void saveTimetableImage(TimetableImage image, ZipOutputStream os) throws IOException {
    // copy image to zip
    ZipEntry entry = new ZipEntry(location + image.getFilename());
    if (image.getImageFile() == null) {
        // skip images without image file
        log.warn("Skipping image {} because the gtm doesn't contain a file.", image.getFilename());
        return;
    }
    ByteSource src = Files.asByteSource(image.getImageFile());
    entry.setSize(src.size());
    os.putNextEntry(entry);
    src.copyTo(os);
}
Also used : ZipEntry(java.util.zip.ZipEntry) ByteSource(com.google.common.io.ByteSource)

Example 25 with ByteSource

use of com.google.common.io.ByteSource in project grafikon by jub77.

the class ResourceHelper method readResource.

public static String readResource(final String filename, final ClassLoader cl) {
    try {
        ByteSource bs = new ByteSource() {

            @Override
            public InputStream openStream() throws IOException {
                return getStream(filename, cl);
            }
        };
        CharSource cs = bs.asCharSource(StandardCharsets.UTF_8);
        return cs.read();
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
        return "";
    }
}
Also used : CharSource(com.google.common.io.CharSource) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException)

Aggregations

ByteSource (com.google.common.io.ByteSource)138 IOException (java.io.IOException)58 Test (org.junit.Test)58 InputStream (java.io.InputStream)42 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)18 Metacard (ddf.catalog.data.Metacard)17 ContentItem (ddf.catalog.content.data.ContentItem)16 StringWriter (java.io.StringWriter)14 FileInputStream (java.io.FileInputStream)13 Test (org.junit.jupiter.api.Test)12 URI (java.net.URI)11 Path (java.nio.file.Path)11 ArrayList (java.util.ArrayList)11 URL (java.net.URL)10 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)9 FilterInputStream (java.io.FilterInputStream)8