/*
 * Copyright 2017, Sascha Häberling
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.retrostore.resources;

import com.google.common.io.ByteStreams;
import org.retrostore.request.Cache;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Loads files as resources and caches them aggressively.
 */
public class DefaultResourceLoader implements ResourceLoader {
  private static final Logger LOG = Logger.getLogger("DefaultResourceLoader");

  private final Cache mCache;

  public DefaultResourceLoader(Cache cache) {
    mCache = cache;
  }

  @Override
  public Optional<byte[]> load(final String filename) {
    return mCache.get(filename, () -> loadFile(filename));
  }

  private byte[] loadFile(String filename) {
    // TODO: Debug mode for local reloading
    try {
      File file = new File(filename);
      LOG.info("Loading file: " + file.getAbsolutePath());
      return toBytes(new FileInputStream(file));
    } catch (IOException ignore) {
      LOG.log(Level.INFO, String.format("Cannot load file as resource '%s'.", filename));
    }
    return null;
  }

  @Override
  public Optional<byte[]> loadUrl(String urlStr) {
    try {
      URL url = new URL(urlStr);
      InputStream is = url.openStream();
      return Optional.of(toBytes(is));
    } catch (MalformedURLException e) {
      LOG.log(Level.SEVERE, "Invalid URL", e);
    } catch (IOException e) {
      LOG.log(Level.SEVERE, "Cannot read URL", e);
    }
    return Optional.empty();
  }

  private byte[] toBytes(InputStream input) throws IOException {
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    ByteStreams.copy(input, data);
    return data.toByteArray();
  }
}

@@ -2110,24 +2110,56 @@
 RE, 
-%22Invalid URL%22, e
+String.format(%22Invalid URL: %25s%22, e.getMessage())
 );%0A 
@@ -2215,16 +2215,30 @@
 SEVERE, 
+String.format(
 %22Cannot 
@@ -2245,20 +2245,38 @@
 read URL
-%22, e
+: %25s%22, e.getMessage())
 );%0A    %7D

Optimize error output for dev.