View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import java.io.IOException;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.apache.commons.vfs2.util.Messages;
24  
25  /**
26   * Thrown for file system errors.
27   */
28  public class FileSystemException extends IOException {
29  
30      /**
31       * serialVersionUID format is YYYYMMDD for the date of the last binary change.
32       */
33      private static final long serialVersionUID = 20101208L;
34  
35      /** URL pattern */
36      private static final Pattern URL_PATTERN = Pattern.compile("[a-z]+://.*");
37  
38      /** Password pattern */
39      private static final Pattern PASSWORD_PATTERN = Pattern.compile(":(?:[^/]+)@");
40  
41      /**
42       * Array of complementary info (context).
43       */
44      private final String[] info;
45  
46      /**
47       * Throws a FileSystemException when the given object is null.
48       *
49       * @param obj
50       *            the object reference to check for null.
51       * @param code
52       *            message used when {@code
53       *                FileSystemException} is thrown
54       * @param <T>
55       *            the type of the reference
56       * @return {@code obj} if not {@code null}
57       * @throws FileSystemException
58       *             if {@code obj} is {@code null}
59       * @since 2.3
60       */
61      public static <T> T requireNonNull(final T obj, final String code) throws FileSystemException {
62          if (obj == null) {
63              throw new FileSystemException(code);
64          }
65          return obj;
66      }
67  
68      /**
69       * Throws a FileSystemException when the given object is null.
70       *
71       * @param obj
72       *            the object reference to check for null.
73       * @param code
74       *            message used when {@code
75       *                FileSystemException} is thrown
76       * @param info
77       *            one context information.
78       * @param <T>
79       *            the type of the reference
80       * @return {@code obj} if not {@code null}
81       * @throws FileSystemException
82       *             if {@code obj} is {@code null}
83       * @since 2.3
84       */
85      public static <T> T requireNonNull(final T obj, final String code, final Object... info) throws FileSystemException {
86          if (obj == null) {
87              throw new FileSystemException(code, info);
88          }
89          return obj;
90      }
91  
92      /**
93       * Constructs exception with the specified detail message.
94       *
95       * @param code the error code of the message.
96       */
97      public FileSystemException(final String code) {
98          this(code, null, (Object[]) null);
99      }
100 
101     /**
102      * Constructs exception with the specified detail message.
103      *
104      * @param code the error code of the message.
105      * @param info0 one context information.
106      */
107     public FileSystemException(final String code, final Object info0) {
108         this(code, null, new Object[] { info0 });
109     }
110 
111     /**
112      * Constructs exception with the specified detail message.
113      *
114      * @param code the error code of the message.
115      * @param info0 one context information.
116      * @param throwable the cause.
117      */
118     public FileSystemException(final String code, final Object info0, final Throwable throwable) {
119         this(code, throwable, new Object[] { info0 });
120     }
121 
122     /**
123      * Constructs exception with the specified detail message.
124      *
125      * @param code the error code of the message.
126      * @param info array of complementary info (context).
127      */
128     public FileSystemException(final String code, final Object... info) {
129         this(code, null, info);
130     }
131 
132     /**
133      * Constructs exception with the specified detail message.
134      *
135      * @param code the error code of the message.
136      * @param throwable the original cause
137      */
138     public FileSystemException(final String code, final Throwable throwable) {
139         this(code, throwable, (Object[]) null);
140     }
141 
142     /**
143      * Constructs exception with the specified detail message.
144      *
145      * @param code the error code of the message.
146      * @param info array of complementary info (context).
147      * @param throwable the cause.
148      * @deprecated Use instead {@link #FileSystemException(String, Throwable, Object[])}. Will be removed in 3.0.
149      */
150     @Deprecated
151     public FileSystemException(final String code, final Object[] info, final Throwable throwable) {
152         this(code, throwable, info);
153     }
154 
155     /**
156      * Constructs exception with the specified detail message.
157      *
158      * @param code the error code of the message.
159      * @param info array of complementary info (context).
160      * @param throwable the cause.
161      */
162     public FileSystemException(final String code, final Throwable throwable, final Object... info) {
163         super(code, throwable);
164 
165         if (info == null) {
166             this.info = new String[0];
167         } else {
168             this.info = new String[info.length];
169             for (int i = 0; i < info.length; i++) {
170                 String value = String.valueOf(info[i]);
171                 // mask passwords (VFS-169)
172                 final Matcher urlMatcher = URL_PATTERN.matcher(value);
173                 if (urlMatcher.find()) {
174                     final Matcher pwdMatcher = PASSWORD_PATTERN.matcher(value);
175                     value = pwdMatcher.replaceFirst(":***@");
176                 }
177                 this.info[i] = value;
178             }
179         }
180     }
181 
182     /**
183      * Constructs wrapper exception.
184      *
185      * @param throwable the root cause to wrap.
186      */
187     public FileSystemException(final Throwable throwable) {
188         this(throwable.getMessage(), throwable, (Object[]) null);
189     }
190 
191     /**
192      * Retrieves message from bundle.
193      *
194      * @return The exception message.
195      */
196     @Override
197     public String getMessage() {
198         return Messages.getString(super.getMessage(), (Object[]) getInfo());
199     }
200 
201     /**
202      * Retrieves error code of the exception. Could be used as key for internationalization.
203      *
204      * @return the code.
205      */
206     public String getCode() {
207         return super.getMessage();
208     }
209 
210     /**
211      * Retrieves array of complementary info (context). Could be used as parameter for internationalization.
212      *
213      * @return the context info.
214      */
215     public String[] getInfo() {
216         return info;
217     }
218 }