Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-1420 Allow to change charset in StringRequestTarget
  3. WICKET-1805

Allow to change charset in StringRequestTarget: change CharSet used by the OutStream as well

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.3.4, 1.4-M3
    • 1.3.5, 1.4-RC1
    • wicket
    • None
    • Java 5 on Mac and Win, using FF2/3 and IE7

    Description

      the CharSet is provided as meta info only, the actual CharSet used by the OutStream is not changed. This leads to data being transferred in a different encoding than specified by the ContentType meta data.
      I suggest using another constructor:

      Index: StringRequestTarget.java
      ===================================================================
      — StringRequestTarget.java (revision 689726)
      +++ StringRequestTarget.java (working copy)
      @@ -17,6 +17,7 @@
      package org.apache.wicket.request.target.basic;

      import java.io.OutputStream;
      +import java.nio.charset.Charset;

      import org.apache.wicket.IRequestTarget;
      import org.apache.wicket.RequestCycle;
      @@ -37,8 +38,11 @@
      /** the string for the response. */
      private final String string;

      • /** content type for the string */
      • private final String contentType;
        + /** mime type for the string */
        + private final String mimeType;
        +
        + /** charset of the string */
        + private final Charset charset;

      /**

      • Constructor
        @@ -48,30 +52,51 @@
        */
        public StringRequestTarget(String string) { - this("text/plain", string); + this("text/plain", string, Charset.defaultCharset()); }

        +
        + /**
        + * Constructor
        + *
        + * @param mimeType
        + * mime type of the data the string represents, e.g.
        + * <code>text/html</code>
        + * @param string
        + * string for the response
        + */
        + public StringRequestTarget(String mimeType, String string)
        +

        { + this(mimeType, string, Charset.defaultCharset()); + }

      /**

      • Constructor
      • * @param contentType
      • * content type of the data the string represents eg
      • * <code>text/html; charset=utf-8</code>
        + * @param mimeType
        + * mime type of the data the string represents, e.g.
        + * <code>text/html</code>
      • @param string
      • string for the response
        + * @param the Charset to be used in the response , e.g.
        + * <code>Charset.forName("UTF-8")</code>
        */
      • public StringRequestTarget(String contentType, String string)
        + public StringRequestTarget(String mimeType, String string, Charset charset)
        {
        if (string == null) { throw new IllegalArgumentException("Argument string must be not null"); }
      • if (Strings.isEmpty(contentType))
        + if (Strings.isEmpty(mimeType)) { - throw new IllegalArgumentException("Argument contentType must not be null or empty"); + throw new IllegalArgumentException("Argument mimeType must not be null or empty"); }
      • this.contentType = contentType;
        + if (charset == null)
        + { + throw new IllegalArgumentException("Argument charset must not be null"); + }

        + this.mimeType = mimeType;
        this.string = string;
        + this.charset = charset;
        }

      @@ -83,29 +108,30 @@
      public void respond(RequestCycle requestCycle)
      {
      // Get servlet response to use when responding with resource

      • final Response response = requestCycle.getResponse();
      • response.setContentType(contentType);
      • final StringBufferResourceStream stream = new StringBufferResourceStream(contentType);
      • stream.append(string);
        + final Response response = requestCycle.getResponse();
        + final StringBufferResourceStream stream = new StringBufferResourceStream(mimeType);
        + stream.setCharset(charset);
        + response.setContentType(mimeType + ";charset=" + charset.name());
        + stream.append(string);
      • // Respond with resource
      • try
      • {
      • final OutputStream out = response.getOutputStream();
      • try
      • { - Streams.copy(stream.getInputStream(), out); - }
      • finally
      • { - stream.close(); - out.flush(); - }
      • }
      • catch (Exception e)
      • { - throw new WicketRuntimeException("Unable to render resource stream " + stream, e); - }

        + // Respond with resource
        + try
        +

        Unknown macro: {+ final OutputStream out = response.getOutputStream();+ try+ { + Streams.copy(stream.getInputStream(), out); + }+ finally+ { + stream.close(); + out.flush(); + }+ }

        + catch (Exception e)
        +

        { + throw new WicketRuntimeException("Unable to render resource stream " + stream, e); + }

        }

      /**

      Attachments

        Activity

          People

            ivaynberg Igor Vaynberg
            nbo Niels Boeger
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: