Example #1
0
 /**
  * Tests response handling with specified charset in the header 'Content-Type'.
  *
  * @throws IOException I/O Exception
  * @throws QueryException query exception
  */
 @Test
 public void responseWithCharset() throws IOException, QueryException {
   // Create fake HTTP connection
   final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com"));
   // Set content type
   conn.contentType = "text/plain; charset=CP1251";
   // set content encoded in CP1251
   final String test = "\u0442\u0435\u0441\u0442";
   conn.content = Charset.forName("CP1251").encode(test).array();
   final ItemList res = new HttpResponse(null, ctx.options).getResponse(conn, true, null);
   // compare results
   assertEquals(test, string(res.get(1).string(null)));
 }
Example #2
0
  /**
   * Adds the specified entry to the output stream.
   *
   * @param entry entry descriptor
   * @param con contents
   * @param out output archive
   * @param level default compression level
   * @throws QueryException query exception
   * @throws IOException I/O exception
   */
  private void add(final Item entry, final Item con, final ArchiveOut out, final int level)
      throws QueryException, IOException {

    // create new zip entry
    final String name = string(entry.string(info));
    if (name.isEmpty()) ARCH_EMPTY.thrw(info);
    final ZipEntry ze = new ZipEntry(name);
    String en = null;

    // compression level
    byte[] lvl = null;
    if (entry instanceof ANode) {
      final ANode el = (ANode) entry;
      lvl = el.attribute(Q_LEVEL);

      // last modified
      final byte[] mod = el.attribute(Q_LAST_MOD);
      if (mod != null) {
        try {
          ze.setTime(new Int(new Dtm(mod, info)).itr());
        } catch (final QueryException qe) {
          ARCH_DATETIME.thrw(info, mod);
        }
      }

      // encoding
      final byte[] enc = el.attribute(Q_ENCODING);
      if (enc != null) {
        en = string(enc);
        if (!Charset.isSupported(en)) ARCH_ENCODING.thrw(info, enc);
      }
    }

    // data to be compressed
    byte[] val = checkStrBin(con);
    if (con instanceof AStr && en != null && en != Token.UTF8) val = encode(val, en);

    try {
      out.level(lvl == null ? level : toInt(lvl));
    } catch (final IllegalArgumentException ex) {
      ARCH_LEVEL.thrw(info, lvl);
    }
    out.write(ze, val);
  }