CanNotOpenStreamException.java

  1. /*******************************************************************************
  2.  * Copyright 2012 André Rouél
  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. package net.sf.uadetector.exception;

  17. /**
  18.  * Thrown to indicate that no stream to an {@link java.net.URL} can be established.<br>
  19.  * <br>
  20.  * This exception is intended to tunnel the checked exception {@link java.io.IOException} during the call
  21.  * {@link java.net.URL#openStream()}.
  22.  *
  23.  * @author André Rouél
  24.  */
  25. public class CanNotOpenStreamException extends RuntimeException {

  26.     private static final long serialVersionUID = 8381680536297450770L;

  27.     protected static final String DEFAULT_MESSAGE = "Can not open stream to the given URL.";

  28.     protected static final String MESSAGE_WITH_URL = "Can not open stream to the given URL: %s";

  29.     private static String format(final String url) {
  30.         return String.format(MESSAGE_WITH_URL, url);
  31.     }

  32.     /**
  33.      * Constructs an {@code CanNotOpenStreamException} with the default message
  34.      * {@link CanNotOpenStreamException#DEFAULT_MESSAGE}.
  35.      */
  36.     public CanNotOpenStreamException() {
  37.         super(DEFAULT_MESSAGE);
  38.     }

  39.     /**
  40.      * Constructs an {@code CanNotOpenStreamException} with the message
  41.      * {@link CanNotOpenStreamException#MESSAGE_WITH_URL} including the given URL as string representation.
  42.      *
  43.      * @param url
  44.      *            the URL to which no stream can be established
  45.      */
  46.     public CanNotOpenStreamException(final String url) {
  47.         super(format(url));
  48.     }

  49.     /**
  50.      * Constructs a new exception with the message {@link CanNotOpenStreamException#MESSAGE_WITH_URL} including the
  51.      * given URL as string representation and cause.
  52.      *
  53.      * @param url
  54.      *            the URL to which no stream can be established
  55.      * @param cause
  56.      *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
  57.      *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
  58.      */
  59.     public CanNotOpenStreamException(final String url, final Throwable cause) {
  60.         super(format(url), cause);
  61.     }

  62.     /**
  63.      * Constructs a new exception with the default message {@link CanNotOpenStreamException#DEFAULT_MESSAGE}.
  64.      *
  65.      * @param cause
  66.      *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
  67.      *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
  68.      */
  69.     public CanNotOpenStreamException(final Throwable cause) {
  70.         super(DEFAULT_MESSAGE, cause);
  71.     }

  72. }