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
29      extends IOException
30  {
31      /**
32       * serialVersionUID format is YYYYMMDD for the date of the last binary change.
33       */
34      private static final long serialVersionUID = 20101208L;
35  
36      /** URL pattern */
37      private static final Pattern URL_PATTERN = Pattern.compile("[a-z]+://.*");
38  
39      /** Password pattern */
40      private static final Pattern PASSWORD_PATTERN = Pattern.compile(":(?:[^/]+)@");
41  
42      /**
43       * Array of complementary info (context).
44       */
45      private final String[] info;
46  
47      /**
48       * Constructs exception with the specified detail message.
49       *
50       * @param code the error code of the message.
51       */
52      public FileSystemException(final String code)
53      {
54          this(code, null, (Object[]) null);
55      }
56  
57      /**
58       * Constructs exception with the specified detail message.
59       *
60       * @param code  the error code of the message.
61       * @param info0 one context information.
62       */
63      public FileSystemException(final String code, final Object info0)
64      {
65          this(code, null, new Object[]{info0});
66      }
67  
68      /**
69       * Constructs exception with the specified detail message.
70       *
71       * @param code      the error code of the message.
72       * @param info0     one context information.
73       * @param throwable the cause.
74       */
75      public FileSystemException(final String code,
76                                 final Object info0,
77                                 final Throwable throwable)
78      {
79          this(code, throwable, new Object[]{info0});
80      }
81  
82      /**
83       * Constructs exception with the specified detail message.
84       *
85       * @param code the error code of the message.
86       * @param info array of complementary info (context).
87       */
88      public FileSystemException(final String code, final Object... info)
89      {
90          this(code, null, info);
91      }
92  
93      /**
94       * Constructs exception with the specified detail message.
95       *
96       * @param code the error code of the message.
97       * @param throwable the original cause
98       */
99      public FileSystemException(final String code, final Throwable throwable)
100     {
101         this(code, throwable, (Object[]) null);
102     }
103 
104     /**
105      * Constructs exception with the specified detail message.
106      *
107      * @param code      the error code of the message.
108      * @param info      array of complementary info (context).
109      * @param throwable the cause.
110      * @deprecated Use instead {@link #FileSystemException(String, Throwable, Object[])}. Will be removed in 3.0.
111      */
112     @Deprecated
113     public FileSystemException(final String code,
114                                final Object[] info,
115                                final Throwable throwable)
116     {
117         this(code, throwable, info);
118     }
119 
120     /**
121      * Constructs exception with the specified detail message.
122      *
123      * @param code      the error code of the message.
124      * @param info      array of complementary info (context).
125      * @param throwable the cause.
126      */
127     public FileSystemException(final String code,
128                                final Throwable throwable,
129                                final Object... info)
130     {
131         super(code, throwable);
132 
133         if (info == null)
134         {
135             this.info = new String[0];
136         }
137         else
138         {
139             this.info = new String[info.length];
140             for (int i = 0; i < info.length; i++)
141             {
142                 String value = String.valueOf(info[i]);
143                 // mask passwords (VFS-169)
144                 final Matcher urlMatcher = URL_PATTERN.matcher(value);
145                 if (urlMatcher.find())
146                 {
147                     final Matcher pwdMatcher = PASSWORD_PATTERN.matcher(value);
148                     value = pwdMatcher.replaceFirst(":***@");
149                 }
150                 this.info[i] = value;
151             }
152         }
153     }
154 
155     /**
156      * Constructs wrapper exception.
157      *
158      * @param throwable the root cause to wrap.
159      */
160     public FileSystemException(final Throwable throwable)
161     {
162         this(throwable.getMessage(), throwable, (Object[]) null);
163     }
164 
165     /**
166      * Retrieves message from bundle.
167      * @return The exception message.
168      */
169     @Override
170     public String getMessage()
171     {
172         return Messages.getString(super.getMessage(), (Object[]) getInfo());
173     }
174 
175     /**
176      * Retrieves error code of the exception.
177      * Could be used as key for internationalization.
178      *
179      * @return the code.
180      */
181     public String getCode()
182     {
183         return super.getMessage();
184     }
185 
186     /**
187      * Retrieves array of complementary info (context).
188      * Could be used as parameter for internationalization.
189      *
190      * @return the context info.
191      */
192     public String[] getInfo()
193     {
194         return info;
195     }
196 }