throw a special exception when there is no password for an encrpyted 7z archive - COMPRESS-298

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@1653252 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8770c53..95cc013 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -80,6 +80,11 @@
         Improved error message when tar encounters a groupId that is
         too big to write without using the STAR or POSIX format.
       </action>
+      <action type="fix" date="2015-01-20" issue="COMPRESS-298">
+        SevenZFile now throws the specific PasswordRequiredException
+        when it encounters an encrypted stream but no password has
+        been specified.
+      </action>
     </release>
 
     <release version="1.9" date="2014-10-09"
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
index 38f418c..91fb486 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
@@ -54,7 +54,7 @@
                 System.arraycopy(coder.properties, 2 + saltSize, iv, 0, ivSize);
 
                 if (passwordBytes == null) {
-                    throw new IOException("Cannot read encrypted files without a password");
+                    throw new PasswordRequiredException();
                 }
                 final byte[] aesKeyBytes;
                 if (numCyclesPower == 0x3f) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java
new file mode 100644
index 0000000..8814d3d
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java
@@ -0,0 +1,32 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.apache.commons.compress.archivers.sevenz;
+
+import java.io.IOException;
+
+/**
+ * Exception thrown when trying to read an encrypted entry without
+ * configuring a password.
+ * @since 1.10
+ */
+public class PasswordRequiredException extends IOException {
+
+    public PasswordRequiredException() {
+        super("Cannot read encrypted files without a password");
+    }
+}
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index 0d9e889..bc84d44 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -60,6 +60,15 @@
         }
     }
 
+    public void test7zDecryptUnarchiveWithoutPassword() throws Exception {
+        try {
+            test7zUnarchive(getFile("bla.encrypted.7z"), SevenZMethod.LZMA);
+            fail("Expected a PasswordRequiredException");
+        } catch (PasswordRequiredException ex) {
+            // expected
+        }
+    }
+
     private void test7zUnarchive(File f, SevenZMethod m) throws Exception {
         test7zUnarchive(f, m, null);
     }