private String decodeAuthnRequestXML(String encodedRequestXmlString)
      throws SamlException {
    try {
      // URL decode
      // No need to URL decode: auto decoded by request.getParameter() method

      // Base64 decode
      Base64 base64Decoder = new Base64();
      byte[] xmlBytes = encodedRequestXmlString.getBytes("UTF-8");
      byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

      //Uncompress the AuthnRequest data
      //First attempt to unzip the byte array according to DEFLATE (rfc 1951)
      try {

        Inflater inflater = new Inflater(true);
        inflater.setInput(base64DecodedByteArray);
        // since we are decompressing, it's impossible to know how much space we
        // might need; hopefully this number is suitably big
        byte[] xmlMessageBytes = new byte[5000];
        int resultLength = inflater.inflate(xmlMessageBytes);

        if (!inflater.finished()) {
          throw new RuntimeException("didn't allocate enough space to hold "
            + "decompressed data");
        }

        inflater.end();      
        return new String(xmlMessageBytes, 0, resultLength, "UTF-8");
                   
      } catch (DataFormatException e) {

        // if DEFLATE fails, then attempt to unzip the byte array according to
        // zlib (rfc 1950)      
        ByteArrayInputStream bais = new ByteArrayInputStream(
          base64DecodedByteArray);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InflaterInputStream iis = new InflaterInputStream(bais);
        byte[] buf = new byte[1024];
        int count = iis.read(buf);
        while (count != -1) {
          baos.write(buf, 0, count);
          count = iis.read(buf);
        }
        iis.close();
        return new String(baos.toByteArray());      
      }      
      
    } catch (UnsupportedEncodingException e) {
      throw new SamlException("Error decoding AuthnRequest: " +
            "Check decoding scheme - " + e.getMessage());
    } catch (IOException e) {
      throw new SamlException("Error decoding AuthnRequest: " +
            "Check decoding scheme - " + e.getMessage());
    }
  }