Wicket
  1. Wicket
  2. WICKET-1704

ResourceStreamRequestTarget.configure set wrong ContentLength for non-ascii characters

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.3.5, 1.4-RC1
    • Component/s: wicket
    • Labels:
      None

      Description

      CharSequence export = DataBase.getInstance().exportDiscounts();
      ResourceStreamRequestTarget target = new ResourceStreamRequestTarget(new StringResourceStream(export, "text/plain"));
      target.setFileName("discounts.csv");
      RequestCycle.get().setRequestTarget(target);

      If export contains non-ascii characters. e.g. ěščřžýáíé, is wrong set up the ContentLength of the response. ContentLength is set up with number of characters, but must be set up with number of bytes. Response on output is not all.

      Be sure that byte[].length != "čřžýá".length()

      ResourceStreamRequestTarget.java method configure:

      // WICKET-473 Allow IResourceStream.length() to return -1
      long len = resourceStream.length();
      if (len >= 0)

      { // and the content length response.setContentLength(len); }

        Activity

        Hide
        Jirka added a comment -

        Thanks for fast fix. But it isnt correct, because array of bytes depends on charset.
        Better is

        if (getCharset() != null) {
        try

        { return getString().getBytes(getCharset().name()).length; }

        catch (UnsupportedEncodingException e)

        { throw someException... }

        } else

        { return getString().getBytes().length; }
        Show
        Jirka added a comment - Thanks for fast fix. But it isnt correct, because array of bytes depends on charset. Better is if (getCharset() != null) { try { return getString().getBytes(getCharset().name()).length; } catch (UnsupportedEncodingException e) { throw someException... } } else { return getString().getBytes().length; }
        Hide
        Igor Vaynberg added a comment -

        thanks, you couldve just provided a patch from the get go

        Show
        Igor Vaynberg added a comment - thanks, you couldve just provided a patch from the get go
        Hide
        Jirka added a comment -

        The same bug in class org.apache.wicket.util.resource.StringBufferResourceStream. Maybe others, that extends org.apache.wicket.util.resource.AbstractStringResourceStream or generally implements IStringResourceStream.

        Show
        Jirka added a comment - The same bug in class org.apache.wicket.util.resource.StringBufferResourceStream. Maybe others, that extends org.apache.wicket.util.resource.AbstractStringResourceStream or generally implements IStringResourceStream.
        Hide
        Martijn Dashorst added a comment -

        Moved to next milestone release.

        Show
        Martijn Dashorst added a comment - Moved to next milestone release.

          People

          • Assignee:
            Igor Vaynberg
            Reporter:
            Jirka
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development