StreamSourceImplementation.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.common.pagelinks.impl;

  20. import java.io.ByteArrayInputStream;
  21. import java.io.InputStream;
  22. import java.net.URI;

  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. import com.vaadin.server.StreamResource;

  26. /**
  27.  * The Class StreamSourceImplementation.
  28.  */
  29. public final class StreamSourceImplementation implements StreamResource.StreamSource {

  30.     /** The Constant LOGGER. */
  31.     private static final Logger LOGGER = LoggerFactory.getLogger(StreamSourceImplementation.class);

  32.     /** The Constant serialVersionUID. */
  33.     private static final long serialVersionUID = 1L;

  34.     /** The url. */
  35.     private final String url;

  36.     /**
  37.      * Instantiates a new stream source implementation.
  38.      *
  39.      * @param url the url
  40.      */
  41.     public StreamSourceImplementation(final String url) {
  42.         this.url = url;
  43.     }

  44.     @Override
  45.     public InputStream getStream() {
  46.         try {
  47.             return URI.create(url).toURL().openStream();
  48.         } catch (final Exception e) {
  49.             LOGGER.warn("Problem getting url :"+ url +" error:" + e.getClass().getCanonicalName());
  50.             return new ByteArrayInputStream(new byte[0]);
  51.         }
  52.     }
  53. }